Class: SystemdMon::Notifiers::Email

Inherits:
Base
  • Object
show all
Defined in:
lib/systemd_mon/notifiers/email.rb

Instance Method Summary collapse

Methods inherited from Base

#debug, #log

Constructor Details

#initializeEmail

Returns a new instance of Email.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/systemd_mon/notifiers/email.rb', line 13

def initialize(*)
  super
  if options['smtp']
    opts = options
    Mail.defaults do
      delivery_method :smtp, Hash[opts['smtp'].map { |h, k| [h.to_sym, k] }]
    end
  end

  validate_options!
end

Instance Method Details

#notify!(notification) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/systemd_mon/notifiers/email.rb', line 25

def notify!(notification)
  unit = notification.unit
  subject = "#{notification.type_text}: #{unit.name} on #{notification.hostname}: #{unit.state_change.status_text}"
  message = "Systemd unit #{unit.name} on #{notification.hostname} #{unit.state_change.status_text}: #{unit.state.active} (#{unit.state.sub})\n\n"
  if unit.state_change.length > 1
    message << SystemdMon::Formatters::StateTableFormatter.new(unit).as_text
  end
  message << "\nRegards, SystemdMon"

  send_mail subject, message

  log "sent email notification"
end