Class: SystemdMon::Notifiers::Slack

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

Instance Method Summary collapse

Methods inherited from Base

#debug, #log

Constructor Details

#initializeSlack

Returns a new instance of Slack.



12
13
14
15
16
17
18
19
20
# File 'lib/systemd_mon/notifiers/slack.rb', line 12

def initialize(*)
  super
  self.notifier = ::Slack::Notifier.new(
    options.fetch('webhook_url'),
    channel: options['channel'],
    username: options['username'],
    icon_emoji: options['icon_emoji'],
    icon_url: options['icon_url'])
end

Instance Method Details

#notify!(notification) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/systemd_mon/notifiers/slack.rb', line 46

def notify!(notification)
  unit = notification.unit
  message = "@channel #{notification.type_text}: systemd unit #{unit.name} on #{notification.hostname} #{unit.state_change.status_text}"

  if notification.type == :info
    message = "#{notification.type_text}: systemd unit #{unit.name} on #{notification.hostname} #{unit.state_change.status_text}"
  end

  attach = {
    fallback: "@channel #{message}: #{unit.state.active} (#{unit.state.sub})",
    color: color(notification.type),
    fields: fields(notification)
  }

  debug("sending slack message with attachment: ")
  debug(attach.inspect)

  notifier.ping message, attachments: [attach]
  log "sent slack notification"
end

#notify_start!(hostname) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/systemd_mon/notifiers/slack.rb', line 22

def notify_start!(hostname)
  message = "@channel SystemdMon is starting on #{hostname}"

  attach = {
    fallback: message,
    text: message,
    color: "good"
  }

  notifier.ping "", attachments: [attach]
end

#notify_stop!(hostname) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/systemd_mon/notifiers/slack.rb', line 34

def notify_stop!(hostname)
  message = "@channel SystemdMon is stopping on #{hostname}"

  attach = {
    fallback: message,
    text: message,
    color: "danger"
  }

  notifier.ping "", attachments: [attach]
end