金曜日, 10月 19, 2012

monitでプロセス/デーモンその他もろもろをお手軽監視


サービスで使っているデーモンが、気づいたら落ちていて大惨事になっていること
よくはないですが、たまーに話を聞き相談いただきます

一昔前は、daemontools一択でしたが、21世紀に入りすでに10年以上が経過した今、daemontoolsを使うのはなんとなくqmailを今から選択するような、あくまで気分的にですが、どことなくそんないやな感じがあります

多くの会社さんをご支援していると、いろんな考え方があり
保証がない環境やソフト、組み合わせでは使いたくない
という会社もあれば
保証ってもともとなんだよ、オープンソース中心な時点でリスクあんだよ、がんがんいこーぜ
という
会社もあります。
前者はお客さんとかがいて、しょうがない、という事情が多いですね

シェアを調べてみると、差は縮まっているものの、daemontoolsのほうが高い模様
http://www.google.com/trends/explore#q=daemontools,monit
supervisorというのもあるのだけど、別の意味でのほうがよく使われ、trends調べても意味が薄れるので比較せず

pidファイルが残ったままプロセス自体が死んでしまった場合にも対応できるようです。これはいいな。
http://mmonit.com/wiki/Monit/FAQ#pidfile
Yes, Monit will always check that the pid number in a pid file belongs to a running process.

そのちょっと下には
pidを作らないプロセス
はラッパーを作る、という対処法が載っています
いたれりつくせり

=====$ cat /etc/monit.conf
set init
set daemon 60
set logfile syslog facility log_daemon
include /etc/monit.d/*

set mailserver localhost
set alert you@example.com not { instance }

set mail-format {
From: you@example.com
Subject: $HOST monit alert -- $EVENT $SERVICE
message:$EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION

monit detected alert.
please check message or log.
}
=====

=====$ cat /etc/monit.d/solr
check process solr with pidfile "/var/run/solr/solr.pid"
start = "/etc/init.d/solr start"
stop = "/etc/init.d/solr stop"
if failed port 8983 then restart
if 5 restarts within 5 cycles then timeout
alert you@example.com
=====

あまり迷うところはないのですが、ポイントは
set init
でしょうか。
monit自体が落ちては意味ないので、init経由で立ち上げます。
するともしmonitが死んでも、あーら不思議initが再度たちあげてくれる!という期待値です。
参考
http://mmonit.com/monit/documentation/monit.html#init_support

initとかRunLevelとか自信ない方はこちらをどうぞ
http://www.usupi.org/sysad/188.html
http://itpro.nikkeibp.co.jp/article/COLUMN/20080219/294096/

monitをinit経由で起動する、というところではまりました...
だいたいのネットの情報だと
/etc/inittabに
mo:2345:respawn:/usr/bin/monit -Ic /etc/monit.conf
こう書くんだよ、と教えてくれていますが動かない...

正解は、inittabには触れずに
=====cat /etc/init/monit.conf
start on stopped rc RUNLEVEL=[2345]
stop on starting runlevel [016]

respawn
exec /usr/bin/monit -Ic /etc/monit.conf
=====
その後
telinit q
すること

現在のinitを管理するコマンドが
initctl
です
initctl list
とか
initctl start monit
とかしてつかいます。
さっきまでmonit系など全く出て来ませんでしたが、
/etc/init/monit.confを用意したらばんばん出てくるようになりました。

https://access.redhat.com/knowledge/docs/ja-JP/Red_Hat_Enterprise_Linux/6/html/Migration_Planning_Guide/ch04s02.html
にもあるように、最近のredhat系だと /etc/inittab は使わなくなり書いても無効
/etc/init/ 以下にファイルを用意してくれ、という内容です

仕様変更するならいいけど、inittabに書いといてくれよ!
と数秒腹立てましたが、もしやと確認してみるとちゃんと注意書きが書いてありました^^;;;;;
ネットとlogばかり見ていて、inittabファイルのコメントを読んでいなかったorz...

他の参考
http://d.hatena.ne.jp/donbulinux/20090731/1249073898
http://www.mogumagu.com/wp/wordpress/?p=203
http://www.usupi.org/sysad/158.html
http://mmonit.com/monit/documentation/monit.html
http://techracho.bpsinc.jp/admin/2010_04_05/1313
http://labs.unoh.net/2008/03/_monit.html
http://www.glidenote.com/archives/81
http://mmonit.com/monit/documentation/monit.pdf