Splunk Search

Include zero count in stats count

johandk
Path Finder

I have a search like this:

sourcetype="wineventlog:security" (host="Server1" OR host="server2" OR host="server3") | stats count(host) by host

This returns server1 and server2 and their counts but not server3 cause there is no events for it. How do I force it for server3 to display in the table with a zero count??

Tags (3)
1 Solution

bwooden
Splunk Employee
Splunk Employee

This search would show a count of those servers:

|stats count | eval host="Server1,Server2,Server3" | makemv delim="," host | mvexpand host | append [search sourcetype="wineventlog:security" (host="Server1" OR host="Server2" OR host="Server3")] | stats sum(eval(if(isnull(_time),0,1))) as count by host 

If there are many servers, it may be easier to maintain the list in a lookup file.

Alternatively, if you want to show counts of all servers Splunk has seen you can lead with a metadata command and obviate the need to specify servers.

View solution in original post

samkidman
Engager

Heres a way to do it if you have a large number of hosts that match a regex without using a lookup file:

|stats count | eval [|metadata type=hosts |regex host="<matching hosts>"| fields + host | mvcombine delim="," host | nomv host | format "","","","","",""] | makemv delim="," host | mvexpand host | append [search sourcetype="wineventlog:security" | regex host="<matching hosts>" ] | stats sum(eval(if(isnull(_time),0,1))) as count by host

bwooden
Splunk Employee
Splunk Employee

This search would show a count of those servers:

|stats count | eval host="Server1,Server2,Server3" | makemv delim="," host | mvexpand host | append [search sourcetype="wineventlog:security" (host="Server1" OR host="Server2" OR host="Server3")] | stats sum(eval(if(isnull(_time),0,1))) as count by host 

If there are many servers, it may be easier to maintain the list in a lookup file.

Alternatively, if you want to show counts of all servers Splunk has seen you can lead with a metadata command and obviate the need to specify servers.

Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...