一:rsyslog简介
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协议进行传输
1.1:rsyslog的简单配置记录
一、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
#$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
$template Remote,"/data/fw_logs/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%$DAY%.log" #定义模板,接受日志文件路径,区分了不同主机的日志
:fromhost-ip, !isequal, "127.0.0.1" ?Remote # 过滤server 本机的日志
# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$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.
#$WorkDirectory /var/lib/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList # run asynchronously
#$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 /data/fw_logs/
[root@zabbix ~]# /etc/init.d/rsyslog restart
[root@k8s-master1 fw_logs]# ls
192.168.3.214 192.168.3.216
[root@k8s-master1 fw_logs]# ll
total 0
drwx------ 2 root root 42 Jul 26 09:37 192.168.3.214
drwx------ 2 root root 42 Jul 26 09:54 192.168.3.216
[root@k8s-master1 fw_logs]#
可以将上面rsyslog服务端的rsyslog.conf里的ip白名单设置为客户机的ip端,比如:
$AllowedSender tcp, 172.18.0.0/16 #表示接收172.18.0.0/16网段的客户机的tcp日志输入,前提是打开tcp的514端口
客户机的配置:
只需要在rsyslog.conf文件里添加下面一行:
*.* @172.18.10.20 #后面的ip是rsyslog服务端的ip地址
启动rsyslog日志即可!
二:收集公司服务器日志
以上配置的是将公司防火墙的日志打到rsyslog里。现在有这么一个需求:
公司IDC的另外两台服务器192.168.3.215和192.168.3.216部署了gitlab、nexus、jenkins、jira和wiki,上面的权限设置的比较杂,很多人都有登录需求。现在需要将登录到这两台服务器上的用户的所有操作过程记录下来,记录达到rsyslog日志里,相当于做用户操作记录的审计工作。
配置如下(结合上面的安装配置)(服务端的ip是192.168.3.214):
1)rsyslog服务端配置 (相比于上面的配置,这里去掉了AllowedSender的来源ip的白名单限制。即允许接收所有机器的日志;上面的防火墙日志还是能继续收集)
[root@zabbix ~]# cat /etc/rsyslog.conf|grep -v "#"|grep -v "^$"
$ModLoad imudp
$UDPServerRun 514
$WorkDirectory /var/lib/rsyslog
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$template Remote,"/data/fw_logs/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%$DAY%.log"
:fromhost-ip, !isequal, "127.0.0.1" ?Remote
$IncludeConfig /etc/rsyslog.d/*.conf
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
*.emerg *
uucp,news.crit /var/log/spooler
local7.* /var/log/boot.log
local5.* /var/log/history.log
[root@zabbix ~]# /etc/init.d/rsyslog restart
2)在192.168.3.215上的配置
[root@gitlab ~]# cat /etc/rsyslog.conf|grep -v "#"|grep -v "^$"
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$IncludeConfig /etc/rsyslog.d/*.conf
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
*.emerg *
uucp,news.crit /var/log/spooler
local7.* /var/log/boot.log
local5.* @192.168.3.214
[root@gitlab ~]# /etc/init.d/rsyslog restart
[root@gitlab ~]# cat /etc/profile #在该文件的底部添加下面内容
.......
export HISTTIMEFORMAT
export PROMPT_COMMAND='{ command=$(history 1 | { read x y; echo $y; }); logger -p local5.notice -t bash -i "user=$USER,ppid=$PPID,from=$SSH_CLIENT,pwd=$PWD,command:$command"; }'
3)在另一台192.168.3.216上做类似配置配置
[root@nexus ~]# cat /etc/rsyslog.conf |grep -v "#"|grep -v "^$"
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$IncludeConfig /etc/rsyslog.d/*.conf
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
*.emerg *
uucp,news.crit /var/log/spooler
local7.* /var/log/boot.log
local5.* @192.168.3.214
[root@nexus ~]# /etc/init.d/rsyslog restart
[root@nexus ~]# cat /etc/profile
.......
export HISTTIMEFORMAT
export PROMPT_COMMAND='{ command=$(history 1 | { read x y; echo $y; }); logger -p local5.notice -t bash -i "user=$USER,ppid=$PPID,from=$SSH_CLIENT,pwd=$PWD,command:$command"; }'
4)过一段时间,发现在rsyslog服务端的日志目录/data/fw_logs下面已经有收集到的日志了
[root@zabbix fw_logs]# pwd
/data/fw_logs
[root@zabbix fw_logs]# cd
[root@zabbix ~]# cd /data/fw_logs/
[root@zabbix fw_logs]# ll
[root@k8s-master1 fw_logs]# ll
total 0
drwx------ 2 root root 42 Jul 26 09:37 192.168.3.214
drwx------ 2 root root 42 Jul 26 09:54 192.168.3.216
[root@k8s-master1 fw_logs]# cd 192.168.3.214/
[root@k8s-master1 192.168.3.214]# ls
192.168.3.214_2024-07-26.log
[root@k8s-master1 192.168.3.214]# cat 192.168.3.214_2024-07-26.log
Jul 26 09:37:10 k8s-node1 bash[115607]: user=root,ppid=115025,from=,pwd=/root,command:source /etc/profile
Jul 26 09:42:01 k8s-node1 bash[115845]: user=root,ppid=115025,from=,pwd=/root,command:cat /etc/profile
Jul 26 09:42:33 k8s-node1 bash[115877]: user=root,ppid=115025,from=,pwd=/root,command:cat /etc/rsyslog.conf |grep -v "#"|grep -v "^$"
Jul 26 09:44:39 k8s-node1 bash[115986]: user=root,ppid=115025,from=,pwd=/root,command:ss -tnl
有上面日志可以看出,在192.168.3.214这台机器上的操作记录都被详细记录下来了。这样,就能清楚地知道登录到这台机器上的用户都做了些什么了.......