2010-11-05 73 views
4

我有几个nginx乘客部署的rails应用程序。我希望通过使用monit来监视这些应用程序。我如何使用monit监视这些应用程序?我应该监视nginx吗?如何监控nginx乘客monit

+0

乘客2或乘客3.如果乘客3是独立的或不是? – shingara 2010-11-05 08:08:06

+0

我使用乘客3,但从乘客2复制的配置。基本上,它是乘客2. – Chamnap 2010-11-05 09:19:10

+0

我不确定,但是,monit可以通过给PID来监视进程,因此您可以为每个要监视的进程创建PID文件。 根据nginx的关注,您应该监控nginx作为处理您的web请求并委派给其中一个乘客实例的主要流程。 – 2011-04-18 08:50:12

回答

3

这就是我解决这个问题的方法。首先,我加入到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。

+1

我做了一个插件 - https://github.com/romanbsd/passenger_monit – Roman 2011-04-29 18:15:55

0

如果您希望让它们继续运行并在出现错误时让它们重新启动,那么最好查看supervisord。 supervisord不是轮询查看进程是否正在运行,而是实际运行进程本身。它运行的守护进程需要运行前台才能正常工作,但它非常有效,并且会使服务比monit更快(monit通常会每分钟轮询一次,而supervisord会看到进程结束并立即重新启动)。

我们在生产过程中使用supervisord运行我们所有的守护进程(nginx,beanstalkd,memcached,各种python服务等),然后使用monit监视supervisord作为增加的备份。

+0

OP询问了关于在Nginx Passenger下运行的Rails应用程序,这个问题在这个答案中没有解决。 – Amir 2013-03-20 16:39:55

2

不知道是否为时已晚发布此但这是我如何使用的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的最佳解决方案如果应用程序一次又一次地记忆密集。