通过Fluentd解析nginx错误日志

4

我希望能解析nginx错误日志文件并将解析后的格式放入elasticsearch。我已经成功地对access.log进行了同样的操作,因为它接受log_format指令,但error.log并不支持该指令。我有以下样本错误日志,主要感兴趣:

2018/01/10 06:26:31 [error] 13485#13485: *64285471 limiting connections by zone "rl_conn", client: xx.xx.xx.xx, server: www.xyz.com, request: "GET /api/xyz HTTP/1.1", host: "www.xyz.com"

我想通过某个解析器对其进行解析,以便获得Json格式的数据,如下所示:
{client: "xx.xx.xx.xx", server: "www.xyz.com", host: "www.xyz.com", "request": "GET /api/xyz HTTP/1.1", reason: "limiting connections by zone "rl_conn""}

有人能在这里提供帮助吗?


你找到解决方案了吗? - tuan.dinh
1个回答

1

@serut在这个Github问题的底部提供了一个相当不错的解决方案:https://github.com/fluent/fluentd/issues/2991

# Use NGINX parse for front logs
<label @PARSENGINX>
    <filter front>
        @type parser
        key_name message
        <parse>
            @type nginx
        </parse>
    </filter>
    <filter front>
        # Handle errors
        @type parser
        key_name message
        <parse>
            @type regexp
            expression /^(?<logtime>\d{4}\/\d{1,2}\/\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}) (?<log_level>\[[^\s]+\]) (?<message>.*)$/
            time_key logtime
            time_format %Y/%m/%d %H:%M:%S
        </parse>
    </filter>
    <match **>
        @type copy
        <store>
            @type relabel
            @label @OUTPUT
        </store>
    </match>
</label>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接