Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 111 lines (96 sloc) 3.17 kb
a21828a @flores checks redis replication status
flores authored
1 #!/usr/bin/env ruby
2
3 ###############
4 #
5 # Checks redis replication status.
6 # Can be used with master or slave.
7 #
8 # Copyright C. Flores github@petalphile.com
9 # Released under the MIT License:
10 # no warranty but you should feel free to
11 # do whatever you want with it.
12 #
13 ###############
14
15 require 'optparse'
16
17 options = {}
18
19 optparse = OptionParser.new do|opts|
027e3e7 @flores usage instructions
flores authored
20 opts.banner = "Checks redis replication status. Usage: #{$0} -r <master|slave> <options>"
a21828a @flores checks redis replication status
flores authored
21
22 options[:host] = "localhost"
23 opts.on( '-h', '--host <host>', 'connect to redis running on <host>. defaults to localhost' ) do |host|
24 options[:host] = host.to_s
25 end
26 options[:port] = 6379
27 opts.on( '-p', '--port <port>', 'connect to redis running on <port>. defaults to 6379' ) do |port|
28 options[:port] = port.to_i
29 end
30 options[:role] = nil
445e474 @flores cloudkick verbiage
flores authored
31 opts.on( '-r', '--role <master|slave>', 'redis should be master or slave' ) do |role|
a21828a @flores checks redis replication status
flores authored
32 options[:role] = role.to_s
33 end
8f2cd76 @flores check master redis for n connected slaves
flores authored
34 options[:connected_slaves] = nil
a21828a @flores checks redis replication status
flores authored
35 opts.on( '-c', '--connected_slaves <int>', 'master should have <int> slaves. must be used with -r/--role master. defaults to 1' ) do |int|
36 options[:connected_slaves] = int.to_i
37 end
027e3e7 @flores usage instructions
flores authored
38 options[:slave_lag] = nil
445e474 @flores cloudkick verbiage
flores authored
39 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|
a21828a @flores checks redis replication status
flores authored
40 options[:slave_lag] = int.to_i
41 end
42 opts.on( '--help', 'Display this screen' ) do
43 puts opts
44 exit
45 end
46 end
47
48 begin
49 optparse.parse!
50 if options[:role] == "slave"
51 if options[:connected_slaves]
52 puts "Error: -c/--connected_slaves cannot be used with -r/--role slave."
53 puts optparse
54 exit 2
55 end
56 elsif options[:role] == "master"
57 if options[:slave_lag]
58 puts "Error: -l/--slave_lag cannot be used with -r/--role master."
59 puts optparse
60 exit 2
61 end
62 else
63 puts "Error: must set -r/--role"
64 puts optparse
65 exit 2
66 end
67 rescue OptionParser::InvalidOption
68 puts "Error: unknown option"
69 puts optparse
70 exit 2
71 end
72
73 # the actual check
74
75 redis_info = `redis-cli -h #{options[:host]} -p #{options[:port]} info`
76
77 if $? != 0
445e474 @flores cloudkick verbiage
flores authored
78 puts "status err could not connect to redis on #{options[:host]}:#{options[:port]}"
a21828a @flores checks redis replication status
flores authored
79 exit 1
80 end
81
82 status = Hash.new
83 redis_info.each do |line|
84 key,value = line.chomp.split(':')
85 status[key] = value
86 end
87
88 if options[:role] == "slave"
89 expectedlag = options[:slave_lag].to_i || 0
90 if status["role"] =~ /slave/ \
8f2cd76 @flores check master redis for n connected slaves
flores authored
91 && status["master_link_status"] =~ /up/ \
92 && status["master_last_io_seconds_ago"].to_i <= expectedlag
a21828a @flores checks redis replication status
flores authored
93
aed866f @flores added metric for connected slave and always output status per @kami's re...
flores authored
94 puts "status ok redis thinks it is slave"
a21828a @flores checks redis replication status
flores authored
95 else
445e474 @flores cloudkick verbiage
flores authored
96 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."
a21828a @flores checks redis replication status
flores authored
97 end
98 puts "metric master_last_io_seconds_ago int #{status["master_last_io_seconds_ago"]}"
99 else
8f2cd76 @flores check master redis for n connected slaves
flores authored
100 expectedslaves = options[:connected_slaves] || 1
101 actualslaves = status["connected_slaves"].to_i
102 if status["role"] =~ /master/ && actualslaves == expectedslaves
a21828a @flores checks redis replication status
flores authored
103
aed866f @flores added metric for connected slave and always output status per @kami's re...
flores authored
104 puts "status ok redis is master with #{actualslaves} slaves."
a21828a @flores checks redis replication status
flores authored
105 else
8f2cd76 @flores check master redis for n connected slaves
flores authored
106 puts "status err redis thinks it is #{status["role"]} with #{actualslaves} slaves."
a21828a @flores checks redis replication status
flores authored
107 end
aed866f @flores added metric for connected slave and always output status per @kami's re...
flores authored
108 puts "metric connected_slaves int #{actualslaves}"
a21828a @flores checks redis replication status
flores authored
109 end
110
Something went wrong with that request. Please try again.