流利的正则表达式模式与NGINX日志不匹配错误

时间:2018-12-07 18:28:35

标签: regex nginx 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进行了测试,并将输出用于配置。

我不确定调试此错误该从哪里走。任何帮助都将得到

1 个答案:

答案 0 :(得分:0)

您的表达是正确的。问题是您正在使用不推荐使用的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>