Class: Notifu::Actors::Pagerduty

Inherits:
Notifu::Actor show all
Defined in:
lib/notifu/actors/pagerduty.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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/notifu/actors/pagerduty.rb', line 47

def act
  type = "resolve" if self.issue.code.to_i == 0
  type ||= "trigger"

  self.contacts.each do |contact|
    begin
      c = contact.pagerduty_id
    rescue
      c = false
    end

    Excon.post(Notifu::CONFIG[:actors][:pagerduty][:url],
      tcp_nodelay: true,
      headers: { "ContentType" => "application/json" },
      body: self.post_data(type, c).to_json,
      expects: [ 200 ],
      idempotent: true,
    ) if c
  end
end

#post_data(type, service_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/notifu/actors/pagerduty.rb', line 12

def post_data(type, service_id)
  {
    service_key: service_id,
    event_type: type,
    description: self.text,
    incident_key: self.issue.notifu_id,
    details: {
      status: self.issue.code.to_state,
      dc: self.issue.datacenter,
      host: self.issue.host,
      service: self.issue.service,
      message: self.issue.message,
      first_event: Time.at(self.issue.time_created.to_i),
    },
    client: "Sensu",
    client_url: "#{Notifu::CONFIG[:uchiwa_url]}/#/client/#{self.issue.datacenter}/#{self.issue.host}?check=#{self.issue.service}"
  }
end

#templateObject



43
44
45
# File 'lib/notifu/actors/pagerduty.rb', line 43

def template
  "<%= data[:status] %> [<%= data[:datacenter] %>/<%= data[:host] %>/<%= data[:service] %>]: <%= data[:message] %>"
end

#textObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/notifu/actors/pagerduty.rb', line 31

def text
  data = OpenStruct.new({
    notifu_id: self.issue.notifu_id,
    datacenter: self.issue.datacenter,
    host: self.issue.host,
    service: self.issue.service,
    message: self.issue.message,
    status: self.issue.code.to_state
  })
  ERB.new(self.template).result(data.instance_eval {binding})
end