Iniciar sesión

DISSECT and key=value logs

12 min · 2 ejercicio(s)

Grok is powerful, but every pattern is a regular expression. When a format is fixed (same delimiters in the same order on every line), DISSECT does the job faster and with a simpler syntax:

Mar 12 09:58:20 fw-edge-01 kernel: [FW-ALLOW] IN=lan0 OUT=wan0 SRC=10.10.6.61 DST=45.133.1.20 LEN=60 PROTO=TCP SPT=49712 DPT=443
FROM lab-raw
| WHERE event.dataset == "iptables.log"
| DISSECT message "%{} [FW-%{action}] IN=%{in} OUT=%{out} SRC=%{src} DST=%{dst} LEN=%{len} PROTO=%{proto} SPT=%{spt} DPT=%{dpt}"
| STATS lines = COUNT(*) BY action
Syntax Meaning
%{name} Everything up to the next literal text
%{} or %{?name} Skip this part
%{+name} Append to an earlier key (%{+name} joins two parts into one field)
%{name->} Ignore repeated delimiters (padding) after this key

DISSECT has no types and no regular expressions: fields are strings, convert them with TO_INTEGER(dpt) when needed. If a line doesn't fit, all keys are null, just like Grok.

Grok or DISSECT?

Use DISSECT when … Use Grok when …
the delimiters are always the same parts are optional or vary between lines
you parse millions of lines per second you need to validate a value (IP, INT)
the format is CSV-like, key=value or pipe-separated you need types or alternatives

A common trick is to combine both: DISSECT the fixed header, then GROK only the variable message part.

key=value logs

The firewall line is really a list of key=value pairs. Ingest pipelines have a dedicated kv processor for that, which splits on a field separator (space) and a value separator (=). Note the empty OUT= on blocked inbound packets: DISSECT and kv handle that, a Grok pattern with %{WORD:out} would fail.

🔎 Profundiza: funciones, errores habituales y práctica

Why DISSECT is faster

DISSECT scans the string once from left to right and looks for the next delimiter. A Grok pattern is compiled into a regular expression that may backtrack. For a pipeline that processes tens of thousands of events per second, the difference is noticeable. Elastic's own integrations use dissect wherever the format allows.

Padding

Syslog pads one-digit days with an extra space (Mar 9). A DISSECT pattern %{month} %{day} %{time} breaks on such lines because the first delimiter matches the first space only. %{month->} %{day} %{time} tells DISSECT to skip repeated spaces after month. The example above avoids the problem by skipping the whole header with %{}.

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. DISSECT the firewall

    Write a DISSECT pattern for the firewall log that extracts event.action (ALLOW/BLOCK), network.transport (PROTO) and destination.port (DPT):

    Mar  9 00:06:56 fw-edge-01 kernel: [FW-BLOCK] IN=wan0 OUT= SRC=162.234.241.182 DST=192.0.2.10 LEN=44 PROTO=TCP SPT=39887 DPT=3389

    Skip what you don't need with %{}.

  2. 2. Padding in the header

    DISSECT the syslog header of /var/log/auth.log into host.name, process.name and process.pid. Watch out for the days 1 to 9, which syslog pads with an extra space:

    Mar  9 00:21:07 srv-web-01 sshd[27460]: Invalid user ubuntu from 112.22.254.249 port 58777
    Mar 11 02:14:03 srv-web-01 sshd[2211]: Failed password for root from 185.220.101.47 port 51234 ssh2