用rsyslog与logrotate管理日志

文章目录

如标题所示,这篇要谈的是两个关于日志的工具。

最近自己的记录逐渐趋向于最佳实践,只能适配特定的情况,例如这次就是给几台网关做配置。网关使用了树莓派,系统为debian 8.0,存储空间4GB,可用空间约2.5GB,内存1GB,资源有限。在没有配置好日志收集和切割的情况下,占满所有存储空间只是时间的问题。而为了提升设备寿命,厂家默认将日志路径 /var/log 挂载到了内存中,限制大小为100MB,在信号繁忙的情况下,大概只要两天时间就可以撑爆 /var/log 目录,至于这种情况下,网关是会重启还是崩溃,都不得而知,如果是重启,保存在内存中的日志会被清空,启动后继续工作,而如果是崩溃死机,那问题就更严重了。

1.原始的配置文件内容。

rsyslog的配置**/etc/rsyslog.conf**(这里将所有内容合并到了该文件中,其他的配置文件存放在 /etc/rsyslog.d 目录下)

  1/etc/rsyslog.conf	Configuration file for rsyslog.
  2#
  3#			For more information see
  4#			/usr/share/doc/rsyslog-doc/html/rsyslog_conf.html
  5
  6
  7#################
  8#### MODULES ####
  9#################
 10
 11$ModLoad imuxsock # provides support for local system logging
 12$ModLoad imklog   # provides kernel logging support
 13#$ModLoad immark  # provides --MARK-- message capability
 14
 15# provides UDP syslog reception
 16#$ModLoad imudp
 17#$UDPServerRun 514
 18
 19# provides TCP syslog reception
 20#$ModLoad imtcp
 21#$InputTCPServerRun 514
 22
 23
 24###########################
 25#### GLOBAL DIRECTIVES ####
 26###########################
 27
 28#
 29# Use traditional timestamp format.
 30# To enable high precision timestamps, comment out the following line.
 31#
 32$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
 33
 34#
 35# Set the default permissions for all log files.
 36#
 37$FileOwner root
 38$FileGroup adm
 39$FileCreateMode 0640
 40$DirCreateMode 0755
 41$Umask 0022
 42
 43#
 44# Where to place spool and state files
 45#
 46$WorkDirectory /var/spool/rsyslog
 47
 48#
 49# Include all config files in /etc/rsyslog.d/
 50#
 51$IncludeConfig /etc/rsyslog.d/*.conf
 52
 53
 54###############
 55#### RULES ####
 56###############
 57
 58#
 59# First some standard log files.  Log by facility.
 60#
 61auth,authpriv.*			/var/log/auth.log
 62*.*;auth,authpriv.none		-/var/log/syslog
 63#cron.*				/var/log/cron.log
 64daemon.*			-/var/log/daemon.log
 65kern.*				-/var/log/kern.log
 66lpr.*				-/var/log/lpr.log
 67mail.*				-/var/log/mail.log
 68user.*				-/var/log/user.log
 69
 70#
 71# Logging for the mail system.  Split it up so that
 72# it is easy to write scripts to parse these files.
 73#
 74mail.info			-/var/log/mail.info
 75mail.warn			-/var/log/mail.warn
 76mail.err			/var/log/mail.err
 77
 78#
 79# Logging for INN news system.
 80#
 81news.crit			/var/log/news/news.crit
 82news.err			/var/log/news/news.err
 83news.notice			-/var/log/news/news.notice
 84
 85#
 86# Some "catch-all" log files.
 87#
 88*.=debug;\
 89	auth,authpriv.none;\
 90	news.none;mail.none	-/var/log/debug
 91*.=info;*.=notice;*.=warn;\
 92	auth,authpriv.none;\
 93	cron,daemon.none;\
 94	mail,news.none		-/var/log/messages
 95
 96#
 97# Emergencies are sent to everybody logged in.
 98#
 99*.emerg				:omusrmsg:*
100
101#
102# I like to have messages displayed on the console, but only on a virtual
103# console I usually leave idle.
104#
105#daemon,mail.*;\
106#	news.=crit;news.=err;news.=notice;\
107#	*.=debug;*.=info;\
108#	*.=notice;*.=warn	/dev/tty8
109
110# The named pipe /dev/xconsole is for the `xconsole' utility.  To use it,
111# you must invoke `xconsole' with the `-file' option:
112# 
113#    $ xconsole -file /dev/xconsole [...]1.原始的配置文件内容。
114#
115# NOTE: adjust the list below, or you'll go crazy if you have a reasonably
116#      busy site..
117#
118daemon.*;mail.*;\
119	news.err;\
120	*.=debug;*.=info;\
121	*.=notice;*.=warn	|/dev/xconsole

按照该配置(RULE部分中的内容),守护进程产生的日志会同时输出到 /var/log/daemon.log 以及 /var/log/syslog.log 文件中,导致日志体积翻倍,因为rsyslog只排除了auth,authpriv.none两种类型的日志,而网关上的程序以守护进程运行,会产生大量日志。

logrotate中默认的rsyslog日志配置路径 /etc/logrotate.d/rsyslog

 1/var/log/syslog
 2{
 3    rotate 7
 4    daily
 5    missingok
 6    notifempty
 7    delaycompress
 8    compress
 9    postrotate
10        invoke-rc.d rsyslog rotate > /dev/null
11    endscript
12}
13
14/var/log/mail.info
15/var/log/mail.warn
16/var/log/mail.err
17/var/log/mail.log
18/var/log/daemon.log
19/var/log/kern.log
20/var/log/auth.log
21/var/log/user.log
22/var/log/lpr.log
23/var/log/cron.log
24/var/log/debug
25/var/log/messages
26{
27    rotate 4
28    weekly
29    missingok
30    notifempty
31    compress
32    delaycompress
33    sharedscripts
34    postrotate
35        invoke-rc.d rsyslog rotate > /dev/null
36    endscript
37}

按照该配置,logrotate会对 /var/log/syslog.log 文件,每日执行一次切割,并延迟压缩,最多保存7个副本,也就是说会同时存在syslog.log以及syslog.log.1两个未压缩文件,而对于其他由rsyslog产生的日志每周执行一次切割,延迟压缩,最多保存4个副本。实际测试,日志压缩前后的体积相差接近10倍。

2.修改配置

按照默认配置,100MB的存储空间远远不够,需要做一些小修改。

修改 /etc/rsyslog.conf 中的RULES部分,也就是一些标准日志输出的位置,可以注释掉daemon或者在输出到syslog的日志中排除掉daemon日志。

1// syslog中排除daemon日志,确保不要注释掉daemon日志输出的配置,注意,这种情况下使用journalctl命令无法查看守护进程产生的日志
2*.*;daemon,auth,authpriv.none		-/var/log/syslog
3daemon.*			-/var/log/daemon.log
4// 注释掉daemon日志输出,将daemon日志一起收集到syslog,这种情况下可以使用journalctl命令查看守护进程产生的日志
5*.*;auth,authpriv.none		-/var/log/syslog
6#daemon.*			-/var/log/daemon.log

同时可以注释掉文件末尾4行的日志输出,网关设备繁忙的状态下会报 /dev/xconsole 相关的错误,产生一些无用的日志。

修改 /etc/logrotate.d/rsyslog,在切割日志后立刻压缩,并对daemon.log与syslog.log执行相同的切割动作,注意,对于已压缩的日志,使用journalctl命令是无法获取守护进程在当时产生的日志。

 1/var/log/syslog
 2/var/log/daemon.log
 3{
 4    rotate 7
 5    daily
 6    missingok
 7    notifempty
 8    compress
 9    nodelaycompress
10    sharedscripts
11    postrotate
12        invoke-rc.d rsyslog rotate > /dev/null
13    endscript
14}
15
16/var/log/mail.info
17/var/log/mail.warn
18/var/log/mail.err
19/var/log/mail.log
20/var/log/kern.log
21/var/log/auth.log
22/var/log/user.log
23/var/log/lpr.log
24/var/log/cron.log
25/var/log/debug
26/var/log/messages
27{
28    rotate 4
29    weekly
30    missingok
31    notifempty
32    compress
33    nodelaycompress
34    sharedscripts
35    postrotate
36        invoke-rc.d rsyslog rotate > /dev/null
37    endscript
38}

3.拓展阅读

原来的计划是更详细写下日志是如何输出,如何指定文件,以及各种相关内容,最后觉得超纲了,这只是一篇博客而不是百科全书,这里放下相关内容。

  1. rsyslog配置简介
  2. Linux日志文件总管——logrotate
  3. stdin、stdout、stderr 标准流本质上到底是什么?
  4. How To View and Configure Linux Logs on Ubuntu and Centos
  5. LinuxLogFiles
  6. Understand logging in Linux