Splunk Search

How to find time difference between below time formats?

manurajrajappan
New Member

New_Time=2020‎-‎01‎-‎22T03:17:36.385000000Z
Previous_Time=2020‎-‎01‎-‎22T03:17:36.388208200Z

I tried below query and getting blank results:
sourcetype="XXXXXX" host="XXXXXX"
| eval sent= strptime(New_Time,"%m/%d/%Y %H:%M:%S:%9N")
| eval start= strptime(Previous_Time,"%m/%d/%Y %H:%M:%S:%9N")
| eval diff= sent-start
| table New_Time, Previous_Time, sent, start, diff

Tags (1)
0 Karma

PavelProstine
Explorer

try with %Y-%m-%dT%H:%M:%S:%9NZ

date +"%Y-%m-%dT%H:%M:%S:%9NZ"
2020-01-22T09:31:49:779258877Z
0 Karma

vnravikumar
Champion

Hi

Check this

| makeresults 
 | eval New_Time="2020-01-22T03:17:36.385000000Z",Previous_Time="2020-01-22T03:17:36.388208200Z" 
 | eval diff = strptime(Previous_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") -strptime(New_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") 
 | fieldformat diff = tostring(diff, "duration") 
 | table diff
0 Karma

manurajrajappan
New Member

Thanks Ravi. But my concern is: I need to configure a query for time differences greater than 3 seconds.

New_Time and Previous_Time are the fields for each event in all servers.

Above query helps me for single time difference. I need to run for all servers and also for all events. How to configure this query?

I tried with below query but no luck.

  | eval New_Time, Previous_Time
  | eval diff = strptime(Previous_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") -strptime(New_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") 
  | fieldformat diff = tostring(diff, "duration") 
  | table diff
0 Karma

vnravikumar
Champion

Hi

check this

 sourcetype="XXXXXX" host="XXXXXX"
  | eval temp = strptime(Previous_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") -strptime(New_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") 
  | fieldformat diff = tostring(temp, "duration") 
  | table New_Time, Previous_Time, sent, start, diff,temp |where temp >3
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Just twisting @vnravikumar 's answer.

@manurajrajappan

Can you please try this?

YOUR_SEARCH | eval diff = strptime(Previous_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") -strptime(New_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") 
| where diff >3
| fieldformat diff = tostring(diff, "duration") 
| table New_Time, Previous_Time, diff

Sample:

| makeresults 
| eval New_Time="2020-01-22T03:17:36.385000000Z",Previous_Time="2020-01-22T03:17:39.388208200Z" 
| rename comment as "Upto this is for data generation only" 
| eval diff = strptime(Previous_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") -strptime(New_Time, "%Y-%m-%dT%H:%M:%S.%9N%Z") 
| where diff >3
| fieldformat diff = tostring(diff, "duration") 
| table New_Time, Previous_Time, diff
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...