Splunk Search

How to calculate the percentage from a dc count?

pavanae
Builder

I have a query as below

| metadata type=hosts | search [| inputlookup hosts_test.csv | eval host=lower(my_hostname) | fields host ] | eval host=lower(host) | append [| inputlookup hosts_test.csv | eval host=lower(my_hostname) | eval recentTime=0, lastTime=0, host=lower(host) | fields host recentTime lastTime ] | dedup host | eval category=case(recentTime>=relative_time(now(), "-24h"), "Systems reported to Splunk in last 24 hours", (recentTime0), "Systems reported to Splunk more than 24 hours ago", recentTime=0, "Systems never reported to Splunk") | stats dc(host) as "Total Hosts" BY category

which displays the result as follows

category Total Hosts
Systems never reported to Splunk 91
Systems reported to Splunk in last 24 hours 1074
Systems reported to Splunk more than 24 hours ago 20

Now, how to display the percentage of each instead of dc count in the result?

0 Karma
1 Solution

elliotproebstel
Champion

The eventstats sum() portion of the answer from @cmerriman doesn't work on my system. This does:

...| stats dc(host) AS total_hosts BY category | addcoltotals labelfield=category label="Total" | eventstats max(total_hosts) AS all_totals | search NOT category="Total" | eval Percentage=tostring(round(total_hosts/all_totals*100,2))."%" | fields - total_hosts all_totals

Note: I changed the first stats command there because I find it easier to not use field names with spaces in them until the final portion of a search. I get easily confused about when things need to be wrapped in single quotes, etc. YMMV

View solution in original post

0 Karma

elliotproebstel
Champion

The eventstats sum() portion of the answer from @cmerriman doesn't work on my system. This does:

...| stats dc(host) AS total_hosts BY category | addcoltotals labelfield=category label="Total" | eventstats max(total_hosts) AS all_totals | search NOT category="Total" | eval Percentage=tostring(round(total_hosts/all_totals*100,2))."%" | fields - total_hosts all_totals

Note: I changed the first stats command there because I find it easier to not use field names with spaces in them until the final portion of a search. I get easily confused about when things need to be wrapped in single quotes, etc. YMMV

0 Karma

cmerriman
Super Champion

try this:

 ..| stats dc(host) as Total_Hosts BY category|eventstats sum(Total_Hosts) as Grand_Total|eval Percent_Total=round(Total_Hosts/Grand_Total*100,2)|fields - Total_Hosts Grand_Total
0 Karma
Get Updates on the Splunk Community!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...