<?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 efficient way of comparing with historic events using percentile in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198145#M57273</link>
    <description>&lt;P&gt;Hi, I have a query that is  meant to compare longitudinal count of an event of a given day (e.g. today) with historical longitudinal percentiles. &lt;/P&gt;

&lt;P&gt;query 1: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=interfaces sourcesession="MICHART_SIU_HL7_INBOUND"  [`define_relative_week(1)`] date_wday!=saturday date_wday!=sunday   | bin _time span=20m |  stats  count as count by _time  | eval timeofday=tonumber(strftime(_time, "%H"))*3600 + tonumber(strftime(_time,"%M"))*60  | stats perc5(count) as perc5 perc95(count) as perc95 by timeofday  |  eval timeofday= tostring(timeofday,"duration")  | join type=outer timeofday [search index=interfaces sourcesession="MICHART_SIU_HL7_INBOUND"   | bin _time span=20m  | eval timeofday=tonumber(strftime(_time, "%H"))*3600 + tonumber(strftime(_time,"%M"))*60| eval timeofday= tostring(timeofday,"duration")| stats  count by timeofday] |sort timeofday
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;where &lt;CODE&gt;define_relative_week&lt;/CODE&gt; is a macro:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;stats count | addinfo | eval earliest=(info_min_time-604800*$n$) | eval earliest=strftime(earliest,"%m/%d/%Y:%H:%M:%S") | eval latest=(info_max_time-86400) | eval latest=strftime(latest,"%m/%d/%Y:%H:%M:%S") | return earliest,latest
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Basically, this search grabs historical data (previous week) and bins the time, and then compute percentile by binned time. &lt;/P&gt;

&lt;P&gt;Some of the things that I'm grappling with are:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;multiple event types. 
How to generalize this to multiple event types?  For example, I have many types of 
events (e.g. with different sourcesession values).  And recomputing the historical percentile for each type of events is going to be too much for computations. &lt;/LI&gt;
&lt;LI&gt;Acceleration.
Suppose for now, there is only one event of interest.  How can I further accelerate this search?  I need to set-up an alarm by comparing current count with 5% and 95% percentiles for the same time period, and the saved alarm/search will run every 15 minutes and inspect the previous 15 minutes to see whether there is any anomaly. &lt;/LI&gt;
&lt;LI&gt; I tried the following query which filters out irrelevant time ranges. But to my surprise it runs slower than the original query.  Any insights why? &lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;query 2:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| gentimes  end=-1 increment=24h [stats count | addinfo | eval start=(info_min_time-604800*2) | eval start=strftime(start,"%m/%d/%y:%H:%M:00")| return start]
| rename starttime as earliest 
| eval latest=earliest+20*60
| sort - earliest
| map maxsearches=99999
      search="search earliest=$earliest$ latest=$latest$ index=interfaces sourcesession=MICHART_SIU_HL7_INBOUND  " | ...
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 13 Jun 2014 22:08:46 GMT</pubDate>
    <dc:creator>kundeng</dc:creator>
    <dc:date>2014-06-13T22:08:46Z</dc:date>
    <item>
      <title>efficient way of comparing with historic events using percentile</title>
      <link>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198145#M57273</link>
      <description>&lt;P&gt;Hi, I have a query that is  meant to compare longitudinal count of an event of a given day (e.g. today) with historical longitudinal percentiles. &lt;/P&gt;

&lt;P&gt;query 1: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=interfaces sourcesession="MICHART_SIU_HL7_INBOUND"  [`define_relative_week(1)`] date_wday!=saturday date_wday!=sunday   | bin _time span=20m |  stats  count as count by _time  | eval timeofday=tonumber(strftime(_time, "%H"))*3600 + tonumber(strftime(_time,"%M"))*60  | stats perc5(count) as perc5 perc95(count) as perc95 by timeofday  |  eval timeofday= tostring(timeofday,"duration")  | join type=outer timeofday [search index=interfaces sourcesession="MICHART_SIU_HL7_INBOUND"   | bin _time span=20m  | eval timeofday=tonumber(strftime(_time, "%H"))*3600 + tonumber(strftime(_time,"%M"))*60| eval timeofday= tostring(timeofday,"duration")| stats  count by timeofday] |sort timeofday
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;where &lt;CODE&gt;define_relative_week&lt;/CODE&gt; is a macro:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;stats count | addinfo | eval earliest=(info_min_time-604800*$n$) | eval earliest=strftime(earliest,"%m/%d/%Y:%H:%M:%S") | eval latest=(info_max_time-86400) | eval latest=strftime(latest,"%m/%d/%Y:%H:%M:%S") | return earliest,latest
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Basically, this search grabs historical data (previous week) and bins the time, and then compute percentile by binned time. &lt;/P&gt;

