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

View solution in original post

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
Did you miss .conf21 Virtual?

Good news! The event's keynotes and many of its breakout sessions are now available online, and still totally FREE!