Splunk Search

How to use stats command after top command.

rakshithreddy
Explorer

Hi all
I am trying to do the following search.
which would result in Top 5 apiname values along with their apitime(avg,min,max) values included but unable to get the list the data.

index=cub source=xyz.log
|top limit=5 apiName
|stats avg(apiTime),min(apiTime),max(apiTime) by apiName

Thank you.

Tags (1)
0 Karma
1 Solution

somesoni2
SplunkTrust
SplunkTrust

After top you only left with 5 records of most occuring apiName, corresponding count and percentage. There is no apiTime field available so the stats fails. Try like this

index=cub source=xyz.log
|stats count avg(apiTime),min(apiTime),max(apiTime) by apiName
| eventstats sum(count) as total | eval perc=count*100/total
| sort 5 -count | fields - total

View solution in original post

woodcock
Esteemed Legend

Like this:

index=cub source=xyz.log
[ index=cub source=xyz.log
| top limit=5 apiName 
| fields apiName ] 
| stats avg(apiTime) min(apiTime) max(apiTime) BY apiName
0 Karma

DalJeanis
SplunkTrust
SplunkTrust

Hmmm. Seems like it would be possible to substitute a tstats command into the subsearch that would make it more efficient than somesoni2's version, solely when there is a high cardinality for apiName and/or a longer time period that makes the stats expensive...

0 Karma

somesoni2
SplunkTrust
SplunkTrust

After top you only left with 5 records of most occuring apiName, corresponding count and percentage. There is no apiTime field available so the stats fails. Try like this

index=cub source=xyz.log
|stats count avg(apiTime),min(apiTime),max(apiTime) by apiName
| eventstats sum(count) as total | eval perc=count*100/total
| sort 5 -count | fields - total

woodcock
Esteemed Legend

This answer is better; mine is mainly for education because it is more like how you were trying to solve it (but don't use it).

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

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 ...