<?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: Number of seconds that have events by host in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320451#M160928</link>
    <description>&lt;P&gt;Try this, which will get you a count by second:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="whatever" index="whatever"... 
| convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS c_time
| stats count AS Events_Per_Second by c_time, host
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So, in my case for my home firewall that shows essentially 1 all the way down (with an occasional 2 or 3), and many seconds "missing" because they had no data.&lt;/P&gt;

&lt;P&gt;If I add this to the end of that:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats count AS Seconds_With_Data
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;That gives the number of seconds in my time frame (in this case I used last 24 hours) where there was data.  My answer was 8212.&lt;/P&gt;

&lt;P&gt;What else did you need?&lt;/P&gt;</description>
    <pubDate>Sun, 28 May 2017 13:53:30 GMT</pubDate>
    <dc:creator>Richfez</dc:creator>
    <dc:date>2017-05-28T13:53:30Z</dc:date>
    <item>
      <title>Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320450#M160927</link>
      <description>&lt;P&gt;Hello experts!&lt;BR /&gt;
My system is potentially producing several events per second and sometimes even several events at the same millisecond.&lt;BR /&gt;
When there are events within a second - no matter how many events, I want to count that second. At the end of say 24 hours scan, I have the number of seconds that had events and by that I'm able to produce a down time. &lt;BR /&gt;
&lt;STRONG&gt;Problem is that I can't figure out how to make that calculation by host.&lt;/STRONG&gt;&lt;BR /&gt;
So, this is my current search string - &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;sourcetype= "" &lt;BR /&gt;
| convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS c_time &lt;BR /&gt;
| stats count as individual_event by c_time &lt;BR /&gt;
| stats sum(individual_event) as total_sec by c_time &lt;BR /&gt;
| stats count AS num_of_seconds_with_errors&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I hope my explanation is suffice. &lt;BR /&gt;
Thanks in advance.&lt;BR /&gt;
Assaf&lt;/P&gt;</description>
      <pubDate>Sun, 28 May 2017 06:49:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320450#M160927</guid>
      <dc:creator>AssafLowenstein</dc:creator>
      <dc:date>2017-05-28T06:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320451#M160928</link>
      <description>&lt;P&gt;Try this, which will get you a count by second:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="whatever" index="whatever"... 
| convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS c_time
| stats count AS Events_Per_Second by c_time, host
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So, in my case for my home firewall that shows essentially 1 all the way down (with an occasional 2 or 3), and many seconds "missing" because they had no data.&lt;/P&gt;

&lt;P&gt;If I add this to the end of that:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats count AS Seconds_With_Data
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;That gives the number of seconds in my time frame (in this case I used last 24 hours) where there was data.  My answer was 8212.&lt;/P&gt;

&lt;P&gt;What else did you need?&lt;/P&gt;</description>
      <pubDate>Sun, 28 May 2017 13:53:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320451#M160928</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2017-05-28T13:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320452#M160929</link>
      <description>&lt;P&gt;How about the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats count where sourcetpe=&amp;lt;YourSourceType&amp;gt; by host _time span=1s
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;tstats&lt;/STRONG&gt; should work better than stats for the scenario describled above where you are interested only in events aggregated by metadata fields (&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Tstats"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Tstats&lt;/A&gt;)&lt;/P&gt;</description>
      <pubDate>Sun, 28 May 2017 13:56:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320452#M160929</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-05-28T13:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320453#M160930</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats count where sourcetype=YourSourcetypeHere BY host _time span=1s 
| stats count(eval(count&amp;gt;0)) AS OK_seconds BY host
| addinfo | eval span_in_seconds = info_max_time - info_min_time + 1 | fields - info_*
| eval down_seconds = span_in_seconds - OK_seconds
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 May 2017 19:47:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320453#M160930</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-05-28T19:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320454#M160931</link>
      <description>&lt;P&gt;I'm not looking for events per second. Let me reiterate.. if some event happened then I need to count that second. For example, if for host X I had events in the following milliseconds - &lt;BR /&gt;
19:00:45:000&lt;BR /&gt;
19:00:45:030&lt;BR /&gt;
19:00:45:150&lt;BR /&gt;
19:00:46:180&lt;BR /&gt;
19:00:46:240&lt;/P&gt;

&lt;P&gt;then for 19:00:45 i'll get 1 (more than 1 event happened in that second so I'm counting that second) and for 19:00:46 i'll get 1 (again, 2 events happened in that second so I'm counting that second).&lt;BR /&gt;
Eventually, for host X in that time frame the final result will be 2 because we found 2 seconds that had events.&lt;BR /&gt;
The same should be done for the rest of the hosts.&lt;/P&gt;

&lt;P&gt;End goal is to calculate overall system uptime. uptime is defined as time w/o any events. so we wish to calculate uptime for each host and then average across all hosts.&lt;/P&gt;

&lt;P&gt;Hope that clears things up now.&lt;BR /&gt;
Thanks again.&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2017 10:52:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320454#M160931</guid>
      <dc:creator>AssafLowenstein</dc:creator>
      <dc:date>2017-05-29T10:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320455#M160932</link>
      <description>&lt;P&gt;Did you try my answer?  That is &lt;EM&gt;exactly&lt;/EM&gt; what it does.&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2017 13:10:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320455#M160932</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-05-29T13:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320456#M160933</link>
      <description>&lt;P&gt;Great.  In that case, I think you already have most of the answer.  &lt;/P&gt;

&lt;P&gt;The second piece, adding &lt;CODE&gt;| stats count AS Seconds_With_Data&lt;/CODE&gt; to the end isn't a sum of events per second, it's literally a count of individual seconds that had any data at all in whatever time period you chose.  So, if you did "last 24 hours" it will be a count of the seconds where there was a 1 event or more, ignoring all the seconds without any events. &lt;/P&gt;

