Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Commit

Permalink
check_twemproxy only returns errors if requests have been attempted
Browse files Browse the repository at this point in the history
The check would return errors for shards with no active
connections. This is a valid state for a shard to be in,
as Twemproxy will disconnect shards that have no activity.

Instead, we check twice and only report errors if there have
been requests to a shard that is down.

Also, the check has been translated entirely into ruby.
  • Loading branch information
sax committed Sep 1, 2013
1 parent 0f532f1 commit 3077835
Showing 1 changed file with 54 additions and 60 deletions.
114 changes: 54 additions & 60 deletions check_twemproxy
@@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env ruby
# ======================================================================================== # ========================================================================================
# Twemproxy Status Check using JSON status page # Twemproxy Status Check using JSON status page
# #
Expand All @@ -12,80 +12,74 @@
# CRITICAL otherwise. # CRITICAL otherwise.
# ======================================================================================== # ========================================================================================


require 'optparse'
require 'json'

# Nagios return codes # Nagios return codes
STATE_OK=0 STATE_OK=0
STATE_WARNING=1 STATE_WARNING=1
STATE_CRITICAL=2 STATE_CRITICAL=2
STATE_UNKNOWN=3 STATE_UNKNOWN=3


HOST="" options = Struct.new('Options', :host, :port).new
PORT=22222 options.port = 22222
DESCRIPTION=""
PROBLEM=


# Parse parameters optparse = OptionParser.new do |opts|
while [ $# -gt 0 ]; do opts.banner = "Usage: check_twemproxy [-h host] [-p port]"
case "$1" in
-h | --host)
shift
HOST=$1
;;
-q | --port)
shift
PORT=$1
;;
*) echo "Unknown argument: $1"
exit $STATE_UNKNOWN
;;
esac
shift
done


PATH=/opt/local/bin:$PATH opts.on("-h", "--host HOST", String, "Host name or IP address") do |h|
NODENAME=$HOSTNAME options.host = h
end


function result { opts.on("-p", "--port PORT", Integer, "Port") do |p|
DESCRIPTION=$1 options.port = p
STATUS=$2 end
echo "TWEMPROXY $DESCRIPTION : ${HOST} ${PROBLEM}"
exit $STATUS
}


opts.on('-?', '--help', 'Display this screen') do
puts opts
exit
end
end


if [ -z "$HOST" ]; then begin
echo "ERROR: host is required" optparse.parse!
exit 1 raise OptionParser::MissingArgument.new("host is required") unless options.host
fi rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
puts e.message
puts optparse
exit STATE_UNKNOWN
end


NODENAME=`hostname`


OUTPUT=`nc $HOST $PORT | ruby -e ' firstrun = JSON.parse(`nc #{options.host} #{options.port}`)
require "rubygems" sleep 2
require "json" secondrun = JSON.parse(`nc #{options.host} #{options.port}`)
hash = JSON.parse(STDIN.read)
errors = {} errors = {}
error_clusters = {} error_clusters = {}
hash.keys.find_all{|k| hash[k].is_a?(Hash)}.each do |cluster| secondrun.keys.find_all{|k| secondrun[k].is_a?(Hash)}.each do |cluster|
hash[cluster].keys.find_all{|v| hash[cluster][v].is_a?(Hash)}.each do |server| secondrun[cluster].keys.find_all{|v| secondrun[cluster][v].is_a?(Hash)}.each do |server|
if hash[cluster][server]["server_connections"].to_i <= 0 next if secondrun[cluster][server]["server_connections"].to_i > 0
errors[server] = 1 next if secondrun[cluster][server]["requests"].to_i - firstrun[cluster][server]["requests"].to_i == 0
error_clusters[cluster] = 1
end
end
end


if error_clusters.empty? errors[server] = 1
exit 0 error_clusters[cluster] = 1
else
puts \
"error with redis cluster [#{error_clusters.keys.join(%Q[,])}] " \
"problem shards: #{errors.keys.join(%Q[,])}"
end end
' 2>&1 ` end


unless error_clusters.empty?
problem = <<-END
error with redis cluster [#{error_clusters.keys.join(",")}]
problem shards: #{errors.keys.join(",")}
END
end


if [ -z "$OUTPUT" ]; then if problem.nil?
result "OK" $STATE_OK puts "TWEMPROXY OK : #{options.host}"
exit STATE_OK
else else
PROBLEM="$OUTPUT" puts "TWEMPROXY CRITICAL : #{options.host} #{problem}"
result "CRITICAL" $STATE_CRITICAL exit STATE_CRITICAL
fi end

0 comments on commit 3077835

Please sign in to comment.