Linux日志切割之Logrotate
  TEZNKK3IfmPf 2023年11月12日 46 0

日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到。对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题。除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事。  logrotate是个十分有用的工具,它可以自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。例如,你可以设置logrotate,让/var/log/foo日志文件每30天轮循,并删除超过6个月的日志。配置完后,logrotate的运作完全自动化,不必进行任何进一步的人为干预。

2、安装logrotate

默认centos系统安装自带logrotate,安装方法如下

yum -y install logrotate crontabs

软件信息说明:

# rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf # 主配置文件
/etc/logrotate.d # 配置目录

3、实践配置logrotate

3.1 测试logrotate如何管理日志

这里我们将创建一个10MB的日志文件/var/log/log-file。我们将展示怎样使用logrotate来管理该日志文件。

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

[root@clsn6 ~]# touch /var/log/log-file
[root@clsn6 ~]# head -c 10M < /dev/urandom > /var/log/log-file

由于现在日志文件已经准备好,我们将配置logrotate来轮循该日志文件。让我们为该文件创建一个配置文件。

[root@clsn6 ~]# 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
}

上面的模板是通用的,而配置参数则根据你的需求进行调整,不是所有的参数都是必要的。也可以通过man手册中的例子进行配置。

3.2 配置文件说明

配置参数

说明

monthly

日志文件将按月轮循。其它可用值为'daily','weekly'或者'yearly'。

rotate 5

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

compress

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

delaycompress

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

missingok

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

notifempty

如果日志文件为空,轮循不会进行。

create 644 root root

以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。

postrotate/endscript

在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。


3.3 手动运行logrotate

logrotate可以在任何时候从命令行手动调用。要调用为/etc/lograte.d/下配置的所有日志调用logrotate:

[root@clsn6 ~]# logrotate /etc/logrotate.conf

要为某个特定的配置调用logrotate,执行一次切割任务测试

[root@clsn6 ~]# ll /var/log/log-file
-rw-r--r-- 1 root root 10485760 Feb 7 18:50 /var/log/log-file
[root@clsn6 ~]# logrotate -vf /etc/logrotate.d/log-file
[root@clsn6 ~]# ll /var/log/log-file*
-rw-r--r-- 1 root root 0 Feb 7 19:17 /var/log/log-file
-rw-r--r-- 1 root root 10485760 Feb 7 18:50 /var/log/log-file.1

即使轮循条件没有满足,我们也可以通过使用‘-f’选项来强制logrotate轮循日志文件,‘-v’参数提供了详细的输出。

3.4Logrotate的记录日志

logrotate自身的日志通常存放于/var/lib/logrotate/status目录。如果处于排障目的,我们想要logrotate记录到任何指定的文件,我们可以指定像下面这样从命令行指定。

[root@clsn6 ~]# logrotate -vf -s /var/log/logrotate-status /etc/logrotate.d/log-file
reading config file /etc/logrotate.d/log-file
reading 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 not rotated, old logs are removed
considering log /var/log/log-file
log does not need rotating
not running postrotate script, since no logs were rotated

3.5 Logrotate定时任务

logrotate需要的cron任务应该在安装时就自动创建了,我把cron文件的内容贴出来,以供大家参考。

[root@clsn6 ~]# cat /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

4、实例:Nginx日志切割

操作系统内部默认安装了logrotate日志管理工具,在/etc/logrotate.d/下vi nginx,并输入如下内容并保存。logrotate将每日切割一个日志文件,压缩和定期清理。

/var/log/nginx/*log { 
  su root cloudapp  
create 0666 root cloudapp    
daily      
rotate 5  
dateext
missingok   
  notifempty   
compress
  sharedscripts
  postrotate
        /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

注意:脚本中的cat /usr/local/nginx/logs/nginx.pid这一段如果按照上述准模式安装,此路径pid正确,如果自行其他形式安装,请根据情况修改。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   28   0   0 nginxpasswd
  TEZNKK3IfmPf   2024年05月31日   52   0   0 linux服务器
  TEZNKK3IfmPf   2024年05月31日   31   0   0 linux服务器centos
  TEZNKK3IfmPf   2024年05月31日   34   0   0 nginxpasswd
  TEZNKK3IfmPf   2024年05月31日   29   0   0 linuxbind
TEZNKK3IfmPf