god
Ruby製のプロセス監視ツールGodについてのメモ。
God - A Process Monitoring Framework in Rubyの内容を途中まで適当に意訳してるだけ。
インストール
sudo gem install god
実行方法
# 普通に実行 sudo god -c /path/to/config.god # foreground で実行 sudo god -c /path/to/config.god -D # 直接 start/restart/stop/monitor/unmonitor することも出来るみたい。god のプロセスが動いている前提。 sudo god stop gravatar2-mongrel-8200
定義例
God.watch にブロック渡して、ブロックパラメータのオブジェクトメソッドでいろいろ挙動を定義。God.watch一回につき一つのプロセスの監視が出来る。複数実行することで、複数のプロセスの操作を定義できる。
God.watch do |w| w.name = "gravatar2-mongrel-#{port}" w.interval = 30.seconds # default w.start = "mongrel_rails start -c #{RAILS_ROOT} -p #{port} \ -P #{RAILS_ROOT}/log/mongrel.#{port}.pid -d" w.stop = "mongrel_rails stop -P #{RAILS_ROOT}/log/mongrel.#{port}.pid" w.restart = "mongrel_rails restart -P #{RAILS_ROOT}/log/mongrel.#{port}.pid" w.start_grace = 10.seconds w.restart_grace = 10.seconds w.pid_file = File.join(RAILS_ROOT, "log/mongrel.#{port}.pid") w.behavior(:clean_pid_file) # ... end
- name
- 必須。ユニークに定義する必要がある。
- interval
- プロセスをチェックする間隔
- start_grace
- 通常の監視に移行するまでの時間。オプション。
- start, stop, restart
- 見れば分かる。restart が定義されていない場合、デフォルトで stop して start する。
- pid_file
- pidファイルの場所を指定。監視したいプロセスがデーモンであれば指定する。
- behavior
- start/stop/restart時のフックを指定。:clean_pid_file とすると stop時に pid file を削除してくれるようになる。
start_if
conditionメソッドにブロック渡して、スタートする条件を指定する
- interval
- 外側の interval を上書きする
w.start_if do |start| start.condition(:process_running) do |c| c.interval = 5.seconds c.running = false end end
restart_if
- above
- みたまま。
- times
- 回数の条件を指定。integer か array が使える。integer であれば fail した回数で、[x,y] のようなarrayであれば、 直近の y 回のチェック中に x 回 failしたという意味になる。
容量の指定に kilobytes, megabytes, gigabytes が使える
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 # ... end
CPU 使用量の監視も出来る
w.restart_if do |restart| # ... restart.condition(:cpu_usage) do |c| c.above = 50.percent c.times = 5 end end
lifecycle
start か restart が5分以内に5回実行されたら、なんかうまく起動しない理由があると判断して監視対象から外す→10分後に再び監視対象に。2時間以内にこれが5回あるようなら完全に諦める。的なことができる。
w.lifecycle do |on| on.condition(:flapping) do |c| c.to_state = [:start, :restart] c.times = 5 c.within = 5.minute c.transition = :unmonitored c.retry_in = 10.minutes c.retry_times = 5 c.retry_within = 2.hours end end