forked from jamesgolick/fetlife-nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_resque_status
executable file
·100 lines (87 loc) · 2.7 KB
/
check_resque_status
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
#!/usr/bin/env ruby
require "rubygems"
require "resque"
require "resque/status"
require "optparse"
options = {}
required = [:name]
parser = OptionParser.new do |opts|
opts.banner = "Usage: check_resque_status [options]"
options[:host] = 'localhost'
opts.on("-r", "--redis redishost", "The hostname of the redis server") do |h|
options[:host] = h
end
opts.on("-n", "--name job_name", "The name of the job to check") do |n|
options[:name] = n
end
options[:num_last_status] = 10
opts.on("-l", "--num-last-status num", "Number of last statuses from a job to check ") do |ls|
options[:num_last_status] = ls.to_i
end
options[:last_statuses_with_error] = 3
opts.on("-e", "--last-status-with-error num", "Number of failed from last statuses") do |lse|
options[:last_statuses_with_error] = lse.to_i
end
options[:verbose] = false
opts.on("-v", "--verbose true", "Verbose output") do |v|
options[:verbose] = true
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
parser.parse!
if !required.all? { |k| options.has_key?(k) }
abort parser.to_s
else
redis = Redis.new(:host => options[:host])
Resque.redis = redis
name = options[:name]
array_status = Resque::Plugins::Status::Hash.statuses.inject([]) do |result, e|
if e.name == name
if result.count < options[:num_last_status]
result << e
end
end
result
end
if array_status.count > 0
last_error = array_status[0].status
last_error_message = array_status[0].message
errors_in_last_n_status = array_status.inject(0){ |sum,e|
if options[:verbose]
puts "#{e.status} - #{e.time} - #{e.message}"
end
e.status == 'failed' ? sum+=1 : sum
}
status = :ok
last_three_status = array_status[0,3]
if options[:verbose]
puts "Last three-ish status"
end
errors_in_last_three_status = last_three_status.inject(0){ |sum,e|
if options[:verbose]
puts "#{e.status}"
end
e.status == 'failed' ? sum+=1 : sum
}
if errors_in_last_three_status == array_status.count or errors_in_last_n_status == options[:num_last_status]
status = :critical
elsif errors_in_last_n_status >= options[:last_statuses_with_error]
status = :warning
end
print status.to_s.upcase
print " - "
puts "#{errors_in_last_n_status}/#{array_status.count} - #{last_error_message} | '#{name}'=#{errors_in_last_n_status};#{options[:last_statuses_with_error]};#{options[:num_last_status]};;"
if (status == :critical)
exit(2)
elsif status == :warning
exit(1)
end
else
status = :unknown
puts "#{status.to_s.upcase} - #{name} - No data available"
exit(3)
end
end