Splunk Search

Find difference between time now and last event time

JoshuaJohn
Contributor

I am not sure why I am not getting results with this query, any suggestions?

index= ______
| stats max(_time) as last_event
| eval timenow=strftime(now(), "%Y-%m-%d %H:%M:%S.%3N")
| eval last_event=strftime('last_event', "%Y-%m-%d %H:%M:%S.%3N")
| eval diff = tostring((timenow - last_event), "duration")
| table diff

1 Solution

somesoni2
SplunkTrust
SplunkTrust

First problem with this query was that you're trying to arithmetic operation of string fields (strftime give string formatted date). You need timestamps in epoch format and format it later if needed. Second thing, not problem but big scope of optimization is to use tstats command instead of regular search. So give this a try

| tstats max(_time) as last_event WHERE index=.......
| eval diff=tostring(now()-last_event,"duration") | table diff

View solution in original post

somesoni2
SplunkTrust
SplunkTrust

First problem with this query was that you're trying to arithmetic operation of string fields (strftime give string formatted date). You need timestamps in epoch format and format it later if needed. Second thing, not problem but big scope of optimization is to use tstats command instead of regular search. So give this a try

| tstats max(_time) as last_event WHERE index=.......
| eval diff=tostring(now()-last_event,"duration") | table diff
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...