1) 什麼是logrotate?
logrotate是一個日誌管理程式,用來把舊的記錄檔刪除(備份),並建立新的記錄檔,這個過程稱為“轉儲”。我們可以根據日誌的大小,或者根據其使用的天數來轉儲,這個過程一般由cron來執行。
logrotate還可以用於壓縮記錄檔,以及發送日誌到指定的email。
2) logrotate的預設設定檔以及它的主要參數
logrotate 的設定檔是 /etc/logrotate.conf 主要參數如下表:
參數 功能
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)RedHad Linux的logrotate.conf解析
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# send errors to root
errors root
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
1
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own lastlog or wtmp --we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
/var/log/lastlog {
monthly
rotate 1
}
# system-specific logs may be configured here
使用include 選項讀取其他設定檔
include 選項允許系統管理員把分散到幾個檔案的轉儲資訊,集中到一個主要的設定檔。當 logrotate 從logrotate.conf 讀到include 選項時,會從指定檔案讀入配置資訊,就好像他們已經在/etc/logrotate.conf 中一樣。
第13行 include /etc/logrotate.d 告訴 logrotate 讀入存放在/etc/logrotate.d 目錄中的日誌轉儲參數,當系統中安裝了RPM 軟體包時,使用include 選項十分有用。RPM 軟體包的日誌轉儲參數一般存放在/etc/logrotate.d 目錄。
include 選項十分重要,一些應用把日誌轉儲參數存放在 /etc/logrotate.d 。
典型的應用有:apache, linuxconf, samba, cron 以及syslog。
這樣,系統管理員只要管理一個 /etc/logrotate.conf 檔案就可以了。
當 /etc/logrotate.conf 讀入檔案時,include 指定的檔案中的轉儲參數將覆蓋預設的參數,如下例:
上面的資訊放在檔案logrotate中,它位於目錄/etc/cron.daily中,由/etc/crontab配置起來由cron程式執行。
# linuxconf 的參數
/var/log/htmlaccess.log
{
errors jim
notifempty
nocompress
weekly
prerotate
/usr/bin/chattr -a /var/log/htmlaccess.log
endscript
postrotate
/usr/bin/chattr +a /var/log/htmlaccess.log
endscript
}
/var/log/netconf.log
{
nocompress
monthly
}
在這個例子中,當 /etc/logrotate.d/linuxconf 檔案被讀入時,下面的參數將覆蓋/etc/logrotate.conf中預設的參數。
Notifempty
errors jim
4)logrotate 的執行
logrotate 的運行分為三步:
判斷系統的記錄檔,建立轉儲計劃以及參數,通過cron daemon 運行
下面的代碼是 Red Hat Linux 預設的crontab 來每天運行logrotate。
#/etc/cron.daily/logrotate
#! /bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
上面的資訊儲存在檔案logrotate中,它其實是一個設定檔,位於/etc/cron.daily目錄裡。
/etc/cron.daily中的所有設定檔是每天都要由cron執行的程式的設定檔。它們都由系統管理員維護的/etc/crontab設定並執行。
發現asterisk的log日誌/var/log/asterisk/full竟然有170M,所以尋找日誌輪轉的方式。按照如上的資料,只需要在修改/etc/logrotate.conf,增加
/var/log/asterisk/messages /var/log/asterisk/*log /var/log/asterisk/full {
missingok
rotate 5
daily
create 0640 asterisk asterisk
postrotate
/usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null
endscript
}
reference :http://www.voip-info.org/tiki-index.php?page=logrotate