プロジェクト毎にファイルを分ける用Nagios運用方法の一例

Nagiosの設定ファイルは、標準だと contacts.cfg だとか commands.cfg だとか名前が付いている割に、結局は cfg_file や cfg_dir で読み込まれてどのファイルに何を書いて良いようになっています。

今更Nagios?という感じもありますが、うちみたいな小さなプロジェクトを抱えている開発会社では、各担当者が監視設定をすることが多いので、さくっと外部監視を仕掛ける程度なら学習コストも低いので、以下に紹介するような1プロジェクト=1ファイルで分かりやすく運用しています。

標準的な設定はテンプレートで用意

ホストやサービスの設定は、初期値をregister=0にしたテンプレートで設定しておきます。


define host{
name generic-host
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
check_command check-host-alive
max_check_attempts 10
notification_interval 60
notification_period 24x7
notification_options d,u,r
contact_groups admins
register 0
}

define service{
name generic-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
notification_interval 60
is_volatile 0
check_period 24x7
normal_check_interval 5
retry_check_interval 1
max_check_attempts 4
notification_period 24x7
notification_options w,u,c,r
contact_groups admins
register 0
}

define contact{
name generic-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options c,r
host_notification_options d,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
register 0
}

プロジェクト毎の設定ファイル

そうしたら、プロジェクト毎にファイルを用意します。


# 全体設定

define hostgroup{
hostgroup_name exampleproject
}

define host{
use generic-host
name exampleproject-host
contact_groups exampleproject_alert_group
hostgroups exampleproject
}

define service{
use generic-service
name exampleproject-service
contact_groups exampleproject_alert_group
}

define contactgroup{
contactgroup_name exampleproject_alert_group
alias exampleproject Alert
}

define contact{
use generic-contact
name exampleproject_alert_template
alias exampleproject Alert
contactgroups exampleproject_alert_group
register 0
}

# 通知先
define contact{
use exampleproject_alert_template
contact_name exampleproject_warning_ml
email warning-ml@exampleproject.example.jp
# 予兆を知るためWarn/Unknownを通知
service_notification_options w,u,c,r
}

define contact{
use exampleproject_alert_template
contact_name exampleproject_emergency_ml
email emergency-ml@exampleproject.example.jp
}

# 共通監視項目 (hostgroup:exampleprojectの全サーバに適用)
define service{
use exampleproject-service
name exampleproject-ping
service_description PING
check_command check_ping!5000,20%!10000,100%
hostgroup_name exampleproject
}

# サーバ
define host{
use exampleproject-host
host_name server1.example.jp
address 192.0.2.1
}

# 個別監視項目
define service{
use exampleproject-service
host_name server1.example.jp
service_description HTTP
check_command check_http_path_string!www.example.jp!/news!Announes
}

define service{
use exampleproject-service
host_name server1.example.jp
service_description Certificate
check_command check_https_certificate!www.example.jp
}

これで監視項目の部分は本当に設定が必要な内容だけになって見通しが良くなります。

プロジェクト開始時は、ファイルをコピーしておもむろにviを開き、:%s/exampleproject/hoge/g で一括置換してからサーバ名や監視項目を設定するだけでOKです。

継承関係

継承関係を図で表すとこんな感じ。とにかく共通部分をテンプレートにくくり出して継承するだけで、それなりに管理に耐えるようにはなっています。