Logrotateの設定
対象サーバについて
| 製品名 | OpenBlockS 600 |
| OS(kernel ver) | Debian lenny(2.6.29) |
| CPU | 600MHz(AMCC PowerPC 405EX) |
| メモリ | 1GB(DDR2 SDRAM) |
| ストレージ | 8GB(Compact Flash) |
コンフィグ設定
編集するファイル:/etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
#weekly
monthly
# keep 4 weeks worth of backlogs
#rotate 4
rotate 3
# 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
rotate 3
}
/var/log/btmp {
missingok
monthly
create 0660 root utmp
# rotate 1
rotate 3
}
# system-specific logs may be configured here
上記設定について
| weekly | 毎週ログを置き換える。毎日はdaily、毎月はmonthly |
| rotate 4 | ログを4世代分残す。weeky の場合は4週間という意味 |
| create | 新規ログファイルをローテーションした直後に作成する |
| compress | lotateしたログファイルを圧縮する。デフォルトでは圧縮しないようになっている。 |
→初期設定では1週間ごとに4回lotateする設定となっていますが、ここでは1ヶ月ごとに3回(3ヶ月分)lotateするように変更しています。
このファイルで定義していないログについてはlotateされない点に注意して下さい。
例えば、wtmpやbtmpは以下のようになります。
# ls -la /var/log/btmp* -rw-rw---- 1 root utmp 384 2010-08-14 12:04 btmp -rw-rw---- 1 root utmp 0 2010-07-01 06:25 btmp.1 -rw-rw---- 1 root utmp 768 2010-06-15 01:22 btmp.2 -rw-rw---- 1 root utmp 0 2010-05-01 06:25 btmp.3 # ls -la /var/log/wtmp* -rw-rw-r-- 1 root utmp 65664 2010-08-29 21:46 wtmp -rw-rw-r-- 1 root utmp 22656 2010-07-26 02:00 wtmp.1 -rw-rw-r-- 1 root utmp 27648 2010-06-27 23:57 wtmp.2 -rw-rw-r-- 1 root utmp 84480 2010-05-31 00:58 wtmp.3
アプリケーションごとのコンフィグ設定
rsyslog経由のログは/etc/logrotate.d/rsyslog、アプリケーションが直接吐き出すログは/etc/logrotate.d/[アプリケーション名]とします。
rsyslog経由のログとして、今回新たにiptables.log、router.log、dhcp3-server.logを例として挙げています。
編集するファイル:/etc/logrotate.d/rsyslog
/var/log/syslog
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
/var/log/router.log
/var/log/iptables.log
/var/log/dhcp3-server.log
{
rotate 3
monthly
missingok
notifempty
# compress
# delaycompress
sharedscripts
postrotate
invoke-rc.d rsyslog reload > /dev/null
endscript
}
上記設定について
| missingok | ログファイルが存在しなくてもエラーを出さずに処理を続行 |
| notifempty | ログファイルが空ならローテーションしない |
| delaycompress | ログの圧縮作業を次回のローテーション時まで遅らせる。compressと共に指定 |
| sharedscripts | 全ログで共通して1回postrotateが呼ばれる。 |
| postrotate~endscript | postrotateとendscriptの間に記述されたコマンドをログローテーション後に実行。 |
postrotateにてrsyslogをreloadすることで、元のファイルではなく新たに作成されたファイルにログを吐き出すようにしています。
rsyslog経由ではなく直接ログを吐き出しているアプリケーション(例えばtelnetやvsftpd)については以下のように、別途ファイルを作成して定義します。
作成するファイル:/etc/logrotate.d/telnet
/var/log/telnet.log {
nocompress
missingok
notifempty
rotate 3
monthly
postrotate
invoke-rc.d xinetd reload > /dev/null
endscript
}
lotateの実施間隔の確認(cron)
まず、crontabの設定を確認します。
確認するファイル:/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 10 * * * * 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の使い方についてはこちらのサイトの記載が分かりやすいです。
まず、test -x という部分を見てみる。これは、「ファイルが存在すれば実行する」ということを意味している。test -x /usr/bin/anacron で、/usr/sbin/anacronが存在するかどうかをチェックし、存在しなければを表すのが「|| 」の記号になる。run-parts はコマンドを表し、ディレクトリ内にあるスクリプトを実行するコマンドのことである。つまり、/etc/cron.daily/ 以下にあるスクリプトが実行できるようになる。結論としてこの1行は「/usr/sbin/anacronがあるかどうかチェックし、なければ、run- parts コマンドを実行しろ」ということを表している。
次に、/etc/cron.daily/の中を見てみると、logrotateがありますので確認します。
確認するファイル:/etc/cron.daily/logrotate
#!/bin/sh test -x /usr/sbin/logrotate || exit 0 /usr/sbin/logrotate /etc/logrotate.conf
上記から、1日1回logrotateが実施されていることが分かります。
logrotateコマンドを実行した場合、前回ローテーションを行った日から設定した期日が過ぎていればローテーションし、まだ期日が来ていないならローテーションしないという動作をします。
前回ローテーションを行った日は以下のファイルで分かります。
ここを書き換えることでローテーションがうまくいっているか試験することも可能です。
確認するファイル:/var/lib/logrotate/status
logrotate state -- version 2 "/var/log/apt/term.log" 2010-8-1 "/var/log/aptitude" 2010-8-1 "/var/log/dpkg.log" 2010-8-1 "/var/log/syslog" 2010-8-1 "/var/log/mail.info" 2010-8-1 ...