streamstats
Running aggregates in time order: cumulative counts, moving averages and previous values per group.
Use streamstats to add running aggregates to your events in time order. You can calculate cumulative counts, moving averages, or previous values within each group.
Syntax
... | streamstats [current=<bool>] [window=<N>] <agg> [as <name>] [, <agg> ...] [by <field> [, <field> ...]]| Option | Default | Description |
|---|---|---|
current | true | Include the current event in its own aggregate. current=false uses only earlier events. |
window | all earlier events | Limit the aggregate to N events. With current=true, this includes the current event; with current=false, it covers N earlier events. |
agg | Any stats aggregation, such as count, avg(f), last(f). Name each with as. | |
by | Keep a separate running aggregate per group. |
Write current= before window=, and both before the aggregations. You can start a query with streamstats and no pipe.
How it works
- Events are processed oldest first (
timeascending), within eachbygroup. - Every event is kept. Each gets one new column per aggregation, holding the value up to that event.
current=falsewithlast(f)gives the previous event's value: useful for time-between-events and change detection.
Examples
The fifth and later failed logon for each user, as they happen:
class_uid=3002 status_id=2 | streamstats count as failures by user.name | where failures >= 5 | table time, user.name, src_endpoint.ip, failuresSeconds since the previous logon by the same user:
class_uid=3002 | streamstats current=false last(time) as prev_time by user.name | where epoch(prev_time) > 0 | eval gap_s=epoch(time) - epoch(prev_time) | table time, user.name, src_endpoint.ip, gap_sA user's logon from a different source IP than their previous logon:
class_uid=3002 status_id=1 | streamstats current=false last(src_endpoint.ip) as prev_ip by user.name | eval changed=if(src_endpoint.ip != prev_ip, 1, 0) | where changed=1 | table time, user.name, prev_ip, src_endpoint.ipMoving average of response size per host over the previous 10 requests:
class_uid=4002 | streamstats current=false window=10 avg(http_response.length) as moving_avg by http_request.url.hostname | where http_response.length > moving_avg * 10Running total of bytes sent per source:
class_uid=4001 | streamstats sum(traffic.bytes_out) as running_bytes by src_endpoint.ipNotes
- Use
epoch(time)for time arithmetic in seconds. Withcurrent=false,last(time)is null on the first event in a group. Filter missing previous values withisnotnull(prev_time). - In
where, a bare word on the right of=or!=is a literal value, not a field. To compare two fields, compute the comparison in eval first, as in the source-IP example. - To add a whole-group aggregate to each event instead of a running one, use eventstats.
What's next
- eventstats: whole-group aggregates on each event
- stats: aggregate into one row per group
- transaction: group related events into sessions