天天看點

Linux日志檔案總管——logrotate

原文:http://xmodulo.com/2014/09/logrotate-manage-log-files-linux.html作者: Sarmed Rahman

譯文:LCTT  http://linux.cn/article-4126-1.html 譯者: GOLinux

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

logrotate是個十分有用的工具,它可以自動對日志進行截斷(或輪循)、壓縮以及删除舊的日志檔案。例如,你可以設定logrotate,讓/var/log/foo日志檔案每30天輪循,并删除超過6個月的日志。配置完後,logrotate的運作完全自動化,不必進行任何進一步的人為幹預。另外,舊日志也可以通過電子郵件發送,不過該選項超出了本教程的讨論範圍。

主流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。

    size=50M

樣例三

我們想要讓舊日志檔案以建立日期命名,這可以通過添加dateext常熟實作。

    dateext

這将讓歸檔檔案在它們的檔案名中包含日期資訊。

排障

這裡提供了一些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-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 rotated, old logs are removed

considering log /var/log/log-file

  log needs rotating

rotating log /var/log/log-file, log->rotateCount is 5

dateext 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 exist

renaming /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 exist

log /var/log/log-file.6.gz doesn't exist -- won't try to dispose of it

renaming /var/log/log-file to /var/log/log-file.1

creating new /var/log/log-file mode = 0644 uid = 0 gid = 0

running postrotate script

compressing 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定時任務

logrotate需要的cron任務應該在安裝時就自動建立了,我把cron檔案的内容貼出來,以供大家參考。

# cat /etc/cron.daily/logrotate 

#!/bin/sh

# Clean non existent log file entries from status file

cd /var/lib/logrotate

test -e status || touch status

head -1 status > status.clean

sed 's/"//g' status | while read logfile date

do

    [ -e "$logfile" ] && echo "\"$logfile\" $date"

done >> status.clean

mv status.clean status

test -x /usr/sbin/logrotate || exit 0

/usr/sbin/logrotate /etc/logrotate.conf

小結一下,logrotate工具對于防止因龐大的日志檔案而耗盡存儲空間是十分有用的。配置完畢後,程序是全自動的,可以長時間在不需要人為幹預下運作。本教程重點關注幾個使用logrotate的幾個基本樣例,你也可以定制它以滿足你的需求。

希望本文對你有所幫助。

via: http://xmodulo.com/2014/09/logrotate-manage-log-files-linux.html

作者:Sarmed Rahman 譯者:GOLinux 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出