我有一个相当普通的、最小的 Debian Jessie 发行版,其中的日志文件/var/log
正在轮换(似乎是任意的)但从未被删除:
$ sudo du -sk /var/log/* | sort -rn
4167364 /var/log/daemon.log.1
1710535 /var/log/daemon.log
596240 /var/log/syslog.1
521091 /var/log/syslog
19905 /var/log/daemon.log.2.gz
15803 /var/log/syslog.4.gz
15187 /var/log/syslog.5.gz
14598 /var/log/syslog.7.gz
14595 /var/log/syslog.6.gz
14545 /var/log/syslog.3.gz
14496 /var/log/syslog.2.gz
1365 /var/log/messages.1
476 /var/log/kern.log.1
403 /var/log/messages
286 /var/log/lastlog
229 /var/log/auth.log.1
128 /var/log/syslog.1.gz
95 /var/log/auth.log
56 /var/log/wtmp
35 /var/log/debug.1
24 /var/log/faillog
19 /var/log/messages.2.gz
11 /var/log/kern.log.2.gz
3 /var/log/fsck
3 /var/log/debug.2.gz
3 /var/log/auth.log.2.gz
3 /var/log/apt
2 /var/log/kern.log
1 /var/log/sysstat
1 /var/log/dpkg.log
1 /var/log/dmesg
1 /var/log/alternatives.log
0 /var/log/debug
0 /var/log/btmp
现在,从我对 journald.conf 文档的阅读来看,默认SystemKeepFree
值为 15%,并且SystemMaxuse
为 10%,所以我不明白为什么它们会增长得这么大。我的根挂载实际上只有 8GB,所以我发现这一点是因为我的 rootfs 使用率实际上达到了 100%:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 7529518 7525422 0 100% /
....
我突然想到,rsyslog 可能是将这些文件写入其中的人,/var/log
并且有库存/etc/ryslog.conf
。但为什么在某些情况下文件似乎正在轮换但从未被删除?
参考/etc/rsyslog.conf:
$ cat /etc/rsyslog.conf
# /etc/rsyslog.conf Configuration file for rsyslog.
#
# For more information see
# /usr/share/doc/rsyslog-doc/html/rsyslog_conf.html
#################
#### MODULES ####
#################
$ModLoad imuxsock # provides support for local system logging
$ModLoad imklog # provides kernel logging support
#$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 traditional timestamp format.
# To enable high precision timestamps, comment out the following line.
#
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
#
# Set the default permissions for all log files.
#
$FileOwner root
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022
#
# Where to place spool and state files
#
$WorkDirectory /var/spool/rsyslog
#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf
###############
#### RULES ####
###############
#
# First some standard log files. Log by facility.
#
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log
#
# Logging for the mail system. Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info -/var/log/mail.info
mail.warn -/var/log/mail.warn
mail.err /var/log/mail.err
#
# Logging for INN news system.
#
news.crit /var/log/news/news.crit
news.err /var/log/news/news.err
news.notice -/var/log/news/news.notice
#
# Some "catch-all" log files.
#
*.=debug;\
auth,authpriv.none;\
news.none;mail.none -/var/log/debug
*.=info;*.=notice;*.=warn;\
auth,authpriv.none;\
cron,daemon.none;\
mail,news.none -/var/log/messages
#
# Emergencies are sent to everybody logged in.
#
*.emerg :omusrmsg:*
#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
# news.=crit;news.=err;news.=notice;\
# *.=debug;*.=info;\
# *.=notice;*.=warn /dev/tty8
# The named pipe /dev/xconsole is for the `xconsole' utility. To use it,
# you must invoke `xconsole' with the `-file' option:
#
# $ xconsole -file /dev/xconsole [...]
#
# NOTE: adjust the list below, or you'll go crazy if you have a reasonably
# busy site..
#
daemon.*;mail.*;\
news.err;\
*.=debug;*.=info;\
*.=notice;*.=warn |/dev/xconsole
(/etc/rsyslog.d/ 下没有任何内容)
存货确认地址/etc/systemd/journald.conf
:
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See journald.conf(5) for details.
[Journal]
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitIntervalSec=30s
#RateLimitBurst=1000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#SystemMaxFiles=100
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#RuntimeMaxFiles=100
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
我实际上在 journald.conf.d 下添加了一个文件:
$ cat /etc/systemd/journald.conf.d/custom.conf
# See: https://www.freedesktop.org/software/systemd/man/journald.conf.html
# vi: ft=dosini
[Journal]
MaxFileSec=0
SystemMaxUse=5M
Compress=yes
# Uncomment this to never write to FS:
#Storage=volatile
因此,确实会发生一些轮换,但并不一致,而且也没有达到 journald 所要求的限制。
注意我正在从 jessie-backports 运行 systemd-230。
问题
- 什么已经旋转文件了?
- 为什么总使用量没有像预期那样受到限制?
- journald 实际上是在写入这些文件还是 rsyslog?(我实际上看不到
/var
journald 存储任何数据(无论是二进制数据还是其他数据)的任何其他位置。) - 我可以在 rsyslog 配置中设置文件大小上限吗?
- 我需要 logrotate 帮我做这个吗?最好是 journald 或 rsyslogd 可以直接处理这个问题。
谢谢!
更新
尽管 journald.conf 文档建议ForwardToSyslog=no
默认情况下,仔细查看我发布的常规 journald.conf,它说编译后的默认值是ForwardToSyslog=yes
。因此看起来 rsyslog 正在执行它的工作/var/log
答案1
logrotated
确实管理这些日志(用于 rsyslog 和其他日志)。但是……您不断看到相同的文件名,所以这可能是您所想的。默认配置是保留 4 或 7 个旧日志和当前日志。这是默认配置 - 您可以看到 /var/log/syslog 将保留 7 个加上当前日志,其他将保留 4 个。
debian@templatevm:~$ cat /etc/logrotate.d/rsyslog
/var/log/syslog
{
rotate 7
daily
missingok
notifempty
delaycompress
compress
postrotate
invoke-rc.d rsyslog rotate > /dev/null
endscript
}
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
rotate 4
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
invoke-rc.d rsyslog rotate > /dev/null
endscript
}
如果您只想根据大小进行旋转,则可以将daily
参数更改为size NNNN
N 为 100M 之类的大小 - 因此size 100M
限制为 100mb
然后,如果你的日志文件增长很快,你可以改变 logrotate 检查的频率,方法是将符号链接/etc/cron.hourly
放在/etc/cron.daily
您的新/etc/logrotate.d/rsyslog
文件可能如下所示
/var/log/syslog
{
rotate 7
daily
missingok
notifempty
delaycompress
compress
postrotate
invoke-rc.d rsyslog rotate > /dev/null
endscript
}
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
rotate 4
size 25M
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
invoke-rc.d rsyslog rotate > /dev/null
endscript
}