#!/usr/bin/env ruby Signal.trap("INT") { exit 1 } require 'remon' require 'remon/logger' require 'set' require 'optparse' logger = Remon::Logger.logger logger.level = Logger::INFO options = {} check_load_paths = Set.new opts_parser = OptionParser.new do |opts| banner = [] banner << "Usage: remon [options] /path/to/config.rb" banner << "Options: " opts.banner = banner.join("\n") opts.on("-d", "--config-dir [config dir]" , "set config dir") do |d| options[:config_dir] = d end opts.on("--checks-dir [checks dir]" , "set checks dir") do |d| check_load_paths << d end opts.on("-v", "--version", "Show version") do |v| puts ::Remon::VERSION exit end opts.on("--debug", "Show debug messages") do options[:debug] = true logger.level = ::Logger::DEBUG end opts.on("--trace", "Show debug messages and exception stack trace") do options[:debug] = true options[:trace] = true logger.level = ::Logger::DEBUG logger.trace_mode = true end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end opts_parser.order!(ARGV) require 'remon/config' require 'remon/scheduler' require 'remon/deduped_queue' require 'remon/check_runner' require 'remon/event_processor' require 'remon/sysinfo' begin Remon::Sysinfo.init Thread.abort_on_exception = true config_path = ARGV.shift config = Remon::Config.new( config_file: config_path, config_dir: options[:config_dir], load_paths: check_load_paths ).config queue = Remon::DedupedQueue.new scheduler = Remon::Scheduler.new(config[:schedule], queue: queue, scheduler_offset: config[:scheduler_offset]) processor = Remon::EventProcessor.new(config[:process_proc]) processor.start check_runner = Remon::CheckRunner.new( task_queue: queue, result_queue: processor.queue, num_workers: config[:workers] ) check_runner.start i = ENV["SCHEDULER_ITERATIONS"] || :inf i = i.is_a?(String) ? i.to_i : i scheduler.run(i) rescue Remon::Error => e cause = e.cause if true puts cause cause ? (raise cause) : (raise e) else logger.debug "#{cause.message}" if cause logger.error "#{e.message}" abort end end