class SystemdMon::Notifiers::Slack

Attributes

notifier[RW]

Public Class Methods

new(*) click to toggle source
Calls superclass method SystemdMon::Notifiers::Base::new
# 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

Public Instance Methods

notify!(notification) click to toggle source
# 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}"

  return if notification.type == :info

  # 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) click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

color(type) click to toggle source
# File lib/systemd_mon/notifiers/slack.rb, line 92
def color(type)
  case type
  when :alert
    'danger'
  when :warning
    '#FF9900'
  when :info
    '#0099CC'
  else
    'good'
  end
end
fields(notification) click to toggle source
# File lib/systemd_mon/notifiers/slack.rb, line 72
def fields(notification)
  f = [
    {
      title: "Hostname",
      value: notification.hostname,
      short: true
    },
    {
      title: "Unit",
      value: notification.unit.name,
      short: true
    }
  ]

  changes = notification.unit.state_change.diff.map(&:last)
  f.concat(changes.map { |v|
    { title: v.display_name, value: v.value, short: true }
  })
end