Class: SystemdMon::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/systemd_mon/monitor.rb

Instance Method Summary collapse

Constructor Details

#initialize(dbus_manager) ⇒ Monitor

Returns a new instance of Monitor.



10
11
12
13
14
15
16
17
# 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

Instance Method Details

#add_notifier(notifier) ⇒ Object



19
20
21
22
# File 'lib/systemd_mon/monitor.rb', line 19

def add_notifier(notifier)
  notification_centre << notifier
  self
end

#on_change(&callback) ⇒ Object



36
37
38
39
# File 'lib/systemd_mon/monitor.rb', line 36

def on_change(&callback)
  self.change_callback = callback
  self
end

#on_each_state_change(&callback) ⇒ Object



41
42
43
44
# 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) ⇒ Object



24
25
26
27
# 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) ⇒ Object



29
30
31
32
33
34
# 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

#startObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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