Nagios でサーバ監視
Nagios というサーバ外形監視ツールをインスコしたよ。ブラウザでサーバのステータスがわかるんだよ。PING だけじゃなく、FTP とか HTTP が動いてるか確認できるのでオススメだよ。読み方がよくわかんないけど、元ネットワーク屋さんの同居人は「ナギオス」って呼んでたよ!
Dabian なので:
$ sudo aptitude install nagios-text
データを DB に貯める nagios-mysql とか nagios-pgsql もあるけど、一番簡易なテキストで。nagios2 も出てるけど、なんとなく見送った。インストール途中でパスワードの入力を求められるけど、これはウェブアプリの Basic 認証で使われる。おうちから見ようと思ったら、インターネットに晒す必要があるからね。
んでだ、とりあえず起動したいよね? んだら、/etc/nagios/apache.conf を /etc/apache2/sites-available の下に適当な名前で置いて、a2ensite して、apache を再起動するよ:
$ sudo cp /etc/nagios/apache.conf /etc/apache2/sites-available/nagios $ sudo a2ensite nagios $ sudo /etc/init.d/apache2 restart
これで、http://(インスコしたサーバ)/nagios/ にアクセスすれば、Nagios のページが見れるよ。多分大丈夫だけど、ページが見れなかったら、nagios が起動してないかもよ?:
$ sudo /etc/init.d/nagios start
これだけじゃ、監視サーバの設置してあるゲートウェイの PING しか監視できてないので、追加するよ。そうだなー、ウェブサーバの PING と HTTP を監視するって感じで行こうかな。
まずは、監視したいウェブサーバの設定を追記するよ。/etc/hosts.cfg を開け:
$ sudo vi /etc/nagios/hosts.cfg define host{ use generic-host host_name fuko ; 略称 alias Filn Main Server ; 正式名称?みたいな address (ウェブサーバのIPアドレス) check_command check-host-alive max_check_attempts 10 notification_interval 120 notification_period 24x7 notification_options d,u,r }
こんな感じで。個々の奴はググって。
$ sudo vi /etc/nagios/hostgroups.cfg define hostgroup{ hostgroup_name web-servers alias Web Servers contact_groups web-admins members fuko }
ホストは1つ以上の hostgroup に属さないといけないんだ。書き忘れても、nagios サービス再起動時にエラー出るから安心。
次はどのサービスを監視するか設定するよ:
$ sudo vi /etc/nagios/services.cfg define service{ use generic-service ; Name of service template to use host_name fuko service_description PING is_volatile 0 check_period 24x7 max_check_attempts 3 normal_check_interval 5 retry_check_interval 1 contact_groups web-admins notification_interval 120 notification_period 24x7 notification_options c,r check_command check_ping!100.0,20%!500.0,60% } define service{ use generic-service ; Name of service template to use host_name fuko service_description HTTP is_volatile 0 check_period 24x7 max_check_attempts 3 normal_check_interval 5 retry_check_interval 1 contact_groups web-admins notification_interval 120 notification_period 24x7 notification_options w,u,c,r check_command check_http!/minisns.pl!2!10 }
check_ping!100.0,20%!500.0,60% は ping コマンドで、100.0ms 以上時間がかかって 20% ロスで Warning、500.0ms 以上 60% ロスでもうだめぽーメールを送るって意味だよ。Nagios コマンドを書くときは、! で区切るんだね。
check_http!/hoge.pl!2!10 は /hoge.pl に HTTP アクセスして、2秒以上で警告、10秒でアウト!の設定だよ。
んでだ、最後に、異常があったときにメールを飛ばす人のグループとユーザーを設定するよ。
$ sudo vi contactgroups.cfg define contactgroup{ contactgroup_name web-admins alias Web Technicians members babie ; 好きな名前 }
$ sudo vi contacts.cfg define contact{ contact_name babie alias Michiaki Baba service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commands notify-by-email,notify-by-epager host_notification_commands host-notify-by-email,host-notify-by-epager email babie.tanaka@example.com ; メアド pager babie.tanaka@mobile.com ; 携帯メアド・・・ }
email だけじゃなく、携帯も設定できるんだね! 休出カモンな感じだね!
contacts.cfg で同じように人を追加して、contactgroups.cfg の members にカンマ区切りで追加すれば、複数人にメールを飛ばせるよ! 死なばもろともだね!
Nagios って簡単だね!