rsyslog如何过滤日志消息

rsyslog是一个高性能和灵活的日志系统,可以用于收集、处理和分发系统内产生的各种日志。rsyslog包含了强大的过滤机制,使得用户可以对日志进行更加精细的掌控和管理。

常见问题

在使用rsyslog软件的过程中我们常见的问题如下: 如何使用过滤rsyslog日志? 如何从中删除特定日志/var/log/secure? 如何过滤一些日志消息,使他们不登录日志文件? 如何禁用syslog重新启动消息时syslog服务重启了吗?

解决方案

1)在/etc/rsyslog.d/目录中创建自定义配置文件,配置文件名必须以“.conf”结尾。文件内容格式如下所示:

if $syslogtag contains '(procces you want to filter)' and $msg contains '(message you want to filter)' then stop

2)重新启动rsyslog服务。 对于RHEL5和RHEL6:

# service rsyslog restart

对于 RHEL7 或更高版本:

# systemctl restart rsyslog.service

实战案例

停止记录ssh连接日志信息

Nov  9 10:50:35 dhcp210-244 sshd[21850]: Accepted password for root from 10.65.223.218 port 50227 ssh2
Nov  9 10:50:35 dhcp210-244 sshd[21850]: pam_unix(sshd:session): session opened for user root by (uid=0)
Nov  9 10:55:45 dhcp210-244 sshd[21850]: pam_unix(sshd:session): session closed for user root by (uid=0)

在/etc/rsyslog.d/下面创建配置文件,如下所示:

# cat  /etc/rsyslog.d/35-pam_unix.conf
if $syslogtag contains 'sshd' and $msg contains 'Accepted password for root from' then stop   
if $syslogtag contains 'sshd' and $msg contains 'session opened for user' then stop           
if $syslogtag contains 'sshd' and $msg contains 'session closed for' then stop                

停止记录vsftpd日志信息

Dec 30 13:01:19 rhel6test init: vsftpd main process (3128) terminated with status 1
Dec 30 13:01:19 rhel6test init: vsftpd main process ended, respawning

在/etc/rsyslog.d/下面创建配置文件,如下所示。

# cat /etc/rsyslog.d/vsftpd-norespawn.conf 

if $syslogtag contains 'init' and $msg contains 'vsftpd main process ended, respawning' then stop
if $syslogtag contains 'init' and $msg contains 'terminated with status 1' then stop

原创文章,作者:运维侠,如若转载,请注明出处:https://www.yunweixia.com/solutions/rsyslog-how-to-filter-log-messages.html

(0)
运维侠的头像运维侠共建用户
上一篇 2025年7月13日 18:11
下一篇 2025年7月15日 18:11

相关推荐

发表回复

登录后才能评论