Post

Mastering the Tstats Command in Splunk

Mastering the Tstats Command in Splunk

Introduction

The tstats command in Splunk 9.2.1 is a powerful tool that enhances your data search capabilities. It provides optimized performance by leveraging indexed fields in Splunk Enterprise. This guide will walk you through the functionalities, syntax, and practical applications of the tstats command.

Benefits of Using Tstats

  • Improved Performance: Executes faster than traditional search commands by utilizing indexed data.
  • Flexibility: Supports various statistical functions like count, sum, avg, min, max, and more.
  • Efficiency: Reduces the search load on Splunk instances by querying indexed data directly.

Syntax and Usage

The basic syntax for tstats is as follows:

1
| tstats [stats-functions] from [datasets] where [filter-condition] by [field-list]

Example

To count the number of events:

1
2
3
4
5
# Count events from index=_internal
| tstats count where index=_internal

# Successful authentication events for each user broken down by hour
| tstats count from datamodel=Authentication where Authentication.action="success" by _time span=1h, Authentication.user

Key Parameters

  • Stats Functions: count, sum, avg, min, max, etc.
  • Datasets: The Splunk indexes or data models.
  • Filter Condition: Conditions to filter data.
  • Field List: Fields to group the statistics by.
FunctionDescriptionExample
countCounts the number of events.| tstats count where index=_internal
sumSums the values of a numeric field.| tstats sum(bytes) where index=web_logs by host
avgCalculates the average value of a numeric field.| tstats avg(duration) where index=transactions by service
minFinds the minimum value of a numeric field.| tstats min(response_time) where index=web_logs by endpoint
maxFinds the maximum value of a numeric field.| tstats max(cpu_usage) where index=system_logs by host
valuesReturns a list of distinct values of a field.| tstats values(status_code) where index=web_logs by host
dcCounts the distinct values of a field.| tstats dc(user) where index=authentication by src_ip

Timespan

The timespan argument in the tstats command allows you to specify a time range for your statistical calculations. This is particularly useful for breaking down data into more granular time intervals for detailed analysis.

Syntax

1
| tstats [stats-functions] from [datasets] where [filter-condition] by [field-list] _time span=[time-interval]
Time ScaleSyntaxDescription
Secondss, sec, secs, second, secondsTime scale in seconds.
Minutesm, min, mins, minute, minutesTime scale in minutes.
Hoursh, hr, hrs, hour, hoursTime scale in hours.
Daysd, day, daysTime scale in days.
Monthsmon, month, monthsTime scale in months.

Example

To count events in 10-minute intervals:

1
| tstats count where index=_internal by _time span=10m

Source

This post is licensed under CC BY 4.0 by the author.