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
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...