Iniciar sesión

Pattern syntax: %{SYNTAX:SEMANTIC}

14 min · 2 ejercicio(s)

A Grok pattern is a mix of literal text and named patterns:

Accepted %{WORD:method} for %{USERNAME:user} from %{IP:source_ip}
Part Meaning
Accepted , for, from Literal text, must appear exactly like this (including the spaces)
%{WORD:method} Pattern WORD (letters, digits, underscore), stored as method
%{USERNAME:user} Pattern USERNAME ([a-zA-Z0-9._-]+), stored as user
%{IP} A pattern without a name matches but stores nothing

The first word is the SYNTAX (which pattern), the second the SEMANTIC (the field name). Field names may contain dots, so %{IP:source.ip} writes directly into an ECS field.

The pattern doesn't need to match the whole line

Grok searches the pattern anywhere in the string. The pattern above matches in the middle of

Mar 11 02:37:13 srv-web-01 sshd[9150]: Accepted password for deploy from 185.220.101.47 port 50022 ssh2

To force a match at the start or end, use the regex anchors ^ and $. Anchors also make patterns faster, because the engine stops trying at every position.

No match means null

If the pattern does not match a line, every named field is null for that row. That is often what you want: GROK doubles as a filter.

FROM lab-raw
| WHERE event.dataset == "system.auth"
| GROK message "Accepted %{WORD:method} for %{USERNAME:user} from %{IP:source_ip}"
| WHERE user IS NOT NULL
| STATS logons = COUNT(*) BY method, user

Special characters

Grok patterns are regular expressions. These characters must be escaped with a backslash when you mean them literally: [ ] ( ) { } . * + ? ^ $ | \\. The sshd process id in sshd[2211] is matched with sshd\[%{INT:pid}\].

In ES|QL a normal string "…" treats the backslash as an escape character itself. Use triple quotes """…""" for patterns with backslashes, then you write them exactly as in the Grok Debugger:

FROM lab-raw
| WHERE event.dataset == "system.auth"
| GROK message """sshd\[%{INT:pid}\]: %{GREEDYDATA:text}"""
| KEEP pid, text

Precision matters

Failed password for %{USERNAME:user} from matches "Failed password for root from", but not "Failed password for invalid user root from": USERNAME can't contain spaces, and the literal from doesn't follow. Read your data before you trust a count.

🔎 Profundiza: funciones, errores habituales y práctica

DATA vs. GREEDYDATA

Pattern Regex Behaviour
%{DATA:x} .*? Lazy: as little as possible, until the next part of the pattern matches
%{GREEDYDATA:x} .* Greedy: as much as possible, up to the end of the line

%{DATA} in the middle of a pattern, %{GREEDYDATA} only at the very end. Two GREEDYDATA in one pattern are almost always a mistake and can be slow, because the engine has to try every way to split the text between them.

Whitespace

Log lines often pad with several spaces (Mar 9, INFO [auth]). A literal space only matches exactly one space. Use \s+ or %{SPACE} (zero or more) when the number can vary. SYSLOGTIMESTAMP already handles the double space before one-digit days.

All fields are strings

Without a type, every captured field is a keyword string, even %{INT:pid}. Comparing it with a number (pid > 1000) fails. Module 2 shows how to convert types inside the pattern.

Test patterns step by step

Build long patterns from left to right. Start with the first token, run, check, add the next one. If a pattern suddenly returns only nulls, the last thing you added is the problem.

Ejercicios prácticos de esta lección

Resuelve estos ejercicios con una consulta en el laboratorio Elasticsearch real. Inicia sesión gratis para acceder al laboratorio, las pistas y la corrección automática.

  1. 1. Who logged in, and how?

    Successful SSH logons look like this:

    Mar 11 02:37:13 srv-web-01 sshd[9150]: Accepted password for deploy from 185.220.101.47 port 50022 ssh2
    Mar  9 09:12:40 srv-db-03 sshd[3317]: Accepted publickey for h.yilmaz from 10.10.5.51 port 50122 ssh2: ED25519 SHA256:…

    Write a pattern that extracts the logon method (password, publickey) into method and the account into user.name. The lab applies it to all Accepted lines and counts logons per method and user.

  2. 2. Escape the brackets

    Extract the process ID of sshd from every line of /var/log/auth.log into process.pid:

    Mar 11 02:14:03 srv-web-01 sshd[2211]: Failed password for root from 185.220.101.47 port 51234 ssh2

    The lab counts the distinct PIDs and the lines your pattern missed.