Nagios
サービス監視をNagiosを使ってやってみる
ユーザー関連の設定
useradd -d /usr/local/nagios nagios
mkdir /usr/local/nagios
chown nagios:nagios /usr/local/nagios
インストール
./configure
make all
make fullinstall
make install-config
インストールが完了したら、/usr/local/nagios/share/docsを削除し、Nagios-JPからダウンロードした日本語ドキュメントと入れ替える。その後、Nagios本体ソース内にあるsample-config/httpd.confをApache2の設定ファイルディレクトリ内にnagios.confとしてコピー。更にNAGIOS_HOME/etc/htpasswd.usersというファイルをhtpasswdを使用してnagiosadminっていうユーザーを追加し作成しておく
設定1(nagios.cfg)
$NAGIOS_HOME/etcにある設定ファイルを.cfg形式にコピーする。nagios.cfgを以下のように設定する
# 今回は使用しないので除外する
# cfg_file=/usr/local/nagios/etc/localhost/cfg
# 以下は利用するので先頭のコメントを外す
cfg_file=/usr/local/nagios/etc/contactgroups.cfg
cfg_file=/usr/local/nagios/etc/contacts.cfg
cfg_file=/usr/local/nagios/etc/hostgroups.cfg
cfg_file=/usr/local/nagios/etc/hosts.cfg
cfg_file=/usr/local/nagios/etc/services.cfg
cfg_file=/usr/local/nagios/etc/timeperiods.cfg
# 0が指定されているので1に変更する
check_external_commands=1
# usが指定されているのでiso8601に変更する
date_format=iso8601
nagios.cfgの設定はこんくらい。上で設定したファイルを作成・設定していく
設定2(cgi.cfg)
主にWebコンソール関連の設定を行う
# 以下が全てコメントで無効化されているので外して有効にし、アクセスできるユーザーを指定する
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
nagiosadminはhtpasswdで作成したユーザー名を指定する
設定3(timeperiods.cfg)
define timeperiod{
timeperiod_name 24×7
alias 24 Hours A Day, 7 Days A Week
sunday 00:00-24:00
monday 00:00-24:00
tuesday 00:00-24:00
wednesday 00:00-24:00
thursday 00:00-24:00
friday 00:00-24:00
saturday 00:00-24:00
}
設定4(contacts.cfg)
define contact{
contact_name nagios-admin
alias Nagios Admin
service_notification_period 24×7
host_notification_period 24×7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-by-email
host_notification_commands host-notify-by-email
email nagios@hoge.com
}
設定5(contactgroups.cfg)
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagios-admin
}
設定6(hosts.cfg)
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
notification_period 24×7
check_period 24×7
max_check_attempts 10
check_command check-host-alive
notification_interval 120
notification_options d,u,r
contact_groups admins
register 0
}
define host{
use generic-host
host_name localhost
alias localhost
address 127.0.0.1
}
設定7(hostgroups.cfg)
define hostgroup{
hostgroup_name home_servers
alias Home Servers
members localhost
}
設定8(services.cfg)
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
is_volatile 0
check_period 24×7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 60
notification_period 24×7
register 0
}
# 自ホスト生存監視
define service{
use generic-service
host_name localhost
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
設定はこれにて終了。以下を実行する
cd /usr/local/nagios/bin
./nagios -v ../etc/nagios.cfg
エラーが出ずにサーバーウェイトモードになればオッケー
使ってみる
実際にサービスがちゃんと監視できてるかチェックしてみる。例としてMySQLサーバーが起動しているかチェックしてみる。最初に/usr/local/nagios/etc/commands.cfgに以下を設定
define command{
command_name check_mysql
command_line $USER1$/check_mysql -u hoge -p hoge
}
んで監視対象として扱う為に、/usr/local/nagios/etc/services.cfgに以下を追加
define service{
use generic-service
host_name localhost
service_description mysql
check_command check_mysql
}
service nagios start
を行い、異常な場合はcontacts.cfgで指定したemail部分のメールアドレスに警告メールが送信される
ApacheでのVirtualHost設定メモ
<VirtualHost *:80>
ServerName nagios.kdeveloper.biz
DocumentRoot /usr/local/nagios/share
ScriptAlias /cgi-bin "/usr/local/nagios/sbin"
<Directory /usr/local/nagios/share>
AuthName "Nagios Resource"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/nagios.users
Require valid-user
</Directory>
<Directory /usr/local/nagios/sbin>
Options +ExecCGI
AuthName "Nagios Resource"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/nagios.users
Require valid-user
</Directory>
</VirtualHost>