Splunk Search

How to run the main search only if the subsearch returns true?

prakashbhanu407
New Member

I have to run the Main search only on the last working day of the month, and I got to a search that should work, but the main search is not taking the boolean response of subsearch as a condition to process the rest of the search.

Here is my search:

index=MyIndex queryType="ts" filename=Pharmacy*.JSON [| gentimes start=-1 | eval lastDOM=relative_time(now(),"+1mon@mon-1d") | eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"),strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),1=1,lastDOM) | eval query=if(relative_time(now(),"@d")=lastWDOM,1=1,1=2) | table query] | table _time,accountName,filename,filesize 
0 Karma

somesoni2
Revered Legend

Try like this

index=MyIndex queryType="ts" filename=Pharmacy*.JSON [| gentimes start=-1 | eval lastDOM=relative_time(now(),"+1mon@mon-1d") | eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"),strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),1=1,lastDOM) | eval index=if(relative_time(now(),"@d")=lastWDOM,"MyIndex","YouWillNotFindThisIndex") | table index] | table _time,accountName,filename,filesize

Basically, if the subsearch condition is false, it'll return a value for field (metadata field used for better performance) index which may never exist so main search will not run. It returns the same index name as main search in case the condition is true.

javiergn
Super Champion

You can use map to simulate a conditional execution:

| gentimes start=-1
| eval lastDOM=relative_time(now(),"+1mon@mon-1d") 
| eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"), strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),1=1,lastDOM) 
| eval n_searches=if(relative_time(now(),"@d")=lastWDOM, 1, 0)
| where n_searches > 0
| map maxsearches=1 search="
    | search index=MyIndex queryType=\"ts\" filename=Pharmacy*.JSON 
    | table _time,accountName,filename,filesize
"

ibob0304
Communicator

I couldn't thank much on using map, it solved my long standing issue.. thanks alot..

0 Karma

woodcock
Esteemed Legend

You cannot assign boolean values to a field. Try this:

index=MyIndex queryType="ts" filename=Pharmacy*.JSON earliest=[| gentimes start=-1 | addinfo | eval lastDOM=relative_time(now(),"+1mon@mon-1d") | eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"),strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),true(),lastDOM) | eval earliest=if(relative_time(now(),"@d")=lastWDOM,"Do Not Run",info_min_time) | return $earliest] | table _time accountName filename filesize

This will cause the search to crash if it does not meet your criteria.

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...