Splunk Search

Executing Conditional Queries in Splunk Based on Input Value

taruntalreja
New Member

I have two query in splunk query 1 and query 2 and an input. Based on the input, i need to execute either query 1 or query 2. I am trying something like below query but it is not working for me.

 

| makeresults
| eval myInput="*"

| append [
search "my search related to query 1"
| rex field=_raw "Job id : (?<job_id>[^,]+)"
| where myInput="*"
| eval query_type="query1"
| table job_id, query_type, myInput
]
| append [
search "my search related to query 2"
| rex field=_raw "Job id : (?<job_id>[^,]+)"
| where myInput!="*"
| eval query_type="query2"
| table job_id, query_type, myInput
]

 




Labels (1)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

SPL does not support conditional execution of commands.  It can be simulated in a dashboard by setting a token to the desired search string and referencing the token in the query.

<fieldset>
  <input token="myInput"...>
    <change>
      <condition match="<<option 1>>">
        <set token="query">SPL for option 1</set>
      </condition>
      <condition match="<<option 2>>">
        <set token="query">SPL for option 2></set>
      </condition>
    </change>
  </input>
</fieldset>
...
<row>
  <panel>
    <table>
      <search>
        <query>$query$</query>
      </search>
    </table>
  </panel>
</row>
---
If this reply helps you, Karma would be appreciated.
0 Karma

alexandarmatev1
Loves-to-Learn
| makeresults
| eval myInput="*"

| append [
    | search "my search related to query 1"
    | rex field=_raw "Job id : (?<job_id>[^,]+)"
    | eval query_type=if(myInput="*", "query1", null())
    | where query_type="query1"
    | table job_id, query_type, myInput
]

| append [
    | search "my search related to query 2"
    | rex field=_raw "Job id : (?<job_id>[^,]+)"
    | eval query_type=if(myInput!="*", "query2", null())
    | where query_type="query2"
    | table job_id, query_type, myInput
]
0 Karma

taruntalreja
New Member

This solution does not work, I am getting empty result. I think there is an issue and myInput variable is not passed in append. One more issue with this solution is that both the queries will be running but we know beforehand which query to run, so I am looking for some optimized solution where only 1 query is ran based on the filter.

0 Karma

alexandarmatev1
Loves-to-Learn

Why don't you try with macros and if, case statement? 

0 Karma
Get Updates on the Splunk Community!

Automatic Discovery Part 1: What is Automatic Discovery in Splunk Observability Cloud ...

If you’ve ever deployed a new database cluster, spun up a caching layer, or added a load balancer, you know it ...

Real-Time Fraud Detection: How Splunk Dashboards Protect Financial Institutions

Financial fraud isn't slowing down. If anything, it's getting more sophisticated. Account takeovers, credit ...

Splunk + ThousandEyes: Correlate frontend, app, and network data to troubleshoot ...

 Are you tired of troubleshooting delays caused by siloed frontend, application, and network data? We've got a ...