#!/usr/bin/env ruby require 'optparse' require 'rubygems' require 'reptile' Reptile::Log.level = :info Mixlib::Log::Formatter.show_time = false 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("--status", "Displays the slave status") do commands << 'check_slaves' end opts.on("--diff", "Checks the row count difference between master and slave") do commands << 'diff_tables' end opts.on("--report", "Prints a report") do commands << 'report' end opts.on("--heartbeat", "Checks the heartbeat timestamp difference between master and slave") do commands << 'heartbeat' end opts.on("--stop-slaves", "Stops all slaves") do commands << 'stop_slaves' end opts.on("--start-slaves", "Starts all slaves") do commands << 'start_slaves' end log_levels = [:debug, :info, :warn, :error, :fatal] opts.on("-l", "--log-level [LEVEL]", log_levels, "Specify log level (#{log_levels.join(',')})") do |t| Reptile::Log.level = t.to_sym 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 config file." end config_location_param = ARGV.first config_file = nil if config_location_param if File.exist?(config_location_param) && !File.directory?(config_location_param) config_file = config_location_param else abort "Please specify the config file." end else ["/etc/reptile.yml", "/etc/reptile/reptile.yml", "./reptile.yml"].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::Log.debug "Loading config from #{config_file}" 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