【问题标题】:_grokparsefailure Tag in all parsed logs with multiple grok filter_grokparsefailure 使用多个 grok 过滤器在所有已解析日志中标记
【发布时间】:2020-09-02 08:14:22
【问题描述】:

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

我的日志的所有行都被正确解析,但我在每个人中都有_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 处于活动状态),并且每一行日志都被解析 BUT,其中包含 _grokparsefailure!即使 break_on_match 默认为 true 并且当它与 grok 2 匹配时,也不应该使用 grok 3 进行测试。

我在 stackoverflow 中读到了与我类似的其他问题:Similar Question 1 并且我在 grok 过滤器之前添加了 mutate 块(因为日志的每一行都以 \n 结尾)但没有任何改变并且问题仍然存在!

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

【问题讨论】:

    标签: logstash elastic-stack logstash-grok logstash-configuration


    【解决方案1】:

    事实上,这是预期的行为,您有点混淆了 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 过滤器,您的第二个将是多模式grok,break_on_match 设置为 true。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多