Skip to content

Instantly share code, notes, and snippets.

@CamonZ
Forked from nofxx/my.god
Created September 8, 2009 07:07
Show Gist options
  • Save CamonZ/182760 to your computer and use it in GitHub Desktop.
Save CamonZ/182760 to your computer and use it in GitHub Desktop.
#
# God please,
#
# Generic Monitor
#
def generic_monitoring(w, options = {})
w.interval = 30.seconds # default
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 10.seconds
c.running = false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.notify = {:contacts => ["nofxx", "developers"], :priority => 1, :category => "mem"}
c.above = options[:memory_limit]
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.notify = {:contacts => ["nofxx", "developers"], :priority => 1, :category => "cpu"}
c.above = options[:cpu_limit]
c.times = 5
end
end
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
end
# Nginx
#
God.watch do |w|
w.name = "nginx"
w.uid = "root"
w.gid = "root"
w.group = "webapp"
w.start = "/etc/rc.d/nginx start"
w.stop = "/etc/rc.d/nginx stop"
w.restart = "/etc/rc.d/nginx restart"
w.start_grace = 30.seconds
w.restart_grace = 30.seconds
w.pid_file = File.join("/var/run/nginx.pid")
w.behavior(:clean_pid_file)
generic_monitoring(w, :cpu_limit => 80.percent, :memory_limit => 800.megabytes)
end
# Nanites
#
for nanite in [:foo, :bar]
God.watch do |w|
w.name = nanite.to_s
w.group = "nanites"
w.uid = WEBAPP[:uid]
w.gid = WEBAPP[:gid]
w.pid_file = File.join(WEBAPP[:path], "tmp", "pids", "nanite.nanite-#{nanite}.pid")
w.start = "nanite-agent --token #{nanite} --nanite #{WEBAPP[:path]}/nanite/#{nanite} --pid-dir #{WEBAPP[:path]}/tmp/pids --log-dir #{WEBAPP[:path]}/log --daemonize"
w.stop = "kill `cat #{w.pid_file}`"
w.restart = "#{w.stop} && #{w.start}"
w.start_grace = 20.seconds
w.restart_grace = 20.seconds
w.behavior :clean_pid_file
generic_monitoring(w, :cpu_limit => 80.percent, :memory_limit => 800.megabytes)
end
end
# PostgreSQL
#
God.watch do |w|
w.name = "postgresql"
w.uid = "root"
w.gid = "root"
w.group = "db"
w.start = "/etc/rc.d/postgresql start" #w.start = "service postgresql start"
w.stop = "/etc/rc.d/postgresql stop" #w.stop = "service postgresql stop"
w.restart = "/etc/rc.d/postgresql restart"
w.start_grace = 30.seconds
w.restart_grace = 30.seconds
w.pid_file = File.join(WEBAPP[:db], "postmaster.pid")
w.behavior(:clean_pid_file)
generic_monitoring(w, :cpu_limit => 80.percent, :memory_limit => 800.megabytes)
end
# Tyrant
#
God.watch do |w|
w.name = "tyrant"
w.uid = "root"
w.gid = "root"
w.group = "db"
w.start = "rake -f #{WEBAPP[:path]}/Rakefile tyrant:start"
w.stop = "rake -f #{WEBAPP[:path]}/Rakefile tyrant:stop"
w.restart = "rake -f #{WEBAPP[:path]}/Rakefile tyrant:restart"
w.start_grace = 30.seconds
w.restart_grace = 30.seconds
w.pid_file = File.join(WEBAPP[:path], "tmp", "pids", "tyrant.pid")
w.behavior(:clean_pid_file)
generic_monitoring(w, :cpu_limit => 80.percent, :memory_limit => 800.megabytes)
end
# RabbitMQ
#
God.watch do |w|
w.name = "rabbitmq"
w.uid = "root"
w.gid = "root"
w.group = "db"
w.start = "/etc/rc.d/rabbitmq start"
w.stop = "/etc/rc.d/rabbitmq stop"
w.restart = "/etc/rc.d/rabbitmq restart"
w.start_grace = 30.seconds
w.restart_grace = 30.seconds
w.pid_file = File.join("/var", "run", "rabbitmq.pid")
w.behavior(:clean_pid_file)
generic_monitoring(w, :cpu_limit => 80.percent, :memory_limit => 800.megabytes)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment