_grokparsefailure 在所有具有多个 grok 过滤器的已解析日志中标记

_grokparsefailure Tag in all parsed logs with multiple grok filter

我正在尝试使用 Elastic Stack 解析我的世界日志,但我遇到了一个非常奇怪的问题(对我来说可能很奇怪!)

我的日志的所有行都被正确解析,但我在每个日志中都有 _grokparsefailure 标记。

我的 logstash 管道配置是这样的:

input {
  file {
    path => [ "/path/to/my/log" ]
    #start_position => "beginning"
    tags => ["minecraft"]
  }
}

filter {
  if "minecraft" in [tags] {

#    mutate {
#      gsub => [
#        "message", "\n", ""
#      ]
#    }



    #############################
    #           Num 1           #
    #############################
    grok {
      match => [ "message", "\[%{TIME:timestamp}] \[(?<originator>[^\/]+)?/%{LOGLEVEL:level}]: %{GREEDYDATA:message}" ]
      overwrite => [ "message" ]
      break_on_match => false
    }


    #############################
    #           Num 2           #
    #############################
    grok {
      match => [ "message", "UUID of player %{USERNAME} is %{UUID}" ]
      add_tag => [ "player", "uuid" ]
      break_on_match => true
    }


    #############################
    #           Num 3           #
    #############################
    grok {
      match => [ "message",  "\A(?<player>[a-zA-Z0-9_]+)\[/%{IPV4:ip_address}:%{POSINT}\] logged in with entity id %{POSINT:entity_id} at \(\[(?<world>[a-zA-Z]+)\](?<pos>[^\)]+)\)\Z" ]
      add_tag => [ "player", "join" ]
      break_on_match => true
    }
#
#    grok {
#      match => [ "message",  "^(?<player>[a-zA-Z0-9_]+) has just earned the achievement \[(?<achievement>[^\[]+)\]$" ]
#      add_tag => [ "player", "achievement" ]
#    }
#
#    grok {
#      match => [ "message", "^(?<player>[a-zA-Z0-9_]+) left the game$" ]
#      add_tag => [ "player", "part" ]
#    }
#
#    grok {
#      match => [ "message", "^<(?<player>[a-zA-Z0-9_]+)> .*$" ]
#      add_tag => [ "player", "chat" ]
#    }
  }
}

output {
        elasticsearch {
                hosts => ["elasticsearch:xxxx"]
                user => "xxxx"
                password => "xxxxxx"
        index => "minecraft_s1v15_%{+YYYY.MM.dd}"
        }
}

我的日志示例是:

