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!

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