rsyslog使用field切分日誌

場景:

有一個解析亂七八糟日誌的需求,格式由於各種原因改不了,並且日誌的目的地只能傳輸到rsyslog。需求就是從亂七八糟的日誌中提取出需要的信息。

解析前的日誌如下:

|! |! |!2|!1|!0|!0|! |! |! |! |!0|!0|! |! |! |! |!0|!0|! |!|! |! |!0|!0|! |! |!1.1.1.1|!1540438466000|!1540436316761|!2.2.2.2|!0|!0|!0|!0.0|!6|! |!0|!0|!0|!0|!0.0|!0.0|! |! |! |! |! |! |!0|!0|!0|!0|!0|!0|!0|!0|! |! |! |! |! |! |! |!yxgl-web03 root: 3.3.3.3#0112018-01-25#01111:34:26#011POST#011/index.html#011"http://4.4.4.4/get"#011"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"#011200#011359|!

要的日誌:

3.3.3.3#0112018-01-25#01111:34:26#011POST#011/index.html#011"http://4.4.4.4/get"#011"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"#011200#011359

rsyslog實現

1,rsyslog相關軟件版本:

rsyslog-kafka-8.38.0-1.el7.x86_64
rsyslog-8.38.0-1.el7.x86_64

2,配置

set $!usr!one = field($msg, " root: ", 2);
set $!usr!two = field($!usr!one, "|!", 1);
template (name="l7" type="string" string="%$!usr!two%")
local5.info /tmp/1.log;l7

通過field把msg以" root: "爲分隔符切分,然後取第二個字段並賦值給\$!usr!one;然後,再把\$!usr!one以"|!"爲分隔符切分,取第一個字段即爲我們要的數據。

參考文檔

https://www.rsyslog.com/doc/v8-stable/rainerscript/functions/rs-field.html
https://www.rsyslog.com/doc/v8-stable/configuration/templates.html
https://www.rsyslog.com/doc/v8-stable/configuration/properties.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章