/
pagerduty.rb
147 lines (129 loc) · 4.83 KB
/
pagerduty.rb
1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'pagerduty/version'
require 'pagerduty/http_transport'
class PagerdutyException < Exception
attr_reader :pagerduty_instance, :api_response
def initialize(instance, resp)
@pagerduty_instance = instance
@api_response = resp
end
end
class Pagerduty
attr_reader :service_key
# @param [String] service_key The GUID of one of your "Generic API" services.
# This is the "service key" listed on a Generic API's service detail page.
#
def initialize(service_key)
@service_key = service_key
end
# Send PagerDuty a trigger event to report a new or ongoing problem. When
# PagerDuty receives a trigger event, it will either open a new incident, or
# add a new trigger log entry to an existing incident, depending on the
# provided incident_key.
#
# @param [String] description A short description of the problem that led to
# this trigger. This field (or a truncated version) will be used when
# generating phone calls, SMS messages and alert emails. It will also appear
# on the incidents tables in the PagerDuty UI. The maximum length is 1024
# characters.
#
# @option options [String] :incident_key Identifies the incident to which
# this trigger event should be applied. If there's no open (i.e. unresolved)
# incident with this key, a new one will be created. If there's already an
# open incident with a matching key, this event will be appended to that
# incident's log. The event key provides an easy way to "de-dup" problem
# reports. If this field isn't provided, PagerDuty will automatically open a
# new incident with a unique key.
#
# @option options [String] :client The name of the monitoring client that is
# triggering this event.
#
# @option options [String] :client_url The URL of the monitoring client that
# is triggering this event.
#
# @option options [Hash] :details An arbitrary hash containing any data you'd
# like included in the incident log.
#
# @return [PagerdutyIncident] The triggered incident.
#
def trigger(description, options = {})
resp = api_call("trigger", options.merge(:description => description))
ensure_success(resp)
PagerdutyIncident.new service_key, resp["incident_key"]
end
# @return [PagerdutyIncident] The incident referenced by the key.
#
def get_incident(incident_key)
PagerdutyIncident.new service_key, incident_key
end
protected
def api_call(event_type, args)
args = args.merge(
:service_key => service_key,
:event_type => event_type,
)
Pagerduty.transport.send(args)
end
def ensure_success(response)
unless response["status"] == "success"
raise PagerdutyException.new(self, response)
end
end
# @api private
def self.transport
Pagerduty::HttpTransport
end
end
class PagerdutyIncident < Pagerduty
attr_reader :incident_key
# @param [String] service_key The GUID of one of your "Generic API" services.
# This is the "service key" listed on a Generic API's service detail page.
#
# @param [String] incident_key The unique identifier for the incident.
#
def initialize(service_key, incident_key)
super service_key
@incident_key = incident_key
end
# Acknowledge the referenced incident. While an incident is acknowledged, it
# won't generate any additional notifications, even if it receives new
# trigger events. Send PagerDuty an acknowledge event when you know someone
# is presently working on the problem.
#
# @param [String] description Text that will appear in the incident's log
# associated with this event.
#
# @param [Hash] details An arbitrary hash containing any data you'd like
# included in the incident log.
#
# @return [PagerdutyIncident] self
#
def acknowledge(description = nil, details = nil)
modify_incident("acknowledge", description, details)
end
# Resolve the referenced incident. Once an incident is resolved, it won't
# generate any additional notifications. New trigger events with the same
# incident_key as a resolved incident won't re-open the incident. Instead, a
# new incident will be created. Send PagerDuty a resolve event when the
# problem that caused the initial trigger event has been fixed.
#
# @param [String] description Text that will appear in the incident's log
# associated with this event.
#
# @param [Hash] details An arbitrary hash containing any data you'd like
# included in the incident log.
#
# @return [PagerdutyIncident] self
#
def resolve(description = nil, details = nil)
modify_incident("resolve", description, details)
end
private
def modify_incident(event_type, description, details)
options = { :incident_key => incident_key }
options[:description] = description if description
options[:details] = details if details
resp = api_call(event_type, options)
ensure_success(resp)
self
end
end