Iniciar sesión

Grok in ingest pipelines

15 min · 2 ejercicio(s)

A pattern that works in ES|QL belongs in an ingest pipeline, so every new document arrives parsed. A pipeline is a list of processors that run before a document is stored:

PUT _ingest/pipeline/kestrel-sshd
{
  "description": "Parse sshd logons into ECS",
  "processors": [
    {
      "grok": {
        "field": "message",
        "patterns": [
          "%{SYSLOGTIMESTAMP:system.auth.timestamp} %{HOSTNAME:host.hostname} sshd\\[%{INT:process.pid:long}\\]: %{WORD:system.auth.ssh.event} %{WORD:system.auth.ssh.method} for (?:invalid user )?%{USERNAME:user.name} from %{IP:source.ip} port %{INT:source.port:long}"
        ],
        "ignore_missing": true
      }
    },
    { "date": { "field": "system.auth.timestamp", "formats": ["MMM  d HH:mm:ss", "MMM dd HH:mm:ss"] } },
    { "set": { "field": "event.category", "value": ["authentication"] } }
  ],
  "on_failure": [
    { "set": { "field": "error.message", "value": "{{ _ingest.on_failure_message }}" } }
  ]
}
Option Meaning
patterns A list, the first match wins
pattern_definitions Your own named patterns
ignore_missing Don't fail when message doesn't exist
ecs_compatibility v1 (default) or disabled for legacy field names of composite patterns
on_failure What to do when no pattern matches: here the error is stored, the document is kept

Note the JSON escaping: in JSON a backslash is written \\, so the regex \[ becomes \\[.

Test before you deploy

POST _ingest/pipeline/kestrel-sshd/_simulate
{ "docs": [ { "_source": { "message": "Mar 11 02:37:13 srv-web-01 sshd[9150]: Accepted password for deploy from 185.220.101.47 port 50022 ssh2" } } ] }

The simulate API shows the document after every processor. In Kibana, Dev Tools → Grok Debugger and Stack Management → Ingest Pipelines (with Test pipeline) do the same with a UI.

Map to ECS while you parse

Name the captures after ECS fields (source.ip, user.name, process.pid) instead of inventing your own. Then KQL queries, dashboards and the prebuilt detection rules work on your data without changes. The optional ECS track explains the schema in detail.

🔎 Profundiza: funciones, errores habituales y práctica

Performance checklist

  • Anchor patterns with ^ when they start at the beginning of the line.
  • Avoid several DATA/GREEDYDATA in a row; use specific patterns (NOTSPACE, INT) where possible.
  • Put the most common line type first in patterns.
  • Filter early: an if condition on the processor ("if": "ctx.message?.contains('sshd[')") avoids trying patterns on lines that can't match.
  • Use dissect or kv for fixed formats.

The node stats API (GET _nodes/stats/ingest) shows the time spent per processor, so you can find slow patterns in production.

Where pipelines are attached

  • As index.default_pipeline in an index template, so every document is processed.
  • Per request (?pipeline=…) for bulk loads.
  • Elastic Agent integrations install and attach their own pipelines. Add your logic in the @custom pipeline the integration calls, so upgrades don't overwrite it.

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. The pipeline pattern

    Write the pattern you would put into a grok processor for all SSH logon lines (Accepted and Failed, valid and invalid accounts). Extract:

    Field Example
    system.auth.ssh.event Accepted, Failed
    system.auth.ssh.method password, publickey
    user.name deploy
    source.ip 185.220.101.47
  2. 2. A header for every line

    A pipeline should parse the common part of every shop log line. Extract log.level, log.logger (the component in brackets) and the rest of the line into shop.text:

    2026-03-09T06:22:33.850Z INFO  [checkout] order placed order_id=ORD-2026-004014 …
    2026-03-13T21:19:50.123Z WARN  [admin] admin login failed user=admin client=203.0.113.66
    2026-03-13T21:07:02.512Z ERROR [db] query failed: …

    The number of spaces after the level varies. Anchor the pattern at the start of the line.