我有nginx乘客部署的几个rails应用程序.我希望使用monit监视这些应用程序.如何使用monit监控这些应用程序?我也应该监控nginx吗?解决方法:这就是我解决这个问题的方法.首先,我添加到application.rb:# Monit suppor...

我有nginx乘客部署的几个rails应用程序.我希望使用monit监视这些应用程序.如何使用monit监控这些应用程序?我也应该监控nginx吗?
解决方法:
这就是我解决这个问题的方法.首先,我添加到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.
沃梦达教程
本文标题为:ruby-on-rails – 如何使用monit监控nginx乘客


基础教程推荐
猜你喜欢
- ruby – 如何使用Nginx,Passenger,Sinatra创建多个位置 2023-09-20
- 如何将mysql数据库文件连接到Rails应用程序上的本地ruby 2023-09-21
- 深入探究Golang中log标准库的使用 2023-07-25
- 汇编语言:比较指令、跳转指令、JCC的使用 2023-07-06
- 详解swift中xcworkspace多项目管理 2023-07-05
- R语言的一个加法函数使用介绍 2022-11-14
- 解决R语言中install_github中无法安装遇到的问题 2022-11-26
- R语言因子型数值转数值型的操作 2022-11-23
- R语言入门使用RStudio制作包含Rcpp代码的R包 2022-12-05
- R语言向量下标和子集的使用 2022-12-10