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!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...