#!/usr/bin/env ruby require 'optparse' require 'reptile' commands = [] OptionParser.new do |opts| opts.banner = "Usage: #{File.basename($0)} [path_to_config_file]" opts.on("-h", "--help", "Displays this help info") do puts opts exit 0 end opts.on("-s", "--status", "Displays the slave status") do commands << 'check_slaves' end opts.on("-d", "--diff", "Checks the row count difference between master and slave") do commands << 'diff_tables' end opts.on("-r", "--report", "Sends a report email") do commands << 'report' end opts.on("-b", "--heartbeat", "Checks the heartbeat timestamp difference between master and slave") do commands << 'heartbeat' end opts.on("-x", "--stop_slaves", "Stops all slaves") do commands << 'stop_slaves' end opts.on("-g", "--start_slaves", "Starts all slaves") do commands << 'start_slaves' end begin opts.parse!(ARGV) rescue OptionParser::ParseError => e warn e.message puts opts exit 1 end end config_file = 'replication.yml' if ARGV.empty? && !File.exists?(config_file) abort "Please specify the directory containing the '#{config_file}' file, e.g. `#{File.basename($0)} ~/repl'" elsif !ARGV.empty? && !File.exists?(ARGV.first) abort "`#{ARGV.first}' does not exist." elsif !ARGV.empty? && !File.directory?(ARGV.first) abort "`#{ARGV.first}' is not a directory." elsif !ARGV.empty? && ARGV.length > 1 abort "Too many arguments; please specify only the directory to #{File.basename($0)}." end Reptile::ReplicationMonitor.load_config_file(ARGV.first.nil? ? config_file : "#{ARGV.first}/#{config_file}") if (commands.include?('start_slaves') || commands.include?('stop_slaves')) Reptile::Runner.send(commands[0]) else (commands.empty? ? ['check_slaves', 'heartbeat', 'diff_tables'] : commands).each do |command| Reptile::ReplicationMonitor.send(command) end end