当前位置: 首页 > 工具软件 > Rsyslog > 使用案例 >

Linux日志系统-01:什么是rsyslog?

黄查猛
2023-12-01

目录:

一、rsyslog是什么?

二、rsyslog的配置文件/etc/rsyslog.conf

三、详解rsyslog的配置文件/etc/rsyslog.conf

------------------------------------------------------------------------------------------------------

一、rsyslog是什么?

在linux系统中日志可以分为:
(1)klogd:kernel,记录内核相关的日志
(2)syslogd:service,记录应用程序的日志
(3)rsyslog:是CentOS 6以后的系统使用的日志系统,rsyslog是用来管理、记录日志的程序。rsyslog是一个C/S架构的服务,可监听于某套接字,帮其它主机记录日志信息。

二、rsyslog的配置文件/etc/rsyslog.conf

# rsyslog 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 ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# 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

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state


#### 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                                                 :omusrmsg:*

# 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.
#$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 ###

三、详解rsyslog的配置文件/etc/rsyslog.conf

(1)日志级别

1)debug:调试级别(程序调试信息)

2)info:通知消息(程序的正常输出)

3)notice:注意级别(程序可能有错误)

4)warning:警告级别(警告错误)

5)error:错误级别

6)crit:严重错误级别

7)alter:发出严重报警级别

8)emerg:系统级别故障

PS:记录日志的时候会把本级别以及高于本级别的日志信息都会记录下来。

例子1:

mail.info              info和info以上级别

mail.!info             除了info以外的全部级别

mail.=info             仅仅记录info级别的日志

mail.*                 记录mail设施全部级别的日志

*.info                 记录所以设施的info及其以上级别的日志

mail,auth.info         记录mail和auth两个设施的info及其以上级别的日志

mail.none              不记录任何级别的日志

例子2:

将mail的全部日志记录到/var/log/mail.log

mail.*     /var/log/mail.log

(2)日志设施

日志设施(facility)就是对日志信息分类,将不同类别的日志记录到不同的位置,比如系统安全级别的日志记录和用户认证的日志在/var/log/secure,常见日志类别:
 

1)auth:用户认证相关的日志

2)authpriv:记录授权相关的日志

3)mail:邮件相关的日志

4)cron:计划任务的日志

5)daemon:守护进程相关的日志

6)user:系统系统相关的日志

7)kernel:内核相关的日志

8)rsyslog:syslog自己的日志

9)local0~local7:8个用户自定义设施

PS:定义日志级别的时候可以使用通配符,比如,*表示全部级别;,表示多个级别(debug,info,notice);!表示某个级别除外;=表示仅仅某个级别的日志

(3)日志的保存位置

1)保存到本地

情况1:如果在messages文件前加一个”-“表示异步写入。异步写入就是把日志先写到内存中,如果内存满了再将内存的数据写入磁盘。例如:

# Log all the mail messages in one place.

mail.*                             -/var/log/maillog

情况2:none的使用

下面rsyslog的配置文件/etc/rsyslog.conf中使用了none,并用分号隔开,下面规则含义是把所有info级别的日志写入 /var/log/messages,但是不写入mail、authpriv、cron的日志。

# 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

2)保存到mysql数据库其格式为:

facility.priority :ommysql:DBHOST,DB,DBUSER,DBUSERPASS

3)发送给指定的用户:如果将日志发送给所有用户用*表示,例如:

rybody gets emergency messages

*.emerg                         :omusrmsg:*

4)保存到日志服务器

#### RULES ####

*.*     @日志服务器地址

PS:日志配置格式--》设施.级别     日志位置

(4)日志默认保存格式

日志默认保存格式可参考系统日志信息/var/log/messages

时间            pid    用户        事件

May  9 10:21:14 192  journal: horizontal-workspaces@gnome-shell-extensions.gcampax.github.com unknown type 3

May  9 10:21:14 192  journal: alternate-tab@gnome-shell-extensions.gcampax.github.com unknown type 3

May  9 10:21:15 192  gnome-shell: GNOME Shell started at Sun May 09 2021 10:20:11 GMT+0800 (CST)

May  9 10:21:18 192  journal: Only 5 apps for popular list, hiding

 

 类似资料: