如何使用logrotate分割管理dnsmasq的日志 CentOS6.5 64bit

logrotate 程序是一个日志文件管理工具,Centos系统基本都默认已安装。它可以用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。也可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行。logrotate 程序还可以用于压缩日志文件,以及发送日志到指定的E-mail 。

logrotate

目前logrotate的版本是logrotate-3.7.8,它的默认配置文件及中文说明:
cat /etc/logrotate.conf

 

# see "man logrotate" for details
# 所有的日志文件每周转储一次
weekly

# 转储的文件保留4份,也就是4周的文件
rotate 4

# 转储日志后,logrotate自动创建新的日志文件
create

# 给转储的日志文件加上日期后缀
dateext

# 压缩日志文件。默认是注释掉的
#compress

# 读入/etc/logrotate.d目录下的日志转储参数,当系统中安装了RPM软件包时,RPM包的日志转储参数一般会自动建立在/etc/logrotate.d目录下
include /etc/logrotate.d

# 对/var/log/wtmp日志转储的配置
/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.

 

dnsmasq日志分割案例

1. 首先需要开启dnsmasq的日志记录

vi /etc/dnsmasq.conf

log-queries
log-facility=/var/log/dnsmasq/dnsmasq.log

2. 建立logrotate的配置文件

vi /etc/logrotate.d/dnsmasq
输入:
/var/log/dnsmasq/dnsmasq.log {
notifempty
weekly
dateext
rotate 58
sharedscripts
postrotate
[ ! -f /var/run/dnsmasq.pid ] || kill -USR2 `cat /var/run/dnsmasq.pid`
endscript
}

参数说明:

notifempty :如果是空文件的话,不转储。
weekly :日志文件每天转储一次。
rotate 5 ;转储文件分为5份。
postrotate/endscript :日志转储后执行的脚本。

完整参数参考

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).

3. 执行logrotate

/usr/sbin/logrotate -vf /etc/logrotate.conf

logrotate命令格式解释:
logrotate [OPTION...] <configfile>
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。

如果没有报错,生成了转储文件,dnsmasq正常运行,就OK了。

4. cron定时自动执行logrotate

在/etc/cron.daily目录下有logrotate执行的脚本。你可以通过crontab程序每天执行一次。也可以自己设定特定的计划来执行自己的日志切割脚本。
测试一下脚本先:
sh /etc/cron.daily/logrotate
vi /etc/crontab
33 3 * * * root run-parts /etc/cron.daily

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

奇客罗方公众号 奇客罗方小程序 奇客罗方客服 ITGeeker Telegram

网站由ITGeeker技术奇客开发并管理;隶属于GeekerCloud奇客罗方智能科技
Site designed and developed by ITGeekerwhich is a sub-website of GeekerCloud
网站地图 | 沪ICP备2021031434号-4