选择器
要匹配的消息由匹配设施和优先级的选择器指定,而应用于匹配消息的操作由操作字段指定。
例如,以下配置行告诉 Rsyslog 将操作 /var/log/kernlog 应用于具有 kern 工具和调试级别的所有消息:
# cat /etc/rsyslog.conf kern.debug /var/log/kernlog
选择器中的优先级声明是分层的。
Rsyslog 将匹配指定优先级和更高优先级的所有消息。
选择器 kern.debug 匹配内核产生的所有具有 debug 或者更高优先级的消息;因为 debug 是可能的最低优先级,所以选择器 kern.debug 匹配所有具有 kern 工具的消息。
此外,星号可以用作通配符来表示所有优先级,因此 kern.* 也将匹配内核产生的所有消息。
与优先级字段不同,设施字段不是分层的。
但是,仍然可以匹配来自不同设施的多个消息。
可以在一行中列出多个选择器,用分号分隔。
当需要对多条消息应用相同的操作时,这会很有用。
类似地,星号通配符可用于指定所有设施,提供另一种将操作应用于各种消息的方法。
系统日志设施和优先级
该工具用于指定生成消息的程序类型。
然后可以将 Syslog 守护程序配置为以不同方式处理来自不同来源的消息。
下表列出了标准定义的设施,并简要说明了它们的用途:
Facility | 描述 |
---|---|
auth/authpriv | 安全/授权消息 |
cron | crond 和 atd 守护进程消息 |
daemon | 其他系统守护进程 |
kern | 内核消息 |
local0 - local7 | 保留供本地使用 |
lpr | 行式打印机子系统 |
邮件子系统 | |
news | USENET新闻子系统 |
syslog | 系统日志守护进程内部生成的消息 |
user | 通用用户级消息 |
uucp | UUCP子系统 |
消息的优先级或者级别旨在确定消息的重要性。
下表列出了标准优先级及其含义的简要说明:
优先级 | 描述 |
---|---|
emerg | 系统无法使用 |
alert | 必须立即采取行动 |
crit | 临界条件 |
err | 错误条件 |
warning | 警告条件 |
notice | 正常但重要的情况 |
info | 信息性消息 |
debug | 调试信息 |
操作(action)
有许多操作(action),但规则中只能包含一个操作:
- 可以在操作字段中列出文件名,指定应将所选消息写入的文件位置。通常情况下,这些文件可以是文本文件,但也可以是终端或者打印机等设备文件。
- 也可以指定用户名。如果指定用户在 Rsyslog 处理消息时登录到系统,则消息将打印到该用户的所有终端。
- 操作的星号告诉 Rsyslog 将消息写入所有登录用户(它会发送到所有活动终端)。
- 消息可以发送到远程主机。操作@host 告诉Rsyslog 将消息转发到机器主机,该主机的Syslog 守护进程将再次处理该消息。
默认 /etc/rsyslog.conf 文件
下面是 CentOS 6 中默认的 /etc/rsyslog.conf 配置文件。
# 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 #$UDPServerRun 514 # Provides TCP syslog reception #$ModLoad imtcp #$InputTCPServerRun 514 #### GLOBAL DIRECTIVES #### # Use default timestamp format $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat # 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 # 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 ###
所有系统和内核消息都会传递到 rsyslogd。
对于收到的每条日志消息,Rsyslog 查看其配置文件 /etc/rsyslog.conf 以确定如何处理该消息。
Rsyslog 在配置文件中查找与该消息匹配的所有规则语句,并按照每个规则语句的指示处理该消息。
如果没有规则语句与消息匹配,Rsyslog 将丢弃它。
规则语句指定了两件事:
- 匹配什么消息(选择器),以及
- 如何处理匹配的消息(动作)。