<?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 Re: How to calculate a per-second event count for a rolling time window? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-a-per-second-event-count-for-a-rolling-time/m-p/51598#M12494</link>
    <description>&lt;P&gt;This is currently a bit tricky. The first method mentioned (a simple stats dividing the event count by the search time window) is the one that should work but as of Splunk 4.2.2, real-time search windows do not back-fill with historical events that would match the window when the search is fired. This will however be possible in 4.2.3 and beyond.&lt;/P&gt;

&lt;P&gt;In the meantime, you can achieve the desired result with the following search :&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;index=my_events | eval rt_window=300 | eval search_time=now() | eval seconds_elapsed=(_time - search_time) | eval secs=case(seconds_elapsed&amp;lt;0,"1",seconds_elapsed&amp;lt;rt_window,seconds_elapsed,seconds_elapsed&amp;gt;rt_window OR seconds_elapsed=rt_window,rt_window) | stats count as ecount, last(secs) AS seconds| stats values(ecount) AS "event count", values(seconds) AS "real-time search window (last X seconds)", values(eval(ecount/seconds)) AS eps&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;The logic behind this search is that we should divide the event count (the &lt;CODE&gt;ecount&lt;/CODE&gt; field in this search) by the number of seconds that the time window spans (here &lt;CODE&gt;rt_window&lt;/CODE&gt;, which is 300 seconds in the case of our 5-minute RT window) &lt;STRONG&gt;unless&lt;/STRONG&gt; the search has not run for a full time window cycle yet. In that case, we will use &lt;A href="http://www.splunk.com/base/Documentation/latest/SearchReference/CommonEvalFunctions" target="_blank"&gt;eval case()&lt;/A&gt; to set the value of the divisor to the span of time that the search has run for &lt;CODE&gt;(seconds_elapsed = _time - search_time)&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Fortunately, this will be much easier to do in 4.2.3 with the RT-window back-fill option!&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 09:46:18 GMT</pubDate>
    <dc:creator>hexx</dc:creator>
    <dc:date>2020-09-28T09:46:18Z</dc:date>
    <item>
      <title>How to calculate a per-second event count for a rolling time window?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-a-per-second-event-count-for-a-rolling-time/m-p/51597#M12493</link>
      <description>&lt;P&gt;I would like to display a per-second event count for a rolling time window, say 5 minutes.&lt;BR /&gt;&lt;BR /&gt;
I have tried the following approaches but without success :&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;Using stats during a 5-minute window real-time search :&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;&lt;CODE&gt;sourcetype=my_events | stats count as ecount | stats values(eval(ecount/300)) AS eps&lt;/CODE&gt;&lt;BR /&gt;&lt;BR /&gt;
=&amp;gt; This takes 5 minutes to give an accurate result. Until the search has run for the full length of the real-time window and "filled it" with events, the resulting EPS is inaccurate.&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;Using stats and timechart with a span of 5 minutes during a 5-minute window real-time search :&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;&lt;CODE&gt;sourcetype=my_events | bin _time span=5min | stats count | timechart span=5min per_second(count) AS eps&lt;/CODE&gt;&lt;BR /&gt;&lt;BR /&gt;
=&amp;gt; This discretizes results in 5 minute buckets, which is not what I want.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 09:46:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-a-per-second-event-count-for-a-rolling-time/m-p/51597#M12493</guid>
      <dc:creator>hexx</dc:creator>
      <dc:date>2020-09-28T09:46:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a per-second event count for a rolling time window?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-a-per-second-event-count-for-a-rolling-time/m-p/51598#M12494</link>
      <description>&lt;P&gt;This is currently a bit tricky. The first method mentioned (a simple stats dividing the event count by the search time window) is the one that should work but as of Splunk 4.2.2, real-time search windows do not back-fill with historical events that would match the window when the search is fired. This will however be possible in 4.2.3 and beyond.&lt;/P&gt;

&lt;P&gt;In the meantime, you can achieve the desired result with the following search :&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;index=my_events | eval rt_window=300 | eval search_time=now() | eval seconds_elapsed=(_time - search_time) | eval secs=case(seconds_elapsed&amp;lt;0,"1",seconds_elapsed&amp;lt;rt_window,seconds_elapsed,seconds_elapsed&amp;gt;rt_window OR seconds_elapsed=rt_window,rt_window) | stats count as ecount, last(secs) AS seconds| stats values(ecount) AS "event count", values(seconds) AS "real-time search window (last X seconds)", values(eval(ecount/seconds)) AS eps&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;The logic behind this search is that we should divide the event count (the &lt;CODE&gt;ecount&lt;/CODE&gt; field in this search) by the number of seconds that the time window spans (here &lt;CODE&gt;rt_window&lt;/CODE&gt;, which is 300 seconds in the case of our 5-minute RT window) &lt;STRONG&gt;unless&lt;/STRONG&gt; the search has not run for a full time window cycle yet. In that case, we will use &lt;A href="http://www.splunk.com/base/Documentation/latest/SearchReference/CommonEvalFunctions" target="_blank"&gt;eval case()&lt;/A&gt; to set the value of the divisor to the span of time that the search has run for &lt;CODE&gt;(seconds_elapsed = _time - search_time)&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Fortunately, this will be much easier to do in 4.2.3 with the RT-window back-fill option!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 09:46:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-a-per-second-event-count-for-a-rolling-time/m-p/51598#M12494</guid>
      <dc:creator>hexx</dc:creator>
      <dc:date>2020-09-28T09:46:18Z</dc:date>
    </item>
  </channel>
</rss>

