使用logrotate管理系统日志
2010-12-24 16:26:42 阿炯

系统管理员可以使用logrotate 程序用来管理系统中的新近发生的事件,logrotate 还可以用来备份日志文件,logrotate是对日志文件做轮换。就是把现在的log命名为log.1,然后继续写log。如果存在log.1就命名log.1为log.2然后命名log为log.1,依此类推,但并非没有尽头。这个尽头就是您在logrotate的配置文件中的定义,若系统默认的是到5。那么log.5将会被删除,logrotate非守护进程而是按计划运行的。

logrotate配置文件主要在两个地方:/etc/logrotate.conf以及/etc/logrotate.d/下的明细配置文件。其中/etc/logrotate.conf文件是主配置文件,/etc/logrotate.d/下的明细配置文件都会被读入/etc/logrotate.conf进行执行,所以请注意这里还涉及到了公有变量和私有变量。logrotate的执行是由crond服务来调用的,其脚本是/etc/cron.daily/logrotate,每天自动执行。/usr/sbin/logrotate工具调用了/var/lib/logrotate/logrotate.status和/etc/logrotate.conf两个文件。然后将执行的结果(就是那个$?,成功为0,不成功为非0)进行判断,非0时执行/usr/bin/logger命令,最后退出。看完定时计划任务,再来看主配置文件/etc/logrotate.conf(也就是公有变量)。

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

Normally, logrotate is run as a daily cron job. It will not modify a log multiple times in one day unless the criterium for that log is based on the log's size and logrotate is being run multiple times each day, or unless the -f or -force option is used.

Any number of config files may be given on the command line. Later config files may override the options given in earlier files, so the order in which the logrotate config files are listed in is important. Normally, a single config file which includes any other config files which are needed should be used. See below for more information on how to use the include directive to accomplish this. If a directory is given on the command line, every file in that directory is used as a config file.

If no command line arguments are given, logrotate will print version and copyright information, along with a short usage summary. If any errors occur while rotating logs, logrotate will exit with non-zero status.

语  法:logrotate [-?dfv][-s ][--usage][配置文件]

说明:使用logrotate指令,可让你轻松管理系统所产生的记录文件。它提供自动替换,压缩,删除和邮寄记录文件,每个记录文件都可被设置成每日,每周或每月处理,也能在文件太大时立即处理。您必须自行编辑,指定配置文件,预设的配置文件存放在/etc目录下,文件名称为 logrotate.conf。

参  数:
-?或--help  在线帮助。
-d或--debug  详细显示指令执行过程,便于排错或了解程序执行的情况。
-f或--force  强行启动记录文件维护操作,纵使logrotate指令认为没有需要亦然。
-s或--state=  使用指定的状态文件。
-v或--version  显示指令执行过程。
--usage  显示指令基本用法。

日志文件的管理:
一、logrotate 配置
二、缺省配置 logrotate
三、使用include 选项读取其他配置文件
四、使用include 选项覆盖缺省配置
五、为指定的文件配置转储参数

一、logrotate 配置
logrotate 程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行。它还可以用于压缩日志文件,以及发送日志到指定的E-mail ,配置文件是 /etc/logrotate.conf。主要参数如下表:

参数 功能
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).


更详细一点的配置参数说明:
missingok  日志切割期间产生错误将被忽略(如果日志丢失,不报错继续切割)
daily、weekly、monthly、yearly  每天、每周、每月、每年执行
create MODE OWNER GROUP  切割后指定创建新的空白文件的属性
nocreate  不建立新的日志文件
rotate N  保留N份,多余删除,不计算新建日志文件
dateext  用当前日期作为后缀命名格式(默认年月日)
dateformat .%s  配合dateext使用,紧跟在下一行出现,定义切割后的文件名,只支持%Y,%m,%d,%s
size/minsize  达到指定大小才会切割,默认单位bytes,还可以是KB和MB
compress  切割结束后,归档并使用gzip格式压缩
nocompress  解除compress参数
delaycompress  总是与compress参数一起使用,指示logrotate不要将最近的归档压缩,压缩将在下一次切割进行。
nodelaycompress  解除delaycompress参数
ifempty  即使日志为空,也执行切割
notifempty  如果日志为空,切割不执行
prerotate/endscript  在所有其他指令之前执行prerotate和endscript之间的命令。
postrotate/endscript  在所有其他指令完成后,postrotate和endscript之间的命令将被执行。
sharescripts  所有日志切割后统一执行一次脚本,如果没有配置该参数,则每个日志切割后都会执行一次脚本。
errors ADDRESS  切割时的错误信息发送到指定的Emial地址
mail ADDRESS  切割日志发送到指定的Email地址
nomail  切割日志不发送邮件
olddir DIRECTORY  切割后的日志文件放入指定目录,必须与当前日志处在同一文件系统
noolddir  切割后的日志文件与当前文件放在同一目录下
copytruncate  用于还在打开中的日志文件,把当前日志备份并截断;先拷贝后清空,可能丢失部分日志
nocopytruncate  备份日志文件,但不截断。