[11:21:46] [User Authenticator #7/INFO]: UUID of player MyAwsomeUsername is d800b63e-c2d2-3140-83a7-32315d09feca
[11:21:46] [Server thread/INFO]: MyAwsomeUsername joined the game
[11:21:46] [Server thread/INFO]: MyAwsomeUsername[/111.111.111.111:45140] logged in with entity id 6868 at ([world]61.45686149445207, 70.9375, -175.44700729217607)
[11:21:49] [Server thread/INFO]: MyAwsomeUsername issued server command: //efererg
[11:21:52] [Async Chat Thread - #1/INFO]: <MyAwsomeUsername> egerg
[11:21:54] [Async Chat Thread - #1/INFO]: <MyAwsomeUsername> ef
[12:00:19] [Server thread/INFO]: MyAwsomeUsername lost connection: Disconnected
[12:00:19] [Server thread/INFO]: MyAwsomeUsername left the game
[12:00:21] [User Authenticator #8/INFO]: UUID of player MyAwsomeUsername is d800b63e-c2d2-3140-83a7-32315d09feca
[12:00:21] [Server thread/INFO]: MyAwsomeUsername joined the game
[12:00:21] [Server thread/INFO]: MyAwsomeUsername[/111.111.111.111:45470] logged in with entity id 11767 at ([world]61.45686149445207, 70.9375, -175.44700729217607)
[12:00:27] [Server thread/INFO]: MyAwsomeUsername issued server command: /wgergerger
[12:00:29] [Async Chat Thread - #2/INFO]: <MyAwsomeUsername> gerg
[12:00:33] [Async Chat Thread - #2/INFO]: <MyAwsomeUsername> gerger
[12:00:35] [Async Chat Thread - #2/INFO]: <MyAwsomeUsername> rerg
[12:00:37] [Server thread/INFO]: MyAwsomeUsername lost connection: Disconnected
[12:00:37] [Server thread/INFO]: MyAwsomeUsername left the game
[12:00:38] [User Authenticator #8/INFO]: UUID of player MyAwsomeUsername is d800b63e-c2d2-3140-83a7-32315d09feca
[12:00:38] [Server thread/INFO]: MyAwsomeUsername joined the game
[12:00:38] [Server thread/INFO]: MyAwsomeUsername[/111.111.111.111:45476] logged in with entity id 11793 at ([world]62.97573252632079, 71.0, -179.01739415148737)
[12:00:40] [Server thread/INFO]: MyAwsomeUsername lost connection: Disconnected
[12:00:40] [Server thread/INFO]: MyAwsomeUsername left the game
[12:00:51] [User Authenticator #8/INFO]: UUID of player MyAwsomeUsername is d800b63e-c2d2-3140-83a7-32315d09feca
[12:00:51] [Server thread/INFO]: MyAwsomeUsername joined the game
[12:00:51] [Server thread/INFO]: MyAwsomeUsername[/111.111.111.111:45486] logged in with entity id 11805 at ([world]62.97573252632079, 71.0, -179.01739415148737)
[12:00:55] [Server thread/INFO]: MyAwsomeUsername lost connection: Disconnected
[12:00:55] [Server thread/INFO]: MyAwsomeUsername left the game


解释:

我评论了其他 grok 以更简单地解释问题(当解除它们时完全相同的问题)

我测试了3种情况:

  1. 评论 2 和 3 以及其他评论,只有 1 个处于活动状态,在这种情况下,日志的每一行都被解析,但记录中没有任何 _grokparsefailure
  2. 只有 1 和 2 被评论,其他人也被评论。在这种情况下,与 grok 编号 2 匹配的日志行被解析为没有 _grokparsefailure,而其他行得到 _grokparsefailure。这仍然是有道理的!
  3. 在最后一种情况下,我取消了所有 3 个 grok(1、2、3 处于活动状态)的注释,并且日志的每一行都被解析 但是 其中包含 _grokparsefailure!即使 break_on_match 默认为 true 并且当它与 grok 2 匹配时也不应使用 grok 3 进行测试。

我在 Whosebug 中阅读了一些与我类似的其他问题: 并且我在 grok 过滤器之前添加了 mutate 块(因为日志的每一行都以 \n 结尾)但是什么都没有改变,问题仍然存在!

我认为我需要提及的另一件事是我知道在 grok 2(3 和其他)旁边添加更多 grok 导致此标记导致某些日志根本不匹配 grok 2 并且必须换行他们用正则表达式。但目前至少匹配 grok 2 的日志应该是好的(没有 _grokparsefailure),但它们不是! (在 Whosebug 问题中阅读它:Similar Question 2

事实上,这是预期的行为,您有点混淆了 logstash 和 grok 的工作方式。

首先,所有过滤器都是相互独立的,在grok中使用break_on_match只会影响grok,对其他grok过滤器没有影响在那之后出现在你的管道中。 break_on_match 也只有当您在同一个 grok 中有多个模式时才有意义,但您的情况并非如此。

其次,由于 Logstash 是串行的并且您没有使用任何条件,因此您的 grok 过滤器将应用于管道中的每条消息,它是否已经被解析并不重要,这就是正在让你的台词获得 _grokparsefailure

要解决这个问题,您需要使用条件。

您的前两个 grok 过滤器不需要条件,第一个只是获取日志行的不同部分并覆盖到 message 字段中,第二个将是只是您的第一个测试,对于第二个测试之后的每个 grok,您将需要以下配置。

if "_grokparsefailure" in [tags] {
  grok {
    match => "your pattern"
    add_tag => "your tags"
    remove_tag => ["_grokparsefailure"]
  }
}

仅当消息在 tags 字段中包含 _grokparsefailure 时才会应用此 grok,如果消息与您的模式匹配,则此标签将被删除,如果不匹配匹配,标签保留,消息可以通过以下 groks 进行测试。

最后,您的 grok 配置应该如下所示。

grok {
  "your first grok"
}

grok {
  "your second grok, can be any of the others"
}

if "_grokparsefailure" in [tags] {
  grok {
    "your grok N"
    remove_tag => ["_grokparsefailure"]
  }
}

这是唯一需要的,因为您要为每封邮件添加不同的标签,例如,如果您将此逻辑移动到 mutate 过滤器,则您只能使用两个 grok 过滤器,第二个将是 multi-pattern grokbreak_on_match 设置为 true

grok {
  match => { 
    "message" => [ 
      "pattern from grok 2",
      "pattern from grok 3",
      "pattern from grok N"
    ]
  }
  break_on_match => true
}