Using stats to aggregate values

While top is very convenient, stats is extremely versatile. The basic structure of a stats statement is:

stats functions by fields 

Many of the functions available in stats mimic similar functions in SQL or Excel, but there are many functions unique to Splunk too. The simplest stats function is count. Given the following query, the results will contain exactly one row, with a value for the field count:

sourcetype=tm1* error | stats count

Using the by clause, stats will produce one row per unique value for each field listed, which is similar to the behavior of top. Run the following query:

sourcetype=tm1* error | stats count by date_month date_wday

It will produce a table like this:

There are a few things to note about these results:

Using stats, you can add as many by fields or functions as you want into a single statement. Let's run this query:

sourcetype=tm1* error | stats count avg(linecount) max(linecount) 
as "Slowest Time" by date_month date_wday

The results look like those in the following screenshot:

Let's step through every part of this query, just to be clear:

The quotes are necessary ("Slowest Time") because the field name contains a space:

If an event is missing a field that is referenced in a stats command, you may not see the results you are expecting. For instance, when computing an average, you may wish for events missing a field to count as zeros in the average. Also, for events that do not contain a field listed in the by fields, the event will simply be ignored.

To deal with both of these cases, you can use the fillnull command to make sure that the fields you want exist. We will cover this in Chapter 6, Advanced Search Examples.

Let's look at another example, using a time-based function and a little trick. Let's say we want to know the most recent time at which a particular user saw an error each day.

We can use the following query:

sourcetype=tm1* Error TheUser="Admin" | stats count 
first(date_wday) max(_time) as _time by source

This query produces the following table:

Let's step through this example:

We have only seen a few functions in stats. There are dozens of functions and some advanced syntax that we will touch upon in later chapters. The simplest way to find the full listing is to search with your favorite search engine for the Splunk stats functions.