developers blog

さくら VPS でログを監視する (CentOS7 + nginx + Logwatch)

はじめに

さくらの VPS (CentOS7 + nginx) の環境に Logwatch を入れてログを監視します。

注意事項

コマンドはすべて root 権限での操作です。

環境

  • VPS : さくらVPS 1G SSD
  • OS : CentOs 7.3
  • その他 : nginx/1.10.2, Logwatch 7.4.0

Logwatch とは

カスタマイズ可能な、ログモニタリングシステムで、一定期間のログのレポートを作成してくれます。

Logwatch のインストール

yum コマンドでパッケージをインストールします。

1# yum install logwatch

Logwatch の設定

logwatch の設定ファイルを変更する場合は、/usr/share/logwatch/default.conf/logwatch.conf の内容から、変更したい箇所を /etc/logwatch/conf/logwatch.conf へ追記して修正します。
メールの宛先を変更する場合は、以下のように追記します。

1# vi /etc/logwatch/conf/logwatch.conf
logwatch.conf
1MailTo = xxx@goodlife.tech

ログレポートの確認

ログレポートを標準出力に出力するには、何も指定せずに実行するか、--output stdout と指定して実行します。デフォルトの出力先は logwatch.conf で設定されていて、stdout です。

1# logwatch
2# logwatch --output stdout

サービスを指定してログを表示する場合は、--service オプションを指定します。cron のログを表示する場合は以下の通りです。

1# logwatch --service cron

nginx 用の設定

logwatch には、標準で nginx 用の設定ファイルが存在しないため、httpd 用の設定ファイルをコピーして作成します。

1cp /usr/share/logwatch/default.conf/logfiles/http.conf /etc/logwatch/conf/logfiles/nginx.conf
2cp /usr/share/logwatch/default.conf/services/http.conf /etc/logwatch/conf/services/nginx.conf
3cp /usr/share/logwatch/scripts/services/http /etc/logwatch/scripts/services/nginx
4
5cp /usr/share/logwatch/default.conf/logfiles/http-error.conf /etc/logwatch/conf/logfiles/nginx-error.conf
6cp /usr/share/logwatch/default.conf/services/http-error.conf /etc/logwatch/conf/services/nginx-error.conf
7cp /usr/share/logwatch/scripts/services/http-error /etc/logwatch/scripts/services/nginx-error

設定ファイルを変更する

nginx のサービス設定ファイルを以下のように変更します。

1# vi /etc/logwatch/conf/services/nginx.conf
/etc/logwatch/conf/services/nginx.conf
1- Title = "httpd"
2+ Title = "nginx"
3
4- LogFile = http
5+ LogFile = nginx

nginx のログファイルの形式を設定します。

1# vi /etc/logwatch/conf/logfiles/nginx.conf
/etc/logwatch/conf/logfiles/nginx.conf
1- LogFile = httpd/*access_log
2- LogFile = apache/*access.log.1
3- LogFile = apache/*access.log
4- LogFile = apache2/*access.log.1
5- LogFile = apache2/*access.log
6- LogFile = apache2/*access_log
7- LogFile = apache-ssl/*access.log.1
8- LogFile = apache-ssl/*access.log
9+ LogFile = /var/log/nginx/access.log
10
11- Archive = archiv/httpd/*access_log.*
12- Archive = httpd/*access_log.*
13- Archive = apache/*access.log.*.gz
14- Archive = apache2/*access.log.*.gz
15- Archive = apache2/*access_log.*.gz
16- Archive = apache-ssl/*access.log.*.gz
17- Archive = archiv/httpd/*access_log-*
18- Archive = httpd/*access_log-*
19- Archive = apache/*access.log-*.gz
20- Archive = apache2/*access.log-*.gz
21- Archive = apache2/*access_log-*.gz
22- Archive = apache-ssl/*access.log-*.gz
23+ Archive = /var/log/nginx/access.log-*.gz

nginx-error のサービス設定ファイルを以下のように変更します。

1# vi /etc/logwatch/conf/services/nginx-error.conf
/etc/logwatch/conf/services/nginx-error.conf
1- Title = http errors
2+ Title = nginx errors
3
4- LogFile = http-error
5+ LogFile = nginx-error

nginx-error のログファイルの形式を設定します。

1# vi /etc/logwatch/conf/logfiles/nginx-error.conf
/etc/logwatch/conf/logfiles/nginx-error.conf
1- LogFile = httpd/*error_log
2- LogFile = apache/*error.log.1
3- LogFile = apache/*error.log
4- LogFile = apache2/*error.log.1
5- LogFile = apache2/*error.log
6- LogFile = apache2/*error_log
7- LogFile = apache-ssl/*error.log.1
8- LogFile = apache-ssl/*error.log
9+ LogFile = /var/log/nginx/error.log
10
11- Archive = archiv/httpd/*error_log.*
12- Archive = httpd/*error_log.*
13- Archive = apache/*error.log.*.gz
14- Archive = apache2/*error.log.*.gz
15- Archive = apache2/*error_log.*.gz
16- Archive = apache-ssl/*error.log.*.gz
17- Archive = varnish/*error.log.*.gz
18- Archive = archiv/httpd/*error_log-*
19- Archive = httpd/*error_log-*
20- Archive = apache/*error.log-*.gz
21- Archive = apache2/*error.log-*.gz
22- Archive = apache2/*error_log-*.gz
23- Archive = apache-ssl/*error.log-*.gz
24+ Archive = /var/log/nginx/error.log-*.gz

Logwatch を実行して、nginx のログが出力されれば成功です。

1# logwatch --output stdout --service nginx
2
3--------------------- nginx Begin ------------------------
4(略)
5---------------------- nginx End -------------------------