我有几个nginx乘客部署的rails应用程序。我希望通过使用monit来监视这些应用程序。我如何使用monit监视这些应用程序?我应该监视nginx吗?如何监控nginx乘客monit
回答
这就是我解决这个问题的方法。首先,我加入到application.rb中:
# 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每个实例。作为pidfile的pid。
我做了一个插件 - https://github.com/romanbsd/passenger_monit – Roman 2011-04-29 18:15:55
如果您希望让它们继续运行并在出现错误时让它们重新启动,那么最好查看supervisord。 supervisord不是轮询查看进程是否正在运行,而是实际运行进程本身。它运行的守护进程需要运行前台才能正常工作,但它非常有效,并且会使服务比monit更快(monit通常会每分钟轮询一次,而supervisord会看到进程结束并立即重新启动)。
我们在生产过程中使用supervisord运行我们所有的守护进程(nginx,beanstalkd,memcached,各种python服务等),然后使用monit监视supervisord作为增加的备份。
OP询问了关于在Nginx Passenger下运行的Rails应用程序,这个问题在这个答案中没有解决。 – Amir 2013-03-20 16:39:55
不知道是否为时已晚发布此但这是我如何使用的monit(5.14)停止客运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"
外壳脚本调用的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应用程序,但它至少可以防止导轨消耗大量MEMOR的最佳解决方案如果应用程序一次又一次地记忆密集。
- 1. rvm乘客nginx
- 2. nginx与乘客
- 3. 配置monit的监控在Ubuntu/Nginx的设置
- 4. nginx proxy_pass与乘客
- 5. RVM,乘客,在现有乘客/ NGINX之上的NGINX
- 6. Ruby:乘客+ nginx:如何安装redmine
- 7. 如何监测乘客/ mod_rails过程?
- 8. 用Monit监控远程文件系统
- 9. 使用monit监控工具设置Delayed_job
- 10. Monit监视上帝?
- 11. 乘客5 Nginx X-Accel-Redirect
- 12. 的Ubuntu 17.04的nginx /乘客
- 13. 使用nginx的+乘客
- 14. Nginx未知乘客指令
- 15. RVM + Nginx +乘客在厨师
- 16. 乘客NGINX模块失败
- 17. 安装Nginx与乘客安装nginx模块。我如何卸载?
- 18. 如何监控Monit的gearmand守护进程?
- 19. 如何使用Monit监控Caddy Web服务器
- 20. 如何用supervisord监控nginx和cron?
- 21. 客运,Nginx等Capistrano的 - 乘客根本
- 22. 使用ngxtop监控Nginx
- 23. 的nginx +乘客+的phpmyadmin =拒绝访问
- 24. 构建的nginx +乘客安装失败
- 25. 与Capistrano的+ Nginx的+乘客+ MYSQL到VPS
- 26. 与乘客和nginx一起使用deprec
- 27. gzip不能在nginx +乘客中工作
- 28. 在乘客中配置nginx 3.0.2独立
- 29. 与nginx和乘客一起使用SimpleCov
- 30. 问题与乘客,nginx和Rails3
乘客2或乘客3.如果乘客3是独立的或不是? – shingara 2010-11-05 08:08:06
我使用乘客3,但从乘客2复制的配置。基本上,它是乘客2. – Chamnap 2010-11-05 09:19:10
我不确定,但是,monit可以通过给PID来监视进程,因此您可以为每个要监视的进程创建PID文件。 根据nginx的关注,您应该监控nginx作为处理您的web请求并委派给其中一个乘客实例的主要流程。 – 2011-04-18 08:50:12