Splunk Search

How to use addcoltotals to calculate percentage?

LearningGuy
Builder


How to use addcoltotals to calculate percentage?
For example:  my search below   scoreSum % is empty 
Thank you for your help

index=test
| stats sum(score) as scoreSum by vuln
| addcoltotals labelfield =vuln   label=Total_scoreSum scoreSum
| eval scoreSum_pct = scoreSum/Total_scoreSum*100 . "%"
| table vuln, scoreSum, scoreSum_pct

Result:

vulnscoreSumscoreSum %
vulnA20 
vulnB40 
vulnC80 
Total_scoreSum140 


Expected result

vulnscoreSumscoreSum_pct
vulnA2014.3%
vulnB4028.6%
vulnC8057.1%
Total_scoreSum140100%
Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

index=test
| stats sum(score) as scoreSum by vuln
| eventstats sum(scoreSum) as total
| eval scoreSum_pct=100*scoreSum/total
| fields - total
| addcoltotals labelfield =vuln   label=Total_scoreSum scoreSum scoreSum_pct
| eval scoreSum_pct = round(scoreSum_pct,1) . "%"
| table vuln, scoreSum, scoreSum_pct

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

index=test
| stats sum(score) as scoreSum by vuln
| eventstats sum(scoreSum) as total
| eval scoreSum_pct=100*scoreSum/total
| fields - total
| addcoltotals labelfield =vuln   label=Total_scoreSum scoreSum scoreSum_pct
| eval scoreSum_pct = round(scoreSum_pct,1) . "%"
| table vuln, scoreSum, scoreSum_pct

LearningGuy
Builder

Hello,
I tried your suggestion and it worked successfully. I accepted this as solution. I appreciate your help. Thank you.

1)    If I printed out "total" field, it can give total 140 in each row.   
How did eventstats know how to calculate the total of 140, when "stats" command has scoreSum of 20/40/80?         
If I played around and used "stats" or "eventstats group by vuln", it didn't work.  Please suggest

         | eventstats sum(scoreSum) as total    

vulnscoreSumtotalscoreSum_pct
vulnA2014014.3%
vulnB4014028.6%
vulnC8014057.1%
Total_scoreSum140420100%



2)   If there are hundreds of row, there will be multi pages. The "Total_scoreSum" field will appear at the end of the row.
       Is there a way to display it on the first page, but at the bottom, not at the top (using sort)?
| addcoltotals labelfield =vuln label=Total_scoreSum scoreSum scoreSum_pct

Thank you so much

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

1) eventstats adds the aggregated value (sum in this instance) to each event, stats replaces the events with the aggregated statisitcs

2) No, this is not normally possible - addtotals adds an extra event (row) to the pipeline at the end. The way the pipeline is displayed happens after the total row has been added and there is no way to predict how big the first page of the display is going to be ahead of time.

Get Updates on the Splunk Community!

.conf24 | Day 0

Hello Splunk Community! My name is Chris, and I'm based in Canberra, Australia's capital, and I travelled for ...

Enhance Security Visibility with Splunk Enterprise Security 7.1 through Threat ...

(view in My Videos)Struggling with alert fatigue, lack of context, and prioritization around security ...

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...