NagiosでEC2上のLinuxのログ監視(3)
はじめに
過去2回(その1、その2)で、Nagiosを使ってローカルのログを監視する所まで説明した。
今回は、NRPEというものを使ってリモートのサーバーのログを監視する。前回までで基本的な部分は出来ているので、後はそんなに難しくないはず。
今回、紛らわしいので以下の用語を使う。
- Nagiosがインストールされているマシン → 「ホスト(host)」
- ログ監視対象のマシン → 「リモート(remote)」または「リモートホスト(remote host)」。本記事ではwebサーバーを想定しているため、web.example.com というホスト名。
NRPE とは?
この図を見るのがが一番わかり易い。
必要パッケージのインストール
リモート側
リモートに、第1回で作成した check_logfiles のRPMとnrpeをインストール。nagios-* パッケージは要らないみたい。
[root@remote ~]# rpm -ivh /check_logfiles-3.4.2-1.amzn1.i386.rpm [root@remote ~]# yum install nrpe
ホスト側
先ほどの構成図を見れば分かる通り、ホスト側には check_nrpe が必要。
[root@host ~]# yum install nagios-plugins-nrpe
設定
check_logfiles の設定ファイル(リモート)
これは、基本的に前回と同じなので、説明は省略。
$MACROS = { APACHELOGDIR => '/var/log/httpd' }; @searches = ({ tag => 'phperror', logfile => '$APACHELOGDIR$/error_log', criticalpatterns => [ '.*PHP Fatal error.*', '.*\[EMERG\].*', '.*\[ALERT\].*', '.*\[CRIT\].*' ] });
まずはこれをローカルで実行してみて問題ないか確認しておく。
っと、あと、ローカルと同様、以下のように権限を変える必要がある。
[root@remote ~]# chmod 755 /var/log/httpd/
check_nrpe コマンドの設定(ホスト側)
ホスト側では check_nrpe コマンドの設定が必要。/etc/nagios/objects/commands.cfg に以下の設定を追記。
define command { command_name check_nrpe command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -t $ARG1$ -c $ARG2$ -a $ARG3$ }
check_nrpe コマンドは引数を4つ取る($ARG1$〜$ARG3$、あと$HOSTADDRESS$)。前回のエントリーを読んでもらえれば、大体分かると思うけど、次の項で軽く説明。
監視対象ホスト、サービスの定義(ホスト側)
前回は localhost.cfg というファイルにサービス定義を追加したが、今回はリモートのwebサーバーのログを監視するので、localhost.cfg を元に web.cfg というファイルを作った。
定義する内容は以下の2つ。
- リモートホストの定義
- check_nrpeコマンドを使ったサービスの定義
define host{ use linux-server ; Name of host template to use ; This host definition will inherit all variables that are defined ; in (or inherited by) the linux-server host template definition. host_name web alias web address web.example.com } define service { service_description web_log_phperror host_name web check_command check_nrpe!60!check_logfiles!/etc/nagios/$SERVICEDESC$.cfg use logfile-check }
リモートホストの定義に関しては問題無いと思う。
サービス定義の箇所を説明する。"60", "check_logfiles", "/etc/nagios/$SERVICEDESC$.cfg" が、コマンド定義の $ARG1$, $ARG2$, $ARG3$ に渡され、host_name の "web" という値が $HOSTADDRESS$ になる。
あと、ローカルのログ監視の時と同様に logfile-check というサービス定義の内容を引き継ぐ。
NRPEの設定等(リモート側)
これである程度は設定完了なんだけど、まだまだエラーが出て正しく動作しない。
以降、エラーメッセージと共に、それらの細かい設定を説明する。
Connection refused
まずはホスト側で check_nrpe を実行してみて、正しく動作するか確認する。
[root@host ~]# /usr/lib/nagios/plugins/check_nrpe -H web.example.com -c check_logfiles Connection refused by host
接続出来ないエラー。これに対しては以下の2つを行う必要がある(その他、iptables とかを使っている場合はその設定も)。
- リモートでNRPEを起動・・・(当たり前だけど、下のようなコマンドで)
- EC2のセキュリティグループの設定で、NRPEが使うポート5666を開けておく(ここでは説明省略)
[root@remote ~]# /etc/init.d/nrpe start
nrpe.cfg の設定: allowed_hosts (リモート側)
再度check_nrpeを実行してみる。
[root@host ~]# /usr/lib/nagios/plugins/check_nrpe -H web.local.retty.me -c check_logfiles CHECK_NRPE: Error - Could not complete SSL handshake.
リモート側の /var/log/messages に以下のようなログが。
Feb 16 03:57:40 ip-xx-xx-xx-xx nrpe[21952]: Allowing connections from: 127.0.0.1 Feb 16 03:57:45 ip-xx-xx-xx-xx nrpe[21955]: Host yy.yy.yy.yy is not allowed to talk to us!
Bingったところ、 /etc/nagios/nrpe.cfg に、以下の感じで接続を許可するホストを追加しないといけないらしい。
allowed_hosts=127.0.0.1,host.example.com
NRPEの設定ファイルを再読み込みしてこれは解決。
[root@remote ~]# /etc/init.d/nrpe reload Reloading nrpe [ OK ]
nrpe.cfg の設定: コマンド定義(リモート側)
再々度check_nrpeを実行してみる。
[root@host ~]# /usr/lib/nagios/plugins/check_nrpe -H web.example.com -c check_logfiles NRPE: Command 'check_logfiles' not defined
check_logfilesというコマンドがないと言われた。前回ローカルでやったのと似たような設定をする必要がある。具体的には /etc/nagios/nrpe.cfg に、以下の記述を追記する。その後NRPEをリロードする。
command[check_logfiles]=/usr/lib/nagios/plugins/check_logfiles --config $ARG1$
nrpe.cfg の設定: コマンド定義(リモート側)
もう一度挑戦。またエラーが出た。
[root@host nagios]# /usr/lib/nagios/plugins/check_nrpe -H web.example.com -c check_logfiles -a /etc/nagios/web_log_phpfatal.cfg CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages.
リモートの /var/log/messages はこんな感じ
Feb 16 05:18:12 ip-xx-xx-xx-xx nrpe[22940]: Error: Request contained command arguments, but argument option is not enabled! Feb 16 05:18:12 ip-xx-xx-xx-xx nrpe[22940]: Client request was invalid, bailing out...
これもbingったところ、以下の設定をnrpe.cfgに追加する必要があるらしい。
dont_blame_nrpe=1
/var/log/httpd の権限変更(リモート側)
前回と同様、/var/log/httpd を読む権限が無いと以下のようなエラーメッセージが出る。755 にしとけばOK。
[root@host ~]# /usr/lib/nagios/plugins/check_nrpe -H web.example.com -c check_logfiles -a /etc/nagios/web_log_phpfatal.cfg UNKNOWN - (1 unknown in web_log_phpfatal.protocol-2012-02-16-05-50-32) - could not find logfile /var/log/httpd/error_log |phpfatal_lines=0 phpfatal_warnings=0 phpfatal_criticals=0 phpfatal_unknowns=1
最後に
以上の通り check_nrpe を手動で実行して、そこで問題がなければ99%完了。これ以外にも何かエラーが出ても、検索すれば多分解決するはず。
これで、とりあえずログ監視が出来るようになった。
参考にしたページ
最初はぐぐってこのページとか参考にしたけど、結局 check_logfiles のサイトが一番参考になった。
以下、作業ログ
[root@remote ~]# yum install nrpe Loaded plugins: fastestmirror, priorities, security, update-motd Loading mirror speeds from cached hostfile * amzn-main: packages.ap-northeast-1.amazonaws.com * amzn-updates: packages.ap-northeast-1.amazonaws.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package nrpe.i686 0:2.12-16.2.amzn1 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================== Installing: nrpe i686 2.12-16.2.amzn1 amzn-main 229 k Transaction Summary ============================================================================================================================================== Install 1 Package(s) Total download size: 229 k Installed size: 293 k Is this ok [y/N]: y Downloading Packages: nrpe-2.12-16.2.amzn1.i686.rpm | 229 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : nrpe-2.12-16.2.amzn1.i686 1/1 Installed: nrpe.i686 0:2.12-16.2.amzn1 Complete! [root@remote ~]# rpm -ivh /tmp/check_logfiles-3.4.2-1.amzn1.i386.rpm Preparing... ########################################### [100%] 1:check_logfiles ########################################### [100%]