美文网首页
CentOS 7下使用Logrotate管理日志

CentOS 7下使用Logrotate管理日志

作者: 53921f46e0b9 | 来源:发表于2016-04-02 14:37 被阅读5793次

Logrotate是一个日志文件管理工具。用来把旧文件轮转、压缩、删除,并且创建新的日志文件。我们可以根据日志文件的大小、天数等来转储,便于对日志文件管理,一般都是通过cron计划任务来完成的。

安装

# yum install logrotate cron

文件位置

  • 默认状态文件在/var/lib/logrotate.status
  • 默认配置文件是/etc/logrotate.conf

运行原理

Logrotate是基于CRON来运行的,其脚本在/etc/cron.daily/logrotate

#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

实际运行时,Logrotate会调用配置文件/etc/logrotate.conf

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

这里的设置是global attribute,而在/etc/logrotate.d目录里,可以定义每项应用服务的配置文件,并且定义会覆盖当下。

定时执行/etc/cron.daily目录下的文件的设置,则在/etc/anacrontab里定义的

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

关于anacrontab,这里不做介绍。

命令参数

Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
  --version                 Display version information

Help options:
  -?, --help                Show this help message
  --usage                   Display brief usage message

配置参数

  • compress 使用gzip来压缩日志文件
  • nocompress 日志不压缩的参数
  • compresscmd 指定压缩工具,默认gzip
  • uncompresscmd 指定解压工具,默认gunzip
  • delaycompress 推迟要压缩的文件,直到下一轮询周期再执行压缩,可与compress搭配使用
  • dateext 轮询的文件名字带有日期信息
  • dateformat 格式化归档日期后缀,只有%Y, %m, %d%s
  • daily 日志按天轮询
  • weekly 日志按周轮询
  • monthly 日志按月轮询
  • yearly 日志按年轮询日志,不关注
  • maxage count 删除count天前的轮询日志文件
  • rotate count 删除count个外的轮询日志文件
  • notifempty 文件为空时,不进行轮询
  • size size 日志文件根据大小规定进行轮询,默认单位是byte,也可指定kb, M, G;So size 100, size 100k, size 100M and size 100G are all valid.
  • minsize size 文件大小超过size后才能进行轮询,此时会略过时间参数
  • postrotate/endscript 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。
  • sharedscripts 在所有的日志文件都轮询完毕后统一执行一次脚本。当声明日志文件的时候使用通配符时需要用到
  • olddir 指定轮询后的日志文件存放在directory,必须和当前日志文件在同一个文件系统
  • create mode owner group 新日志文件的权限,属主属组

配置参数只列出较为常用的

测试

测试以日志大小为规定的轮询,创建个15M大小的日志文件

# head -c 15M /dev/urandom > /var/log/will-log

设置配置文件

# vim /etc/logrotate.d/will-log
/var/log/will-log {
    compress
    delaycompress
    missingok
    notifempty
    daily
    rotate 7
    size 10M
    olddir /tmp
    dateext
    create 0644 root root
}

debug验证下

# logrotate -d /etc/logrotate.d/will-log 
reading config file /etc/logrotate.d/will-log
olddir is now /tmp

Handling 1 logs

rotating pattern: /var/log/will-log  10485760 bytes (7 rotations)
olddir is /tmp, empty log files are not rotated, old logs are removed
considering log /var/log/will-log
  log needs rotating
rotating log /var/log/will-log, log->rotateCount is 7
dateext suffix '-20160401'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /var/log/will-log to /tmp/will-log-20160401
creating new /var/log/will-log mode = 0644 uid = 0 gid = 0

测试没有问题的话,可以在定时任务执行起,手动执行一次

# logrotate -f /etc/logrotate.d/will-log

实际情况下,还得通知应用服务重载配置,一般会在最后加上重启命令,以Nginx为例:

    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript

USR1亦通常被用来告知应用程序重载配置文件;例如,向Apache HTTP服务器发送一个USR1信号将导致以下步骤的发生:停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改。
内容来自维基SIGUSR1和SIGUSR2

参考

相关文章

网友评论

      本文标题:CentOS 7下使用Logrotate管理日志

      本文链接:https://www.haomeiwen.com/subject/arhylttx.html