#!/usr/bin/env ruby require 'rubygems' 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 opts.on("-e", "--errors", "Show only errors.") do commands << 'errors' end begin opts.parse!(ARGV) rescue OptionParser::ParseError => e warn e.message puts opts exit 1 end end if !ARGV.empty? && ARGV.length > 1 abort "Too many arguments; please specify only the directory containing the 'reptile.yml' file, or a yml config file itself." end config_file_name = "reptile.yml" config_file_locations = ["/etc/#{config_file_name}", "/etc/reptile/#{config_file_name}", "./#{config_file_name}"] config_location_param = ARGV.first config_file = nil if config_location_param if File.directory?(config_location_param) && File.exist?(File.join(config_location_param, config_file_name)) config_file = File.join(config_location_param, config_file_name) elsif File.exist?(config_location_param) && !File.directory?(config_location_param) config_file = config_location_param else abort "Please specify the directory containing the '#{config_file_name}' file, or the config file itself." end else config_file_locations.each do |f| if File.exist?(f) config_file = f break end end if config_file.nil? abort "Couldn't find a config file at #{config_file_locations.join(', ')}" end end Reptile::ReplicationMonitor.load_config_file(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