rsyslog学习1 -- 配置格式

​ 年初搭建了一套ELK来收集现网中网络设备日志,通过设备发送syslog到各idc的rsyslog中转服务器,再转发到kafka,用logstash写入es。因为之前没有做过滤,跑了半年,发现日志量有点大,因为设备型号比较多,cisco/hw/zte/ruijie/h3c/dptech/f5,发的日志也千奇百怪,里面有很多无用的message,对于查找也存在干扰,所以决定好好的对日志进行一下过滤。

​ 过滤可以布置在两个点,一是rsyslog服务器上,二是logstash。所以先从服务器入手,好好的学习一下rsyslog的配置。主要参考文档

配置格式

basic

​ 又叫 sysklogd 格式。基本配置,一句一行,用了多年。

1
2
mail.info /var/log/mail.log
mail.err @@server.example.net

advanced

​ 又叫 RainerScript 格式。v6开始支持,v7有性能问题,目前版本(v8)正常使用。

  • 更多的参数,更好的控制
  • 使用块结构
  • 容易编写
  • 可以使用include
1
mail.err action(type="omfwd" protocol="tcp" queue.type="linkedList")

obsolete legacy

​ 又叫 legacy 格式。如其名,已被 obsoleted。

1
$ModLoad module-name

将配置转为 advanced

不要过度转换

1
mail.info   /var/log/maillog

​ 一些basic的配置不需要转换,保持就好。如果要转的话,可以转成

1
2
3
if prifilt("mail.info") then {
action(type="omfile" file="/var/log/maillog")
}

1
if prifilt("mail.info") then action(type="omfile" file="/var/log/maillog")

1
mail.info action(type="omfile" file="/var/log/maillog")

转换module

​ legacy语法在多次引用时容易出现问题

1
2
3
4
$ModLoad module-name

$ModLoad imtcp
$InputTCPMaxSession 500

​ 可以转换为

1
2
3
module(load="module-name")

module(load="imtcp" maxSessions="500")

转换action

1
filter action

​ 转换action并不一定要转换filter,因为不同格式的filter都可以和action一起工作

​ 下面列了一些从basic到advance的action转换

basic advanced
file path (/var/log/…) action(type=”omfile” file=”/var/log…/” …)
UDP forwarding (@remote) action(type=”omfwd” target=”remote” protocol=”udp” …)
TCP forwarding (@@remote) action(type=”omfwd” target=”remote” protocol=”tcp” …)
user notify (:omusrmsg:user) action(type=”omusrmsg” users=”user” …)
module name (:omxxx:..) action(type=”omxxx” …)

​ 例如

1
2
3
4
5
6
7
8
OLD: :hostname, contains, "remote-sender" @@central
NEW: :hostname, contains, "remote-sender" action(type="omfwd" target="central" protocol="tcp")

OLD: if $msg contains "error" then @central
NEW: if $msg contains "error" then action(type="omfwd" target="central" protocol="udp")

OLD: *.emerg :omusrmsg:*
NEW: *.emerg action(type="omusrmsg" users="*")

带多个参数的action转换

​ 在basic中,多个action可以使用 & 来连接。

1
2
*.error /var/log/errorlog
& @remote

​ 在advanced中,使用block来表示

1
2
3
4
*.error {
action(type="omfile" file="/var/log/errorlog")
action(type="omfwd" target="remote" protocol="udp")
}

​ 使用 stop 或 ~ 来停止动作

1
2
3
4
5
:msg, contains, "error" @remote
& ~

:msg, contains, "error" @remote
& stop
1
2
3
4
:msg, contains, "error" {
action(type="omfwd" target="remote" protocol="udp")
stop
}

1
2
3
4
if $msg contains "error" then {
action(type="omfwd" target="remote" protocol="udp")
stop
}
0%