<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to calculate availability of API on daily basis in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-availability-of-API-on-daily-basis/m-p/336997#M100001</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have multiple APIs in my log whose &lt;CODE&gt;availability duration&lt;/CODE&gt; needs to be determined on daily basis i.e., from 00 to 24 hours based on &lt;CODE&gt;active&lt;/CODE&gt; and &lt;CODE&gt;inactive&lt;/CODE&gt; status, which means, it will have to check the status of the API from the last event of previous day to the first event of current day to check the status of that particular API. But to make any calculation on availability it will have to start the calculation only since 00 hour. &lt;/P&gt;

&lt;P&gt;Kindly help to build the query, this is how far I've managed to go.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;`urlendpoint` 
| search endpoint=* 
| eval Brand="xyz" 
| eval status=case(like(elb_status_code,"2%") OR like(elb_status_code,"3%") OR like(elb_status_code, "505") OR like(elb_status_code, "510") OR like(elb_status_code, "511"), "active", like(elb_status_code,"4%") OR like(elb_status_code,"500"), "inactive")
| reverse 
| streamstats current=f last(_time) AS last_time last(status) AS last_status by endpoint 
| stats values(last_time) AS last_time values(last_status) AS last_status values(status) AS status by endpoint, _time Brand 
| eval active_time=case(last_status="active", _time-last_time) 
| eval inactive_time=case(last_status="inactive", _time-last_time) | eval day = strftime(_time, "%d") 
| eval month=strftime(_time, "%m") 
| eval Date = strftime(_time, "%d/%m/%y") | stats sum(active_time) AS active by day month Date Brand endpoint 
| eval active=active/(3600) 
| sort - month day 
| fields - month
| fillnull value=0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 16 Apr 2018 10:28:01 GMT</pubDate>
    <dc:creator>MousumiChowdhur</dc:creator>
    <dc:date>2018-04-16T10:28:01Z</dc:date>
    <item>
      <title>How to calculate availability of API on daily basis</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-availability-of-API-on-daily-basis/m-p/336997#M100001</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have multiple APIs in my log whose &lt;CODE&gt;availability duration&lt;/CODE&gt; needs to be determined on daily basis i.e., from 00 to 24 hours based on &lt;CODE&gt;active&lt;/CODE&gt; and &lt;CODE&gt;inactive&lt;/CODE&gt; status, which means, it will have to check the status of the API from the last event of previous day to the first event of current day to check the status of that particular API. But to make any calculation on availability it will have to start the calculation only since 00 hour. &lt;/P&gt;

&lt;P&gt;Kindly help to build the query, this is how far I've managed to go.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;`urlendpoint` 
| search endpoint=* 
| eval Brand="xyz" 
| eval status=case(like(elb_status_code,"2%") OR like(elb_status_code,"3%") OR like(elb_status_code, "505") OR like(elb_status_code, "510") OR like(elb_status_code, "511"), "active", like(elb_status_code,"4%") OR like(elb_status_code,"500"), "inactive")
| reverse 
| streamstats current=f last(_time) AS last_time last(status) AS last_status by endpoint 
| stats values(last_time) AS last_time values(last_status) AS last_status values(status) AS status by endpoint, _time Brand 
| eval active_time=case(last_status="active", _time-last_time) 
| eval inactive_time=case(last_status="inactive", _time-last_time) | eval day = strftime(_time, "%d") 
| eval month=strftime(_time, "%m") 
| eval Date = strftime(_time, "%d/%m/%y") | stats sum(active_time) AS active by day month Date Brand endpoint 
| eval active=active/(3600) 
| sort - month day 
| fields - month
| fillnull value=0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Apr 2018 10:28:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-availability-of-API-on-daily-basis/m-p/336997#M100001</guid>
      <dc:creator>MousumiChowdhur</dc:creator>
      <dc:date>2018-04-16T10:28:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate availability of API on daily basis</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-availability-of-API-on-daily-basis/m-p/336998#M100002</link>
      <description>&lt;P&gt;The way I'd approach this is to add some calculations in the middle of the search to find the timestamp of the first event per day, per endpoint and also the last event per day, per endpoint. When calculating the &lt;CODE&gt;active_time&lt;/CODE&gt; and &lt;CODE&gt;inactive_time&lt;/CODE&gt;, I'd check to see if the current event was the first event of the day. If it is, then the calculation of active/inactive time will be &lt;CODE&gt;_time-relative_time(_time, "@d")&lt;/CODE&gt;, and if it's the last event of the day, then the calculation of active/inactive time will be &lt;CODE&gt;relative_time(_time, "+1d@d")-_time&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;So I think it would wind up like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;`urlendpoint` 
| search endpoint=* 
| eval Brand="xyz" 
| eval status=case(like(elb_status_code,"2%") OR like(elb_status_code,"3%") OR like(elb_status_code, "505") OR like(elb_status_code, "510") OR like(elb_status_code, "511"), "active", like(elb_status_code,"4%") OR like(elb_status_code,"500"), "inactive")
| reverse 
| streamstats current=f last(_time) AS last_time last(status) AS last_status by endpoint 
| bin span=1d _time as day
| eventstats earliest(_time) AS first_of_day latest(_time) AS last_of_day BY day
| stats values(last_time) AS last_time values(last_status) AS last_status values(status) AS status by endpoint, _time Brand 
| eval active_time=case(last_status="active" AND _time=first_of_day, _time-relative_time(_time, "@d"), last_status="active" AND _time=last_of_day, _time-relative_time(_time, "@d"), last_status="active", _time-last_time) 
| eval inactive_time=case(last_status="inactive" AND _time=first_of_day, _time-relative_time(_time, "@d"), last_status="inactive" AND _time=last_of_day, _time-relative_time(_time, "@d"), last_status="inactive", _time-last_time) 
| eval day = strftime(_time, "%d") 
| eval month=strftime(_time, "%m") 
| eval Date = strftime(_time, "%d/%m/%y") 
| stats sum(active_time) AS active by day month Date Brand endpoint 
| eval active=active/(3600) 
| sort - month day 
| fields - month
| fillnull value=0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I don't have a good dataset to test this on, so I'm happy to iterate if this gets you part of the way but has issues. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Hopefully the description at the top is clear enough to communicate the intent.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Apr 2018 15:37:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-availability-of-API-on-daily-basis/m-p/336998#M100002</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-16T15:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate availability of API on daily basis</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-availability-of-API-on-daily-basis/m-p/336999#M100003</link>
      <description>&lt;P&gt;Looking at this again - there's definitely a logic error that will arise at line 9, because the first_of_day and last_of_day won't pass through the stats command. But I'm not totally clear on the purpose of that line, so I can't quite figure out how to fix it. &lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 19:06:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-availability-of-API-on-daily-basis/m-p/336999#M100003</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2020-09-29T19:06:35Z</dc:date>
    </item>
  </channel>
</rss>

