Class: Notifu::Actors::Slack

Inherits:
Notifu::Actor show all
Defined in:
lib/notifu/actors/slack.rb

Instance Attribute Summary

Attributes inherited from Notifu::Actor

#contacts, #issue

Instance Method Summary collapse

Methods inherited from Notifu::Actor

#apply_template, #load_data, #perform

Methods included from Util

action_log, log, option

Instance Method Details

#actObject


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/notifu/actors/slack.rb', line 91

def act
  self.contacts.each do |contact|
    if contact.slack_id != nil
      data = self.post_data(contact.slack_template, contact.slack_rich).merge({ channel: contact.slack_id })
    else
      data = self.post_data(contact.slack_template, contact.slack_rich)
    end

    begin
      Excon.post(Notifu::CONFIG[:actors][:slack][:url],
        tcp_nodelay: true,
        headers: { "ContentType" => "application/json" },
        body: data.to_json,
        expects: [ 200 ],
        idempotent: true
      )
    rescue Exception => e
      log "error", "Failed to send message to Slack - #{e.class}: #{e.message}"
    end
  end
end

#colorObject


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/notifu/actors/slack.rb', line 57

def color
  case self.issue.code.to_i
  when 0
    "good"
  when 1
    "warning"
  when 2
    "danger"
  else
    "#999999"
  end
end

#default_templateObject


48
49
50
# File 'lib/notifu/actors/slack.rb', line 48

def default_template
  "#{self.status_icon} *<<%= uchiwa_url %>|<%= datacenter %>/<%= host %>>* <%= address %> #{self.service_icon} *<%= service %>* - <%= message %> _<%= duration %>_"
end

#fallback_templateObject


52
53
54
# File 'lib/notifu/actors/slack.rb', line 52

def fallback_template
  "<%= status %> | <%= datacenter%>/<%= host %>/<%= service %> - <%= message %>"
end

#post_data(template, rich) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/notifu/actors/slack.rb', line 11

def post_data(template, rich)
  return {
    username: "notifu",
    icon_emoji: ":loudspeaker:",
    attachments: [
      {
        fallback: self.apply_template(self.fallback_template),
        color: self.color,
        title: "#{self.issue.host} - #{self.issue.service}",
        title_link: "#{Notifu::CONFIG[:uchiwa_url]}/#/client/#{self.issue.datacenter}/#{self.issue.host}?check=#{self.issue.service}",
        text: self.issue.message,
        fields: [
          {
            title: "duration",
            value: (Time.now.to_i - self.issue.time_created.to_i).duration,
            short: true
          },
          {
            title: "notifu ID",
            value: self.issue.notifu_id,
            short: true
          }
        ]
      }
    ]
  } if rich != nil

  template ||= self.default_template
  return { username: "notifu", icon_emoji: ":loudspeaker:", text: self.text }
end

#service_iconObject


85
86
87
88
89
# File 'lib/notifu/actors/slack.rb', line 85

def service_icon
  return @service_icon if defined? @service_icon
  @service_icon = ":computer:" if self.issue.service == "keepalive"
  @service_icon ||= ":gear:"
end

#status_iconObject


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/notifu/actors/slack.rb', line 70

def status_icon
  return @status_icon if defined? @status_icon
  case self.issue.code.to_i
  when 0
    @status_icon = ":white_check_mark:"
  when 1
    @status_icon = ":exclamation:"
  when 2
    @status_icon = ":bangbang:"
  else
    @status_icon = ":question: _#{self.issue.code.to_s}_"
  end
  @status_icon
end

#text(c) ⇒ Object


42
43
44
45
46
# File 'lib/notifu/actors/slack.rb', line 42

def text(c)
  t = c.slack_template
  t ||= self.default_template
  self.apply_template t
end