20161107012121

Nagios で通知(Notifications)の有効/無効切替を curl で実行します。

_NAGIOS="nagios.example.com"
_NAGIOS_USER="nagiosadmin"
_NAGIOS_PASS="nagiosadmin"
_HOST="localhost"

## 無効化
# Disable notifications for this host
curl -s -S http://${_NAGIOS}/nagios/cgi-bin/cmd.cgi -u ${_NAGIOS_USER}:${_NAGIOS_PASS} -d "cmd_mod=2&cmd_typ=25&host=${_HOST}" \
| grep 'Message'

# Disable notifications for all services on this host
curl -s -S http://${_NAGIOS}/nagios/cgi-bin/cmd.cgi -u ${_NAGIOS_USER}:${_NAGIOS_PASS} -d "cmd_mod=2&cmd_typ=29&host=${_HOST}" \
| grep 'Message'

## 有効化
# Enable notifications for this host
#curl -s -S http://${_NAGIOS}/nagios/cgi-bin/cmd.cgi -u ${_NAGIOS_USER}:${_NAGIOS_PASS} -d "cmd_mod=2&cmd_typ=24&host=${_HOST}" | grep 'Message'

# Enable notifications for all services on this host
#curl -s -S http://${_NAGIOS}/nagios/cgi-bin/cmd.cgi -u ${_NAGIOS_USER}:${_NAGIOS_PASS} -d "cmd_mod=2&cmd_typ=28&host=${_HOST}" | grep 'Message'

経緯

WebUI での設定は対象台数が増加すると少々厳しくなります。 また、コマンドファイルへコマンドを渡すことでも実行可能ですが、nagiosサーバへのログインを行わずに外部から切替を行いたいと思いました。
Nagios に API は無いようですが、WebUI から実行できるのだから、curl でも実行できるだろうという所です。
実行内容は WebUI で Enable|Disable notifications 〜 のリンクをクリックした場合と同等です。

20161107012122

環境

WebUI で同様の操作が実行できることが前提条件です。
外部コマンド で実現しており、その有効か設定が必要です。Version 4.0.8 ではインストール時点で有効化されていました。

  • 外部コマンド
    設定値としては check_external_commands を有効にします。 command_check_interval は明記しない場合、デフォルト 60 秒です。
check_external_commands=1
command_file=/var/spool/nagios/cmd/nagios.cmd 

パラメータ

cmd_mod

“2” を指定で WebUI 上の Commit をクリックした状態になります。 言い換えると指定しないと実行されません。

cmd_typ

コマンドです。 外部コマンドIDにそれぞれ番号が割り振りされており、その番号をしていします。 WebUI 上のそれぞれのコマンドのリンクから確認できます。
通知有効無効に関わる cmd_typ は以下になります。

cmd_typ Command ID Link Name
24 ENABLE_HOST_NOTIFICATIONS Enable notifications for this host
25 DISABLE_HOST_NOTIFICATIONS Disable notifications for this host
28 ENABLE_HOST_SVC_NOTIFICATIONS Enable notifications for all services on this host
29 DISABLE_HOST_SVC_NOTIFICATIONS Disable notifications for all services on this host

host

Nagios 上で定義済みの一意な host_name です。

返却値

WebUI での実行同様の HTML が返却されます。

20161107012123

成功すると infoMessage として以下が返されます。

Your command request was successfully submitted to Nagios for processing.

失敗した場合 errorMessage として返されます。

例) 認証失敗
Sorry, but you are not authorized to commit the specified command.
例) nagios プロセス未起動
Error: Could not stat() command file

通常はダウンタイム設定で行いたい所ですが、メンテナンス作業等で再開時間が定まらない場合などで有用と思います。

元記事はこちら

Nagios 通知有効/無効を curl で切替