Created
February 15, 2017 17:02
-
-
Save maurorappa/76ffdf01f2ebe05d9fb41cbd652cf360 to your computer and use it in GitHub Desktop.
Sensu check Redis test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# No Frills script with basic dependencies | |
# | |
require 'rubygems' if RUBY_VERSION < '1.9.0' | |
require 'sensu-plugin/check/cli' | |
require 'socket' | |
require 'timeout' | |
class PingRedis < Sensu::Plugin::Check::CLI | |
option :host, | |
:short => '-H HOSTNAME', | |
:long => '--hostname HOSTNAME', | |
:description => 'Host to connect to', | |
:default => 'localhost' | |
option :port, | |
:short => '-p PORT', | |
:long => '--port PORT', | |
:proc => proc {|a| a.to_i }, | |
:default => 6379 | |
option :timeout, | |
:short => '-t SECS', | |
:long => '--timeout SECS', | |
:description => 'Connection timeout', | |
:proc => proc {|a| a.to_i }, | |
:default => 1 | |
def get_banner | |
begin | |
timeout(config[:timeout]) do | |
sock = TCPSocket.new(config[:host], config[:port]) | |
sock.puts 'ping' | |
sock.readline | |
end | |
rescue Errno::ECONNREFUSED | |
critical "Connection refused by #{config[:host]}:#{config[:port]}" | |
rescue Timeout::Error | |
critical "Connection or read timed out" | |
rescue Errno::EHOSTUNREACH | |
critical "Check failed to run: No route to host" | |
rescue EOFError | |
critical "Connection closed unexpectedly" | |
end | |
end | |
def run | |
banner = get_banner | |
#message banner | |
banner =~ /PONG/ ? ok : warning | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment