Skip to content

Instantly share code, notes, and snippets.

@phrawzty
Created February 18, 2011 15:20
Show Gist options
  • Save phrawzty/833804 to your computer and use it in GitHub Desktop.
Save phrawzty/833804 to your computer and use it in GitHub Desktop.
Same as pyr's, but with Nagios-correct exit codes.
#!/usr/bin/env ruby
require 'mysql'
require 'yaml'
require 'optparse'
def parse argv
env = {:path => 'check_repli.yml'}
env[:path] = ENV['CHECK_REPLI_CONFIG'] if ENV.include? 'CHECK_REPLI_CONFIG'
OptionParser.new do |opts|
opts.banner = "Usage: check_db_repli -h host"
opts.on("-?", "--help", "Show this message") do
puts opts
exit 3
end
opts.on("-h", "--host host", String, "host to connect to") do |host|
env[:host] = host
end
opts.on("-f", "--config path", String, "configuration file") do |path|
env[:path] = path
end
begin
argv = ["-?"] if argv.empty?
opts.parse!(argv)
unless env.include? :host # need host to continue
STDERR.puts opts
exit 3
end
rescue OptionParser::ParseError => e
STDERR.puts e.message, "\n", opts
exit 3
end
end
env
end
def getconfig env
config = YAML.load_file env[:path]
raise "need configuration for #{env[:host]}" unless config.include? env[:host]
config[env[:host]]
end
def check_sql config, env
dbh = Mysql.connect(config['host'], config['user'], config['password'], config['database'])
res = dbh.query("show slave status")
vals = res.fetch_hash
fail_count = %w(Slave_IO_Running Slave_SQL_Running).select{|k| vals[k] !~ /yes/i}.length
if fail_count > 0
puts "CRIT: SQL replication not running on: #{config['host']}: #{vals['Last_SQL_Error']}"
exit 2
end
end
env = parse ARGV
config = getconfig env
check_sql config, env
# all good so far
puts "OK: SQL replication running"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment