This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
forked from shift/nagios-check-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_redis_replication
executable file
·102 lines (86 loc) · 2.49 KB
/
check_redis_replication
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
#!/usr/bin/env ruby
require 'optparse'
require 'rubygems'
require 'redis'
options = {:host => 'localhost',
:port => 6379,
:db => 0,
:password => nil,
:timeout => 3,
:lag_io => 30,
:max_io => 300}
optparser = OptionParser.new do |opt|
opt.banner = "Usage: #{$0} -H <hostname> [options]"
opt.on('-H', '--host HOSTNAME', 'Hostname') {|o| options[:host] = o if o }
opt.on('-p', '--port', 'Port (Default: "6379")') {|o| options[:port] = o if o }
opt.on('-P', '--password PASSWORD', 'Password (Default: blank)') {|o| options[:password] = o if o }
opt.on('-T', '--timeout', 'Timeout in seconds (Default: 3)') {|o| options[:timeout] = o if o }
opt.on('-M', '--max_io SECONDS', "Max master_last_io_seconds") {|o| options[:max_io] = o.to_i if o}
opt.on('-L', '--lag_io SECONDS', "Max master_last_io_seconds") {|o| options[:lag_io] = o.to_i if o}
end
args = []
begin
args = optparser.parse!
rescue => e
puts "UNKNOWN: blew up parsing options #{e.to_s}"
exit 4
end
class CheckRedisReplication
KEYS = %w(master_host master_last_io_seconds_ago master_link_status role)
def initialize(opts)
@options = opts
end
def check
process Redis.new(@options)
rescue Errno::ECONNREFUSED
puts "CRITICAL: Connection refused."
exit 2
rescue Errno::ENETUNREACH
puts "CRITICAL: Network is unreachable."
exit 2
rescue Errno::EHOSTUNREACH
puts "CRITICAL: No route to host."
exit 2
end
private
def process(redis)
parse redis.info
status = get_status
if (@role == "slave" && @master_link_status == "up" && @last_io)
if (@last_io.to_i < @options[:lag_io])
puts "OK: #{status}"
exit 0
elsif (@last_io.to_i < @options[:max_io])
puts "WARNING: LAG #{status}"
exit 1
else
puts "CRITICAL: LINK TIMEOUT #{status}"
exit 2
end
else
puts "CRITICAL: LINK DOWN #{status}"
exit 2
end
end
def get_status
"#{@role} of #{@master_host} link #{@master_link_status} last #{@last_io}|last_io=#{@last_io}s"
end
def parse(info)
info.each do |key,value|
send("parse_#{key}".to_sym, value) if KEYS.include? key.to_s
end
end
def parse_role(value)
@role = value
end
def parse_master_host(value)
@master_host = value
end
def parse_master_last_io_seconds_ago(value)
@last_io = value
end
def parse_master_link_status(value)
@master_link_status = value
end
end
CheckRedisReplication.new(options).check