Getting Data In

How to filter search result based on case condition

andreigro
New Member

Hi guys, I'm new to splunk and I have one issue with filtering my search results based on a case condition
My search string:
sourcetype="WMI:CPUTime" | eval selected_host=case("$ddl_instance$" == "a", "Server1", "$ddl_instance$" == "b", "Server2", "$ddl_instance$" == "c", "Server3") | timechart span=1d eval(round(avg(PercentProcessorTime),1)) by host limit=20

I now I need to filter this based on the field named host=selected_host

It doesn't sound very complex but I cannot make it work.
Thank you,
Andrei

Tags (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try one of these.

sourcetype="WMI:CPUTime" | eval selected_host=case("$ddl_instance$" == "a", "Server1", "$ddl_instance$" == "b", "Server2", "$ddl_instance$" == "c", "Server3") | where host=selected_host | timechart span=1d eval(round(avg(PercentProcessorTime),1)) by host limit=20

sourcetype="WMI:CPUTime" | where host=case("$ddl_instance$" == "a", "Server1", "$ddl_instance$" == "b", "Server2", "$ddl_instance$" == "c", "Server3") | timechart span=1d eval(round(avg(PercentProcessorTime),1)) by host limit=20
---
If this reply helps you, Karma would be appreciated.

View solution in original post

0 Karma

niketn
Legend

May I know how you are setting $ddl_instance$ token?

It is most likely that eval can be used to set token at the same place for host directly. That way you will be able to add host in your base query hence resulting in filtering required results upfront rather than later. This should improve your search performance. (http://docs.splunk.com/Documentation/Splunk/latest/Viz/tokens#Define_token_filtering_and_formatting)

In the current query you can change by host to by selected_host in your timechart command

sourcetype="WMI:CPUTime" | eval selected_host=case("$ddl_instance$" == "a", "Server1", "$ddl_instance$" == "b", "Server2", "$ddl_instance$" == "c", "Server3") | timechart span=1d eval(round(avg(PercentProcessorTime),1)) by selected_host limit=20
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

richgalloway
SplunkTrust
SplunkTrust

Try one of these.

sourcetype="WMI:CPUTime" | eval selected_host=case("$ddl_instance$" == "a", "Server1", "$ddl_instance$" == "b", "Server2", "$ddl_instance$" == "c", "Server3") | where host=selected_host | timechart span=1d eval(round(avg(PercentProcessorTime),1)) by host limit=20

sourcetype="WMI:CPUTime" | where host=case("$ddl_instance$" == "a", "Server1", "$ddl_instance$" == "b", "Server2", "$ddl_instance$" == "c", "Server3") | timechart span=1d eval(round(avg(PercentProcessorTime),1)) by host limit=20
---
If this reply helps you, Karma would be appreciated.
0 Karma

andreigro
New Member

Thanks a lot! Tried the first query and it worked!

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...