class SystemdMon::Monitor

Attributes

change_callback[RW]
dbus_manager[RW]
each_state_change_callback[RW]
hostname[RW]
notification_centre[RW]
units[RW]

Public Class Methods

new(dbus_manager) click to toggle source
# File lib/systemd_mon/monitor.rb, line 10
def initialize(dbus_manager)
  self.hostname     = `hostname`.strip
  self.dbus_manager = dbus_manager
  self.units        = []
  self.change_callback     = lambda(&method(:unit_change_callback))
  self.notification_centre = NotificationCentre.new
  Thread.abort_on_exception = true
end

Public Instance Methods

add_notifier(notifier) click to toggle source
# File lib/systemd_mon/monitor.rb, line 19
def add_notifier(notifier)
  notification_centre << notifier
  self
end
on_change(&callback) click to toggle source
# File lib/systemd_mon/monitor.rb, line 36
def on_change(&callback)
  self.change_callback = callback
  self
end
on_each_state_change(&callback) click to toggle source
# File lib/systemd_mon/monitor.rb, line 41
def on_each_state_change(&callback)
  self.each_state_change_callback = callback
  self
end
register_unit(unit_name) click to toggle source
# File lib/systemd_mon/monitor.rb, line 24
def register_unit(unit_name)
  self.units << dbus_manager.fetch_unit(unit_name)
  self
end
register_units(*unit_names) click to toggle source
# File lib/systemd_mon/monitor.rb, line 29
def register_units(*unit_names)
  self.units.concat unit_names.flatten.map { |unit_name|
    dbus_manager.fetch_unit(unit_name)
  }
  self
end
start() click to toggle source
# File lib/systemd_mon/monitor.rb, line 46
def start
  startup_check!
  at_exit { notification_centre.notify_stop! hostname }
  notification_centre.notify_start! hostname

  Logger.puts "Monitoring changes to #{units.count} units"
  Logger.debug { " - " + units.map(&:name).join("\n - ") + "\n\n" }
  Logger.debug { "Using notifiers: #{notification_centre.classes.join(", ")}"}

  state_q = Queue.new

  units.each do |unit|
    unit.register_listener! state_q
  end

  [start_callback_thread(state_q),
   start_dbus_thread].each(&:join)
end

Protected Instance Methods

start_callback_thread(state_q) click to toggle source
# File lib/systemd_mon/monitor.rb, line 84
def start_callback_thread(state_q)
  Thread.new do
    manager = CallbackManager.new(state_q)
    manager.start change_callback, each_state_change_callback
  end
end
start_dbus_thread() click to toggle source
# File lib/systemd_mon/monitor.rb, line 78
def start_dbus_thread
  Thread.new do
    dbus_manager.runner.run
  end
end
startup_check!() click to toggle source
# File lib/systemd_mon/monitor.rb, line 68
def startup_check!
  unless units.any?
    raise MonitorError, "At least one systemd unit should be registered before monitoring can start"
  end
  unless notification_centre.any?
    raise MonitorError, "At least one notifier should be registered before monitoring can start"
  end
  self
end
unit_change_callback(unit) click to toggle source
# File lib/systemd_mon/monitor.rb, line 91
def unit_change_callback(unit)
  Logger.puts "#{unit.name} #{unit.state_change.status_text}: #{unit.state.active} (#{unit.state.sub})"
  Logger.debug unit.state_change.to_s
  Logger.puts
  notification_centre.notify! Notification.new(hostname, unit)
end