&lt;P&gt;Since there are 86400 seconds per day...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search here
| convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS c_time 
| stats count AS Events_Per_Second by c_time, host
| stats count AS Seconds_With_Data
| eval PercentDowntime = Seconds_With_Data / 86400
| eval PercentUptime = 1 - PercentDowntime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I &lt;EM&gt;may&lt;/EM&gt; have the math backwards for your uptime/downtime, but that should be easy to fix.&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2017 13:14:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320456#M160933</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2017-05-29T13:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320457#M160934</link>
      <description>&lt;P&gt;Also please see woodcock's answer below, which should be much faster.&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2017 13:18:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320457#M160934</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2017-05-29T13:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320458#M160935</link>
      <description>&lt;P&gt;Thanks. &lt;BR /&gt;
I'm getting &lt;STRONG&gt;Error in 'TsidxStats': WHERE clause is not an exact query&lt;/STRONG&gt;&lt;BR /&gt;
My WHERE clause has &lt;CODE&gt;sourcetype=&amp;lt;some source type&amp;gt; "&amp;lt;string that I'm looking for&amp;gt;"&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2017 13:30:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320458#M160935</guid>
      <dc:creator>AssafLowenstein</dc:creator>
      <dc:date>2017-05-29T13:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320459#M160936</link>
      <description>&lt;P&gt;There is no mention in your question of any &lt;CODE&gt;&amp;lt;string that I'm looking for&amp;gt;&lt;/CODE&gt;.  Because of this additional requirement, the &lt;CODE&gt;tstats&lt;/CODE&gt; option cannot be used and you should go with the answer from @rich7177 which can accommodate this new detail.&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2017 13:39:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320459#M160936</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-05-29T13:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320460#M160937</link>
      <description>&lt;P&gt;Thanks. &lt;BR /&gt;
I'm kinda new to Splunk so didn't realize that having  would make tstats irrelevant. sorry bout that. &lt;BR /&gt;
Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 09:05:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320460#M160937</guid>
      <dc:creator>AssafLowenstein</dc:creator>
      <dc:date>2017-05-30T09:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320461#M160938</link>
      <description>&lt;P&gt;Thanks rich. The answer makes lot of sense. &lt;BR /&gt;
I do miss the "average" part of the calculation. I need to calculate uptime for each host and then make average across all hosts. &lt;/P&gt;

&lt;P&gt;Thanks. &lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 14:15:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320461#M160938</guid>
      <dc:creator>AssafLowenstein</dc:creator>
      <dc:date>2017-05-30T14:15:08Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320462#M160939</link>
      <description>&lt;P&gt;Great, making progress!&lt;/P&gt;

&lt;P&gt;So, check the below:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your_base_search_here earliest=-86400s
| convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS c_time 
| stats count AS Events_Per_Second by c_time, host 
| stats count AS Seconds_With_Data by host 
| eventstats avg(Seconds_With_Data) AS Overall_Seconds_With_Data 
| eval PercentDowntime = Seconds_With_Data / 86400 
| eval OverallPercentDowntime = Overall_Seconds_With_Data / 86400
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I hard-coded an "earliest" in there, you can remove it if you don't want it there.&lt;/P&gt;

&lt;P&gt;The first stats stays the same, but the second we add &lt;CODE&gt;by host&lt;/CODE&gt; to the end so now, host by host, we have a list of how many seconds they sent in data on.  We then run &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Eventstats"&gt;eventstats&lt;/A&gt; to add an overall calculation for the overall downtime.&lt;/P&gt;

&lt;P&gt;Now, this isn't perfect.  If run over long enough periods is should be pretty good, but it fails to account for anything that hasn't sent in any events in the past  seconds.  (By that I mean that if system X hasn't sent anything in over the past 24 hours, it will not show it as "zero uptime" but just not show it at all.)  Hopefully this isn't a problem, but it is fixable if it is - might be worth a new question though because it's a bit of a topic on its own.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 14:33:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320462#M160939</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2017-05-30T14:33:52Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320463#M160940</link>
      <description>&lt;P&gt;Thanks rich for the elaborate explanation. &lt;BR /&gt;
About your last remark about not being perfect, last few seconds shouldn't be problem; plan is to run this search for a minimum of 24 hours period which should be long enough to eliminate any last few seconds discrepancies. &lt;/P&gt;

&lt;P&gt;Last question, might be a bit off topic but I'll try it anyway &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; .. If I wish to use visualization in my dashboard, how do I configure the &lt;STRONG&gt;single value&lt;/STRONG&gt; formatting to show the uptime column and not the host name column? &lt;/P&gt;

&lt;P&gt;Thanks again. I appreciate your time and patience. &lt;BR /&gt;
Assaf&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 03:22:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320463#M160940</guid>
      <dc:creator>AssafLowenstein</dc:creator>
      <dc:date>2017-06-01T03:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Number of seconds that have events by host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320464#M160941</link>
      <description>&lt;P&gt;Figured out how to workaround the single value issue. &lt;BR /&gt;
One just need to place the value in the first index of the table and single value will pick it up.&lt;BR /&gt;
I used &lt;STRONG&gt;table&lt;/STRONG&gt; to rearrange the columns and that's it.&lt;/P&gt;

&lt;P&gt;Thanks again!!&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 07:38:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Number-of-seconds-that-have-events-by-host/m-p/320464#M160941</guid>
      <dc:creator>AssafLowenstein</dc:creator>
      <dc:date>2017-06-01T07:38:43Z</dc:date>
    </item>
  </channel>
</rss>

