Splunk Search

Can you help me with my query involving the eval command and strftime?

tb5821
Communicator
| eval lastChange=strftime(time_of_last_change,"%m-%d-%y %I:%M:%S %p") 
| eval timenow=now()
| eval last1hr=strftime(relative_time(now(), "-1h@s"), "%m-%d-%y %I:%M:%S %p")
| eval timenow=strftime(now(),"%m-%d-%y %I:%M:%S %p")
| eval actualchange= prev_count-count 
| where prev_count != count
| dedup namespace 
| table namespace actualchange prev_count count timenow lastChange last1hr diff

for some reason I can't seem to diff last1hr and lastChange! — what am I doing wrong?

Tags (2)
0 Karma
1 Solution

sduff_splunk
Splunk Employee
Splunk Employee

You are using strftime, which is converting your times into text strings, which you can't do maths operations on.

What format is time_of_last_change? Is it a unix timestamp? If so, you can calculate the difference by doing eval diff=relative_time(now(), "-1h@s") - time_of_last_change.

Otherwise, you most probably want to use the strptime command, which converts times in text format into a unix timestamp, which is seconds. From there, you can calculate the time difference.

View solution in original post

0 Karma

hcanivel_splunk
Splunk Employee
Splunk Employee

Is time_of_last_change in epoch? As sduff has indicated, you're trying to diff string/text values. Not going to work.

Use strptime to convert the string into an integer in epoch.

You'll need to compare two different epoch values to make your tostring(<val>, "duration") usable here.

eval diff = tostring(relative_time(now(), "-1h") - time_of_last_change, "duration")

0 Karma

mstjohn_splunk
Splunk Employee
Splunk Employee

hi @tb5821

Did the answer below solve your problem? If so, please resolve this post by approving it! If your problem is still not solved, keep us updated so that someone else can help ya. Thanks for posting!

0 Karma

sduff_splunk
Splunk Employee
Splunk Employee

You are using strftime, which is converting your times into text strings, which you can't do maths operations on.

What format is time_of_last_change? Is it a unix timestamp? If so, you can calculate the difference by doing eval diff=relative_time(now(), "-1h@s") - time_of_last_change.

Otherwise, you most probably want to use the strptime command, which converts times in text format into a unix timestamp, which is seconds. From there, you can calculate the time difference.

0 Karma

tb5821
Communicator

even if I do | eval diff= last1hr-lastChange that diff field doesn't produce anything!

0 Karma

tb5821
Communicator

tried | eval diff = lastChange - last1hr | eval dur = tostring(diff, "duration") too

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to July Tech Talks, Office Hours, and Webinars!

What are Community Office Hours?Community Office Hours is an interactive 60-minute Zoom series where ...

Updated Data Type Articles, Anniversary Celebrations, and More on Splunk Lantern

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

A Prelude to .conf25: Your Guide to Splunk University

Heading to Boston this September for .conf25? Get a jumpstart by arriving a few days early for Splunk ...