☼
⌃
☰

Splunk Query Language

Table of Contents

Splunk Query Language

Splunk Query Language, also known as SPL, is the language used to interact with the Splunk data platform.

Core Structure

The fundamental structure of an SPL query is a series of commands chained together using the pipe | character. The query flows from left to right, where the output of one command becomes the input for the next.

SPL
search_criteria | transforming_command1 | transforming_command2 ...
  1. Search Criteria: The query almost always begins with a search term that retrieves a set of raw events from Splunk indexes. This can be as simple as a keyword or more specific, like index=web sourcetype=access_log.
  2. Piped Commands: After the initial data retrieval, you use the pipe | to send those results to subsequent commands that filter, group, transform, or perform calculations on the data.

Common Commands and Usage

Here are some of the most fundamental commands you will use in SPL:

  • stats: Calculates aggregate statistics over the results set. This is one of the most powerful commands in SPL.
    • Usage: … | stats count by host (Counts events for each host)
    • Common functions: count(), avg(), sum(), min(), max(), distinctcount()
  • table: Formats the results into a table, displaying only the fields you specify.
    • Usage: … | table _time, src_ip, user (Creates a table with columns for time, source IP, and user)
  • rename: Renames a field for better readability in the final output.
    • Usage: … | rename src_ip as "Source IP Address"
  • sort: Sorts the results by one or more fields. By default, it sorts in ascending order. Use a minus sign (-) for descending order.
    • Usage: … | sort -count (Sorts the results by the 'count' field, largest first)
  • eval: Creates or manipulates fields. You can use it to perform calculations or define new fields based on existing data.
    • Usage: … | eval velocity_kbps = megaBytes / duration * 1024
  • top / rare: top finds the most frequent values of a field, while rare finds the least frequent.
    • Usage: … | top limit=5 user (Shows the top 5 users with the most events)

Examples

Example : Basic Search for Errors

Find all events from web servers that resulted in an error (HTTP status codes 400-599).

SPL
index=web sourcetype=access_log status>=400
  • index=web sourcetype=access_log: Narrows the search to a specific dataset.
  • status>=400: Filters those results for events where the status field is 400 or higher.

Example 2: Counting Errors by Server

Count the number of errors and group them by the web server (host).

SPL
index=web sourcetype=access_log status>=400 | stats count by host
  • … | stats count by host: Takes the error events from the initial search and counts them, creating a separate row for each unique value in the host field.

Example 3: Creating a Report of Top Talkers

Find the top 10 client IPs generating traffic, sort them, and present the results in a clean table.

SPL
index=network sourcetype=firewall 
| top limit=10 src_ip 
| rename src_ip as "Client IP", count as "Connections"
| table "Client IP", Connections
  • … | top limit=10 src_ip: Finds the 10 most common values in the srcip field.
  • … | rename …: Makes the column headers more readable.
  • … | table …: Displays only the renamed fields in the final output.