CentOSで/var/log配下に蓄積されていく各種ログを圧縮したり定期的に置き換え、破棄などの処理をする為
logrotateの設定をします。
logrotateの設定
現状CentOSで構成されているlogrotateの確認をします。
01 02 03 04 05 06 07 08 09 10 11 | # rpm -ql logrotate /etc/cron.daily/logrotate #logrotateを毎日実行するためのcronの設定ファイル /etc/logrotate.conf #logrotateの設定ファイル /etc/logrotate.d #このディレクトリ配下の各ファイルがlogrotate.confにより読み込みされる /usr/sbin/logrotate #logrotateの実行ファイル /usr/share/doc/logrotate-3.7.8 /usr/share/doc/logrotate-3.7.8/CHANGES /usr/share/doc/logrotate-3.7.8/COPYING /usr/share/man/man5/logrotate.conf.5.gz /usr/share/man/man8/logrotate.8.gz /var/lib/logrotate.status #logrotateの各ステータスの記録ファイル |
logrotate.confの設定
以下では、ログローテーションを毎月に変更し、世代管理を3か月間へ設定変更します。
また、各ログファイルは圧縮するように設定します。
それぞれのコマンドはなんのために使われているのか、各箇所でコメントを入れてます。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly monthly #毎月に変更 # keep 4 weeks worth of backlogs rotate 3 #ログを世代管理します。ここでは3か月間に設定 # create new (empty) log files after rotating old ones create #新規ログファイルをローテーション後に作成。デフォルト有効 # use date as a suffix of the rotated file dateext #ログファイルの末尾に日付(YYYYMMDD)がつく。デフォルト有効 # uncomment this if you want your log files compressed compress #ログファイルの圧縮。デフォルトは有効でない為、ディスクの空き容量などを考慮し使用するか決めること # RPM packages drop log rotation information into this directory include /etc/logrotate.d # no packages own wtmp and btmp -- we'll rotate them here /var/log/btmp /var/log/lastlog /var/log/wtmp { monthly create 0664 root utmp minsize 1M rotate 1 } #/var/log/XXXファイルは毎月1世代のログを保管し、所有者がrootでグループがutmpで、なおかつパーミッションが644のログファイルを作成 # system-specific logs may be also be configured here. |
ログローテーションの対象となるファイルの確認
ログローテーションの対象となるファイルは以下のディレクトリ配下で確認できます。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | # ll /etc/logrotate.d/ 合計 12 -rw-r--r--. 1 root root 103 11月 29 22:26 2013 dracut -rw-r--r--. 1 root root 210 8月 15 20:24 2013 syslog -rw-r--r--. 1 root root 100 12月 17 21:27 2013 yum # cat /etc/logrotate.d/syslog /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript } |
logrotateのcronを確認
/etc/cron.daily/logrotateファイルでcronにより、毎日一度logrotateが実行されるように指定されています。
※以下の値はデフォルト
1 2 3 4 5 6 7 8 9 | # cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1 EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0 |
こんな感じです。
ほとんどは”logrotate.conf”で設定しますので、環境に合わせて設定を実行しましょう。
あわせて読みたい


サーバーエンジニアへの構築手順書
サーバエンジニアにおすすめの構築手順をまとめたページです。 サーバ初心者の方。じぶんも初心者なので、そっから目線で構築した設計手順書となっています。 ご興味の...
ITエンジニアの開発・検証・学習としてインターネット上で専用のサーバ(VPS)を利用しましょう!
実務経験はVPSで学べます。
コメント