用logrotate管理每日增長的日誌

logrotate簡介

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

這是logrotate文檔中對其自身的描述,其實logrotate功能很簡單,正如其名,按一定週期自動循環分割和保存日誌防止無限增長,必要的時候進行壓縮、刪除,還可以進行郵件通知等功能。
很多服務器上總會運行着一些會不停生成成噸日誌的程序,最簡單的比如nginx、httpd之類的web服務器的訪問日誌,爲了保持這些日誌的可檢索以及防止無限增長把有限的硬盤都吃乾淨了,就會有五花八門的cron腳本來處理這些日誌,事實上這些事情都可以由logrotate代勞。

安裝logrotate

事實上大多數Linux發行版本都默認安裝了logrotate,可以通過以下命令來判斷系統是否已經預裝了logrotate:
rpm包管理器:

rpm -qa|grep logrotate

dpkg包管理器:

dpkg --list|grep logrotate

更簡單粗暴的方法:

which logrotate

如果能看到有字符串返回的話,你的服務器就應該預裝了logrotate,並且它已經每天在默默地工作了,只不過你並沒有給他分配活兒。如果沒有安裝的話,用正常的包管理工具都能安裝它:

yum install logrotate
apt-get install logrotate

logrotate基本工作原理

作爲一個定時處理的管理工具,logrotate本身是個命令行的工具,然而它需要定時處理你的日誌,所以顯然logrotate還是得基於cron來工作的,否則就沒有人定時來喚醒它讓它幹活了,正常情況下logrotate已經作爲cron.daily的一個任務每天被定時在執行了,這個定時腳本位於/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

上面這段是Ubuntu中默認的logrotate腳本,各個不同版本上的logrotate腳本會有細微的不同,但是核心都是使用/etc/logrotate.conf配置腳本來啓動logrotate。
然而這段腳本只是位於這個目錄裏,爲什麼會被每天調用呢,而且每天什麼點調用呢?可以查看CRON的默認配置文件/etc/crontab(新版CentOS中其默認配置文件位於/etc/anacrontab)的內容:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

事實上CRON默認會存在這4條指令,分別是每小時、每天、每週、每個月會執行對應目錄下的腳本,而不需要再通過日常常用的crontab去配置,可以看到cron.daily目錄下的腳本在每天的6點25分會被執行,這也就意味着在這臺機器上每天6點25分會啓動logrotate進行日誌的處理,各個系統的默認配置也是各不相同的。

最後來看一下默認的配置文件/etc/logrotate.conf:

# see "man logrotate" for details
# rotate log files weekly
weekly

# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
#compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}

# system-specific logs may be configured here

其實默認配置文件中給出的是logrotate的一些默認配置選項,並給出了一些樣例的配置(如wtmp,btmp),這些配置的意義在後文中會予以闡述,而這個配置文件中還包含了include /etc/logrotate.d,這使得我們可以不用將所有的配置扎堆寫入這個默認配置文件中,而是可以分散地管理在/etc/logrotate.d目錄下,事實上/etc/logrotate.d也已經包含了一些樣例的配置文件供參考。

logrotate的配置和使用

如同剛纔所說的,logrotate會每日CRON啓動並執行,我們只需要在/etc/logrotate.d目錄中添加我們需要的配置,就可以利用logrotate來自動管理我們需要的日誌文件。
這裏先來看一個目錄下已經存在的樣例配置dpkg:

/var/log/dpkg.log {
    monthly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}
/var/log/alternatives.log {
    monthly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}

正如其名稱dpkg,這個配置是用來處理包管理器dpkg的默認日誌的,處理的日誌文件是/var/log/dpkg.log與/var/log/alternatives.log這兩個文件,其配置內容如下:

  • monthly:按月處理日誌
  • rotate 12:保留12份日誌
  • compress:日誌分割後會進行gzip壓縮
  • delaycompress:日誌壓縮會被延後到下次分割時進行
  • missingok:目標日誌文件不存在程序也不會報錯退出
  • notifempty:目標日誌文件爲空時不進行分割操作
  • create 644 root root:以644也就是rw-r–r–權限來建立分割出來的文件,同時該分割文件所屬用戶爲root,用戶組爲root

