Splunk Search

Conditional searches in the same search

julienlance
Explorer

Hello !

Is there a way to do conditonal searches depending of the result of a first search ?
I mean, here is an exemple :
- A search command returns a table with a field containing an interesting value, for instance "reachable" or "not-reachable"
- a second search command is executed only if the value is "reachable" is set in the first command
- This second search should be run for each row of the table of the first command.

In both commands, there are "eval" commands which are used for timestamp evaluations.
Here is a simplified version of my query :

index=* sourcetype=routes_devices 
| table _time,host-name, reachability 
| eval result=if(reachability=="reachable",
[search index=* sourcetype=router fields name], "NOK")

Is it possible to that kind of searches ? with the best performance possible.
Thanks for your help !

0 Karma
1 Solution

adonio
Ultra Champion

not sure why not search for all and then eliminate ....
something like this:

index=* sourcetype=routes_devices OR sourcetype=router
 | table _time,host-name, reachability, name, sourcetype
 | eval result=if(reachability=="reachable",name,null())
 | search result=*

but maybe i am missing something

View solution in original post

adonio
Ultra Champion

not sure why not search for all and then eliminate ....
something like this:

index=* sourcetype=routes_devices OR sourcetype=router
 | table _time,host-name, reachability, name, sourcetype
 | eval result=if(reachability=="reachable",name,null())
 | search result=*

but maybe i am missing something

renjith_nair
Legend

@julienlance ,

Looking at your requirement, most probably map command should work for you.
Apart from running the second search for each row, you can set a token in the first search for the desired search and use that token in the second search in map. So if the token is not set , then the second search will not run.

e.g. eval searchToken=case (reachability=="reachable",true())

---
What goes around comes around. If it helps, hit it with Karma 🙂
0 Karma

julienlance
Explorer

Thanks you @renjith.nair, this is a great clue, I will try to use it !
Many thanks

0 Karma

julienlance
Explorer

The eval command you specified doesn't work, as the token can't be a boolean (splunk said). 😞

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...