rsyslog 可以理解为多线程增强版的syslog。 在syslog的基础上扩展了很多其他功能,如数据库支持(MySQL、PostgreSQL、Oracle等)、日志内容筛选、定义日志格式模板等。目前大多数Linux发行版默认也是使用rsyslog进行日志记录。rsyslog提供了三种远程传输协议:
UDP 传输协议
基于传统UDP协议进行远程日志传输,也是传统syslog使用的传输协议; 可靠性比较低,但性能损耗最少, 在网络情况比较差, 或者接收服务器压力比较高情况下,
可能存在丢日志情况。 在对日志完整性要求不是很高,在可靠的局域网环境下可以使用。
TCP 传输协议
基于传统TCP协议明文传输,需要回传进行确认,可靠性比较高; 但在接收服务器宕机或者两者之间网络出问题的情况下,会出现丢日志情况。 这种协议相比于UDP在
可靠性方面已经好很多,并且rsyslog原生支持,配置简单, 同时针对可能丢日志情况,可以进行额外配置提高可靠性,因此使用比较广。
RELP 传输协议
RELP(Reliable Event Logging Protocol)是基于TCP封装的可靠日志消息传输协议; 是为了解决TCP 与 UDP 协议的缺点而在应用层实现的传输协议,也是三者
之中最可靠的。 需要多安装一个包rsyslog-relp以支持该协议。
对于线上服务器,为了日志安全起见,建议使用还是使用 RELP 协议进行传输。
rsyslog服务端的部署
安装rsyslog 程序(rsyslog默认已经在各发行版安装,如果系统中没有的话,可以用yum 进行安装,如下:)
[root@zabbix ~]# yum install rsyslog -y
配置:
[root@zabbix ~]# cat /etc/rsyslog.conf
#rsyslog v5 configuration file
#For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
#If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
####MODULES ####
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog # provides kernel logging support (previously done by rklogd)
$ModLoad immark # provides --MARK-- message capability
#Provides UDP syslog reception
$ModLoad imudp #开启udp的514端口。也可以开启tcp的514端口,这里只接受udp的
$UDPServerRun 514
#Provides TCP syslog reception
#KaTeX parse error: Expected 'EOF', got '#' at position 15: ModLoad imtcp #̲InputTCPServerRun 514
$WorkDirectory /var/lib/rsyslog
$AllowedSender udp, 192.168.17.0/8 #仅仅接收来自192.168.17.0/8网段的主机的udp日志(这个是公司防火墙的ip地址)
####GLOBAL DIRECTIVES ####
#Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
KaTeX parse error: Expected 'EOF', got '&' at position 92: ….* ?RemoteLogs &̲ ~ :fromhost-ip…ActionFileEnableSync on
#Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
####RULES ####
#Log all kernel messages to the console.
#Logging much else clutters up the screen.
#kern.* /dev/console
#Log anything (except mail) of level info or higher.
#Don’t log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
#The authpriv file has restricted access.
authpriv.* /var/log/secure
#Log all the mail messages in one place.
mail.* -/var/log/maillog
local4.* /data/fw.log
#Log cron stuff
cron.* /var/log/cron
#Everybody gets emergency messages
*.emerg *
#Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
#Save boot messages also to boot.log
local7.* /var/log/boot.log
####begin forwarding rule ###
#The statement between the begin … end define a SINGLE forwarding
#rule. They belong together, do NOT split them. If you create multiple
#forwarding rules, duplicate the whole block!
#Remote Logging (we use TCP for reliable delivery)
#An on-disk queue is created for this action. If the remote host is
#down, messages are spooled to disk and sent when it is up again.
#KaTeX parse error: Expected 'EOF', got '#' at position 32: …ar/lib/rsyslog #̲ where to place…ActionQueueFileName fwdRule1 # unique name prefix for spool files
#KaTeX parse error: Expected 'EOF', got '#' at position 30: …DiskSpace 1g #̲ 1gb space limi…ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#KaTeX parse error: Expected 'EOF', got '#' at position 30: …e LinkedList #̲ run asynchrono…ActionResumeRetryCount -1 # infinite retries if host is down
#remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#. @@remote-host:514
####end of the forwarding rule ###
[root@zabbix ~]# mkdir /var/log/syslogdevice
[root@zabbix ~]# systemctl restart rsyslog
netstat -tunlp | grep 514
tcp 0 0 0.0.0.0:514 0.0.0.0:* LISTEN 14673/rsyslogd
tcp6 0 0 :::514 ::? LISTEN 14673/rsyslogd
udp 0 0 0.0.0.0:514 0.0.0.0:* 14673/rsyslogd
udp6 0 0 :::514 ::? 14673/rsyslogd
根据ip区别.log日志
$template Remote,"/data/fw_logs/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%DAY%.log"