<?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 get time  difference between the current event and another event of the same host in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632260#M219628</link>
    <description>&lt;P&gt;So for the idea of correlating multiple events together, you can do this in a single pass without a join e.g.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=abc sourcetype=foo host=hostabc
| eval boot_time=case(searchmatch("Linux version"),_time)
| stats latest(_time) latest(boot_time) by host
| rename latest(*) -&amp;gt; *
| convert timeformat="%F %T" ctime(_time) as Latest_Event_Time ctime(btoot_time) as Boot_Time
| eval delta=_time-boot_time, UP_Time = tostring(delta,"duration")
| fields host Boot_Time Latest_Event_Time UP_Time&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;But the more practical problem you'll run into is the unbounded nature of how far in the past boot time can be... thus requiring this search to become almost an All Time search which doesn't scale well at all.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;If you can add data sources... instead of relying just on this log, you could have a scripted input that captures the output of `uptime` on a regular basis.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;But if not, another option may be to maintain a lookup containing the last boot time of a host, and pull that data in at search time instead... that way your search for looking at the latest events can be a much smaller window. Doing this off the top of my head, assuming a KVStore host_boots keyed by host, something like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=abc sourcetype=foo host=hostabc
| eval boot_time=case(searchmatch("Linux version"),_time)
| stats latest(_time) latest(boot_time) by host
| rename latest(*) -&amp;gt; *
| lookup host_boots host OUTPUT boot_time AS last_boot
| eval boot_time=coalesce(boot_time,last_boot)
| fields - last_boot
| outputlookup append=t key_field=host host_boots
| convert timeformat="%F %T" ctime(_time) as Latest_Event_Time ctime(btoot_time) as Boot_Time
| eval delta=_time-boot_time, UP_Time = tostring(delta,"duration")
| fields host Boot_Time Latest_Event_Time UP_Time&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;The question then becomes if you pull back this lookup for unseen hosts or not... and or if updating in this way makes sense (since the _time would get updated as frequently as the boot_time field...) and some other nuances...&lt;/P&gt;</description>
    <pubDate>Sat, 25 Feb 2023 03:19:59 GMT</pubDate>
    <dc:creator>acharlieh</dc:creator>
    <dc:date>2023-02-25T03:19:59Z</dc:date>
    <item>
      <title>How to get time  difference between the current event and another event of the same host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632255#M219626</link>
      <description>&lt;P&gt;Hello Splunkers ,&lt;/P&gt;&lt;P&gt;I am trying to find the up time of hosts by calculating the difference between the latest event for that host and last time it booted .&lt;BR /&gt;&lt;BR /&gt;The following event describes that particular host has been booted.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class=""&gt;2023-02-24T08:58:38.796336-08:00&lt;/SPAN&gt; &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;hostabc&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/STRONG&gt; &lt;SPAN class=""&gt;kernel:&lt;/SPAN&gt;&lt;SPAN&gt; [ &lt;/SPAN&gt;&lt;SPAN class=""&gt;0.000000&lt;/SPAN&gt;&lt;SPAN&gt;] &lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;Linux&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;version&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class=""&gt;5.15.0-58-generic&lt;/SPAN&gt;&lt;SPAN&gt; (&lt;/SPAN&gt;&lt;SPAN class=""&gt;buildd@lcy02-amd64-101&lt;/SPAN&gt;&lt;SPAN&gt;) (&lt;/SPAN&gt;&lt;SPAN class=""&gt;gcc&lt;/SPAN&gt;&lt;SPAN&gt; (&lt;/SPAN&gt;&lt;SPAN class=""&gt;Ubuntu&lt;/SPAN&gt; &lt;SPAN class=""&gt;11.3.0-1ubuntu1~22.04&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN class=""&gt;11.3.0&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN class=""&gt;GNU&lt;/SPAN&gt; &lt;SPAN class=""&gt;ld&lt;/SPAN&gt;&lt;SPAN&gt; (&lt;/SPAN&gt;&lt;SPAN class=""&gt;GNU&lt;/SPAN&gt; &lt;SPAN class=""&gt;Binutils&lt;/SPAN&gt; &lt;SPAN class=""&gt;for&lt;/SPAN&gt; &lt;SPAN class=""&gt;Ubuntu&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN class=""&gt;2.38&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN class=""&gt;#64-Ubuntu&lt;/SPAN&gt; &lt;SPAN class=""&gt;SMP&lt;/SPAN&gt; &lt;SPAN class=""&gt;Thu&lt;/SPAN&gt; &lt;SPAN class=""&gt;Jan&lt;/SPAN&gt; &lt;SPAN class=""&gt;5&lt;/SPAN&gt; &lt;SPAN class=""&gt;11:43:13&lt;/SPAN&gt; &lt;SPAN class=""&gt;UTC&lt;/SPAN&gt; &lt;SPAN class=""&gt;2023&lt;/SPAN&gt;&lt;SPAN&gt; (&lt;/SPAN&gt;&lt;SPAN class=""&gt;Ubuntu&lt;/SPAN&gt; &lt;SPAN class=""&gt;5.15.0-58.64-generic&lt;/SPAN&gt; &lt;SPAN class=""&gt;5.15.74)&lt;BR /&gt;&lt;BR /&gt;The following event is the latest event of that host&lt;BR /&gt;&lt;STRONG&gt;2023-02-24T14:04:51.115717-08:00&lt;/STRONG&gt; &lt;STRONG&gt;&lt;SPAN class=""&gt;hostabc&lt;/SPAN&gt;&lt;/STRONG&gt; sssd_nss&lt;SPAN&gt;[&lt;/SPAN&gt;248054&lt;SPAN&gt;]&lt;/SPAN&gt;: Starting up&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Firstly I want to get the difference between&amp;nbsp;&lt;STRONG&gt;2023-02-24T14:04:51.115717-08:00 -&amp;nbsp;2023-02-24T08:58:38.796336-08:00&amp;nbsp;&lt;BR /&gt;&lt;/STRONG&gt;Secondly If the difference is greater than 60 minutes create a new file called status and say it as&lt;STRONG&gt; down&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/STRONG&gt;Thanks in Advance&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 22:15:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632255#M219626</guid>
      <dc:creator>power12</dc:creator>
      <dc:date>2023-02-24T22:15:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to get time  difference between the current event and another event of the same host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632257#M219627</link>
      <description>&lt;P&gt;I tried the below search&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=abc host=hostabc
| stats latest(_raw) as raw latest(_time) as time by host
| eval Latest_Event_Time=strftime(time,"%Y-%m-%d %H:%M:%S")
| join host
    [search index=abc host=hostabc "Linux version" 
| stats latest(_time) as btime by host
| eval Boot_Time=strftime(btime,"%Y-%m-%d %H:%M:%S")]
| eval UP_Time=tostring(time-btime,"duration")
| fields host Boot_Time Latest_Event_Time  UP_Time&lt;/LI-CODE&gt;&lt;P&gt;.Is there any other more faster and easy way ?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 23:07:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632257#M219627</guid>
      <dc:creator>power12</dc:creator>
      <dc:date>2023-02-24T23:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to get time  difference between the current event and another event of the same host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632260#M219628</link>
      <description>&lt;P&gt;So for the idea of correlating multiple events together, you can do this in a single pass without a join e.g.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=abc sourcetype=foo host=hostabc
| eval boot_time=case(searchmatch("Linux version"),_time)
| stats latest(_time) latest(boot_time) by host
| rename latest(*) -&amp;gt; *
| convert timeformat="%F %T" ctime(_time) as Latest_Event_Time ctime(btoot_time) as Boot_Time
| eval delta=_time-boot_time, UP_Time = tostring(delta,"duration")
| fields host Boot_Time Latest_Event_Time UP_Time&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;But the more practical problem you'll run into is the unbounded nature of how far in the past boot time can be... thus requiring this search to become almost an All Time search which doesn't scale well at all.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;If you can add data sources... instead of relying just on this log, you could have a scripted input that captures the output of `uptime` on a regular basis.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;But if not, another option may be to maintain a lookup containing the last boot time of a host, and pull that data in at search time instead... that way your search for looking at the latest events can be a much smaller window. Doing this off the top of my head, assuming a KVStore host_boots keyed by host, something like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=abc sourcetype=foo host=hostabc
| eval boot_time=case(searchmatch("Linux version"),_time)
| stats latest(_time) latest(boot_time) by host
| rename latest(*) -&amp;gt; *
| lookup host_boots host OUTPUT boot_time AS last_boot
| eval boot_time=coalesce(boot_time,last_boot)
| fields - last_boot
| outputlookup append=t key_field=host host_boots
| convert timeformat="%F %T" ctime(_time) as Latest_Event_Time ctime(btoot_time) as Boot_Time
| eval delta=_time-boot_time, UP_Time = tostring(delta,"duration")
| fields host Boot_Time Latest_Event_Time UP_Time&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;The question then becomes if you pull back this lookup for unseen hosts or not... and or if updating in this way makes sense (since the _time would get updated as frequently as the boot_time field...) and some other nuances...&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2023 03:19:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632260#M219628</guid>
      <dc:creator>acharlieh</dc:creator>
      <dc:date>2023-02-25T03:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to get time  difference between the current event and another event of the same host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632262#M219630</link>
      <description>&lt;P&gt;The most obvious approach would be to use transaction to track your events from the same host with a proper starts_with option so that it begins on each boot. But the transaction command is a "heavy" command performancewise and might not work well with your case. Especially if you have no time constraints.&lt;/P&gt;&lt;P&gt;You can probably use streamstats to "carry over" last boot time to the subsequent results.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2023 08:01:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632262#M219630</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-02-25T08:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to get time  difference between the current event and another event of the same host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632279#M219636</link>
      <description>&lt;P&gt;You are correct that join is nonperformant and is best avoided. &amp;nbsp;I'd like to first illustrate a literal interpretation of your requirement, then a more straight-forward, much simpler solution based on the nature of your events.&lt;/P&gt;&lt;H3&gt;Literal interpretation&lt;/H3&gt;&lt;P&gt;Here we used eventstats to figure out when the server last booted.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex "kernel:\s*\[\s*0.0+\]\s*(?&amp;lt;booted&amp;gt;[^\(]+)\s*\(" ``` 0.000000 is boot time ```
| eventstats max(_time) as last_boot by booted host
| stats max(_time) as _time by last_boot host
| eval UP_Time = _time - last_boot
```| where _time - lastboot &amp;gt; 3600```&lt;/LI-CODE&gt;&lt;P&gt;In the above, I extracted kernel version into booted as bonus information, assuming the kernel did not change during the search period. &amp;nbsp;If there is a chance that kernel version changes, you can narrow the regex match to only extract the string "Linux", for example.&lt;/P&gt;&lt;P&gt;I don't know why you want to convert UP_Time to string if it is intended for any purpose other than/in addition to display. &amp;nbsp;So, that function is skipped.&lt;/P&gt;&lt;H3&gt;Linux syslog&lt;/H3&gt;&lt;P&gt;Ultimately, however, the data you have seems to have come from Linux syslog, in which every event already carries UP_Time in that first bracket. &amp;nbsp;No need to perform any calculation at all.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex "^[^\[]+\s(?&amp;lt;process&amp;gt;\S+)[:\s+]*\[\s*(?&amp;lt;UP_Time&amp;gt;\d+)"
| stats max(UP_Time) as UP_Time by host
``` |  where UP_Time &amp;gt; 3600 ```&lt;/LI-CODE&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 06:26:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632279#M219636</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-02-26T06:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to get time  difference between the current event and another event of the same host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632389#M219678</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/56406"&gt;@acharlieh&lt;/a&gt;&amp;nbsp; I ran your first search...it shows me the UP_Time but the&amp;nbsp;boot_time output is blank or&amp;nbsp; it shows empty results&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2023 16:44:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632389#M219678</guid>
      <dc:creator>power12</dc:creator>
      <dc:date>2023-02-27T16:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to get time  difference between the current event and another event of the same host</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632391#M219680</link>
      <description>&lt;P&gt;If there isn't a boot event (i.e. an event with the words "Linux version" in it) for a particular host in your time window, boot_time will come back as blank...&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;This is the problem I was mentioning:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;But the more practical problem you'll run into is the unbounded nature of how far in the past boot time can be... thus requiring this search to become almost an All Time search which doesn't scale well at all.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2023 16:48:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-difference-between-the-current-event-and-another/m-p/632391#M219680</guid>
      <dc:creator>acharlieh</dc:creator>
      <dc:date>2023-02-27T16:48:41Z</dc:date>
    </item>
  </channel>
</rss>

