-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathredis_replication_check.rb
executable file
·110 lines (96 loc) · 3.1 KB
/
redis_replication_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
#!/usr/bin/env ruby
###############
#
# Checks redis replication status.
# Can be used with master or slave.
#
# Copyright C. Flores github@petalphile.com
# Released under the MIT License:
# no warranty but you should feel free to
# do whatever you want with it.
#
###############
require 'optparse'
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Checks redis replication status. Usage: #{$0} -r <master|slave> <options>"
options[:host] = "localhost"
opts.on( '-h', '--host <host>', 'connect to redis running on <host>. defaults to localhost' ) do |host|
options[:host] = host.to_s
end
options[:port] = 6379
opts.on( '-p', '--port <port>', 'connect to redis running on <port>. defaults to 6379' ) do |port|
options[:port] = port.to_i
end
options[:role] = nil
opts.on( '-r', '--role <master|slave>', 'redis should be master or slave' ) do |role|
options[:role] = role.to_s
end
options[:connected_slaves] = nil
opts.on( '-c', '--connected_slaves <int>', 'master should have <int> slaves. must be used with -r/--role master. defaults to 1' ) do |int|
options[:connected_slaves] = int.to_i
end
options[:slave_lag] = nil
opts.on( '-l', '--replication_lag <int>', 'master io cannot be more than <int> seconds behind. used with -r/--role slave. default is 0' ) do |int|
options[:slave_lag] = int.to_i
end
opts.on( '--help', 'Display this screen' ) do
puts opts
exit
end
end
begin
optparse.parse!
if options[:role] == "slave"
if options[:connected_slaves]
puts "Error: -c/--connected_slaves cannot be used with -r/--role slave."
puts optparse
exit 2
end
elsif options[:role] == "master"
if options[:slave_lag]
puts "Error: -l/--slave_lag cannot be used with -r/--role master."
puts optparse
exit 2
end
else
puts "Error: must set -r/--role"
puts optparse
exit 2
end
rescue OptionParser::InvalidOption
puts "Error: unknown option"
puts optparse
exit 2
end
# the actual check
redis_info = `redis-cli -h #{options[:host]} -p #{options[:port]} info`
if $? != 0
puts "status err could not connect to redis on #{options[:host]}:#{options[:port]}"
exit 1
end
status = Hash.new
redis_info.each do |line|
key,value = line.chomp.split(':')
status[key] = value
end
if options[:role] == "slave"
expectedlag = options[:slave_lag].to_i || 0
if status["role"] =~ /slave/ \
&& status["master_link_status"] =~ /up/ \
&& status["master_last_io_seconds_ago"].to_i <= expectedlag
puts "status ok redis thinks it is slave"
else
puts "status err redis thinks it is #{status["role"]}, master is #{status["master_link_status"]} and last io was #{status["master_last_io_seconds_ago"]}s."
end
puts "metric master_last_io_seconds_ago int #{status["master_last_io_seconds_ago"]}"
else
expectedslaves = options[:connected_slaves] || 1
actualslaves = status["connected_slaves"].to_i
if status["role"] =~ /master/ && actualslaves == expectedslaves
puts "status ok redis is master with #{actualslaves} slaves."
else
puts "status err redis thinks it is #{status["role"]} with #{actualslaves} slaves."
end
puts "metric connected_slaves int #{actualslaves}"
end