Splunk Search

How to incorporate tag in the if-then-else Splunk construct

chersergei
New Member

Hello,
I created SPL search, that should pull out the log entries, based on the if-then-else condition, but it does not work correctly.
Please, find this search below:

index=cdc_apache OR index=datapower
| eval tag_name=if(index=="cdc_apache", "ED_ENDI_Digital_Retail_WebS_Test", 'NULL') 
| where (tag=tag_name) 
| where isnum(status)

I need to find the log entries from both indexes: index=cdc_apache and index=datapower. But I'm getting log entries ONLY from index=cdc_apache. I realize that my SPL query is wrong, but I don't know how to fix it.
Please, help.
Thanks,
Sergei Cher

0 Karma

nickhills
Ultra Champion

Have you considered using Splunk tags with event types?

For your three scenarios:
"ED_ENDI_ProdIndexED” consists of the following field-value pairs:
index=cdc_apache, index=cdc_jboss, index=xxx, …, index=yyy

“ED_ENDI_Digital_Retail_WebS_Test” consists of the following field-value pairs:
host=aaa, host=bbb

"ED_ENDI_ProdIndex_Test_1" consist of the following field-value pairs:
host!=aaa host!=bbb

Create an event type for each of the above and assosciate a tag with each one.
Then your search becomes

tag::ED_ENDI_ProdIndexED OR tag::ED_ENDI_Digital_Retail_WebS_Test OR tag::ED_ENDI_ProdIndex_Test_1
| stats values(tag) as tag by index
If my comment helps, please give it a thumbs up!
0 Karma

chersergei
New Member

I did not provide all the details (earlier I simplified my search), therefore, unfortunately, your SPL query will not work for me.
1) I'm using more complicated SPL query, and my goal is to use one tag for ALL the indexes, excluding index=cdc_apache, and use another tag for ONLY one index=cdc_apache.
Why do I want to do it:
I cannot combine hosts AND index in ONE tag, because they are combined by OR statement by default;
I need to combine them the following way in a tag:
index=datapower OR index=aaa OR index=bbb OR (index=cdc_apache AND (host=xxx, host=yyy, etc.)), but Splunk does not allow to do it.

Therefore, my SPL query really looks like this (I NEED TO USE DIFFERENT HOSTS WITH DIFFERENT INDEXES):

tag::index="ED_ENDI_ProdIndexED” 
| eval tag_name=case (index=="cdc_apache","ED_ENDI_Digital_Retail_WebS_Test", index !="cdc_apache", "ED_ENDI_ProdIndex_Test_1") 
| where isnotnull(tag_name) 
| eval tag=tag_name 
| where isnum(status)

In the above query:
tag “ED_ENDI_ProdIndexED” consists of the following field-value pairs:
index=cdc_apache, index=cdc_jboss, index=xxx, …, index=yyy

tag “ED_ENDI_Digital_Retail_WebS_Test” consists of the following field-value pairs:
host=aaa, host=bbb

tag "ED_ENDI_ProdIndex_Test_1" consist of the following field-value pairs:
host!=aaa host!=bbb

What I need as an output of my SPL query:
I need to show the log entries, where there is a numeric “status” field, for all the indexes. BUT for index=cdc_apache I have to use ONLY two hosts OUT of five, for index!= cdc_apache I have to use a different set of hosts (there are VERY MANY of them), except for the host=aaa AND host=bbb.
I realize that the above query does not work (it shows ALL five HOSTS for the index=cdc_apache – I tested it), but I don’t know how to change it in order to make it work.

I also think, that it is NOT valid to put host!=aaa host!=bbb in the tag (even if it is a correct way to do it – doubt it though - it combines these two hosts with OR and I need AND). But I cannot list all the hosts in the “ED_ENDI_ProdIndex_Test_1” tag, because, a) they should be discovered automatically and b) there are too many of them.

Please, advice.

0 Karma

begleyj1
Path Finder

Look at using the case statement within an eval, it will provide the if then else statements. https://docs.splunk.com/Documentation/Splunk/8.0.1/SearchReference/ConditionalFunctions

index IN (cdc_datapower, datapower)
|eval tag_name= case(index=="cdc_apache", "ED_ENDI_Digital_Retail_WebS_Test", index=="datapower", "Another_Tag_Name")
| where isnotnull(tag_name)
| stats values(tag_name) as tag_name by index
0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...