Splunk Search

Query running time

Kwip
Contributor

I want to run a query every 5 minutes starting from today 7 AM to next day 5 AM and so on. Throughout my run earliest time should be 7 AM today and latest can be now.

I tried several combinations but everything messed up when running over the mid noght. after the 12 00 AM the earliest is becoming invalid. All of the below methods taking earliest time as 7 AM till end of the day. when running after the mid night throwing error saying earliest time should not be greater than latest time.

Try 1
| makeresults | eval starttime=strptime(strftime(now(),"%d/%m/%Y 07:00:00 AM"),"%d/%m/%Y %I:%M:%S %p") , endtime=starttime+7920

Try 2
earliest=@d+7h

Try 3
     | eval now=now() 
     | eval earliest=relative_time(now, "@d+1d+7h") 
     | eval earliest=if((earliest<now), earliest, relative_time(now, "@d+7h")) 
     | eval search = "earliest=" . earliest
     | table search]
0 Karma
1 Solution

somesoni2
Revered Legend

Try like this

your base search [| makeresults | eval earliest=if(now()<=relative_time(now(),"@d+5h"),"-1d@d+7h","@d+7h") | eval latest="now" | table earliest latest ]

View solution in original post

0 Karma

nawazns5038
Builder

index=_internal [| makeresults | eval earliest=if(now()<=relative_time(now(),"@d+5h"),"-1d@d+7h","@d+7h") | eval latest="now" | table earliest latest ]

I tried it and it gave me an error "Error in 'search' command: Unable to parse the search: 'AND' operator is missing a clause on the left hand side."

How to create new earliest and latest time fields and use them in query

0 Karma

micahkemp
Champion

This seems to be a duplicate.

0 Karma

micahkemp
Champion

Try something like this:

<your base search>
[| makeresults 
| addinfo 
| eval latest=now() 
| eval today_7_am=relative_time(latest, "@d+7h") 
| eval yesterday_7_am=relative_time(latest, "-1d@d+7h") 
| eval earliest=if(today_7_am<latest, today_7_am, yesterday_7_am) 
| table earliest]

That does a simple comparison of your calculated earliest times to determine which was actually in the past, and chooses that one.

Kwip
Contributor

Verifying this query. Working fine now. Will verify over mid night window and let you know the outcome.

0 Karma

Kwip
Contributor

@ micahkemp,
Wow!! Thank you very much It is working fine!

0 Karma

somesoni2
Revered Legend

Try like this

your base search [| makeresults | eval earliest=if(now()<=relative_time(now(),"@d+5h"),"-1d@d+7h","@d+7h") | eval latest="now" | table earliest latest ]
0 Karma

Kwip
Contributor

@somesoni2
I am getting below error while using this query after the splunk upgrade to 7.0. Any thoughts?

Error in 'search' command: Unable to parse the search: 'AND' operator is missing a clause on the left hand side.

0 Karma

somesoni2
Revered Legend

Try this

your base search 
 [| makeresults 
 | eval earliest=if(now()<=relative_time(now(),"@d+5h"),"-1d@d+7h","@d+7h") 
 | eval latest="now" 
 | table earliest latest | format ]
 | stats/eval/table/or whatever operation you want to perform.

OR

your base search 
 [| makeresults 
 | eval earliest=if(now()<=relative_time(now(),"@d+5h"),"-1d@d+7h","@d+7h") 
 | eval latest="now" 
 | table earliest latest  | format "" "" "" "" "" ""]
 | stats/eval/table/or whatever operation you want to perform.
0 Karma

Kwip
Contributor

Thank you @somesoni2, It is working fine.

0 Karma

Kwip
Contributor

Verifying this query. Working fine now. Will verify over mid night window and let you know the outcome.

0 Karma

Kwip
Contributor

@ somesoni2,
Wow!! Thank you very much It is working fine!

0 Karma

nawazns5038
Builder

How did you verify ?

How did you use the earliest and latest time fields created by the query into _time of Splunk.

0 Karma

Kwip
Contributor

@nawazns5038
You no need anything else apart from adding the particular portion in the query.

your base search 
[| makeresults 
| eval earliest=if(now()<=relative_time(now(),"@d+5h"),"-1d@d+7h","@d+7h") 
| eval latest="now" 
| table earliest latest ]
| stats/eval/table/or whatever operation you want to perform.

While running this query, running span will be automatically taken from this query.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Classic British Rock Bands

This puzzle series (first published here) is based on extracting information from statements about well-known ...

New to the Community? Join Us for an Exclusive Post .conf26 Welcome Session (Plus a ...

Keep the .conf26 Energy Going: Welcome to the Splunk Community! Whether we scanned your badge at the Community ...

Accelerate Splunk App Development with AI

  Learn how to automate the repetitive parts of your build cycle.   Register to Attend   Join our Tech Talk ...