Splunk Search

Show stats for a fixed set of hosts

prashantjois
Explorer

I'm trying to build a table of outages. For example:

Host Num. Outages
A 1
B 2
C 0

Servers will log an outage with the words "outage detected" in a field called errorMsg

My search would normally be as follows:

index=serverdata errorMsg="outage detected" | stats count by host

However, if no outages are detected, then no events are logged. So the above command would only show results for hosts A and B. I would also like it to show 0 for C.

How can I achieve this?

Tags (1)
0 Karma
1 Solution

somesoni2
Revered Legend

Try this

index=serverdata errorMsg="outage detected" | stats count by host | append [| metadata type=hosts index=serverdata | table host | eval count=0] | stats max(count) as count by host

OR (slower)

| metadata type=hosts index=serverdata | table host | eval count=0 | join type=left host [search  index=serverdata errorMsg="outage detected" | stats count by host]

View solution in original post

somesoni2
Revered Legend

Try this

index=serverdata errorMsg="outage detected" | stats count by host | append [| metadata type=hosts index=serverdata | table host | eval count=0] | stats max(count) as count by host

OR (slower)

| metadata type=hosts index=serverdata | table host | eval count=0 | join type=left host [search  index=serverdata errorMsg="outage detected" | stats count by host]

prashantjois
Explorer

Thanks for the suggestion. This appears to work if there is at least 1 event but will return no results if there are no outages at all.

0 Karma

somesoni2
Revered Legend

What about second search? Does that also not producing result when there are no Outage?

0 Karma

prashantjois
Explorer

oh you're right, the second search does work! Thanks!

Follow-up question: Is it possible to do this with anything other than the default fields (host/source/sourcetype)? Reading the docs for metadata it seems like it only returns the values for those three. My events also return a field called "app" which has the app which caused the outage and I would like to get a similar count by app.

Would I just write those values to a summary index myself?

0 Karma

somesoni2
Revered Legend

You're right, the metadata command will return summaries by hosts/sourcetypes/sources and we can't add custom fields.

You can either do a summary indexing to get list of host-app combination or you can use a lookup table file to store the same (save on space). A sample daily search would be like this.

| inputlookup host_app_lookup.csv | append [search earliest=-1d@d latest=@d index=serverdata | stats count by host,app | table host,app] | stats count by host,app | table host,app | outputlookup append=false host_app_lookup.csv

You can run this every night at 1 AM and it will update the lookup table with new combinations of app-host found yesterday.

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...