日志檔案包含了關于系統中發生的事件的有用資訊,在排障過程中或者系統性能分析時經常被用到。對于忙碌的伺服器,日志檔案大小會增長極快,伺服器會很快消耗磁盤空間,這成了個問題。除此之外,處理一個單個的龐大日志檔案也常常是件十分棘手的事。

主流Linux發行版上都預設安裝有logrotate包,如果出于某種原因,logrotate沒有出現在裡頭,你可以使用apt-get或yum指令來安裝。
在Debian或Ubuntu上:
# apt-get install logrotate cron
在Fedora,CentOS或RHEL上:
# yum install logrotate crontabs
logrotate的配置檔案是/etc/logrotate.conf,通常不需要對它進行修改。日志檔案的輪循設定在獨立的配置檔案中,它(們)放在/etc/logrotate.d/目錄下。
樣例一
在第一個樣例中,我們将建立一個10MB的日志檔案/var/log/log-file。我們将展示怎樣使用logrotate來管理該日志檔案。
我們從建立一個日志檔案開始吧,然後在其中填入一個10MB的随機比特流資料。
# 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}
這裡:
- 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 程序将立即再次讀取其配置并繼續運作。
上面的模闆是通用的,而配置參數則根據你的需求進行調整,不是所有的參數都是必要的。
樣例二
在本例中,我們隻想要輪循一個日志檔案,然而日志檔案大小可以增長到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定時任務
# cat /etc/cron.daily/logrotate
#!/bin/sh # Clean non existent log file entries from status filecd /var/lib/logrotatetest -e status || touch statushead -1 status > status.cleansed 's/"//g' status | while read logfile datedo [ -e "$logfile" ] && echo "\"$logfile\" $date"done >> status.cleanmv status.clean status test -x /usr/sbin/logrotate || exit 0/usr/sbin/logrotate /etc/logrotate.conf