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

#load_data, #perform

Methods included from Util

option

Instance Method Details

#actObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/notifu/actors/slack.rb', line 107

def act
  self.contacts.each do |contact|
    begin
      data = { channel: contact.slack_id }.merge(self.post_data(contact.slack_rich)) if contact.slack_rich
      data ||= { channel: contact.slack_id }.merge(self.post_data)
    rescue
      data = self.post_data
    end
    puts data.to_yaml
    Excon.post(Notifu::CONFIG[:actors][:slack][:url],
      tcp_nodelay: true,
      headers: { "ContentType" => "application/json" },
      body: data.to_json,
      expects: [ 200 ],
      idempotent: true
    )
  end
end

#colorObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/notifu/actors/slack.rb', line 45

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

#fallback_templateObject

template for fallback message



98
99
100
# File 'lib/notifu/actors/slack.rb', line 98

def fallback_template
  "*<%= data[:status] %>* <%= data[:datacenter]%> <%= data[:host] %> <%= data[:service] %> - <%= data[:message] %> (<%= data[:duration] %>) <%= data[:uchiwa_url] %>"
end

#post_data(rich = false) ⇒ Object



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
41
42
43
# File 'lib/notifu/actors/slack.rb', line 12

def post_data(rich = false)
  return {
    username: "notifu",
    icon_emoji: ":loudspeaker:",
    attachments: [
      {
        fallback: self.text(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
  return {
    username: "notifu",
    icon_emoji: ":loudspeaker:",
    text: self.text(self.template)
  }
end

#service_iconObject



73
74
75
76
77
# File 'lib/notifu/actors/slack.rb', line 73

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

#status_iconObject



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

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

#templateObject

template for plain message



103
104
105
# File 'lib/notifu/actors/slack.rb', line 103

def template
  "<%= data[:status_icon] %> *<<%= data[:uchiwa_url] %>|<%= data[:datacenter] %>/<%= data[:host] %>>* <%= data[:address] %> <%= data[:service_icon] %> *<%= data[:service] %>* - <%= data[:message] %> _<%= data[:duration] %>_"
end

#text(tmpl) ⇒ Object

fallback simple message (templated, see below)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/notifu/actors/slack.rb', line 80

def text(tmpl)
  data = OpenStruct.new({
    notifu_id: self.issue.notifu_id,
    datacenter: self.issue.datacenter,
    host: self.issue.host,
    address: self.issue.address,
    service: self.issue.service,
    message: self.issue.message,
    status: self.issue.code.to_state,
    duration: (Time.now.to_i - self.issue.time_created.to_i).duration,
    uchiwa_url: "#{Notifu::CONFIG[:uchiwa_url]}/#/client/#{self.issue.datacenter}/#{self.issue.host}?check=#{self.issue.service}",
    service_icon: self.service_icon,
    status_icon: self.status_icon
  })
  ERB.new(tmpl).result(data.instance_eval {binding})
end