1. 程式人生 > >nginx的日誌切割(logrotate)

nginx的日誌切割(logrotate)

日誌切割這東西,在我不瞭解之前,一直覺得它很牛逼,其實這東西很簡單,認真看上面寫這篇文章,一天就能看的差不多懂,都是很簡單的東西,不要被名字嚇到!

一、logrotate日誌切割是linux自帶的

二、logrotate配置檔案分為主配置和子配置

主配置:/etc/logrotate.conf

子配置:/etc/logrotate.d/下面的檔案

三、logrotate是基於CRON執行的,其指令碼為/etc/cron.daily/logrotate

[[email protected] ~]# cat /etc/cron.daily/logrotate 
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

實際執行時會呼叫配置檔案/etc/logrotate.conf

四、logrotate基於CRON執行,所以執行時間由CRON控制

ubuntu檢視/etc/crontab

centos檢視/etc/anacrontab

[email protected]:/etc/logrotate.d# cat /etc/crontab 
# /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 )
#

沒錯,run-parts是執行一個目錄中的所有指令碼或程式,--report的功能與--verbose功能類似,自己去領悟

沒錯,logrotate指令碼是/etc/cron.daily/目錄下面的指令碼,CRON每天06:25執行/etc/cron.daily/目錄下面的所有指令碼

五、logrotate命令,下面複製貼上一下:

logrotate命令格式:
logrotate [OPTION...] <configfile>
-d, --debug :debug模式,測試配置檔案是否有錯誤。
-f, --force :強制轉儲檔案。
-m, --mail=command :壓縮日誌後,傳送日誌到指定郵箱。
-s, --state=statefile :使用指定的狀態檔案。
-v, --verbose :顯示轉儲過程。

六、logrotate配置檔案常見引數,同樣複製貼上一下

compress                                   通過gzip 壓縮轉儲以後的日誌
nocompress                                不做gzip壓縮處理
copytruncate                              用於還在開啟中的日誌檔案,把當前日誌備份並截斷;是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日誌資料。
nocopytruncate                           備份日誌檔案不過不截斷
create mode owner group             輪轉時指定建立新檔案的屬性,如create 0777 nobody nobody
nocreate                                    不建立新的日誌檔案
delaycompress                           和compress 一起使用時,轉儲的日誌檔案到下一次轉儲時才壓縮
nodelaycompress                        覆蓋 delaycompress 選項,轉儲同時壓縮。
missingok                                 如果日誌丟失,不報錯繼續滾動下一個日誌
errors address                           專儲時的錯誤資訊傳送到指定的Email 地址
ifempty                                    即使日誌檔案為空檔案也做輪轉,這個是logrotate的預設選項。
notifempty                               當日志文件為空時,不進行輪轉
mail address                             把轉儲的日誌檔案傳送到指定的E-mail 地址
nomail                                     轉儲時不傳送日誌檔案
olddir directory                         轉儲後的日誌檔案放入指定的目錄,必須和當前日誌檔案在同一個檔案系統
noolddir                                   轉儲後的日誌檔案和當前日誌檔案放在同一個目錄下
sharedscripts                           執行postrotate指令碼,作用是在所有日誌都輪轉後統一執行一次指令碼。如果沒有配置這個,那麼每個日誌輪轉後都會執行一次指令碼
prerotate                                 在logrotate轉儲之前需要執行的指令,例如修改檔案的屬性等動作;必須獨立成行
postrotate                               在logrotate轉儲之後需要執行的指令,例如重新啟動 (kill -HUP) 某個服務!必須獨立成行
daily                                       指定轉儲週期為每天
weekly                                    指定轉儲週期為每週
monthly                                  指定轉儲週期為每月
rotate count                            指定日誌檔案刪除之前轉儲的次數,0 指沒有備份,5 指保留5 個備份
dateext                                  使用當期日期作為命名格式
dateformat .%s                       配合dateext使用,緊跟在下一行出現,定義檔案切割後的檔名,必須配合dateext使用,只支援 %Y %m %d %s 這四個引數
size(或minsize) log-size            當日志文件到達指定的大小時才轉儲,log-size能指定bytes(預設)及KB (sizek)或MB(sizem).
當日志文件 >= log-size 的時候就轉儲。 以下為合法格式:(其他格式的單位大小寫沒有試過)
size = 5 或 size 5 (>= 5 個位元組就轉儲)
size = 100k 或 size 100k
size = 100M 或 size 100M

