Apacheのlogrotate設定

2010-02-17

Apacheのlogrotate設定

いまさらだけど、Apacheのログローテートやってみる。 いつもは、他人任せなのでちゃんと理解してみよう。 Redhat系なら、多分デフォルトでlogrotateは入ってる。 設定ファイルは

  • /etc/logrotate.conf
  • /etc/logrotate.d/*
    設定ファイル1番目は、大本の設定ファイルでコレを変更すると大事なので、触らない。 なので、2個目の方、logrotate.dディレクトリの配下に適当な名前で設定ファイルを作る。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ vi /etc/logrotate.d/httpd

/usr/local/httpd/logs/*_log {
rotate 90
daily
create 644 root root
ifempty
olddir /var/log/httpd
dateext
compress
postrotate
/etc/init.d/httpd restart > /dev/null 2>/dev/null
endscript
}

毎日更新なので、次の日に確認してもいいけど、ドキドキしちゃうので、テスト。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ logrotate -dv /etc/logrotate.d/httpd

reading config file /etc/logrotate.d/httpd
reading config info for /usr/local/httpd/logs/*_log
olddir is now /var/log/httpd

Handling 1 logs

rotating pattern: /usr/local/httpd/logs/*_log after 1 days (180 rotations)
olddir is /var/log/httpd, empty log files are rotated, old logs are removed
considering log /usr/local/httpd/logs/access_log
log needs rotating
rotating log /usr/local/httpd/logs/access_log, log->rotateCount is 90
glob finding old rotated logs failed
renaming /usr/local/httpd/logs/access_log to /var/log/httpd/access_log-20090103
creating new log mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg /usr/local/httpd/logs/*_log : "
/etc/init.d/httpd restart > /dev/null 2>/dev/null
"
compressing log with: /bin/gzip
running postrotate script
running script with arg /usr/local/httpd/logs/*_log : "
/etc/init.d/httpd restart > /dev/null 2>/dev/null

もし、**「log does not need rotating」**ってなったら、原因は・・・

  • 対象のログがない
  • もう今日のローテーションはしちゃった
    の2点が考えられる。1の場合は、設定ミスなのか、ログがはかれてないのか。 2番目の場合は、/var/lib/logrotate.statusを編集して、該当ログファイルの日付を編集すればOK ↓こんな感じ
1
2
3
$ vi /var/lib/logrotate.status

"/usr/local/httpd/logs/access_log" 2009-1-3 ←ここの日付をいじる。

以上で、logrotate設定完了。便利だなぁ。