我有几个使用nginx passenger部署的rails应用程序。我想使用monit监控这些应用程序。如何使用monit监控这些应用程序?我是否还需要监控nginx?
我有几个使用nginx passenger部署的rails应用程序。我想使用monit监控这些应用程序。如何使用monit监控这些应用程序?我是否还需要监控nginx?
# Monit support
if defined?(PhusionPassenger)
require 'pidfile_manager'
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
# We're in smart spawning mode.
PidfileManager.write_pid_file
else
# We're in conservative spawning mode. We don't need to do anything.
end
end
PhusionPassenger.on_event(:stopping_worker_process) do
PidfileManager.remove_pid_file
end
end
然后我实现了PidfileManager:
module PidfileManager
extend self
BASENAME = '/var/tmp/rack.*.pid'
def write_pid_file
pid = Process.pid
count = 1
pidfile = nil
go_over_pid_files do |file, saved_pid|
file_id = file[/(\d+)/,1].to_i
# Increase counter only if we met the same file id
count += 1 if file_id == count
# We're already there
return if saved_pid == pid
# Check if the process is alive
res = begin
Process.kill(0, saved_pid)
rescue Errno::ESRCH
nil
end
# It's dead, reuse
unless res
pidfile = file
break
end
end
pidfile ||= BASENAME.sub('*', count.to_s)
File.open(pidfile, 'w') {|f| f.write(pid.to_s)}
end
def remove_pid_file
pid = Process.pid
go_over_pid_files do |file, saved_pid|
if pid == saved_pid
File.unlink(file)
break
end
end
end
private
def go_over_pid_files
Dir[BASENAME].each do |file|
saved_pid = File.read(file).to_i
yield file, saved_pid
end
end
end
然后你只需要告诉monit使用/var/tmp/rack.X.pid作为pidfile来监控每个实例。
不确定是否为时已晚,但以下是我如何使用monit(5.14)停止Passenger Rails应用程序消耗过多内存的方法:
Monit:
check program ourapp_live with path "/usr/local/bin/check_passenger_mem_usage ourapp 500" as "ourappuser"
if status != 0 then alert
if status != 0 then restart
start program = "/bin/touch /var/www/vhosts/ourapp/railsapp/current/tmp/restart.txt"
stop program = "/bin/true"
shell脚本monit调用了(check_passenger_mem_usage):
#!/bin/bash
#
#
USER=$1
TRIGGER=$2
if [ -z $USER ] || [ -z $TRIGGER ]
then
echo "missing args"
echo "usage:"
echo " check_passenger_mem_usage username alert_threshold"
echo
echo "(alert_threshold is in mb)"
echo
exit 1
fi
MAX=`/usr/local/rvm/gems/ruby-1.8.7-p357/wrappers/passenger-memory-stats | grep $USER | awk '{print $2}' | sort -n | tail -1|cut -d. -f1`
if [ "$MAX" -gt $TRIGGER ]
then
echo
echo "${USER}: We got a runaway! Gobbling ${MAX} mb"
echo
exit 1
else
echo
echo "Max is ${MAX}"
exit 0
fi
可能不是最好的解决方案,因为它会重新启动整个Rails应用程序,但至少它可以防止在应用程序偶尔变得内存密集时,Rails消耗大量内存。
如果您想让它们保持运行并在出现错误时重新启动它们,您最好查看supervisord。与轮询进程是否正在运行不同,supervisord实际上会运行进程本身。它运行的守护进程需要在前台运行才能正常工作,但它非常有效,并且会比monit更快地启动服务(monit通常每分钟轮询一次,而supervisord会立即检测到进程结束并重新启动它)。
我们在生产中使用supervisord运行所有我们的守护进程(nginx、beanstalkd、memcached、各种Python服务等),然后使用monit监视supervisord作为额外的备份。