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!

Enterprise Security Content Update (ESCU) | New Releases

In December, the Splunk Threat Research Team had 1 release of new security content via the Enterprise Security ...

Why am I not seeing the finding in Splunk Enterprise Security Analyst Queue?

(This is the first of a series of 2 blogs). Splunk Enterprise Security is a fantastic tool that offers robust ...

Index This | What are the 12 Days of Splunk-mas?

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