Linux日志文件总管——logrotate

日志文件包含了关于系统中发生的事件的有用信息,在排障过程当中或者系统性能分析时常常被用到。对于忙碌的服务器,日志文件大小会增加极快,服务器会很快消耗磁盘空间,这成了个问题。除此以外,处理一个单个的庞大日志文件也经常是件十分棘手的事。vim

logrotate是个十分有用的工具,它能够自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。例如,你能够设置logrotate,让 /var/log/foo日志文件每30天轮循,并删除超过6个月的日志。配置完后,logrotate的运做彻底自动化,没必要进行任何进一步的人为干 预。另外,旧日志也能够经过电子邮件发送,不过该选项超出了本教程的讨论范围。bash

主流Linux发行版上都默认安装有logrotate包,若是出于某种缘由,logrotate没有出如今里头,你可使用apt-get或yum命令来安装。服务器

在Debian或Ubuntu上:dom

apt-get install logrotate cron

logrotate的配置文件是/etc/logrotate.conf,一般不须要对它进行修改。日志文件的轮循设置在独立的配置文件中,它(们)放在/etc/logrotate.d/目录下。ide

样例一

在第一个样例中,咱们将建立一个10MB的日志文件/var/log/log-file。咱们将展现怎样使用logrotate来管理该日志文件。工具

咱们从建立一个日志文件开始吧,而后在其中填入一个10MB的随机比特流数据。post

# touch /var/log/log-file
# head -c 10M < /dev/urandom > /var/log/log-file

因为如今日志文件已经准备好,咱们将配置logrotate来轮循该日志文件。让咱们为该文件建立一个配置文件。性能

# vim /etc/logrotate.d/log-file
/var/log/log-file {    
    monthly    
    rotate 5    
    compress    
    delaycompress    
    missingok    
    notifempty    
    create 644 root root    
    postrotate        
        /usr/bin/killall -HUP rsyslogd    
    endscript
}

 

这里:ui

  • monthly: 日志文件将按月轮循。其它可用值为‘daily’,‘weekly’或者‘yearly’。spa

  • rotate 5: 一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。

  • compress: 在轮循任务完成后,已轮循的归档将使用gzip进行压缩。

  • delaycompress: 老是与compress选项一块儿用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然须要读取最新归档时颇有用。

  • missingok: 在日志轮循期间,任何错误将被忽略,例如“文件没法找到”之类的错误。

  • notifempty: 若是日志文件为空,轮循不会进行。

  • create 644 root root: 以指定的权限建立全新的日志文件,同时logrotate也会重命名原始日志文件。

  • postrotate/endscript: 在全部其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种状况下,rsyslogd 进程将当即再次读取其配置并继续运行。

上面的模板是通用的,而配置参数则根据你的需求进行调整,不是全部的参数都是必要的。

若是出现以下错误

error: skipping "/var/log/smvp/he.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation

则在配置文件末尾增长 su root

样例二

在本例中,咱们只想要轮循一个日志文件,然而日志文件大小能够增加到50MB。

# vim /etc/logrotate.d/log-file
/var/log/log-file {    
    size=50M    
    rotate 5    
    create 644 root root    
    postrotate        
        /usr/bin/killall -HUP rsyslogd    
    endscript
}

 

样例三

咱们想要让旧日志文件以建立日期命名,这能够经过添加dateext常熟实现。

# vim /etc/logrotate.d/log-file
/var/log/log-file {    
    monthly    
    rotate 5    
    dateext   
    create 644 root root   
    postrotate       
    /usr/bin/killall -HUP rsyslogd    
    endscript
}

 

 

这将让归档文件在它们的文件名中包含日期信息。

排障

这里提供了一些logrotate设置的排障提示。

1. 手动运行logrotate

logrotate能够在任什么时候候从命令行手动调用。

要调用为/etc/lograte.d/下配置的全部日志调用logrotate

# logrotate /etc/logrotate.conf

要为某个特定的配置调用logrotate:

# logrotate /etc/logrotate.d/log-file

2. 演练

排障过程当中的最佳选择是使用‘-d’选项以预演方式运行logrotate。要进行验证,不用实际轮循任何日志文件,能够模拟演练日志轮循并显示其输出。

# logrotate -d /etc/logrotate.d/log-file

正如咱们从上面的输出结果能够看到的,logrotate判断该轮循是没必要要的。若是文件的时间小于一天,这就会发生了。

3. 强制轮循

即便轮循条件没有知足,咱们也能够经过使用‘-f’选项来强制logrotate轮循日志文件,‘-v’参数提供了详细的输出。

# logrotate -vf /etc/logrotate.d/log-file
reading config file /etc/logrotate.d/log-filereading config info for /var/log/log-file 
Handling 1 logs rotating pattern: /var/log/log-file  
forced from command line (5 rotations)empty log files are rotated, old logs are removedconsidering log /var/log/log-file  
log needs rotatingrotating log /var/log/log-file, log->rotateCount is 5dateext suffix '-20140916'glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'renaming /var/log/log-file.5.gz to 
/var/log/log-file.6.gz (rotatecount 5, logstart 1, i 5),old log /var/log/log-file.5.gz does not existrenaming /var/log/log-file.4.gz to /var/log/log-file.5.gz (rotatecount 5, logstart 1, i 4)
,old log /var/log/log-file.4.gz does not exist. . .renaming /var/log/log-file.0.gz to /var/log/log-file.1.gz (rotatecount 5, logstart 1, i 0),old log /var/log/log-file.0.gz does not existlog 
/var/log/log-file.6.gz doesn't exist -- won't try to dispose of itrenaming /var/log/log-file to /var/log/log-file.1creating new /var/log/log-file mode = 0644 uid = 0 gid = 0running postrotate scriptcompressing log with: /bin/gzip

4. Logrotate的记录日志

logrotate自身的日志一般存放于/var/lib/logrotate/status目录。若是处于排障目的,咱们想要logrotate记录到任何指定的文件,咱们能够指定像下面这样从命令行指定。

# logrotate -vf –s /var/log/logrotate-status /etc/logrotate.d/log-file

5. Logrotate定时任务

logrotate须要的cron任务应该在安装时就自动建立了,我把cron文件的内容贴出来

# cat /etc/cron.daily/logrotate
#!/bin/sh

# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
/usr/sbin/logrotate /etc/logrotate.d/log-file

小结一下,logrotate工具对于防止因庞大的日志文件而耗尽存储空间是十分有用的。配置完毕后,进程是全自动的,能够长时间在不须要人为干预下运行。本教程重点关注几个使用logrotate的几个基本样例,你也能够定制它以知足你的需求。

相关文章
相关标签/搜索