-
Notifications
You must be signed in to change notification settings - Fork 24
/
check_redis
executable file
·150 lines (124 loc) · 5.37 KB
/
check_redis
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
#!/usr/bin/env ruby
require 'optparse'
require 'rubygems'
require 'redis'
options = { :conn => { :host => "localhost", :port => 6379, :password => nil, :timeout => 5 },
:graphite => {},
:nagios => {}
}
OptionParser.new do |opt|
opt.banner = "Usage: #{$0} command <options>"
opt.separator ""
opt.separator "Nagios options:"
opt.on("-w", "--warn WARN", Integer, "Nagios warning level") { |warn| options[:nagios][:warn] = warn }
opt.on("-c", "--crit CRIT", Integer, "Nagios critical level") { |crit| options[:nagios][:crit] = crit }
options[:invert] = 0
opt.on('-i', '--invert', 'Invert the warning and critical thresholds (less than instead of greater than or equal to)') { |o| options[:invert] = 1 if o }
opt.separator ""
opt.separator "Connection options:"
opt.on('-H', '--host [HOSTNAME]', 'Hostname (Default: "localhost")') { |o| options[:conn][:host] = o if o }
opt.on('-p', '--port [PORT]', 'Port (Default: "6379")') { |o| options[:conn][:port] = o if o }
opt.on('-P', '--password [PASSWORD]', 'Password (Default: blank)') { |o| options[:conn][:password] = o if o }
opt.on('-t', '--timeout [TIMEOUT]', Integer, 'Timeout in seconds (Default: 5)') { |o| options[:conn][:timeout] = o if o }
opt.on('-D', '--database [DATABASE]', Integer, 'If using key, select db (Default: 0)') { |o| options[:conn][:db] = o if o }
options[:use_stats] = 1
opt.on('-K', '--key', 'Use redis key instead of stats') { |o| options[:use_stats] = 0 if o }
opt.on('-o', '--operation [OPERATION]', 'A redis operation (redis command) to perform. (Default: \'info\', \'get\' if -K)') { |o| options[:operation] = o if o }
opt.separator ""
opt.separator "Graphite options:"
opt.on('-G', '--graphite-server [HOSTNAME]', 'Graphite Hostname') { |o| options[:graphite][:host] = o if o }
opt.on('-O', '--graphite-port [PORT]', 'Graphite Port') { |o| options[:graphite][:port] = o if o }
opt.on('-M', '--metric-prefix [PREFIX]', 'Metric Prefix, command will be appended to it') { |o| options[:graphite][:metric] = o if o }
opt.on('-R', '--graph-elapsed-time', 'Also graph the time the check took') { |o| options[:graphite][:graph_elapsed_time] = o if o }
opt.separator ""
opt.on_tail("-h", "--help", "Show this message") do
puts opt
exit 0
end
end.parse!
command = ARGV[0]
class CheckRedis
def initialize(opts, cmd)
start_time = Time.now
@redis = Redis.new(opts[:conn])
if (opts[:use_stats] == 1)
stats = @redis.info
# if role is passed as the key, use the master or slave offset as the real key to check
if cmd == "role"
role = stats[cmd]
if role == "master" || role == "slave"
cmd = "#{role}_repl_offset"
end
end
if stats[cmd]
value = stats[cmd].to_i
else
puts "UNKNOWN: No such key in info - #{cmd}"
exit 3
end
else
if opts[:operation]
keyvalue = @redis.send("#{opts[:operation]}", cmd)
else
keyvalue = @redis.get(cmd)
end
if keyvalue
value = keyvalue.to_i
else
puts "UNKNOWN: No such key - #{cmd}"
exit 3
end
end
# Determine how long it took to query Redis
stop_time = Time.now
elapsed_time = (stop_time - start_time)
# Report results to Graphite
if opts[:graphite]
graph_result(value, opts[:graphite], cmd, elapsed_time)
end
check_result(value, opts[:nagios], cmd, opts[:invert], elapsed_time)
rescue Errno::ECONNREFUSED => e
puts e.message
exit 2
rescue Errno::ENETUNREACH => e
puts e.message
exit 2
rescue Errno::EHOSTUNREACH => e
puts e.message
exit 2
rescue Errno::EACCES => e
puts e.message
exit 2
rescue Redis::CommandError => e
puts "Did you enter the correct password? #{e.message}"
exit 3
end
private
def check_result(value, options, cmd, invert, elapsed_time)
if invert == 0 && value >= options[:crit]
puts "CRIT: #{cmd} exceeds critical level of #{options[:crit]} : #{value} | #{cmd}=#{value};#{options[:warn]};#{options[:crit]};; #{cmd}-elapsedTime=#{elapsed_time}"
exit 2
elsif invert == 0 && value >= options[:warn]
puts "WARN: #{cmd} exceeds warning level of #{options[:warn]} : #{value} | #{cmd}=#{value};#{options[:warn]};#{options[:crit]};; #{cmd}-elapsedTime=#{elapsed_time}"
exit 1
elsif invert == 1 && value < options[:crit]
puts "CRIT: #{cmd} is lower than the critical level of #{options[:crit]} : #{value} | #{cmd}=#{value};#{options[:warn]};#{options[:crit]};; #{cmd}-elapsedTime=#{elapsed_time}"
exit 2
elsif invert == 1 && value < options[:warn]
puts "WARN: #{cmd} is lower than warning level of #{options[:warn]} : #{value} | #{cmd}=#{value};#{options[:warn]};#{options[:crit]};; #{cmd}-elapsedTime=#{elapsed_time}"
exit 1
else
puts "OK: #{cmd} is ok | #{cmd}=#{value};#{options[:warn]};#{options[:crit]};; #{cmd}-elapsedTime=#{elapsed_time}"
exit 0
end
end
def graph_result(value, options, cmd, elapsed_time)
require 'graphite-api'
graphite_client = GraphiteAPI.new( graphite: "#{options[:host]}:#{options[:port]}" )
graphite_client.metrics "#{options[:metric]}.#{cmd}" => value
if options[:graph_elapsed_time]
graphite_client.metrics "#{options[:metric]}.#{cmd}-elapsedTime" => elapsed_time
end
end
end
CheckRedis.new(options, command)