Docker syslog驱动程序,在logstash中使用多行解析

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

我通过syslog驱动程序将docker日志转发到logstash。这适用于普通日志行,但存在多行问题。我遇到的问题是docker日志转发将syslog消息格式添加到每个日志行。如果我使用logstash过滤器多行(不推荐使用logstash),我可以成功组合多行并删除其他行上的syslog消息...但是,这不是线程安全的。我无法通过输入编解码器获得逻辑,这是logstash建议的。

例如:

Docker命令:

docker run --rm -it \
      --log-driver syslog \
      --log-opt syslog-address=tcp://localhost:15008 \
      helloWorld:latest

登录docker容器:

Log message A
<<ML>> Log message B
  more B1
  more B2
  more B3
Log message C

将收到的日志记录到logstash中

<30>Jul 13 16:04:36 [1290]: Log message A
<30>Jul 13 16:04:37 [1290]: <<ML>> Log message B
<30>Jul 13 16:04:38 [1290]:  more B1
<30>Jul 13 16:04:39 [1290]:  more B2
<30>Jul 13 16:04:40 [1290]:  more B3
<30>Jul 13 16:04:41 [1290]:Log message C

现在我可以使用以下过滤器来解析所有内容:

logstash过滤多行

input { 
  tcp {
   port => 15008
   type => "multiline"
 }
}

filter {
  if ( [type] == "multiline") {
    grok {
      match => { "message" => [
        "^<(?<ignore>\d*)>(?<syslogDateTime>[\S]*)\s\[(?<pid>\d*)\]:.(?<newMessage>[\s\S]*)"
      ]}
    }

    multiline {
      pattern => "^[\s\S]*\<\<[M][L]\>\>"
      negate => true
      what => "previous"
      source => "newMessage"
      stream_identity => "%{host}.%{pid}"
    }
}

这正是我在logstash消息中想要的

产量

message: Log message A
message: <<ML>> Log message B more B1 more B2 more B3
message: Log message C

但是,这会运行几分钟......但随后会挂起并停止处理

试图通过编解码器多行来使其工作,这是logstash推荐

logstash编解码器多行

 input { 
      tcp {
       port => 15008
       type => "multiline"
       codec => multiline {
         pattern => "^[\s\S]*\<\<[M][L]\>\>"
         negate => true
         what => "previous"
       }
     }
    }

    filter {
      if ( [type] == "multiline") {
        grok {
          match => { "message" => [
            "^<(?<ignore>\d*)>(?<syslogDateTime>[\S]*)\s\[(?<pid>\d*)\]:.(?<newMessage>[\s\S]*)"
          ]}
        }
    }

它正确地组合了多行,但我现在将这些syslog消息混合到我的多行消息中

产量

message: Log message A
message: <<ML>> Log message B <30>Jul 13 16:04:38 [1290]: more B1 <30>Jul 13 16:04:39 [1290]: more B2 <30>Jul 13 16:04:40 [1290]: more B3
message: Log message C

如何让编解码器处理像过滤器一样输出?

docker logstash multiline logstash-configuration
1个回答
0
投票

好吧,我通过使用logstash编解码器多行并在grok匹配后添加另一个过滤器来实现此功能

    mutate {
      gsub => [
        "message", "<\d*>[\s\S]*?\[\d*\]:.", " "
      ]
    }
© www.soinside.com 2019 - 2024. All rights reserved.