MAKIZOU.COM

WEB系SEのホームページ作成&サーバ構築運用メモ

ログ・ローテーションでディスクのダイエット管理

ログ・ローテーションとは

各種ログファイル(参照:/etc/logrotate.d/)は放置しておくと追記のみですので、アクセスがあれば時間と共に肥大化します。その肥大化する問題に関してログの定期的なローテーションを管理する方法をメモします。


【4日間のログ・ローテーションの例】
今日 1日後 2日後 3日後
access.log←新規作成 access.log→access.log.1
access.log←新規作成
access.log.1→access.log.2
access.log→access.log.1
access.log←新規作成
access.log.2→access.log.3
access.log.1→access.log.2
access.log→access.log.1
access.log←新規作成

logrotateはcronによって定期的に実行されています。

# ls -alh /etc/cron.daily/logrotate
-rwxr-xr-x 1 root root 180 10月 20 2004 /etc/cron.daily/logrotate

logrotate の中身を見てみましょう。

# view /etc/cron.daily/logrotate


#!/bin/sh

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

logrotate の設定ファイルは /etc/logrotate.conf の様です。

全体のローテーションの設定

logrotate.conf の内容にしたがって、ログ・ローテーションを行っていきます。なお、ここに設定している事はローテーション全体的な事で、個々の設定は /etc/logrotate.d 以下に設定内容を変更したいときだけ記載します。したがって、個々の設定が優先されます。

logrotate.conf の中身を見てみましょう。

# view /etc/logrotate.conf


# see “man logrotate” for details
# rotate log files weekly
weekly
        
#←参照:設定値 - 毎週ローテーション

# keep 4 weeks worth of backlogs
rotate 4
        
#←参照:設定値 - 4回ローテーション

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
        
#← /etc/logrotate.d 以下の個々の設定ファイルをインクルード

# no packages own wtmp — we’ll rotate them here
/var/log/wtmp {
        
#← wtmp ログの設定
    monthly
    create 0664 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

個々のローテーションの設定

/etc/logrotate.d/ 以下のファイルを見てみましょう。

# ls -alh /etc/logrotate.d/


合計 72K
drwxr-xr-x 2 root root 4.0K 3月 12 00:59 .
drwxr-xr-x 73 root root 8.0K 7月 24 00:40 ..
-rw-r–r– 1 root root 144 8月 9 2004 acpid
-rw-r–r– 1 root root 161 1月 5 2006 cups
-rw-r–r– 1 root root 186 4月 5 2005 cyrus-imapd
-rw-r–r– 1 root root 175 9月 5 2005 httpd
-rw-r–r– 1 root root 571 7月 28 2005 mgetty
-rw-r–r– 1 root root 345 4月 3 2005 mysqld
-rw-r–r– 1 root root 163 8月 19 2005 named
-rw-r–r– 1 root root 136 11月 3 2004 ppp
-rw-r–r– 1 root root 242 9月 2 2004 psacct
-rw-r–r– 1 root root 61 11月 2 2004 rpm
-rw-r–r– 1 root root 543 10月 20 2005 squid
-rw-r–r– 1 root root 228 1月 15 2005 syslog
-rw-r–r– 1 root root 48 7月 8 2004 tux
-rw-r–r– 1 root root 92 10月 4 2004 vsftpd.log
-rw-r–r– 1 root root 89 7月 6 2005 yum

個々のローテーションの設定のフォーマットは以下のようになってます。

ログ・ファイル名 [ ログ・ファイル名…] {
    設定値
       :
}

例として http(Apache) のファイルを見てみましょう。

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
    endscript
}

Apacheのログ・ローテーション変更する

全体のログ・ローテーションでは毎週作成、4世代残す設定なっておりますが、Apacheのログは最近は大きくなりがちですので、毎週を毎日に変更したいと思います。

以下、http(Apache) のファイル(修正後)を見てみましょう。

/var/log/httpd/*log {
    daily
        
#←参照:設定値 - 毎日ローテーション
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
    endscript
}

設定の終了後、Apacheに新しい設定を読み込んでもらう為に再起動を行います。再起動を行わないと新しい設定でのローテーションを行えません。

個々のローテーションの設定

上記、ローテンションの設定で使う各パラメータ(設定値)の一覧です。

【ローテーションの設定値】
設定値 説明
daily 毎日ローテーション。
weekly 毎週ローテーション。
monthly 毎月ローテーション。
size ファイルサイズ ログファイルが指定したファイルサイズ以上であればローテーション。
rotate 回数 世代数。指定した回数だけローテーション。
create [パーミッション] [ユーザ名] [グループ名] ローテーション後に空のログファイルを作成。
nocreate ローテーション後に空のログファイルを作成しません。
compress ローテーションされたログをgzipで圧縮。
nocompress ローテーションされたログを圧縮しません。
ifempty ログファイルが空でもローテーションします。
notifempty ログファイルが空ならローテーションしない。
missingok ログファイルが存在しなくてもエラーを出しません。
nomissingok ログファイルが存在しない場合エラーを出す。
olddir ディレクトリ名 指定したディレクトリ内にローテーションされたログを格納。
noolddir ローテーション対象のログと同じディレクトリにローテーションされたログを格納。
prerotate - endscript postrotateとendscriptの間に記述されたコマンドをローテーション前に実行。
postrotate - endscript postrotateとendscriptの間に記述されたコマンドをローテーション後に実行。
sharedscripts 複数指定したログファイルに対してpostrotateまたはprerotateで記述されたコマンドを実行。

  • カテゴリ:Web開発
  • 公開日:
  • 1つ星2つ星3つ星4つ星5つ星
    Loading ... Loading ...
    ↑ 記事評価をお願いします。

コメント&トラックバック

トラックバック用URL

コメント




使用できるXHTMLタグ <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>