用于logstash的grok的条件匹配

问题描述 投票:4回答:2

我有这种格式的PHP日志

[Day Mon DD HH:MM:SS YYYY] [Log-Type] [client <ipv4 ip address>] <some php error type>: <other msg with /path/of/a/php/script/file.php and something else>
[Day Mon DD HH:MM:SS YYYY] [Log-Type] [client <ipv4 ip address>] <some php error type>: <other msg without any file name in it>
[Day Mon DD HH:MM:SS YYYY] [Log-Type] [client <ipv4 ip address>] <some msg with out semicolon in it but /path/of/a/file inside the message>

这是我试图通过logstash处理后发送到Graylog2。使用this post here,我能够开始。现在我想得到一些额外的字段,以便我的最终版本看起来像这样。

{
       "message" => "<The entire error message goes here>",
      "@version" => "1",
    "@timestamp" => "converted timestamp from Day Mon DD HH:MM:SS YYYY",
          "host" => "<ipv4 ip address>",
       "logtime" => "Day Mon DD HH:MM:SS YYYY",
      "loglevel" => "Log-Type",
      "clientip" => "<ipv4 ip address>",
      "php_error_type" => "<some php error type>"
      "file_name_from_the_log" => "/path/of/a/file || /path/of/a/php/script/file.php"
      "errormsg" => "<the error message after first colon (:) found>"
}

我有个别行的表达式,或者至少我认为这些应该能够使用grokdebugger进行解析。这样的事情:

%{DATA:php_error_type}: %{DATA:message_part1}%{URIPATHPARAM:file_name}%{GREEDYDATA:errormsg}
%{DATA:php_error_type}: %{GREEDYDATA:errormsg}
%{DATA:message_part1}%{URIPATHPARAM:file_name}%{GREEDYDATA:errormsg}

但不知何故,我发现很难让它适用于整个日志文件。

有什么建议吗?此外,不确定日志文件中是否会出现任何其他类型的错误消息。但目的是为所有人提供相同的格式。有任何建议如何处理这些日志以获得上述格式?

logstash logstash-grok
2个回答
8
投票

grok filter可以配置多种模式:

grok {
  match => [
    "message", "%{DATA:php_error_type}: %{DATA:message_part1}%{URIPATHPARAM:file_name}%{GREEDYDATA:errormsg}",
    "message", "%{DATA:php_error_type}: %{GREEDYDATA:errormsg}",
    "message", "%{DATA:message_part1}%{URIPATHPARAM:file_name}%{GREEDYDATA:errormsg}"
  ]
}

(而不是具有多个模式的单个过滤器,您可以使用多个grok过滤器,但是您可能希望使用tag_on_failure => []禁用_grokparsefailure标记。)


6
投票

如果某些时候丢失了日志行的某些部分,则可以使用以下语法:

(?:%{PATTERN1}|%{PATTERN2})

要么

(?:%{PATTERN1}|)

允许PATTERN1 OR ''。 (空)

使用此功能,您只能管理一种模式:

grok {
   match => [
      "message", "(?:%{DATA:php_error_type}: |)(?:%{DATA:message_part1}:)(?:%{URIPATHPARAM:file_name}|)%{GREEDYDATA:errormsg}",
   ]
}

如果您遇到问题,可以用更严格的模式替换%{DATA}

你也可以使用这种语法(更像正则表达式)

(?:%{PATTERN1})?

要调试复杂的grok模式,我建议:

© www.soinside.com 2019 - 2024. All rights reserved.