Splunk Search

How to calculate age of event?

ednk
Explorer

Hi 

I have for each event the open_time and update_time,

I want to calculate the age of the event,

like: 

open_time               update_time           age

2022-03-26            2022-04-26            1m

2022-04-22            2022-04-26             4d

 

any idea ?

thanks

Labels (2)
Tags (2)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@ednk 

You can use tostring function to get age sorta from days. 

Can you please try this?

| makeresults | eval _raw="open_time               update_time           age
2022-02-26            2022-04-26            1m
2022-04-22            2022-04-26             4d" 
| multikv forceheader=1
|table open_time update_time 
| rename comment as "Upto now is for sample data only" 
| eval open_time = strptime(open_time, "%Y-%m-%d") 
| eval update_time = strptime(update_time, "%Y-%m-%d") 
| eval field_in_secs= update_time - open_time
| eval string_dur=tostring(field_in_secs, "duration")
| eval formatted_dur = replace(string_dur,"(?:(\d+)\+)?0?(\d+):0?(\d+):0?(\d+)","\1d \2h \3m \4s")
| eval Age=replace(formatted_dur, "^d (0h (0m )?)?","")

 

 Thanks
KV


If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

0 Karma

ednk
Explorer

@kamlesh_vaghela 

thank you, 

this is what I got.

how can I remove the seconds and milliseconds?

open_time update_time Age field_in_secs formatted_dur string_dur

1645826400.0000001650920400.00000058d 23h 0m 0s.0000005094000.00000058d 23h 0m 0s.00000058+23:00:00.000000
1650574800.0000001650920400.0000004d 0h 0m 0s.000000345600.0000004d 0h 0m 0s.0000004+00:00:00.000000
Tags (1)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@ednk 

Please try this.

| makeresults | eval _raw="open_time               update_time           age
2022-02-26            2022-04-26            1m
2022-04-22            2022-04-26             4d" 
| multikv forceheader=1
|table open_time update_time 
| rename comment as "Upto now is for sample data only" 
| eval open_time = round(strptime(open_time, "%Y-%m-%d"))
| eval update_time = round(strptime(update_time, "%Y-%m-%d"))
| eval field_in_secs= update_time - open_time
| eval string_dur=tostring(field_in_secs, "duration")
| eval formatted_dur = replace(string_dur,"(?:(\d+)\+)?0?(\d+):0?(\d+):0?(\d+)","\1d \2h \3m \4s")
| eval Age=replace(formatted_dur, "^d (0h (0m )?)?","")

 

KV

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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...