您的位置:首页 > 其它

logrotate日志管理工具-填坑篇

2015-12-30 15:56 369 查看
我的系统是 CentOS release 6.6 (Final) 自带logrotate 没有安装去 yum install logrotate

假设安装了:

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

1.打开看一下/etc/logrotate.conf(logrotate.d/ 下面是被包含的小任务)

[root@iZ25eiwrq9eZ ~]# cat /etc/logrotate.
logrotate.conf  logrotate.d/
[root@iZ25eiwrq9eZ ~]# cat /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.


2. 配置选项说明
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:在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
sharedscripts:所有的日志文件都轮转完毕后统一执行一次脚本
daily:指定转储周期为每天
weekly:指定转储周期为每周
monthly:指定转储周期为每月
rotate count:指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
size size:当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB

3. 命令参数说明
# logrotate --help

1

2

3

4

5

6

Usage: logrotate [OPTION...] <configfile>

-d, --debug 调试模式,输出调试结果,并不执行。隐式-v参数

-f, --force 强制模式,对所有相关文件进行rotate

-m, --mail=command 发送邮件 (instead of `/bin/mail')

-s, --state=statefile 状态文件,对于运行在不同用户情况下有用

-v, --verbose 显示debug信息

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

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


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

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


注意的是:

1、postrotate/endscript 中的脚本一旦执行错误,便不会执行compress。 compress是比postrotate/endscript晚执行的。

2、/etc/cron.daily/logrotate 是logrote的定时任务文件,据观察,是每天早上四点到五点执行的。不科学。不要以为日志文件是以某日期结尾的就认为该日志是那天的,真正的意思是那天轮询的,那天那时刻生成的。

3、.gz文件普通的命令对付不了,cat grep .... 应该换做 zgrep .....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  logrote 注意事项