暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

日志搜集分析(八)-- fluent插件简化Nginx日志匹配

拖地先生 2018-11-30
598

系统搭建分析请参考公众号菜单中的日志分析。




fluentd 默认配置中有source,match,filter三段内容

其中source段为日志输入源,其中format 为日志解析规则 必选字段
但是format 匹配规则用正则匹配,人难以理解且难调试

format /^(?<remote>[^ ]*) - - \[(?<time_local>[^]]*)\] (?<request_time>[^ ]*) (?<upstream_request_time>[^ ]*) "(?<method>\S+)(?<path>[^\"]*) \S*" (?<status>[^ ]*) (?<body_bytes_sent>[^ ]*) "(?<http_referer>[^ ]*)" "(?<clientVersion>[^\"]*)" "(?<http_x_forwarded_for>.*)" (?<request_body>[^ ]*) (?<request_params>[^ ]*)$/

fluentd官方文档中内置了几个常用的解析器:

  • regexp

  • apache2

  • apache_error

  • nginx

  • syslog

  • csv

  • tsv

  • ltsv

  • json

  • multiline

  • none

使用内置的解析器(如:json、csv、ltsv)就可以避免理解难以调试的正则匹配规则,以下是使用ltsv
格式的例子:

格式化nginx日志

log_format log_ltsv 'remote:$remote_addr\t'
                    'remote_user:$remote_user\t'
                    'time_local:$time_local\t'  
                    'request_time:$request_time\t'   
                    'upstream_response_time:$upstream_response_time\t'      
                    'method:$request_method\t'      
                    'path:/ec$request_uri\t'   
                    'status:$status\t'  
                    'body_bytes_sent:$body_bytes_sent\t'                    
                    'clientVersion:$http_user_agent\t'          
                    'http_x_forwarded_for:$http_x_forwarded_for\t'                       
                    'request_body:$request_body\t'   
                    'request_params:$args';

在nginx server中access_log配置使用

server {
    ## 其他配置    
   access_log api.log log_ltsv;     ## 其他配置
}

在td-agent.conf 中配置

<source>  
 type tail   path api.log ### 日志路径    pos_file log.pos   format ltsv ## 重要
</source>



  • 参考1:https://yq.aliyun.com/articles/484403

  • 参考2:https://docs.fluentd.org/v0.12/articles/parser-plugin-overview


文章转载自拖地先生,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论