Class: Malsh::Notification::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/malsh/notification/slack.rb

Class Method Summary collapse

Class Method Details

.doit?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/malsh/notification/slack.rb', line 66

def self.doit?
  i(slack_webhook slack_channel).all? {|k| Malsh.options.key?(k) || ENV[k.to_s.upcase] }
end

.notifierObject



70
71
72
73
74
75
76
77
# File 'lib/malsh/notification/slack.rb', line 70

def self.notifier
  ::Slack::Notifier.new(
    ENV["SLACK_WEBHOOK"] || Malsh.options[:slack_webhook],
    channel: ENV["SLACK_CHANNEL"] || Malsh.options[:slack_channel],
    username: ENV["SLACK_USER"] || Malsh.options[:slack_user] || 'Mackerel-Check',
    link_names: 1
  )
end

.notify_alert(subject, alerts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/malsh/notification/slack.rb', line 20

def self.notify_alert(subject, alerts)
  org = Mackerel.org.name
  attachments = []
  alerts.each do |alert|
    color = case alert.status
            when 'CRITICAL'
              'danger'
            when 'WARNING'
              'warning'
            else
              ''
            end

    title = if Malsh.alert_has_host?(alert)
              alert.host.name
            else
              alert.monitor.name
            end

    author_name = if Malsh.alert_has_host?(alert)
                    alert.host.roles.map{|k, v| v.map{|r| "#{k}: #{r}"}}.flatten.join(" ")
                  else
                    ''
                  end

    attachments << {
        author_name: author_name,
        title: title,
        title_link: "https://mackerel.io/orgs/#{org}/alerts/#{alert.id}",
        text: alert.message,
        color: color,
        fields: [
            {
                title: 'Type',
                value: alert.type
            },
            {
                title: 'OpenedAt',
                value: Time.at(alert.openedAt).strftime("%Y/%m/%d %H:%M:%S")
            }
        ]
    }
  end
  notifier.ping "*#{subject}*", attachments: attachments
end

.notify_host(subject, hosts) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/malsh/notification/slack.rb', line 4

def self.notify_host(subject, hosts)
  return unless doit?
  lists = if Malsh.options[:org]
            hosts.map do |h|
              "<https://mackerel.io/orgs/#{Malsh.options[:org]}/hosts/#{h.id}/-/setting|#{h.name}(#{h.roles.keys.join(",")})>"
            end
          else
            hosts.map(&:name)
          end
  note = {
    text: lists.join("\n"),
    color: "warning"
  }
  notifier.ping "*#{subject}*", attachments: [note]
end