条件和正则表达式对 logstash 中 grok 过滤器的怀疑

Conditionals and regex doubts with grok filter in logstash

我正在以一种实用的方法迈出使用 elastic-stack 的第一步,试图让它在我的环境中与应用程序一起工作。我很难从头开始理解如何编写 grok 过滤器。我想要一个像这个一样的工作,所以从那个开始,我可以工作其余的。

我已经参加了一些 udemy 课程,我正在阅读这个 "Elastic Stack 6.0",我正在阅读文档,但我找不到让这项工作按预期工作的方法。

到目前为止,我使用的唯一真正有效的 grok 过滤器就像 (/etc/logstash/config.d/beats.conf)

input {
  beats {
    port => 5044
  }
}

filter {
  grok {
    match => { 'message' => "%{DATE:date} %{TIME:time} % 
{LOGLEVEL:loglevel}"
    } 
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"] 
  }
}

这是我需要处理的日志条目之一,但有许多条目具有不同的形式。我只需要整理这个,这样我就可以调整过滤器以适应其余部分。

2019-02-05 19:13:04,394 信息 [qtp1286783232-574:http://localhost:8080/service/soap/AuthRequest] [name=admin@example.com;oip=172.16.1.69;ua=zclient/8.8.9_GA_3019;soapId=3bde7ed0;] SoapEngine - 处理程序异常:[admin] 身份验证失败,密码无效

我想要这个信息,只有当有 "soapId" 并且 "INFO" 旁边的字段以 "qtq":

开头时
date: 2019-02-05
time: 19:13:04,394
loglevel: INFO
identifier: qtp1286783232-574
soap: http://localhost:8080/service/soap/AuthRequest
Which could also end in things like "GetInfoRequest" or "NoOpRequest"
account: admin@example.com
oip: 172.16.1.69
client: zclient/8.8.9_GA_3019
soapid: 3bde7ed0
error: true (if either "invalid password" or "authentication failed" are found in the line)

如果条件不满足,那么我会应用其他过滤器(希望我能够编写改编这个作为基础)。

如果输入的密码无效,则输出中不能有 false。您只能匹配字符串中的内容。

我想你可以使用

%{DATE:date} %{TIME:time} %{LOGLEVEL:loglevel} *\[(?<identifier>qtp[^\]\[:]*):(?<soap>[^\]\[]*)]\s*\[name=(?<account>[^;]+);oip=(?<oip>[0-9.]+);ua=(?<client>[^;]+);soapId=(?<soapId>[^;]+);].*?(?:(?<error>authentication failed).*)?$

以下是添加模式的详细信息:

  • * - 0+ 个空格
  • \[ - 一个 [ 字符
  • (?<identifier>qtp[^\]\[:]*) - 命名组 "identifier":qtp 然后是 :][[= 以外的 0+ 个字符70=]
  • : - 冒号
  • (?<soap>[^\]\[]*) - 命名组 "soap":除 ][
  • 之外的 0+ 个字符
  • ]\s*\[name= - 一个 ],然后是 0+ 个空格和 [name= 子字符串
  • (?<account>[^;]+) - 命名组 "account":除 ;
  • 以外的 1+ 个字符
  • ;oip= - 文字子串
  • (?<oip>[0-9.]+) - 命名组 "oip": 1+ 数字 and/or 点
  • ;ua= - 文字子串
  • (?<client>[^;]+) - 命名组 "client":除 ;
  • 以外的 1+ 个字符
  • ;soapId= - 文字子串
  • (?<soapId>[^;]+) - 命名组 "soapId":除 ;
  • 之外的 1+ 个字符
  • ;] - 文字子串
  • .*? - 除换行字符外的任何 0+ 个字符,尽可能少
  • (?:(?<error>authentication failed).*)? - 匹配 1 次或 0 次出现的可选组
    • 命名组 "error":authentication failed 子字符串
    • .* - 该行的所有其余部分
  • $ - 输入结束。