首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用monit监控nginx乘客

如何使用monit监控nginx乘客
EN

Stack Overflow用户
提问于 2010-11-05 10:56:13
回答 3查看 2.7K关注 0票数 4

我有几个由nginx passenger部署的rails应用程序。我希望使用monit来监控这些应用程序。如何使用monit监控这些应用程序?我也应该监控nginx吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-28 20:34:10

这就是我解决这个问题的方法。首先,我给application.rb添加了:

代码语言:javascript
复制
# 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:

代码语言:javascript
复制
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来监视每个实例。

票数 3
EN

Stack Overflow用户

发布于 2015-06-24 20:04:20

不确定现在发布这篇文章是否太晚了,但这就是我如何使用monit (5.14)来阻止passenger rails应用程序消耗太多内存的方法:

monit:

代码语言:javascript
复制
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):

代码语言:javascript
复制
#!/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消耗大量的内存,如果应用程序时不时地占用一点内存。

票数 2
EN

Stack Overflow用户

发布于 2011-04-18 16:39:20

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4103257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档