【问题标题】:fluentd regex pattern not match error for NGINX logsNGINX 日志的流利的正则表达式模式不匹配错误
【发布时间】:2018-12-07 18:28:35
【问题描述】:

我已经设置了 fluentd 来解析 nginx 访问日志,因为我在日志中添加了一些额外的字段,我无法使用 nginx 格式。我的配置是:

   <source>
    @type tail
    @id service_www_accesslog
    path /var/log/containers/imager-nginx*.log
    pos_file /var/log/imager-nginx-containers.log.pos
    tag influxdb.*
    read_from_head true
    format /(?<host>[^ ]*) \[(?<time>[^\]]*)\] \[Cache:(?<cache>\b\w+\b)\] "(?<CSMethod>\b\w+\b) (?<CSURIStem>(?:\/[A-Za-z0-9$.+!*'(){},~:;=@#%&_\-]*)+) (?<request>.*?)" (?<SCStatus>(?:(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))) (?<image_size>(?:(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))) "(?<referer>.*?)" "(?<user_agent>.*?)" "(?<xforward>.*?)" (?<request_time>(?:(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))) (?<upstream_response>.*?) (?<upstream_connect>.*?) (?<upstream_header>.*)/
    time_format %d/%b/%Y:%H:%M:%S %z
    @log_level debug
  </source>

日志样本:

10.244.1.0 [07/Dec/2018:16:51:49 +0000] [Cache:MISS] "GET /10e8cd74-94ec-4652-a5c9-d5df1110b9eb/475/400/60/false/blah.jpg HTTP/1.1" 200 60435 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36" "172.23.8.123" 0.162 0.160 0.001 0.151

在流畅的输出中我得到一个错误:

2018-12-07 16:51:50 +0000 [warn]: #0 [service_www_accesslog] pattern not match: "{\"log\":\"10.244.1.0 [07/Dec/2018:16:51:49 +0000] [Cache:MISS] \\\"GET /10e8cd74-94ec-4652-a5c9-d5df1110b9eb/475/400/60/false/blah.jpg HTTP/1.1\\\" 200 60435 \\\"-\\\" \\\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36\\\" \\\"172.23.8.123\\\" 0.162 0.160 0.001 0.151\\n\",\"stream\":\"stdout\",\"time\":\"2018-12-07T16:51:49.224406444Z\"}"

我已经使用几个正则表达式测试器测试了正则表达式,它确实有正确的输出,我还使用Fluentular 进行了测试并将输出用于配置。

在调试此错误时,我不确定从哪里开始。任何帮助都会得到帮助

【问题讨论】:

  • 请张贴清晰的输入样本和所需的输出。
  • @PedroLobito 不确定我应该添加哪些额外信息?有一个日志条目示例提供,我希望没有错误。

标签: regex nginx fluentd


【解决方案1】:

你的表达是正确的。问题是您正在使用已弃用的 format 参数。相反,您需要使用parser 插件,并且有内置的nginx 解析器,您可以在其中设置表达式。下面将是您更新的 fluent.conf,它可以很好地解析您的输入。

<source>
  @type tail
  @id service_www_accesslog
  path /var/log/containers/imager-nginx*.log
  pos_file /var/log/imager-nginx-containers.log.pos
  tag influxdb.*
  read_from_head true
  <parse>
   @type nginx
   expression /(?<host>[^ ]*) \[(?<time>[^\]]*)\] \[Cache:(?<cache>\b\w+\b)\] "(?<CSMethod>\b\w+\b) (?<CSURIStem>(?:\/[A-Za-z0-9$.+!*'(){},~:;=@#%&_\-]*)+) (?<request>.*?)" (?<SCStatus>(?:(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))) (?<image_size>(?:(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))) "(?<referer>.*?)" "(?<user_agent>.*?)" "(?<xforward>.*?)" (?<request_time>(?:(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))) (?<upstream_response>.*?) (?<upstream_connect>.*?) (?<upstream_header>.*)/
   time_format %d/%b/%Y:%H:%M:%S %z
  </parse>
  @log_level debug
</source>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多