在/var/log下列出文件列表,可以看到dpkg的日誌的確被分割並壓縮了:
這裏寫圖片描述

仿照這個樣例文件,我們可以配置一個類似的處理任務,這裏處理一下nginx的log文件,一般web serve的access log文件都是日誌重災區,建立一個/etc/logrotate.d/nginx配置文件,內容如下:

/home/wwwlogs/*.log {
    daily
    rotate 7
    dateext
    compress
    delaycompress    
    missingok
    notifempty
    create 644 root root
    sharedscripts
    postrotate
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    endscript
}

與之前的樣例配置類似,注意到聲明日誌文件位置時使用了*通配符,這意味着/home/wwwlogs/下的所有.log文件都會被logrotate處理,這裏多出了幾行配置,說明如下:

  • daily:每天處理日誌
  • dateext:備份的日誌文件的後綴不再是默認的.1 .2 .3的遞增格式,而是.YYYYMMDD的日期格式
  • sharedscripts:配置該選項時,prerotate和postrotate段的腳本會在所有文件被處理結束(被處理前)統一執行,而不是每個文件前後分別執行一次
  • postrotate:處理日誌後鉤子,postrotate和endscript構成一個shell語句塊,在日誌文件被處理後這部分語句會被執行,對應的還有prerotate語句塊

    這樣nginx的log文件的處理配置就完成了,但是我們可能還需要確認一下配置的內容到底是否正確呢,這時候可以利用logrotate命令行的debug選項來進行測試,命令如下:

logrotate -d -f /etc/logrotate.d/nginx

開啓了debug選項時,logrotate會詳細地給出處理日誌過程中的處理信息,但是並不會真正地去處理日誌文件,所以可以用來測試配置文件處理的是否正確,這行命令的輸出如下:

reading config file /etc/logrotate.d/nginx

Handling 1 logs

rotating pattern: /home/wwwlogs/*.log  forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /home/wwwlogs/access.log
  log needs rotating
considering log /home/wwwlogs/jp01.sanaecon.com.log
  log needs rotating
considering log /home/wwwlogs/nginx_error.log
  log needs rotating
rotating log /home/wwwlogs/access.log, log->rotateCount is 7
dateext suffix '-20151108'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
rotating log /home/wwwlogs/jp01.sanaecon.com.log, log->rotateCount is 7
dateext suffix '-20151108'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
rotating log /home/wwwlogs/nginx_error.log, log->rotateCount is 7
dateext suffix '-20151108'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /home/wwwlogs/access.log to /home/wwwlogs/access.log-20151108
creating new /home/wwwlogs/access.log mode = 0644 uid = 0 gid = 0
renaming /home/wwwlogs/jp01.sanaecon.com.log to /home/wwwlogs/jp01.sanaecon.com.log-20151108
creating new /home/wwwlogs/jp01.sanaecon.com.log mode = 0644 uid = 0 gid = 0
renaming /home/wwwlogs/nginx_error.log to /home/wwwlogs/nginx_error.log-20151108
creating new /home/wwwlogs/nginx_error.log mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg /home/wwwlogs/*.log : "
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
"

可以看到logrotate如我們設想的那樣在正常處理日誌文件,至此一個logrotate配置就完成了。

logrotate命令行除了可以用來展示配置文件配置是否正確以外,還可以用來手動執行日誌分割,使用以下命令行:

logrotate -f /etc/logrotate.d/nginx

就會脫離CRON手動運行一次日誌分割和處理任務,事實上通過這個方式也可以用第三方的其他程序來管理日誌分割的頻率。

本文中給出了一些常見的配置文件中的配置參數的含義,更多其他的配置參數的含義和功能可以參考官方的配置文檔:http://linuxconfig.org/logrotate-8-manual-page

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章