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



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

def act
  self.contacts.each do |contact|
    Excon.post(contact.slack_url,
      tcp_nodelay: true,
      headers: { "ContentType" => "application/json" },
      body: self.post_data,
      expects: [ 200 ],
      idempotent: true
    )
  end
end

#colorObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/notifu/actors/slack.rb', line 54

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

#post_dataObject



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
44
45
46
47
48
49
50
51
52
# File 'lib/notifu/actors/slack.rb', line 16

def post_data
  {
    username: "notifu",
    icon_emoji: ":loudspeaker:",
    attachments: [
      {
        fallback: self.text,
        color: self.color,
        title: "#{self.issue.host} - #{self.issue.service}",
        title_link: "https://sensu.skypicker.com/#/client/#{Notifu::CONFIG[:actors][:slack][:dc]}/#{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: "started",
            value: Time.at(self.issue.time_created.to_i),
            short: true
          },
          {
            title: "occurrences/trigger",
            value: "#{self.issue.occurrences_count}/#{self.issue.occurrences_trigger}",
            short: true
          },
          {
            title: "notifu ID",
            value: self.issue.notifu_id,
            short: true
          }
        ]
      }
    ]
  }.to_json
end

#templateObject



12
13
14
# File 'lib/notifu/actors/slack.rb', line 12

def template
    "<%= data[:status] %> [<%= data[:host] %>/<%= data[:service] %>]: <%= data[:message] %> (<%= data[:duration] %>) NID:<%= data[:notifu_id] %>]"
end

#textObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/notifu/actors/slack.rb', line 67

def text
  data = OpenStruct.new({
    notifu_id: self.issue.notifu_id,
    host: self.issue.host,
    service: self.issue.service,
    message: self.issue.message,
    status: self.issue.code.to_state,
    first_event: Time.at(self.issue.time_created.to_i),
    duration: (Time.now.to_i - self.issue.time_created.to_i).duration,
    occurrences_count: self.issue.occurrences_count,
    occurrences_trigger: self.issue.occurrences_trigger
  })
  ERB.new(self.template).result(data.instance_eval {binding})
end