Splunk Search

stats by date_hour add zero count for hours with no events

ickymettle
Explorer

Hi folks,

I'm working on a search to return the number of events by hour over any specified time period. At the moment i've got this on the tail of my search:

... | stats count by date_hour | sort date_hour

I want this search to return the count of events grouped by hour for graphing.

This for the most part works. However if the search returns no events for a given hour, that hour doesn't appear in the resulting table.

Is there a way to modify this to essentially add 0's for the hours with no events? Given stats is only aggregating on fields that exist in the result data and it isn't really a "time" aware function I can't see a solution.

Is there even a better way do do this? This is for a dashboard where I want to graph the busiest time of day across a given time range and want the query flexible enough to just be able to change the time range (7d, last month, last year).

Thanks, Marcus

1 Solution

ziegfried
Influencer

I had a similar problem in the past and solved it by implementing a custom search commands that added the missing elements (days in my case). But I think your query can be solved using a combination of timechart, eval and stats:

... | timechart span=1h count | eval hour=strftime(_time,"%H:00") | stats sum(count) as count by hour

View solution in original post

ziegfried
Influencer

I had a similar problem in the past and solved it by implementing a custom search commands that added the missing elements (days in my case). But I think your query can be solved using a combination of timechart, eval and stats:

... | timechart span=1h count | eval hour=strftime(_time,"%H:00") | stats sum(count) as count by hour

ickymettle
Explorer

this did the trick nicely, thanks!

0 Karma

ftk
Motivator

If you are just doing this for graphing, I recommend using timechart instead of stats. You can tell timechart to use spans of 1 hour, and for every hour with 0 events you can configure whether you want to treat this as 0 (null) or simply connect with the other data data points.

Try using it like so:

... | timechart span=1h count

ickymettle
Explorer

this works fine for a 24 hour time range, however in my particular use case I needed to sum the events by hour over multiple days to graph the busiest hour of the day over more than 1 day

0 Karma
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...