class SystemdMon::CLI

Attributes

me[RW]
options[RW]
verbose[RW]

Public Class Methods

new() click to toggle source
# File lib/systemd_mon/cli.rb, line 9
def initialize
  self.me      = "systemd_mon"
  self.verbose = true
end

Public Instance Methods

start() click to toggle source
# File lib/systemd_mon/cli.rb, line 14
def start
  yaml_config_file = ARGV.first
  self.options = load_and_validate_options(yaml_config_file)
  self.verbose = options['verbose'] || false
  Logger.verbose = verbose

  start_monitor

rescue SystemdMon::Error => e
  err_string = e.message
  if verbose
    if e.original
      err_string << " - #{e.original.message} (#{e.original.class})"
      err_string << "\n\t#{e.original.backtrace.join("\n\t")}"
    else
      err_string << " (#{e.class})"
      err_string << "\n\t#{e.backtrace.join("\n\t")}"
    end
  end
  fatal_error(err_string)
rescue => e
  err_string = e.message
  if verbose
    err_string << " (#{e.class})"
    err_string << "\n\t#{e.backtrace.join("\n\t")}"
  end
  fatal_error(err_string)
end

Protected Instance Methods

fatal_error(message, code = 255) click to toggle source
# File lib/systemd_mon/cli.rb, line 78
def fatal_error(message, code = 255)
  $stderr.puts " #{me} error: #{message}"
  exit code
end
load_and_validate_options(yaml_config_file) click to toggle source
# File lib/systemd_mon/cli.rb, line 58
def load_and_validate_options(yaml_config_file)
  options = load_options(yaml_config_file)

  unless options.has_key?('notifiers') && options['notifiers'].any?
    fatal_error("no notifiers have been defined, there is no reason to continue")
  end
  unless options.has_key?('units') && options['units'].any?
    fatal_error("no units have been added for watching, there is no reason to continue")
  end
  options
end
load_options(yaml_config_file) click to toggle source
# File lib/systemd_mon/cli.rb, line 70
def load_options(yaml_config_file)
  unless yaml_config_file && File.exists?(yaml_config_file)
    fatal_error "First argument must be a path to a YAML configuration file"
  end

  YAML.load_file(yaml_config_file)
end
start_monitor() click to toggle source
# File lib/systemd_mon/cli.rb, line 44
def start_monitor
  monitor = Monitor.new(DBusManager.new)

  # Load units to monitor
  monitor.register_units options['units']

  options['notifiers'].each do |name, notifier_options|
    klass = NotifierLoader.new.get_class(name)
    monitor.add_notifier klass.new(notifier_options)
  end

  monitor.start
end