Splunk Search

How to search the distinct count of a field based on a condition, then calculate percentage?

jawebb
Explorer

Here is an example of my search:

Search | stats dc(eval(DISPLAYNAME)) AS Total,dc(eval(STATUS="Offline")) AS Offline| eval Percentage=round((100*(Total-Offline)/Total))."%" | fields - Total, Offline

My goal is to take the total number of devices as referenced by the dc(DISPLAYNAME). Then, I'd like to take the same number, but under the condition that the STATUS field is equal to "Offline". When I do the count, I get a correct number until the next data import, then it duplicates as it should. Distinct count only returns a "1". I guess I'm not quite sure how to eval the DISPLAYNAME field again, but only for offline devices.

Any help would be appreciated.

Thanks

Tags (4)
0 Karma
1 Solution

woodcock
Esteemed Legend

Like this:

| eval DISPLAYNAMEforOFFLINES = if((STATUS="Offline"), DISPLAYNAME, null()) | stats dc(eval(DISPLAYNAME)) AS Total dc(DISPLAYNAMEforOFFLINES) AS Offline| eval Percentage=round((100*(Total-Offline)/Total))."%" | fields - Total, Offline

View solution in original post

0 Karma

woodcock
Esteemed Legend

Like this:

| eval DISPLAYNAMEforOFFLINES = if((STATUS="Offline"), DISPLAYNAME, null()) | stats dc(eval(DISPLAYNAME)) AS Total dc(DISPLAYNAMEforOFFLINES) AS Offline| eval Percentage=round((100*(Total-Offline)/Total))."%" | fields - Total, Offline
0 Karma

jawebb
Explorer

Thank you! Works great. I also found that the join command worked for me as well, but I prefer your way.

Search | stats dc(eval(DISPLAYNAME)) AS Total| join [search "index" (STATUS=Offline) | stats dc(eval(DISPLAYNAME)) AS Offline] | eval Percentage=round((100*(Total-Offline)/Total))."%" | fields - Total, Offline

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

The join approach would run the search twice, so it's not a great idea if avoidable (it is).

0 Karma

woodcock
Esteemed Legend

It is also subject to subsearch limits; my solution is not.

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