Splunk Search

What is a better way of comparing EPOCH times?

Kendo213
Communicator

I have the query below that checks for the expiration date of a certificate, converts it to epoch time, and then basically changes the value of the result as it 'degrades' (gets closer to expiration). I have a feeling this is really messy and could be improved on, so I'm just looking for general recommendations on a better way of doing it. It works, but to me it looks excessive.

index=test host=mycertificateauthority| rex field=Line "(?\d{1,2}\/\d{1,2}\/\d{4})" | stats count by _time,host,date | eval dateepoch=strptime(date,"%m/%d/%Y") | eval thirtydays=(relative_time(dateepoch,"-30d"))  | eval fifteendays=(relative_time(dateepoch,"-15d")) | eval fivedays=(relative_time(dateepoch,"-5d")) | eval result=case((now()<=thirtydays),"0",(now()>=thirtydays) AND (now()<=fifteendays) AND (now()<=fivedays) AND (now()<=dateepoch),"1",(now()>=thirtydays) AND (now()>=fifteendays) AND (now()<=fivedays) AND (now()<=dateepoch),"2",(now()>=thirtydays) AND (now()>=fifteendays) AND (now()>=fivedays) AND (now()<=dateepoch),"3",(now()>=thirtydays) AND (now()>=fifteendays) AND (now()>=fivedays) AND (now()>=dateepoch),"4")
Tags (2)
0 Karma
1 Solution

somesoni2
Revered Legend

Try like this

index=test host=mycertificateauthority| rex field=Line "(?<date>\d{1,2}\/\d{1,2}\/\d{4})" | stats count by _time,host,date | eval dateepoch=strptime(date,"%m/%d/%Y") | eval thirtydays=(relative_time(dateepoch,"-30d")) | eval fifteendays=(relative_time(dateepoch,"-15d")) | eval fivedays=(relative_time(dateepoch,"-5d")) 
| eval result=case((now()<=thirtydays),"0",
(now()>=thirtydays) AND (now()<=fifteendays) ,"1",
(now()>=fifteendays) AND (now()<=fivedays) ,"2",
(now()>=fivedays) AND (now()<=dateepoch),"3",
(now()>=dateepoch),"4") 

since you're using case, you don't have to repeat already checked expressions.

View solution in original post

0 Karma

somesoni2
Revered Legend

Try like this

index=test host=mycertificateauthority| rex field=Line "(?<date>\d{1,2}\/\d{1,2}\/\d{4})" | stats count by _time,host,date | eval dateepoch=strptime(date,"%m/%d/%Y") | eval thirtydays=(relative_time(dateepoch,"-30d")) | eval fifteendays=(relative_time(dateepoch,"-15d")) | eval fivedays=(relative_time(dateepoch,"-5d")) 
| eval result=case((now()<=thirtydays),"0",
(now()>=thirtydays) AND (now()<=fifteendays) ,"1",
(now()>=fifteendays) AND (now()<=fivedays) ,"2",
(now()>=fivedays) AND (now()<=dateepoch),"3",
(now()>=dateepoch),"4") 

since you're using case, you don't have to repeat already checked expressions.

0 Karma

Kendo213
Communicator

Great, thank you. I was thinking I was missing something, this helps.

0 Karma

skoelpin
SplunkTrust
SplunkTrust

How fast does it run? You could package this up in a macro so its more user friendly.

If its fast and works correctly, then there isn't much need to clean it up

0 Karma
Get Updates on the Splunk Community!

Modern way of developing distributed application using OTel

Recently, I had the opportunity to work on a complex microservice using Spring boot and Quarkus to develop a ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had 3 releases of new security content via the Enterprise Security ...

Archived Metrics Now Available for APAC and EMEA realms

We’re excited to announce the launch of Archived Metrics in Splunk Infrastructure Monitoring for our customers ...