av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

如何用logrotate管理每日增長的日志

瀏覽:2日期:2023-05-12 10:25:34
目錄
  • logrotate簡介
  • 安裝logrotate
  • logrotate基本工作原理
  • 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 logrotateapt-get install logrotate

logrotate基本工作原理

作為一個定時處理的管理工具,logrotate本身是個命令行的工具,然而它需要定時處理你的日志,所以顯然logrotate還是得基于cron來工作的,否則就沒有人定時來喚醒它讓它干活了,正常情況下logrotate已經作為cron.daily的一個任務每天被定時在執行了,這個定時腳本位于/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/"http://g" status | while read logfile datedo    [ -e "$logfile" ] && echo "\"$logfile\" $date"done >> status.cleanmv status.clean statustest -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/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user  command17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly25 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 weeklyweekly# use the syslog group by default, since this is the owning group# of /var/log/syslog.su root syslog# keep 4 weeks worth of backlogsrotate 4# create new (empty) log files after rotating old onescreate# uncomment this if you want your log files compressed#compress# packages drop log rotation information into this directoryinclude /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    postrotatekill -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/nginxHandling 1 logsrotating pattern: /home/wwwlogs/*.log  forced from command line (7 rotations)empty log files are not rotated, old logs are removedconsidering log /home/wwwlogs/access.log  log needs rotatingconsidering log /home/wwwlogs/jp01.sanaecon.com.log  log needs rotatingconsidering log /home/wwwlogs/nginx_error.log  log needs rotatingrotating log /home/wwwlogs/access.log, log->rotateCount is 7dateext 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 failedglob finding old rotated logs failedrotating log /home/wwwlogs/jp01.sanaecon.com.log, log->rotateCount is 7dateext 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 failedglob finding old rotated logs failedrotating log /home/wwwlogs/nginx_error.log, log->rotateCount is 7dateext 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 failedglob finding old rotated logs failedrenaming /home/wwwlogs/access.log to /home/wwwlogs/access.log-20151108creating new /home/wwwlogs/access.log mode = 0644 uid = 0 gid = 0renaming /home/wwwlogs/jp01.sanaecon.com.log to /home/wwwlogs/jp01.sanaecon.com.log-20151108creating new /home/wwwlogs/jp01.sanaecon.com.log mode = 0644 uid = 0 gid = 0renaming /home/wwwlogs/nginx_error.log to /home/wwwlogs/nginx_error.log-20151108creating new /home/wwwlogs/nginx_error.log mode = 0644 uid = 0 gid = 0running postrotate scriptrunning 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

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持。

標簽: Linux
主站蜘蛛池模板: 欧美啪啪网站 | 成人免费视频网址 | 亚洲女人的天堂 | 国产91在线观看 | 亚洲综合久久精品 | 日本久久精 | 91精品国产91久久久久青草 | 香蕉大人久久国产成人av | 亚洲第一视频网站 | 人人人艹 | 国精产品一区一区三区免费完 | 美女国产精品 | 视频一区二区三区四区五区 | 日本午夜免费福利视频 | 国产精品毛片一区二区在线看 | 谁有毛片 | 91麻豆精品国产91久久久更新资源速度超快 | 中文字幕人成乱码在线观看 | 午夜视频网站 | 久在线| 亚洲国产日本 | 欧美xxxx日本 | 久久精品91 | 久久爆操| 欧美日韩亚洲在线 | 亚洲成人三级 | 日韩视频a| 中文字幕在线第一页 | 精品网站999www | 男女精品久久 | 午夜影院| 国产日韩欧美二区 | 欧美高清视频一区 | 在线一区二区国产 | 最新日韩在线 | 国产农村妇女精品一二区 | 免费性视频 | 久久中文字幕在线 | 久久久久久国产精品 | 日韩在线一区二区三区 | 日韩一区二区成人 |