Permalink
Browse files

Merge pull request #32 from flores/master

redis replication check (for master or slave)
  • Loading branch information...
Kami committed Oct 4, 2011
2 parents 3117346 + aed866f commit 5c3289c15de95b6738062ff7ac8d3b2d2483c613
Showing with 118 additions and 8 deletions.
  1. +1 −1 any_connections.sh
  2. +1 −1 haproxy.rb
  3. +1 −1 http_connections.sh
  4. +1 −1 https_connections.sh
  5. +3 −3 new_ssl_connections.rb
  6. +1 −1 postfix_status.rb
  7. +110 −0 redis_replication_check.rb
View
@@ -27,7 +27,7 @@ if [ $SIMULTANEOUS -lt 1 ]; then
echo "status err no server listening!"
SIMULTANEOUS=0
else
- echo "status critical no users connected!";
+ echo "status err no users connected!";
fi
exit
else
View
@@ -55,5 +55,5 @@
ctl.close
else
- puts "status critical haproxy is not running!"
+ puts "status err haproxy is not running!"
end
View
@@ -37,7 +37,7 @@ if [ $SIMULTANEOUS -lt $THRESHOLD ]; then
echo "status err no server listening!"
exit
else
- echo "status critical less than $THRESHOLD users connected!";
+ echo "status err less than $THRESHOLD users connected!";
fi
else
echo "status ok ok";
View
@@ -37,7 +37,7 @@ if [ $SIMULTANEOUS -lt $THRESHOLD ]; then
echo "status err no server listening!"
exit
else
- echo "status critical less than $THRESHOLD users connected!";
+ echo "status err less than $THRESHOLD users connected!";
fi
exit
else
View
@@ -31,15 +31,15 @@
openssl = `which openssl`
unless openssl =~ /\w+/
- raise("status critical #{$0} needs openssl in the \$PATH")
+ raise("status err #{$0} needs openssl in the \$PATH")
end
openssl=openssl.chomp
# pick one of the ciphers
cipher = `#{openssl} ciphers HIGH`.grep /:.+?:/
-results = `#{openssl} s_time -connect #{host} -time #{time} -new -cipher #{cipher} 2>&1` || raise("status critical cannot test #{host}!")
+results = `#{openssl} s_time -connect #{host} -time #{time} -new -cipher #{cipher} 2>&1` || raise("status err cannot test #{host}!")
# how openssl displays new connections
if results.grep /tttttttttttttttttttttttttttttttttt/
@@ -55,6 +55,6 @@
end
end
else
- puts "status critical unexpected results! try running #{openssl} s_client -connect #{host} manually"
+ puts "status err unexpected results! try running #{openssl} s_client -connect #{host} manually"
end
View
@@ -25,7 +25,7 @@
puts "metric postfix_queue_size int #{messages}"
end
else
- puts "status critical postfix not running!"
+ puts "status err postfix not running!"
exit 2
end
View
@@ -0,0 +1,110 @@
+#!/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
+

0 comments on commit 5c3289c

Please sign in to comment.