・当ブログはアフィリエイト広告を利用しています。

httpdのログ・ローテートができない

CentOS
xresch / Pixabay
この記事は約17分で読めます。
記事内には広告が含まれています。

以前から気が付いていたのだが、httpdのログがローテートせずに肥大化している。
大きなもので40MB。1日に何万、何十万アクセスがあるようなサーバーではなく、細々とした個人のブログサーバーなので、見る人にとっては気にする必要ないのかもしれないが、容量が増える一方なので、非常に気になっていた。

httpdのログリスト(標準)

-rw-r–r–. 1 root root 19889716 7月 21 03:26 access_log
-rw-r–r–. 1 root root 6593712 7月 19 22:20 error_log
-rw-r–r–. 1 root root 34760842 7月 21 03:30 ssl_access_log
-rw-r–r–. 1 root root 355361 7月 21 00:04 ssl_error_log
-rw-r–r–. 1 root root 43657279 7月 21 03:30 ssl_request_log※上記他に過去のログもあるが、日付が2018年のものばかりで、ログのローテートが行われていない

でも、今までhttp周りの設定作業等を行うと必ずと言っていいほど、blogの運用が止まってしまうので、変更を躊躇(ちゅうちょ)していた。

スポンサーリンク

結論から言うと(まだ、不具合あり)

/etc/logrotate.d/httpd 内のlogのパスが間違っていたようだ

修正箇所は1行目の httpd24の部分をhttpd に変えただけ。

修正前

# cat /etc/logrotate.d/httpd
/var/log/httpd24/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/sbin/service httpd24-httpd reload > /dev/null 2>/dev/null || true
endscript
}

解説

自宅サーバーはCentOS7に標準のhttpd(Apache2.2)をインストールし、後(のち)にhttpd24(Apatche2.4)をインストールしている。
Apatche2.4にしてたぶんしばらくはうまくログ・ローテートされていたかと思う。しかし、Apache2.4を再インストールしたり、色々いじっているうちに、ログが、ローテートしなくなってしまったようだ。

解決の糸口

httpd24(Apache2.4)は標準のデイレクトリに全て上書きされるのではなく、一部別の場所にインストールされたり、ログのパスが変わった?ようだ。

/var/log/httpd24 内のログリスト

-rw-r–r–. 1 root root 0 6月 2 03:20 access_log
-rw-r–r–. 1 root root 99487 4月 3 14:21 access_log-20190407
-rw-r–r–. 1 root root 945 4月 18 19:27 access_log-20190419
-rw-r–r–. 1 root root 141356 5月 26 03:39 access_log-20190526
-rw-r–r–. 1 root root 13423 5月 26 13:42 access_log-20190602
-rw-r–r–. 1 root root 0 6月 9 03:14 error_log
-rw-r–r–. 1 root root 3336 4月 18 19:27 error_log-20190419
-rw-r–r–. 1 root root 10755 5月 26 03:40 error_log-20190526
-rw-r–r–. 1 root root 10051 5月 29 21:54 error_log-20190602
-rw-r–r–. 1 root root 1415 6月 5 23:50 error_log-20190609
-rw-r–r–. 1 root root 0 6月 9 03:14 ssl_access_log
-rw-r–r–. 1 root root 8566 4月 18 19:14 ssl_access_log-20190419
-rw-r–r–. 1 root root 8411 5月 26 03:32 ssl_access_log-20190526
-rw-r–r–. 1 root root 56133 5月 26 13:38 ssl_access_log-20190602
-rw-r–r–. 1 root root 242 6月 5 23:55 ssl_access_log-20190609
-rw-r–r–. 1 root root 0 6月 9 03:14 ssl_error_log
-rw-r–r–. 1 root root 547 4月 18 19:08 ssl_error_log-20190419
-rw-r–r–. 1 root root 1704 5月 26 02:09 ssl_error_log-20190526
-rw-r–r–. 1 root root 6061 5月 29 21:54 ssl_error_log-20190602
-rw-r–r–. 1 root root 622 6月 5 23:50 ssl_error_log-20190609
-rw-r–r–. 1 root root 0 6月 9 03:14 ssl_request_log
-rw-r–r–. 1 root root 12374 4月 18 19:14 ssl_request_log-20190419
-rw-r–r–. 1 root root 11305 5月 26 03:32 ssl_request_log-20190526
-rw-r–r–. 1 root root 82613 5月 26 13:38 ssl_request_log-20190602
-rw-r–r–. 1 root root 322 6月 5 23:55 ssl_request_log-20190609

こちらの方のログは2019.6.02~2019.06.09を最後に過去4回分ログが残っている。でも、1か月おきでもなく、1週間おきでもない。

最終のログは、正式なパスで、途中、過去のログがhttp24のパスで保管されるようなイメージだ。
どうも、この辺りの、パスの指定がおかしいのではないかと疑った。