&lt;P&gt;Some of the things that I'm grappling with are:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;multiple event types. 
How to generalize this to multiple event types?  For example, I have many types of 
events (e.g. with different sourcesession values).  And recomputing the historical percentile for each type of events is going to be too much for computations. &lt;/LI&gt;
&lt;LI&gt;Acceleration.
Suppose for now, there is only one event of interest.  How can I further accelerate this search?  I need to set-up an alarm by comparing current count with 5% and 95% percentiles for the same time period, and the saved alarm/search will run every 15 minutes and inspect the previous 15 minutes to see whether there is any anomaly. &lt;/LI&gt;
&lt;LI&gt; I tried the following query which filters out irrelevant time ranges. But to my surprise it runs slower than the original query.  Any insights why? &lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;query 2:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| gentimes  end=-1 increment=24h [stats count | addinfo | eval start=(info_min_time-604800*2) | eval start=strftime(start,"%m/%d/%y:%H:%M:00")| return start]
| rename starttime as earliest 
| eval latest=earliest+20*60
| sort - earliest
| map maxsearches=99999
      search="search earliest=$earliest$ latest=$latest$ index=interfaces sourcesession=MICHART_SIU_HL7_INBOUND  " | ...
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jun 2014 22:08:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198145#M57273</guid>
      <dc:creator>kundeng</dc:creator>
      <dc:date>2014-06-13T22:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: efficient way of comparing with historic events using percentile</title>
      <link>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198146#M57274</link>
      <description>&lt;P&gt;Wow - this is complicated. I think you can simplify a lot by using Splunk's built-in time math...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=interfaces earliest=-7d latest=@d sourcesession="MICHART_SIU_HL7_INBOUND"
| bucket _time span=20m | eval timeOfDay=strftime(_time,"%H:%M")
| stats count by timeOfDay
| start p5(count) as perc5 p95(count) as perc95 by timeOfDay 
| append [ search index=interfaces earliest=@d
     | bucket _time span=20m | eval timeOfDay=strftime(_time,"%H:%M")
     | stats count by timeOfDay  ]
| chart first(perc5) as perc5 first(perc95) as perc95 first(count) as count by timeOfDay
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This makes a nice chart. If you want to build an alert - try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=interfaces earliest=-7d latest=@d 
| bucket _time span=20m | eval timeOfDay=strftime(_time,"%H:%M")
| stats count by timeOfDay sourcesession
| start p5(count) as perc5 p95(count) as perc95 by timeOfDay sourcesession
| append [ search index=interfaces earliest=@d
     | bucket _time span=20m | eval timeOfDay=strftime(_time,"%H:%M")
     | stats count by timeOfDay sourcesession ]
| stats first(perc5) as perc5 first(perc95) as perc95 first(count) as count by timeOfDay sourcesession
| where count &amp;lt; perc5 OR count &amp;gt; perc95
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Set the alert condition for "# results &amp;gt; 0". Note that the alert checks across all source sessions at once. If you leave off the last line, it will make a report as well, but it won't look as nice as the first one.&lt;/P&gt;

&lt;P&gt;Oh, I didn't use the macro at all.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jun 2014 00:34:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198146#M57274</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2014-06-14T00:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: efficient way of comparing with historic events using percentile</title>
      <link>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198147#M57275</link>
      <description>&lt;P&gt;doesn't quite work yet. Besides some errors:&lt;BR /&gt;
*"stats count by _time sourcesession" should be added before eval. &lt;BR /&gt;
*typo "start"&lt;/P&gt;

&lt;P&gt;There are two intertwining problems in result table. Suppose current time is 10:20, and suppose between 9:00-10:00, there is NO data at all for a sourcesession. &lt;BR /&gt;
1. The abnormal row  will be filtered out, because it's count==NULL.  However, if I fill NULL count with 0, &lt;BR /&gt;
2.  then I will include extra rows such as rows for timeofday=11:00, because its count is also NULL. &lt;/P&gt;

&lt;P&gt;How do I deal with this?&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jun 2014 16:28:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198147#M57275</guid>
      <dc:creator>kundeng</dc:creator>
      <dc:date>2014-06-16T16:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: efficient way of comparing with historic events using percentile</title>
      <link>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198148#M57276</link>
      <description>&lt;P&gt;So here is the query that is being tested and it needs to be tweaked to further reduce false positives.  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=interfaces earliest=-10d latest=@d    | bucket _time span=10m   | stats count by _time sourcesession  | eval timeOfDay=strftime(_time,"%H:%M")  | stats p5(count) as perc5 p95(count) as perc95 by timeOfDay sourcesession  | append   [ search index=interfaces earliest=-40m latest=-10m       | bucket _time span=10m | eval timeOfDay=strftime(_time,"%H:%M")      | stats count by timeOfDay sourcesession |fillnull ]
 | stats first(perc5) as perc5 first(perc95) as perc95 first(count) as count by timeOfDay sourcesession | eval etime=strptime(timeOfDay,"%H:%M") | where etime&amp;gt;=relative_time(now(),"-40m") AND etime&amp;lt;relative_time(now(),"-10m") | fillnull | where (count&amp;lt;perc5) | fields - etime | sort sourcesession timeOfDay | stats count by sourcesession |search count&amp;gt;=3
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jun 2014 14:26:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/efficient-way-of-comparing-with-historic-events-using-percentile/m-p/198148#M57276</guid>
      <dc:creator>kundeng</dc:creator>
      <dc:date>2014-06-19T14:26:28Z</dc:date>
    </item>
  </channel>
</rss>

