Reporting

hel p for calculating an interval in seconds between now date and a timestamp event

jip31
Motivator

hello

I need to monitore events included in a now() date and the event creation date
So I need to calculate the interval between these 2 dates in seconds
I have no error when I execute my code but the DiffInSeconds eval doesnt works correctly
So I think there is something wrong
For example, if the now() date time is 9:00 and the event creation date time is 9:05 (so 300 seconds), normally I should have some events displayed because if I execute just index="x" sourcetype="winhostmon" Type=Service Name=SplunkForwarder I have events
so what is the issue p^ease??

index="x" sourcetype="winhostmon" Type=Service Name=SplunkForwarder 
 | eval timenow =now() 
 | eval EventCreatedTime=_time 
 | eval DiffInSeconds = (timenow - EventCreatedTime) 
 | eval Status=if(DiffInSeconds<900, "Online", "Offline") 
 | convert timeformat="%d-%b-%Y %H:%M:%S %p %Z" ctime(EventCreatedTime) 
 | table host EventCreatedTime DiffInMinutes Status
Tags (1)
0 Karma
1 Solution

DavidHourani
Super Champion

Hi @jip31,

Great job on making the query the only problem is that you need only one entry per host and in your case you will end up with many. I've done some optimization by replacing some of your commands, it should work like this :

index="_internal" sourcetype="splunkd"
| stats latest(_time) as _time by host
| eval DiffInSeconds = (now() - _time) 
| eval DiffInMinutes=DiffInSeconds/60
| eval Status=if(DiffInSeconds<900, "Online", "Offline") 
| eval EventCreatedTime=strftime(_time,"%d-%b-%Y %H:%M:%S %p %Z" )
| table host EventCreatedTime DiffInMinutes Status

Let me know how it works out for you.

Best regards,
David

View solution in original post

0 Karma

DavidHourani
Super Champion

Hi @jip31,

Great job on making the query the only problem is that you need only one entry per host and in your case you will end up with many. I've done some optimization by replacing some of your commands, it should work like this :

index="_internal" sourcetype="splunkd"
| stats latest(_time) as _time by host
| eval DiffInSeconds = (now() - _time) 
| eval DiffInMinutes=DiffInSeconds/60
| eval Status=if(DiffInSeconds<900, "Online", "Offline") 
| eval EventCreatedTime=strftime(_time,"%d-%b-%Y %H:%M:%S %p %Z" )
| table host EventCreatedTime DiffInMinutes Status

Let me know how it works out for you.

Best regards,
David

0 Karma

jip31
Motivator

HI David
oh perfect!! thank you

0 Karma

koshyk
Super Champion

I couldn't find any major issues in your search, except the DiffInMinutes field
Please try

index="_internal" sourcetype="splunkd"
  | eval timenow=now() 
  | eval EventCreatedTime=_time 
  | eval DiffInSeconds = (timenow - EventCreatedTime) 
  | eval Status=if(DiffInSeconds<900, "Online", "Offline") 
  | convert timeformat="%d-%b-%Y %H:%M:%S %p %Z" ctime(EventCreatedTime) 
  | eval DiffInMinutes=DiffInSeconds/60
  | table host EventCreatedTime DiffInMinutes Status

cheers

0 Karma

jip31
Motivator

perfect also koshyk thanks to you

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...