で、Googleさんの出番

通常、ログ・ローテートの設定は/etc/logrotate.d 内のファルダにサービス毎に記述する形式になる。

httpdの場合は/etc/logrotate.d/httpd に記述されている。
1行目をみてみると
/var/log/httpd24/*log {

普通に考えると、こちらの方
/var/log/httpd/*log {

で修正を行った。

ログ・ローテートの状況は/var/lib/logrotate/logrotate.status ファイルに記録される
中身を見ると、httpdとhttpd24が混在していて、使っていないはずの他のサービスも残ってしまってわかりにくい。

# cat /var/lib/logrotate/logrotate.status     一部抜粋
logrotate state — version 2
“/var/log/httpd24/ssl_access_log” 2019-6-9-3:14:6
“/var/log/httpd24/error_log” 2019-6-9-3:14:6
“/var/log/httpd/ssl_access_log” 2019-6-16-3:13:7
“/var/log/httpd24/access_log” 2019-6-2-3:20:6
“/var/log/httpd/*log” 2018-10-10-16:0:0
“/var/log/httpd/error_log” 2019-6-16-3:13:7
“/var/log/httpd/ssl_error_log” 2019-6-16-3:13:7
“/var/log/httpd24/*log” 2018-10-30-3:0:0
“/var/log/httpd24/ssl_request_log” 2019-6-9-3:14:6
“/var/log/httpd24/ssl_error_log” 2019-6-9-3:14:6
“/var/log/httpd/ssl_request_log” 2019-6-16-3:13:7
“/var/log/httpd/access_log” 2019-6-16-3:13:7

 

httpdを再起動すると、

/var/log/httpd 内のログリスト

-rw-r–r–. 1 root root 0 7月 23 03:26 access_log
-rw-r–r–. 1 root root 0 7月 23 03:26 error_log
-rw-r–r–. 1 root root 0 7月 23 03:26 ssl_access_log
-rw-r–r–. 1 root root 0 7月 23 03:26 ssl_error_log
-rw-r–r–. 1 root root 0 7月 23 03:26 ssl_request_log
-rw-r–r–. 1 root root 361774 7月 23 06:08 ssl_error_log-20190723
-rw-r–r–. 1 root root 6790507 7月 23 02:16 error_log-20190723
-rw-r–r–. 1 root root 20182606 7月 23 06:32 access_log-20190723
-rw-r–r–. 1 root root 35454852 7月 23 06:55 ssl_access_log-20190723
-rw-r–r–. 1 root root 44508055 7月 23 06:55 ssl_request_log-20190723

あれ!正式のログが0バイトに!

過去のログが新しくできたが時刻から見ると、刻々と時刻が変わる。

過去じゃなくて、これ(日付付きログ)が最新ログとなってしまってる。これでは困る!

仕方がないので

ファイル名に日付の付いているlogを念のため別の場所に移動して削除した。

(例)
#mkdir log_bak
#mv -i *log-20190723 log_bak

確認してみる。

/var/log/httpd 内のログリスト

-rw-r–r–. 1 root root 3599 7月 25 10:28 access_log
-rw-r–r–. 1 root root 1127 7月 25 09:37 error_log
drwxr-xr-x. 2 root root 4096 7月 25 09:37 log_bak
-rw-r–r–. 1 root root 34964 7月 25 10:35 ssl_access_log
-rw-r–r–. 1 root root 0 7月 23 03:26 ssl_error_log
-rw-r–r–. 1 root root 45062 7月 25 10:35 ssl_request_log

うまくいったようだが、ローテートの方はまだだ。

/var/lib/logrotate/logrotate.status 内のファイル名の日付部を手作業で変更(6ファイル分)して、-dv オプションで確認してみる

 

#logrotate -dv /etc/logrotate.conf

reading config file /etc/logrotate.d/httpd
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/httpd/*log weekly (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
log needs rotating
considering log /var/log/httpd/error_log
log needs rotating
considering log /var/log/httpd/ssl_access_log
log needs rotating
considering log /var/log/httpd/ssl_error_log
log does not need rotating (log is empty)considering log /var/log/httpd/ssl_request_log
log needs rotating
rotating log /var/log/httpd/access_log, log->rotateCount is 4
dateext suffix ‘-20190725
glob pattern ‘-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
renaming /var/log/httpd/access_log.4 to /var/log/httpd/access_log.5 (rotatecount 4, logstart 1, i 4),
renaming /var/log/httpd/access_log.3 to /var/log/httpd/access_log.4 (rotatecount 4, logstart 1, i 3),
renaming /var/log/httpd/access_log.2 to /var/log/httpd/access_log.3 (rotatecount 4, logstart 1, i 2),
renaming /var/log/httpd/access_log.1 to /var/log/httpd/access_log.2 (rotatecount 4, logstart 1, i 1),
renaming /var/log/httpd/access_log.0 to /var/log/httpd/access_log.1 (rotatecount 4, logstart 1, i 0),
rotating log /var/log/httpd/error_log, log->rotateCount is 4
dateext suffix ‘-20190725’
glob pattern ‘-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]’
renaming /var/log/httpd/error_log.4 to /var/log/httpd/error_log.5 (rotatecount 4, logstart 1, i 4),
renaming /var/log/httpd/error_log.3 to /var/log/httpd/error_log.4 (rotatecount 4, logstart 1, i 3),
renaming /var/log/httpd/error_log.2 to /var/log/httpd/error_log.3 (rotatecount 4, logstart 1, i 2),
renaming /var/log/httpd/error_log.1 to /var/log/httpd/error_log.2 (rotatecount 4, logstart 1, i 1),
renaming /var/log/httpd/error_log.0 to /var/log/httpd/error_log.1 (rotatecount 4, logstart 1, i 0),
rotating log /var/log/httpd/ssl_access_log, log->rotateCount is 4
dateext suffix ‘-20190725’
glob pattern ‘-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]’
renaming /var/log/httpd/ssl_access_log.4 to /var/log/httpd/ssl_access_log.5 (rotatecount 4, logstart 1, i 4),
renaming /var/log/httpd/ssl_access_log.3 to /var/log/httpd/ssl_access_log.4 (rotatecount 4, logstart 1, i 3),
renaming /var/log/httpd/ssl_access_log.2 to /var/log/httpd/ssl_access_log.3 (rotatecount 4, logstart 1, i 2),
renaming /var/log/httpd/ssl_access_log.1 to /var/log/httpd/ssl_access_log.2 (rotatecount 4, logstart 1, i 1),
renaming /var/log/httpd/ssl_access_log.0 to /var/log/httpd/ssl_access_log.1 (rotatecount 4, logstart 1, i 0),
rotating log /var/log/httpd/ssl_request_log, log->rotateCount is 4
dateext suffix ‘-20190725’
glob pattern ‘-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]’
renaming /var/log/httpd/ssl_request_log.4 to /var/log/httpd/ssl_request_log.5 (rotatecount 4, logstart 1, i 4),
renaming /var/log/httpd/ssl_request_log.3 to /var/log/httpd/ssl_request_log.4 (rotatecount 4, logstart 1, i 3),
renaming /var/log/httpd/ssl_request_log.2 to /var/log/httpd/ssl_request_log.3 (rotatecount 4, logstart 1, i 2),
renaming /var/log/httpd/ssl_request_log.1 to /var/log/httpd/ssl_request_log.2 (rotatecount 4, logstart 1, i 1),
renaming /var/log/httpd/ssl_request_log.0 to /var/log/httpd/ssl_request_log.1 (rotatecount 4, logstart 1, i 0),
fscreate context set to system_u:object_r:httpd_log_t:s0
renaming /var/log/httpd/access_log to /var/log/httpd/access_log.1
fscreate context set to system_u:object_r:httpd_log_t:s0
renaming /var/log/httpd/error_log to /var/log/httpd/error_log.1
fscreate context set to system_u:object_r:httpd_log_t:s0
renaming /var/log/httpd/ssl_access_log to /var/log/httpd/ssl_access_log.1
fscreate context set to system_u:object_r:httpd_log_t:s0
renaming /var/log/httpd/ssl_request_log to /var/log/httpd/ssl_request_log.1
running postrotate script
running script with arg /var/log/httpd/*log : ”
/sbin/service httpd24-httpd reload > /dev/null 2>/dev/null || true

removing old log /var/log/httpd/access_log.5
error: error opening /var/log/httpd/access_log.5: そのようなファイルやディレクトリはありません

ローテートできてるような結果だ。
強制的にログ・ローテートしてみる。
# logrotate -f /etc/logrotate.conf

どうやら、うまく行ったようだ。

/var/log/httpd 内のログリスト

-rw-r–r–. 1 root root 302 7月 25 10:46 access_log
-rw-r–r–. 1 root root 3830 7月 25 10:41 access_log-20190725
-rw-r–r–. 1 root root 1127 7月 25 10:45 error_log
-rw-r–r–. 1 root root 1244 7月 25 10:43 error_log-20190725
drwxr-xr-x. 2 root root 4096 7月 25 09:37 log_bak
-rw-r–r–. 1 root root 10254 7月 25 11:04 ssl_access_log
-rw-r–r–. 1 root root 37085 7月 25 10:38 ssl_access_log-20190725
-rw-r–r–. 1 root root 0 7月 23 03:26 ssl_error_log
-rw-r–r–. 1 root root 13580 7月 25 11:04 ssl_request_log
-rw-r–r–. 1 root root 47799 7月 25 10:38 ssl_request_log-20190725

コメント

タイトルとURLをコピーしました