在Linux系統中使用logrotate來管理記錄檔的方法

來源:互聯網
上載者:User

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

  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。

  代碼如下:

  # 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-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的幾個基本範例,你也可以定製它以滿足你的需求。

  希望本文對你有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.