Révision 48d91aca
Initial version
| plugins/other/unicorn_status | ||
|---|---|---|
| 1 |
#!/usr/bin/env ruby |
|
| 2 |
# |
|
| 3 |
# unicorn_status - A munin plugin for Linux to monitor unicorn processes |
|
| 4 |
# |
|
| 5 |
# Copyright (C) 2010 Shinji Furuya - shinji.furuya@gmail.com |
|
| 6 |
# Licensed under the MIT license: |
|
| 7 |
# http://www.opensource.org/licenses/mit-license.php |
|
| 8 |
# |
|
| 9 |
|
|
| 10 |
# set path to your rails app |
|
| 11 |
RAILS_ROOT = "/path/to/rails/app" |
|
| 12 |
|
|
| 13 |
module Munin |
|
| 14 |
class UnicornStatus |
|
| 15 |
attr_reader :pid_file |
|
| 16 |
|
|
| 17 |
def initialize(rails_root) |
|
| 18 |
@pid_file = "#{rails_root}/tmp/pids/unicorn.pid"
|
|
| 19 |
end |
|
| 20 |
|
|
| 21 |
def master_pid |
|
| 22 |
File.read(pid_file).to_i |
|
| 23 |
end |
|
| 24 |
|
|
| 25 |
def worker_pids |
|
| 26 |
result = [] |
|
| 27 |
ps_output = `ps w --ppid #{master_pid}`
|
|
| 28 |
ps_output.split("\n").each do |line|
|
|
| 29 |
chunks = line.strip.split(/\s+/, 5) |
|
| 30 |
pid, pcmd = chunks[0], chunks[4] |
|
| 31 |
next if pid !~ /\A\d+\z/ or pcmd !~ /worker/ |
|
| 32 |
result << pid.to_i |
|
| 33 |
end |
|
| 34 |
result |
|
| 35 |
end |
|
| 36 |
|
|
| 37 |
def worker_count |
|
| 38 |
worker_pids.size |
|
| 39 |
end |
|
| 40 |
|
|
| 41 |
def idle_worker_count |
|
| 42 |
result = 0 |
|
| 43 |
before_cpu = {}
|
|
| 44 |
worker_pids.each do |pid| |
|
| 45 |
before_cpu[pid] = cpu_time(pid) |
|
| 46 |
end |
|
| 47 |
sleep 1 |
|
| 48 |
after_cpu = {}
|
|
| 49 |
worker_pids.each do |pid| |
|
| 50 |
after_cpu[pid] = cpu_time(pid) |
|
| 51 |
end |
|
| 52 |
worker_pids.each do |pid| |
|
| 53 |
result += 1 if after_cpu[pid] - before_cpu[pid] == 0 |
|
| 54 |
end |
|
| 55 |
result |
|
| 56 |
end |
|
| 57 |
|
|
| 58 |
def cpu_time(pid) |
|
| 59 |
usr, sys = `cat /proc/#{pid}/stat | awk '{print $14,$15 }'`.strip.split(/\s+/).collect { |i| i.to_i }
|
|
| 60 |
usr + sys |
|
| 61 |
end |
|
| 62 |
end |
|
| 63 |
end |
|
| 64 |
|
|
| 65 |
case ARGV[0] |
|
| 66 |
when "autoconf" |
|
| 67 |
puts "yes" |
|
| 68 |
when "config" |
|
| 69 |
puts "graph_title Unicorn - Status" |
|
| 70 |
puts "graph_args -l 0" |
|
| 71 |
puts "graph_vlabel number of workers" |
|
| 72 |
puts "graph_category Unicorn" |
|
| 73 |
puts "total_worker.label total_workers" |
|
| 74 |
puts "idle_worker.label idle_workers" |
|
| 75 |
else |
|
| 76 |
m = Munin::UnicornStatus.new(RAILS_ROOT) |
|
| 77 |
puts "total_worker.value #{m.worker_count}"
|
|
| 78 |
puts "idle_worker.value #{m.idle_worker_count}"
|
|
| 79 |
end |
|
Formats disponibles : Unified diff