Syslog 简介

介绍

在Unix类操作系统上,syslog广泛应用于系统日志。syslog日志消息既可以记录在本地文件中,也可以通过网络发送到接收syslog的服务器。接收syslog的服务器可以对多个设备的syslog消息进行统一的存储,或者解析其中的内容做相应的处理。常见的应用场景是网络管理工具、安全管理系统、日志审计系统。

完整的syslog日志中包含产生日志的程序模块(Facility)、严重性(Severity或 Level)、时间、主机名或IP、进程名、进程ID和正文。在Unix类操作系统上,能够按Facility和Severity的组合来决定什么样的日志消息是否需要记录,记录到什么地方,是否需要发送到一个接收syslog的服务器等。由于syslog简单而灵活的特性,syslog不再仅限于 Unix类主机的日志记录,任何需要记录和发送日志的场景,都可能会使用syslog。

长期以来,没有一个标准来规范syslog的格式,导致syslog的格式是非常随意的。最坏的情况下,根本就没有任何格式,导致程序不能对syslog 消息进行解析,只能将它看作是一个字符串。

在2001年定义的 RFC3164 中,描述了BSD syslog。后来2009年又采用了新的规定 RFC5424。不过这个规范的很多内容都不是强制性的,常常是“建议”或者“约定”,也由于这个规范出的比较晚,很多设备并不遵守或不完全遵守这个规范。接下来就介绍一下这个规范。

约定发送syslog的设备为Device,转发syslog的设备为Relay,接收syslog的设备为Collector。Relay本身也可以发送自身的syslog给Collector,这个时候它表现为一个Device。Relay也可以只转发部分接收到的syslog消息,这个时候它同时表现为Relay和Collector。

syslog消息发送到Collector的UDP 514端口,不需要接收方应答,RFC3164建议 Device 也使用514作为源端口。规定syslog消息的UDP报文不能超过1024字节,并且全部由可打印的字符组成。完整的syslog消息由3部分组成,分别是PRIHEADERMSG。大部分syslog都包含PRI和MSG部分,而HEADER可能没有。


格式

下面是一个syslog消息:

<30>Oct 9 22:33:20 hlfedora auditd[1787]: The audit daemon is exiting.

syslog格式:

<PRI>HEADER MESSAGE

syslog的消息长度:不超过1024。

PRI: 		    <30>
HEADER: 		Oct 9 22:33:20 hlfedora
MESSAGE:		auditd[1787]: The audit daemon is exiting.
PRI

即Priority(优先级),有效值范围为0 - 191。不能有空格、数字前也不能补0。合法的形式如:<15>。PRI值包含两部分信息:Facility和Level,Facility值用于判断哪个设备产生了日志信息。,Level值用于判断严重等级。计算方法:

PRI = Facility * 8 + Level
Facility = PRI / 8
Level = PRI % 8

Facility可选值为:

0      kernel messages
1      user-level messages
2      mail system
3      system daemons
4      security/authorization messages
5      messages generated internally by syslogd
6      line printer subsystem
7      network news subsystem
8      UUCP subsystem
9      clock daemon
10     security/authorization messages
11     FTP daemon
12     NTP subsystem
13     log audit
14     log alert
15     clock daemon
16     local use 0 (local0)
17     local use 1 (local1)
18     local use 2 (local2)
19     local use 3 (local3)
20     local use 4 (local4)
21     local use 5 (local5)
22     local use 6 (local6)
23     local use 7 (local7)

Level可选值为:

0      Emergency      system is unusable
1      Alert          action must be taken immediately
2      Critical       critical conditions
3      Error          error conditions
4      Warning        warning conditions
5      Notice         normal but significant condition
6      Informational  informational messages
7      Debug          debug-level messages
HEADER

HEAD包含两部分信息:TIMESTAMPHOSTNAME
TIMESTAMP为时间值,格式为:Mmm dd hh:mm:ss。表示月日时分秒。
HOSTNAME为主机IP地址或主机名。
注意:TIMESTAMP和HOSTNAME后都必须跟一个空格。

MESSAGE

MESSAGE包含两部分信息:TAGCONTENT
TAG为产生消息的程序或进程名称。为长度不超过32的字母数字字符串。
CONTENT为信息的详细内容。


配置文件

/etc/rsyslog.conf
# rsyslog v3 config file
# if you experience problems, check
# http://www.rsyslog.com/troubleshoot for assistance

#### MODULES #### 加载 模块
$ModLoad imuxsock.so  -> 模块名
# provides support for local system logging (e.g. via logger command) 本地系统日志
$ModLoad imklog.so                 # provides kernel logging support (previously done by rklogd)
# $ModLoad immark.so               # provides –MARK– message capability

# Provides UDP syslog reception
# 允许514端口接收使用UDP协议转发过来的日志
# $ModLoad imudp.so
# $UDPServerRun 514

# Provides TCP syslog reception
# 允许514端口接收使用TCP协议转发过来的日志
# $ModLoad imtcp.so
# $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

#### RULES ####
# Log all kernel messages to the console.
# Logging much else clutters up the screen.

# kern.*                                        /dev/console
# 关于内核的所有日志都放到/dev/console(控制台)

# Log anything (except mail) of level info or higher.
# Don’t log private authentication messages!

# 记录所有日志类型的info级别以及大于info级别的信息到/var/log/messages,
# 但是mail邮件信息,authpriv验证方面的信息和cron时间任务相关的信息除外
*.info;mail.none;authpriv.none;cron.none        /var/log/messages

# The authpriv file has restricted access.
# authpriv验证相关的所有信息存放在/var/log/secure
authpriv.*                                      /var/log/secure

# Log all the mail messages in one place.
# 邮件的所有信息存放在/var/log/maillog
# 这里有一个-符号, 表示是使用异步的方式记录, 因为日志一般会比较大
mail.*                                          -/var/log/maillog

# Log cron stuff
# 计划任务有关的信息存放在/var/log/cron
cron.*                                          /var/log/cron

# Everybody gets emergency messages
# 记录所有的大于等于emerg级别信息, 以wall方式发送给每个登录到系统的人
*.emerg                                         *
# *代表所有在线用户

# Save news errors of level crit and higher in a special file.
# 记录uucp,news.crit等存放在/var/log/spooler
uucp,news.crit                                  /var/log/spooler

# Save boot messages also to boot.log       启动的相关信息
local7.*                                        /var/log/boot.log

# :rawmsg, contains, “sdns_log” @@192.168.56.7:10514
# :rawmsg, contains, “sdns_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/spppl/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
# @@表示通过tcp协议发送    @表示通过udp进行转发
# local3.info  @@localhost :514
# local7.*                                    # @@192.168.56.7:514
# ### end of the forwarding rule ###

其他

发送限制

rsyslog 5.8 版本之后默认有发送速度限制,在配置文件将其关闭即可

$SystemLogRateLimitInterval 0
RELP

rsyslog提供三个远程日志传输方式:

  • UDP: 数据包传输可信度不高
  • TCP: 数据包传输可信度比较高
  • RELP: 数据包传输可信度最高,避免数据丢失,比较新的协议,目前应用较少
  • 以下为man手册对RELP协议的一个介绍:
RELP can be used instead of UDP or plain TCP syslog to provide reliable
delivery of syslog messages. Please note that plain TCP syslog does NOT
provide truly reliable delivery, with it messages may be lost when there
is a connection problem or the server shuts down. RELP prevents message
loss in hose cases.

关于RELP的更进一步了解可以参考 Using TLS with RELP RELP Input Module RELP Output Module

参考资料