如何使用Monit监控Nginx Passenger

5

我有几个使用nginx passenger部署的rails应用程序。我想使用monit监控这些应用程序。如何使用monit监控这些应用程序?我是否还需要监控nginx?


乘客2或乘客3。如果乘客3是独立的吗? - shingara
我使用的是Passenger 3,但配置文件是从Passenger 2复制过来的。基本上,它就是Passenger 2。 - Chamnap
我不确定,但是Monit可以监控具有给定PID的进程,因此您可以为要监视的每个进程创建PID文件。 就nginx而言,您应该监视nginx,因为它是处理您的Web请求并委派给其中一个Passenger实例的主要进程。 - Naren Sisodiya
试试这个宝石,它可以帮助你 https://github.com/romanbsd/passenger_monit - user1913545
3个回答

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.pid作为pidfile来监控每个实例。


2

不确定是否为时已晚,但以下是我如何使用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消耗大量内存。


0

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

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


OP特别询问了在Nginx Passenger下运行的Rails应用程序,这个答案没有涉及到。 - Amir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接