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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Get Agentic with Splunk Lantern: Connect to Cisco Cloud Control, Transform ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...

July Community Events: Master ITSI 5.0 & Automate Splunk

Struggling with alert fatigue or feeling like you're spending more time on infrastructure maintenance than ...

New Release of Federated Search: Bringing Splunk Analytics to More of Your Data

Organizations today are generating more data than ever and storing it across cloud object stores, data lakes, ...