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!

Why You Can't Miss .conf25: Unleashing the Power of Agentic AI with Splunk & Cisco

The Defining Technology Movement of Our Lifetime The advent of agentic AI is arguably the defining technology ...

Deep Dive into Federated Analytics: Unlocking the Full Power of Your Security Data

In today’s complex digital landscape, security teams face increasing pressure to protect sprawling data across ...

Your summer travels continue with new course releases

Summer in the Northern hemisphere is in full swing, and is often a time to travel and explore. If your summer ...