Grok 本质 = 命名正则 + 预定义模式
通常用于:
示例:
%{IP:client_ip} %{WORD:method} %{URIPATH:path}Dev Tools → Grok Debugger127.0.0.1 GET /index.html%{IP:client_ip} %{WORD:method} %{URIPATH:path}input {
stdin {}
}
filter {
grok {
match => {
"message" => "%{IP:client_ip} %{WORD:method} %{URIPATH:path}"
}
}
}
output {
stdout { codec => rubydebug }
}bin/logstash -f test.conf127.0.0.1 GET /index.htmlPUT _ingest/pipeline/grok-test
{
"processors": [
{
"grok": {
"field": "message",
"patterns": ["%{IP:client_ip} %{WORD:method} %{URIPATH:path}"]
}
}
]
}POST _ingest/pipeline/grok-test/_simulate
{
"docs": [
{
"_source": {
"message": "127.0.0.1 GET /index.html"
}
}
]
}127.0.0.1 GET /index.html✅ 用:
%{IP:client_ip}\s+%{WORD:method}\s+%{URIPATH:path}2024-01-01T12:00:00✅ 使用:
%{TIMESTAMP_ISO8601:timestamp}patterns_dir => ["/opt/logstash/patterns"]patterns 文件示例:
MY_PATTERN \d{4}-\d{2}-\d{2}cat access.log | logstash -f test.conf(.*) 无限制匹配✅ 标准流程
_grokparsefailure 监控失败日志测试 Grok 配置效果 = 用真实日志 + Grok Debugger / Logstash / Ingest Pipeline,确认字段是否被正确解析。
如果你愿意,可以把:
直接发我,我可以帮你 逐字符优化并验证。