GOD - mongrel監視
GOD。ずっと気になっていた、mongrel_clusterのモニタリングツール。ただの監視だけでなく、ダウン時の再起動、メモリやCPUの条件で再起動とかもやってくれる優れものです。

GOD - http://god.rubyforge.org/
今は亡きフレディーマーキュリーを彷彿するサイト(なんとなく)。
Windows版は今後もリリースされないとのことなので、MaxOSXでやってみました。
テストアプリ作って、mongrel_cluster.ymlを生成。
> cd testapp
> sudo mongrel_rails cluster::configure -e production\
-p 8000 -N 3 -c /Users/hihappy/works/testapp -a 127.0.0.1
上のコマンドで、下のファイルが出来上がります(別に手書きでも良い)。
---
cwd: /Users/hihappy/works/testapp
log_file: log/mongrel.log
port: "8000"
environment: production
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 3
mongrel_clusterの起動停止を確認しておきます。
starting port 8000
starting port 8001
starting port 8002
~/works/testapp> sudo mongrel_rails cluster::stop
stopping port 8000
stopping port 8001
stopping port 8002
GODのインストール。コンパイルが入るので、rubyの*.h(ソースコンパイルやruby-devel)が必要。
サイトのサンプルにあるように、適当な名前(testapp.god)で、設定ファイルを作る。
RAILS_ROOT = "/Users/hihappy/works/testapp"
%w{8000 8001 8002}.each do |port|
God.watch do |w|
w.name = "testapp-mongrel-#{port}"
w.interval = 30.seconds # default
w.start = "mongrel_rails cluster::start --only #{port} \
-C #{RAILS_ROOT}/config/mongrel_cluster.yml"
w.stop = "mongrel_rails cluster::stop --only #{port} \
-C #{RAILS_ROOT}/config/mongrel_cluster.yml"
w.grace = 10.seconds
w.pid_file = File.join(RAILS_ROOT, "log/mongrel.#{port}.pid")
w.group = "mongrels"
w.behavior(:clean_pid_file)
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 5.seconds
c.running = false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = 150.megabytes
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = 50.percent
c.times = 5
end
end
end
end
これで、GOD経由でmongrel_clusterを起動できます。
停止するには、上の w.groupで指定した名前で停止。
GODでクラスタを起動して、横から「ちょっかい」を出してみます。
> ps -A
> sudo kill [pid]
とか、ダウンさせてみてhttp://localhost:8000を連打すると、自動的にmongrelが復活する様子がわかる。
サイトの後半を読んでみるとわかりますが、かなり自由にRubyでプログラムを書き込めるようです。いかほどのものか、もう少し触ってみよう。

