◆ 目的
nagiosで分散監視を実施する。
◆ 簡易構成図
nagios ← 中央サーバ(親)
|
nagios nagios nagios … ← 集約サーバ群(子)
◆ 監視項目、役割
● 中央サーバ(親)
集約サーバ(子)からの通知を受け付ける受動監視に徹する。
親側ではホスト、サービス監視共に能動的に監視をさせないということである。
発報条件として試行回数(Attempt)は1/1にする。
● 集約サーバ(子)
以下を受け持つ。
・能動監視
・受動監視
・TRAP監視(nagiosとの連携方法についてはこちらを参照のこと)
能動監視はnotificationの機能を使い、結果をsend_nscaコマンドで親側へ通知する。
notificationの発動条件(能動、受動、TRAP監視用のテンプレートは別途説明する)。
・ハードエラー時
試行回数を3/3にしておけば、3回目のNG時にnotificationは発動する。
また、復旧時は1/3でnotificationは動作する。
・能動監視時はハードエラーが継続する場合は、
一度アラームがあがったらステータスが変化しない限り通知させない。
検知漏れが怖いなら設定ディフォルトの240分(notification_interval)後に
再通知でもいいだろう。
・受動監視時は試行回数を1/1にしておけば、毎回1回目のNG時にnotificationが発動する。
ステータスが変化しない場合にも発動させる(is_volatileは有効に)。
・HOSTがダウンするとサービス監視は実施されなくなる。
監視が動作しないと慌てないこと。
TRAP監視はnagiosのイベントハンドラ(event_handler)の機能を使い親側へ通知する。
event_handler経由で実行するコマンドは上記と同じくsend_nscaである。
notificationを使わない理由だが、TRAPが複数上がった場合に、
前のアラームが上書きされ、それに気がつかない事態を防ぎたいためである。
TRAP監視のステータスは基本warningとし、warningの状態時に、
再度アラームがTRAPを受けた場合は、ステータスをcriticalとして親側に再通知させる。
イベントハンドラコマンドが実行されるタイミングを確認しておく。
・ソフトエラー状態にある時
・最初にハードエラー状態になった時
・ソフト又はハードエラー状態から復旧する時
ソフトエラーの状態でも実行される点がnotificationと異なる。
サービス名を、TRAP [hoge]、TRAP [piyo]などと分けてもいいのだが、監視対象数は増やしたくない。
nagisoはステータスの管理をデータベースではなくファイルでやっているためである。
TRAPとアラームは一つにし、ステータスのインフォメーションに内容を記載すればいいだろう。
◆ 前提
親側、子側に以下のパッケージはインストール済みとする。
・nagios
・nagios-devel
・nagios-www
・nagios-plugins
・nagios-nsca ← 分散監視に利用する
◆ 親・子nagios共通設定
● /etc/nagios/nagios.cfg
#設定ファイルはconf.d配下に独自に用意する #cfg_file=/etc/nagios/objects/commands.cfg #cfg_file=/etc/nagios/objects/contacts.cfg #cfg_file=/etc/nagios/objects/timeperiods.cfg #cfg_file=/etc/nagios/objects/templates.cfg #cfg_file=/etc/nagios/objects/localhost.cfg cfg_dir=/etc/nagios/conf.d # ステータスファイルはメモリ上におきIO処理を高速化させる status_file=/dev/shm/status.dat # command_fileはディフォルトのままでいいだろうが、変更するなら確認必要 command_file=/var/log/nagios/rw/nagios.cmd
● /etc/nagios/cgi.cfg
# 認証関係の設定 authorized_for_system_information=* authorized_for_configuration_information=* authorized_for_system_commands=* authorized_for_all_services=* authorized_for_all_hosts=* authorized_for_all_service_commands=* authorized_for_all_host_commands=* # 音声ファイルの設定 host_unreachable_sound=hostdown.wav host_down_sound=hostdown.wav service_critical_sound=critical.wav service_warning_sound=warning.wav service_unknown_sound=warning.wav normal_sound=noproblem.wav # 監視の結果にhtmlタグを出力させる可能性があるためエスケープはしない escape_html_tags=0
◆ 親側nagiosの設定
● /etc/nagios/nagios.cfg
# nagios起動時のサービス確認は親側は不要 execute_service_checks=0 # パッシブホストチェック、パッシブサービスチェックを有効化 # 子側からの通知を受け取るため accept_passive_service_checks=1 accept_passive_host_checks=1 # 外部コマンドを有効化 # 親、子どちらかだけで外部コマンドを実行できるポリシーにしてもよい check_external_commands=1
● /etc/nagios/conf.d/base/hosts.cfg
基本となるホストの設定を記載する。
赤色は子側との設定の相違である。
define host {
name nsca-host
active_checks_enabled 0
passive_checks_enabled 1
notifications_enabled 1
event_handler_enabled 0
flap_detection_enabled 0
process_perf_data 0
retain_status_information 1
retain_nonstatus_information 1
check_period none
max_check_attempts 1
contact_groups admins
notification_options n
register 0
}
check_periodがnoneの理由を補足しておく。check_freshnessが1ならfreshness_reportが動いてしまう。
● /etc/nagios/conf.d/base/services.cfg
基本となるサービスの設定を記載する
define service{
name nsca-service
active_checks_enabled 0
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 0
check_freshness 0
notifications_enabled 1
event_handler_enabled 0
flap_detection_enabled 0
process_perf_data 0
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period none
max_check_attempts 1
contact_groups admins
notification_options n
notification_interval 240
notification_period 24x7
check_command no_report
register 0
}
● /etc/nagios/conf.d/service/services.cfg
各種サービスの設定を記載する。
手順の簡略化のため、ホストグループ、ホストの設定ファイルも
ここへあわせて記載する。当然管理のため分けてもよい。
define hostgroup {
hostgroup_name frontend_service
alias frontend_service
}
define hostgroup {
hostgroup_name backend_service
alias backend_service
}
define host {
use nsca-host
host_name host1
hostgroups frontend_service
}
define host {
use nsca-host
host_name host2
hostgroups backend_service
}
define service {
use nsca-service
host_name host1, host2
service_description RESOURCE [load average]
}
define service{
use nsca-service
host_name host1, host2
service_description PASSIVE [log]
}
define service {
use nsca-service
host_name host1, host2
service_description TRAP
}
● /etc/nagios/nsca.cfg
子側からの通知を受け付けるデーモンの設定だが、
変更するとしたらポート番号と、nagios.cfgで指定したcommand_fileと
同じ場所を指すようにそこを見直すぐらいだろうか。
server_port=15667 command_file=/var/log/nagios/rw/nagios.cmd
◆ 子側nagiosの設定
● /etc/nagios/nagios.cfg
# nagios起動時のサービス確認は子側は必要 execute_service_checks=1 # パッシブホストチェック、パッシブサービスチェックを有効化 # 子側であっても監視対象ノードから受動的に結果を受け取ることがある accept_passive_service_checks=1 accept_passive_host_checks=1 # 外部コマンドを有効 # 親、子どちらかだけで外部コマンドを実行できるポリシーにしてもよい check_external_commands=1 # obsess over ホストとサービスは使わない(ディフォルト0) # obsessを利用した分散環境を作らないため明示的に0としておく obsess_over_hosts=0 obsess_over_services=0 # ochp(Obsessive Compulsive Host Processor)コマンドは使わない #ochp_command=somecommand # ocsp(Obsessive Compulsive Service Processor)コマンドは使わない #ocsp_command=somecommand
● /etc/nagios/conf.d/base/hosts.cfg
基本となるホストの設定を記載する。
赤色は親側との設定の相違である。
define host {
name active-host
notifications_enabled 1
event_handler_enabled 0
flap_detection_enabled 0
process_perf_data 0
retain_status_information 1
retain_nonstatus_information 1
check_period 24x7
max_check_attempts 3
normal_check_interval 2
retry_check_interval 1
contact_groups nscas
notification_options d,u,r
notification_interval 240
notification_period 24x7
check_command check-host-alive
register 0
}
● /etc/nagios/conf.d/base/services.cfg
緑色は子側内の設定との相違点である。
define service {
name active-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 0
check_freshness 0
notifications_enabled 1
event_handler_enabled 0
flap_detection_enabled 0
process_perf_data 0
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 4
retry_check_interval 1
contact_groups nscas
notification_options u,w,c,r
notification_interval 240
notification_period 24x7
register 0
}
define service {
name passive-service
active_checks_enabled 0
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 0
check_freshness 0
notifications_enabled 1
event_handler_enabled 0
flap_detection_enabled 0
process_perf_data 0
retain_status_information 1
retain_nonstatus_information 1
is_volatile 1
check_period none
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups nscas
notification_options u,w,c,r
notification_interval 240
notification_period 24x7
check_command check_dummy!0!"OK"
register 0
}
define service {
name trap-service
active_checks_enabled 0
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 0
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 0
process_perf_data 0
retain_status_information 1
retain_nonstatus_information 1
is_volatile 1
check_period none
max_check_attempts 2
normal_check_interval 1
retry_check_interval 1
contact_groups nscas
notification_options none
notification_interval 240
notification_period 24x7
register 0
}
● /etc/nagios/conf.d/service/services.cfg
各種サービス固有の設定を記載する
親側での設定と同様に手順の簡略化のため、ホストグループ、ホストの
設定ファイルもここへあわせて記載する。
define hostgroup {
hostgroup_name frontend_service
alias frontend_service
}
define hostgroup {
hostgroup_name backend_service
alias backend_service
}
define host {
use active-host
host_name host1
address 10.0.0.1
hostgroups frontend_service
}
define host {
use active-host
host_name host2
address 10.0.0.2
hostgroups backend_service
}
define service {
use active-service
host_name host1, host2
service_description RESOURCE [load average]
check_command check_loadaverage!30,30,30!50,50,50
}
define service{
use passive-service
host_name host1, host2
service_description PASSIVE [log]
check_command check_dummy!0!"OK"
}
define service{
use trap-service
host_name host1, host2
service_description TRAP
event_handler notify-service-by-send_nsca_for_trap
check_command check_dummy!0!"OK"
}
● /etc/nagios/conf.d/base/contacts.cfg
試行回数をを3/3にしておけば、3回目のNG時にnotificationが発動する。
また、復旧時は1/3でnotificationは動く。
HOSTがダウンするとサービス監視は実施されなくなるため、
監視が動作しないと慌てないこと。
define contact {
name nsca-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-send_nsca
host_notification_commands notify-host-by-send_nsca
register 0
}
define contact {
contact_name nscaadmin
use nsca-contact
alias Nsca Admin
}
define contactgroup {
contactgroup_name nscas
alias Nagios Nacas
members nscaadmin
}
● /etc/nagios/conf.d/base/commands.cfg
# for notification
define command {
command_name notify-host-by-send_nsca
command_line /usr/lib/nagios/plugins/submit_check_result_host $HOSTNAME$ $HOSTSTATEID$ '$HOSTOUTPUT$'
}
define command {
command_name notify-service-by-send_nsca
command_line /usr/lib/nagios/plugins/submit_check_result_service $HOSTNAME$ '$SERVICEDESC$' $SERVICESTATE$ '$SERVICEOUTPUT$'
}
# for event handler
define command {
command_name notify-service-by-send_nsca_for_trap
command_line /usr/lib/nagios/plugins/submit_check_result_service_for_trap $HOSTNAME$ '$SERVICEDESC$' $SERVICESTATE$ '$SERVICEOUTPUT$' $SERVICESTATETYPE$
}
【補足】
親側にnagiosではなくOVOを使う場合は、
syslogに結果を吐かせればいいだろう。
define command {
command_name notify-host-by-logger
command_line /bin/logger -p local6.info "Nagios `/usr/bin/printf \"%b\" \"[SERVICE NAME] Type: $NOTIFICATIONTYPE$, Host: $HOSTNAME$, State: $HOSTSTATE$, Address: $HOSTADDRESS$, Info: $HOSTOUTPUT$, Date/Time: $LONGDATETIME$\"`"
}
define command {
command_name notify-service-by-logger
command_line /bin/logger -p local6.info "Nagios `/usr/bin/printf \"%b\" \"[SERVICE NAME] Type: $NOTIFICATIONTYPE$, Host: $HOSTNAME$, Service: $SERVICEDESC$, State: $SERVICESTATE$, Date/Time: $LONGDATETIME$, Additional Info: $SERVICEOUTPUT$\"`"
}
● /usr/lib/nagios/eventhandlers/submit_check_result_host
#!/bin/sh return_code=3 case "$2" in UP) return_code=0 ;; DOWN) return_code=1 ;; UNREACHEBLE) return_code=2 ;; UNKNOWN) return_code=3 ;; esac #/usr/bin/printf "%s\t%s\t%s\n" "$1" "$2" "$3" >> /var/tmp/debug_host /usr/bin/printf "%s\t%s\t%s\n" "$1" "$2" "$3" | /usr/sbin/send_nsca $親サーバのIPアドレス -c /etc/nagios/send_nsca.cfg
実行権限を与えておくこと。
# chmod 755 /usr/lib/nagios/eventhandlers/submit_check_result_host
● /usr/lib/nagios/eventhandlers/submit_check_result_service
#!/bin/sh return_code=3 case "$3" in OK) return_code=0 ;; WARNING) return_code=1 ;; CRITICAL) return_code=2 ;; UNKNOWN) return_code=3 ;; esac #/usr/bin/printf "%s\t%s\t%s\t%s\n" "$1" "$2" "$return_code" "$4" >> /var/tmp/debug_service /usr/bin/printf "%s\t%s\t%s\t%s\n" "$1" "$2" "$return_code" "$4" | /usr/sbin/send_nsca $親IP -c /etc/nagios/send_nsca.cfg
実行権限を与えておくこと。
# chmod 755 /usr/lib/nagios/eventhandlers/submit_check_result_service
● /usr/lib/nagios/eventhandlers/submit_check_result_service_for_trap
#!/bin/sh return_code=3 case "$3" in OK) return_code=0 ;; WARNING) return_code=1 ;; CRITICAL) return_code=2 ;; UNKNOWN) return_code=3 ;; esac if [ $return_code == 0 ];then /usr/bin/printf "%s\t%s\t%s\t%s\n" "$1" "$2" 0 "$4" | /usr/sbin/send_nsca $親IP -c /etc/nagios/send_nsca.cfg else case "$5" in SOFT) /usr/bin/printf "%s\t%s\t%s\t%s\n" "$1" "$2" 1 "$4" | /usr/sbin/send_nsca $親IP -c /etc/nagios/send_nsca.cfg ;; HARD) /usr/bin/printf "%s\t%s\t%s\t%s\n" "$1" "$2" 2 "$4" | /usr/sbin/send_nsca $親IP -c /etc/nagios/send_nsca.cfg ;; esac fi
実行権限を与えておくこと。
# chmod 755 /usr/lib/nagios/eventhandlers/submit_check_result_service_for_trap
◆ nagiosのコンフィグ確認
親・子共に実施
# /usr/sbin/nagios -v /etc/nagios/nagios.cfg
◆ nagiosの起動
親・子共に実施する。
# /etc/init.d/nagios start
親だけで実施する。
# /etc/init.d/nsca start