Splunk Search

How to add column values based on subsearch?

Gabriel
Path Finder

Hi everyone

I got the following sample search that yields the table below.

index=server
| stats avg(response_time) by server_name
| sort + avg(response_time)
| streamstats count as rank
| head 3

rank server_name avg(response_time) new_performance_metric
1 best.server 300  
2 second.best.server 350  
3 third.best.server 400  

 

Once I know the top servers, I want to calculate additional new_performance_metric for each of the three servers. Does anyone know how this can be done?

Note:
- I can't use foreach since the metric I want to calculate involves streaming commands. Foreach does not support that.
- I think I can't use a subsearch since it is executed first where the top servers are not known yet.
- I can't precompute the new_performance_metric for all servers and then use something like a lookup since this is computationally too expensive.

My guess is that the solution involves a macro but I couldn't figure it out yet. Many thanks in advance.

Labels (2)
0 Karma
1 Solution

PickleRick
SplunkTrust
SplunkTrust

Your guess is wrong. Macro is expanded before spawning the search. So you can either use subsearch (but the other way around - run this search as a subsearch to generate set of conditions) or use the map command.

View solution in original post

PickleRick
SplunkTrust
SplunkTrust

Your guess is wrong. Macro is expanded before spawning the search. So you can either use subsearch (but the other way around - run this search as a subsearch to generate set of conditions) or use the map command.

Gabriel
Path Finder

I just tried the mapping command. It did precisely what I needed. Thanks a lot, you saved me heaps of time! 💪

0 Karma

PickleRick
SplunkTrust
SplunkTrust

Just remember that map spawns subsearches which have its own limits (in terms of returned results and execution time). It is also considered a "risky command" and as such might be restricted.

Gabriel
Path Finder

I testet it yesterday and it turns out our production environment suppresses dashboards and reports that make use of the mapping command. I had to use subsearches after all. Thanks again for pointing me in the right direction, I am sure the mapping command will come in handy, at some point, even if I can't use it this time 🤓

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...