Splunk Search

subtracting 2 timestamps and then evaluating if that result is longer than 1 hour

samnew4598
Explorer

I have two timestamps that are in this format within my log events:

start: 2005-07-05T04:28:34.453494Z

end: 2005-07-05T05:28:39.462681Z

I would like to subtract the timestamps then evaluate them to see if the result is greater or equal to 1 hour and include that event in my query results

Extra:

if so how long?

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| rex "start: (?<start>\d{4}\-\d\d\-\d\dT\d\d:\d\d:\d\d\.\d+Z)"
| rex "end: (?<end>\d{4}\-\d\d\-\d\dT\d\d:\d\d:\d\d\.\d+Z)"
| eval start=strptime(start,"%Y-%m-%dT%H:%M:%S.%6QZ")
| eval end=strptime(end,"%Y-%m-%dT%H:%M:%S.%6QZ")
| eval diff=end-start
| where diff>=(60*60)
| fieldformat diff=tostring(diff,"duration")

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| rex "start: (?<start>\d{4}\-\d\d\-\d\dT\d\d:\d\d:\d\d\.\d+Z)"
| rex "end: (?<end>\d{4}\-\d\d\-\d\dT\d\d:\d\d:\d\d\.\d+Z)"
| eval start=strptime(start,"%Y-%m-%dT%H:%M:%S.%6QZ")
| eval end=strptime(end,"%Y-%m-%dT%H:%M:%S.%6QZ")
| eval diff=end-start
| where diff>=(60*60)
| fieldformat diff=tostring(diff,"duration")

samnew4598
Explorer

This works great! If you have time can you explain it a bit, I don't know why we need to use "rex" to extract the time? I get the rest of the query as that was one of my initial tries but without the rex. Thanks!

 

0 Karma
Get Updates on the Splunk Community!

A Season of Skills: New Splunk Courses to Light Up Your Learning Journey

There’s something special about this time of year—maybe it’s the glow of the holidays, maybe it’s the ...

Announcing the Migration of the Splunk Add-on for Microsoft Azure Inputs to ...

Announcing the Migration of the Splunk Add-on for Microsoft Azure Inputs to Officially Supported Splunk ...

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI! Discover how Splunk’s agentic AI ...