nagiosでhttpでのステータチェックとアラートメールを送る
nagiosを使ってWebサービスの監視をおこなおうと思います。以下はその際に指定した設定の内容です。
前提
情報源
以下のページで情報を集めました。
"Nagios Core Documentation"
"Nagios 3翻訳プロジェクト Wiki"
"EZ-NET: CentOS 5.4 にサーバー監視ツール Nagios 3.2 をインストールする"非常に丁寧に説明されています。こちらを見れば、私のこのページは見る必要がないと思います。
"Nagios/設定 - cubic9.com"一通りの設定が記載されていたためとても参考になりました。
"http://anabuki.dip.jp/linkstation/nagios-mail.html"メール送信で悩んだ時に参考にさせてもらいました。
ポイント:シンタックスチェックはこまめにおこなう
nagiosには、-vオプションでシンタックスチェックをおこなうことができるようになっています。エラー・ワーニングを細かく出力してくれるため、設定ファイルのメンテナンス時には非常に重宝します。
設定項目の要約
- nagios.conf側に新しく追加する設定ファイルを追加します。
- 今回自分が追加する設定は、一つの設定ファイルに全てまとめます。
- sendmail用のcommand定義(host用とservice用)を作成します。
- 通知用のcontanctを纏めるcontactgroup定義を作成します。
- contact定義だけでも問題ないのですが、複数のcontactを一括で設定したいためグループを作ります。
- 通知用のcontact定義を作成します。
- 通知の設定を指定します。
- hostgroup定義を作成します。
- hostだけでも問題なかったのですが、今後拡張していく可能性があるためグループを作ります。
- host定義を作成します。
- serviceを纏めるservicegroup定義を作成します。
- serviceだけえも問題なかったのですが、今後拡張していく可能性があるためグループを作ります。
- サービス監視用のservice定義を作成します。
- チェック用のcheck_httpコマンドには、-uオプションでURLの指定と-tで接続時間を指定します。
- 異常を検知したらすぐに通知をおこなうようにmax_check_attemptsを1にしています。
- 異常発生後、こまめに通知をおこなうようにnotification_intervalを小さい値にしています。
- 通知の設定を指定します。
nagios.confへの修正
cfg_file=/usr/local/nagios/etc/objects/mysetting.cfg
新しく作成したmysetting.cfg
define command{ command_name notify-host-by-sendmail command_line /usr/bin/printf "%b" "From: $CONTACTEMAIL$\nTo: $CONTACTEMAIL$\nSubject: Host $HOSTSTATE$ alert for $HOSTNAME$!\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/sbin/sendmail $CONTACTEMAIL$ } define command{ command_name notify-service-by-sendmail command_line /usr/bin/printf "%b" "From: $CONTACTEMAIL$\nTo: $CONTACTEMAIL$\nSubject: ** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/sbin/sendmail $CONTACTEMAIL$ } define contactgroup{ contactgroup_name members alias members } define contact{ contact_name admin alias admin contactgroups members host_notification_period 24x7 host_notification_options d,r host_notification_commands notify-host-by-sendmail service_notification_period 24x7 service_notification_options w,u,c,r service_notification_commands notify-service-by-sendmail email <mail address> } define hostgroup{ hostgroup_name hosts alias hosts } define host{ use generic-host host_name host01 alias host01 address <ip address> hostgroups hosts max_check_attempts 1 contact_groups members } define host{ use generic-host host_name host02 alias host02 address <ip address> hostgroups hosts max_check_attempts 1 contact_groups members } define servicegroup{ servicegroup_name services alias services } define service{ use generic-service host_name host01 servicegroups services service_description HTTP check_command check_http!-u /status -t 5 max_check_attempts 1 check_interval 1 retry_interval 1 notification_interval 3 notification_period 24x7 notification_options w,u,c,r contact_groups members } define service{ use generic-service host_name hosts02 servicegroups services service_description HTTP check_command check_http!-u /status -t 5 max_check_attempts 1 check_interval 1 retry_interval 1 notification_interval 3 notification_period 24x7 notification_options w,u,c,r contact_groups members }