七、附上生產環境的幾個配置檔案,不是我寫的,好壞另算

nginx的這個可能是預設的,不清楚

[email protected]:/etc/logrotate.d# cat nginx
/var/log/nginx/*.log {
	daily
	missingok
	rotate 14
	compress
	delaycompress
	notifempty
	create 0640 www-data adm
	sharedscripts
	prerotate
		if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
			run-parts /etc/logrotate.d/httpd-prerotate; \
		fi \
	endscript
	postrotate
		invoke-rc.d nginx rotate >/dev/null 2>&1
	endscript
}

zabbix這個

[email protected]:/etc/logrotate.d# cat zabbix-agent 
/var/log/zabbix/zabbix_agentd.log {
    weekly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 0640 zabbix zabbix
}

php這個慢查詢和錯誤日誌

[email protected]:/etc/logrotate.d# cat php7.0-fpm 
/var/log/php7.0-fpm.log {
	rotate 12
	weekly
	missingok
	notifempty
	compress
	delaycompress
	postrotate
		/usr/lib/php/php7.0-fpm-reopenlogs
	endscript
}

/var/log/php/*.log {
        rotate 12
        daily
        missingok
        notifempty
        compress
        delaycompress 
}

八、我在centos7上測試了下,剛裝好的系統在這個目錄下面只有下面幾個配置檔案

[[email protected] logrotate.d]# ls
bootlog  chrony  syslog  wpa_supplicant  yum

當我yum安裝nginx後,發現就多了個nginx配置檔案,所以你懂的,這東西你不會經常用到,本身logrotate就是linux自帶的,然後軟體廠商還提供了日誌切割的配置檔案,基本簡單修改下就可以了

[[email protected] yum.repos.d]# yum install nginx
已載入外掛:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解決依賴關係
--> 正在檢查事務
---> 軟體包 nginx.x86_64.1.1.15.5-1.el7_4.ngx 將被 安裝
--> 解決依賴關係完成

依賴關係解決

=============================================================================================================
 Package              架構                  版本                                  源                    大小
=============================================================================================================
正在安裝:
 nginx                x86_64                1:1.15.5-1.el7_4.ngx                  nginx                759 k

事務概要
=============================================================================================================
安裝  1 軟體包

總下載量:759 k
安裝大小:2.7 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.15.5-1.el7_4.ngx.x86_64.rpm                                                   | 759 kB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : 1:nginx-1.15.5-1.el7_4.ngx.x86_64                                                        1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  驗證中      : 1:nginx-1.15.5-1.el7_4.ngx.x86_64                                                        1/1 

已安裝:
  nginx.x86_64 1:1.15.5-1.el7_4.ngx                                                                          

完畢!
[[email protected] logrotate.d]# ls
bootlog  chrony  nginx  syslog  wpa_supplicant  yum

檢視下這個nginx的內容

[[email protected] logrotate.d]# cat nginx 
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}
[[email protected] logrotate.d]# cat nginx 
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

花費了差不多一天時間學這個logtotate,基本夠用了,一次不可能全部學會!

純手打!

九、突然想到還有個地方要注意的是,執行日誌切割的時間,前面說過了,logrotate是由cron控制的,logrotate的指令碼在所以Ubuntu下修改run-parts /etc/cron.daily任務的執行時間即可

下面這個應該是預設的吧,每天早上6點25

[email protected]:/etc/logrotate.d# cat /etc/crontab 
# /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 )
#