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/GREEDYDATAin a row; use specific patterns (NOTSPACE,INT) where possible. - Put the most common line type first in
patterns. - Filter early: an
ifcondition on the processor ("if": "ctx.message?.contains('sshd[')") avoids trying patterns on lines that can't match. - Use
dissectorkvfor 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_pipelinein 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
@custompipeline the integration calls, so upgrades don't overwrite it.