二、缺省配置 logrotate
logrotate 缺省的配置/etc/logrotate.conf。

Red Hat Linux 缺省安装的文件内容是:
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# send errors to root
errors root
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
1
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own lastlog or wtmp --we'll rotate them here

/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}

/var/log/lastlog {
monthly
rotate 1
}

# system-specific logs may be configured here

缺省的配置一般放在logrotate.conf 文件的最开始处,影响整个系统。

weekly 指定所有的日志文件每周转储一次。
rotate 4 指定转储文件的保留 4份。
errors root 指定错误信息发送给root。
create 指定 logrotate 自动建立新的日志文件,新的日志文件具有和原来的文件一样的权限。
compress 指定不压缩转储文件,如果需要压缩,去掉注释就可以了。

三、使用include 选项读取其他配置文件
include 选项允许系统管理员把分散到几个文件的转储信息,集中到一个主要的配置文件。当 logrotate 从logrotate.conf 读到include 选项时,会从指定文件读入配置信息,就好像他们已经在/etc/logrotate.conf 中一样。

include /etc/logrotate.d 告诉 logrotate 读入存放在/etc/logrotate.d 目录中的日志转储参数,当系统中安装了RPM 软件包时,使用include 选项十分有用。RPM 软件包的日志转储参数一般存放在/etc/logrotate.d 目录。

include 选项十分重要,一些应用把日志转储参数存放在 /etc/logrotate.d 。

典型的应用有:apache, linuxconf, samba, cron 以及syslog。这样,系统管理员只要管理一个 /etc/logrotate.conf 文件就可以了。

四、使用include 选项覆盖缺省配置
当 /etc/logrotate.conf 读入文件时,include 指定的文件中的转储参数将覆盖缺省的参数,如下例:
# linuxconf 的参数
/var/log/htmlaccess.log
{ errors jim
notifempty
nocompress
weekly
prerotate
/usr/bin/chattr -a /var/log/htmlaccess.log
endscript

postrotate
/usr/bin/chattr +a /var/log/htmlaccess.log
endscript
}

/var/log/netconf.log
{ nocompress
monthly
}

nginx日志按天统计示例:
/var/log/nginx/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 640 www-data adm
    sharedscripts
    prerotate
    sleep 59
    endscript
    postrotate
        if [ -f /var/run/nginx.pid ]; then
          kill -USR1 `cat /var/run/nginx.pid`
        fi
    endscript
}

调试:
logrotate -d /etc/logrotate.d/nginx

执行:
logrotate -f /etc/logrotate.d/nginx

定期执行:
vi /etc/crontab
59 23 * * * root logrotate -f /etc/logrotate.d/nginx

在这个例子中,当 /etc/logrotate.d/linuxconf 文件被读入时,下面的参数将覆盖/etc/logrotate.conf中缺省的参数。
Notifempty
errors hto

五、为指定的文件配置转储参数
经常需要为指定文件配置参数,一个常见的例子就是每月转储/var/log/wtmp。为特定文件而使用的参数格式是:
# 注释
/full/path/to/file
{
option(s)
}

下面的例子就是每月转储 /var/log/wtmp 一次:
#Use logrotate to rotate wtmp
/var/log/wtmp
{
monthly
rotate 1
}

六、其他需要注意的问题
1、尽管花括号的开头可以和其他文本放在同一行上,但是结尾的花括号必须单独成行。
2、使用 prerotate 和 postrotate 选项
下面的例子是典型的脚本 /etc/logrotate.d/syslog,这个脚本只是对/var/log/messages 有效。

/var/log/messages
{
prerotate
/usr/bin/chattr -a /var/log/messages
endscript

postrotate
/usr/bin/kill -HUP syslogd
/usr/bin/chattr +a /var/log/messages
endscript
}

第一行指定脚本对 /var/log messages 有效
prerotate 命令指定转储以前的动作/usr/bin/chattr -a 去掉/var/log/messages文件的“只追加”属性 endscript 结束 prerotate 部分的脚本postrotate 指定转储后的动作。

/usr/bin/killall -HUP syslogd
用来重新初始化系统日志守护程序 syslogd

/usr/bin/chattr +a /var/log/messages
重新为 /var/log/messages 文件指定“只追加”属性,这样防治程序员或用户覆盖此文件。

最后的 endscript 用于结束 postrotate 部分的脚本

3、logrotate 的运行分为三步:
判断系统的日志文件,建立转储计划以及参数,通过cron daemon 运行下面的代码是 Red Hat Linux 缺省的crontab 来每天运行logrotate。

#/etc/cron.daily/logrotate
#! /bin/sh
/usr/sbin/logrotate /etc/logrotate.conf

4、/var/log/messages 不能产生的原因:
这种情况很少见,但是如果你把/etc/services 中的 514/UDP 端口关掉的话,这个文件就不能产生了。

小结:本文通过对Linux 系统上典型的logrotate 配置例子的介绍,详细说明了logrotate 程序的应用方法。希望对系统管理员有所帮助。