forked from alibby/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passenger-check.rb
executable file
·168 lines (142 loc) · 4.15 KB
/
passenger-check.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env ruby
require 'stringio'
require 'ostruct'
require 'pp'
require 'optparse'
module Passenger
class Instance
attr_accessor :dir, :pid, :processes
def initialize(info)
lines = info.split("\n")
@dir = lines.shift
@processes = []
for l in lines do
l =~ /PID: (\d+)\s+Sessions: (\d+)/
if $1 != nil and $2 != nil then
@processes << { :pid => $1.to_i, :sessions => $2.to_i }
end
end
end
end
class Status
attr_accessor :io, :max, :count, :active, :inactive, :instances
def initialize(io)
@instances = []
@io = io
io.readline
@max,@count,@active,@inactive = read_attribute,read_attribute,read_attribute,read_attribute
@global_queue = read_attribute(':') == 'no' ? false : true
@global_waiting = read_attribute(':')
io.readline
io.readline
while(! io.eof?)
@instances << Instance.new(io.readline("\n\n"))
end
end
def percent_used
@count.to_i / @max.to_i
end
private
def read_attribute(delim = /\s+=\s+/)
io.readline.chomp.split(delim).last
end
end
end
def nag_exit(code, msg)
txtcodes = [ "OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPEDENT" ]
puts "PASSENGER #{txtcodes[code]} - #{msg}"
exit(code)
end
def is_a_number?(s)
s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end
def check_max_proc(status, options)
pct = status.active.to_f / status.max.to_f
code = 0
if pct > options[:crit] then
code = 2
elsif pct > options[:warn] then
code = 1
end
nag_exit(code, "Using #{status.active} of #{status.max} (#{pct}%)")
end
def check_spike(status, options)
for i in status.instances do
tot_ses = 0
for p in i.processes do
tot_ses += p[:sessions]
end
avg = tot_ses/(i.processes.count)
for p in i.processes do
if p[:sessions] > (avg * options[:crit]) then
nag_exit(2, "PID #{p[:pid]} has #{p[:sessions]} sessions (avg is #{avg})")
elsif p[:sessions] > (avg * options[:warn]) then
nag_exit(1, "PID #{p[:pid]} has #{p[:sessions]} sessions (avg is #{avg})")
end
end
end
nag_exit(0, "No session spikes (avg sessions is #{avg})")
end
def check_sessions(status, options)
for i in status.instances do
for p in i.processes do
if p[:sessions] > options[:crit] then
nag_exit(2, "PID #{p[:pid]} has #{p[:sessions]} sessions")
elsif p[:sessions] > options[:warn] then
nag_exit(1, "PID #{p[:pid]} has #{p[:sessions]} sessions")
end
end
end
nag_exit(0, "No PID has excessive sessions")
end
options = {}
OptionParser.new do |opts|
opts.banner = %{Usage #{__FILE__} [options]
The values for warning and critical are treated differently depending on the
check used.
For maxproc: Warning and critical are percentages; the maximum percent of
active processes compared to the total max processes.
For spike: Warning and critical are percentages; the maximum percentage
per-PID session spike allowed (as compared to the average)
For sessions: Warning and critical are integers; the maximum number of
sessions per PID
}
opts.on("-?", "-h", "Help") do |o|
nag_exit(3, "Use --help for usage")
end
opts.on("-C", "--check CHECK", "Which check to execute: maxproc/spike/sessions") do |o|
if ["maxproc", "spike", "sessions"].include?(o) then
options[:check] = o
else
nag_exit(3, "Unrecognized check name: use --help for usage")
end
end
opts.on("-w", "--warn", "--warning WARN", "Level for warning") do |o|
if is_a_number?(o) then
options[:warn] = o.to_f
else
nag_exit(3, "Warning threshold #{o} is not numeric")
end
end
opts.on("-c", "--crit", "--critical CRIT", "Level for critical") do |o|
if is_a_number?(o) then
options[:crit] = o.to_f
else
nag_exit(3, "Critical threshold #{o} is not numeric")
end
end
end.parse!(ARGV)
if (options[:check] == nil) or (options[:warn] == nil) or (options[:crit] == nil) then
nag_exit(3, "Options check, warn, and crit must all be specified")
end
Dir['/tmp/passenger_status.*.fifo'].map { |f| open(f) }.each do |io|
status = Passenger::Status.new(io)
case options[:check]
when "maxproc"
check_max_proc(status, options)
when "spike"
check_spike(status, options)
when "sessions"
check_sessions(status, options)
end
end