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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...