-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpagerduty.yaml
387 lines (366 loc) · 13.3 KB
/
pagerduty.yaml
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
---
systems:
pagerduty:
description: |
This system enables Honeydipper to integrate with :code:`pagerduty`, so Honeydipper can
react to pagerduty alerts and take actions through pagerduty API.
meta:
configurations:
- name: API_KEY
description: The API key used for making API calls to :code:`pagerduty`
- name: signatureSecret
description: The secret used for validating webhook requests from :code:`pagerduty`
- name: path
description: The path portion of the webhook url, by default :code:`/pagerduty`
notes:
- For example
- example: |
---
systems:
pagerduty:
data:
API_KEY: ENC[gcloud-kms,...masked...]
signatureSecret: ENC[gcloud-kms,...masked...]
path: "/webhook/pagerduty"
- Assuming the domain name for the webhook server is :code:`myhoneydipper.com', you should
configure the webhook in your pagerduty integration with url like below
- |
.. code-block::
https://myhoneydipper.com/webhook/pagerduty
data:
API_KEY: _place_holder_
path: "/pagerduty"
signatureHeader: X-PagerDuty-Signature
triggers:
alert:
driver: webhook
if_match:
method: POST
url: $sysData.path
json:
event:
event_type: incident.triggered
export:
alert_message: $event.json.event.data.title
alert_alias: $event.json.event.data.title
alert_Id: $event.json.event.data.id
alert_service: $event.json.event.data.service.summary
alert_system: "pagerduty"
alert_url: $event.json.event.data.html_url
_event_id: $event.json.event.data.id
_event_detail: |-
```{{ .event.json.event.data.title }}
service: {{ .event.json.event.data.service.summary }}```
_event_url: $event.json.event.data.html_url
links:
alert:
text: alert {{ .event.json.event.data.id }}
url: $event.json.event.data.html_url
service:
text: service {{ .event.json.event.data.service.summary }}
url: $event.json.event.data.service.html_url
parameters:
verifySystem: true
description: This event is triggered when an pagerduty incident is raised.
meta:
matching_parameters:
- name: .json.event.data.title
description: This field can used to match alert with only certain messages
- name: .json.event.data.service.summary
description: This field is to match only the alerts with certain service
exports:
- name: alert_message
description: This context variable will be set to the detailed message of the alert.
- name: alert_service
description: This context variable will be set to the service of the alert.
- name: alert_Id
description: This context variable will be set to the short alert ID.
- name: alert_system
description: This context variable will be set to the constant string, :code:`pagerduty`
- name: alert_url
description: This context variable will be set to the url of the alert, used for creating links
notes:
- Pagerduty manages all the alerts through incidents. Although the trigger is named :code:`alert` for compatibility reason, it actually
matches an incident.
- See below snippet for example
- example: |
---
rules:
- when:
source:
system: pagerduty
trigger: alert
if_match:
json:
data:
title: :regex:^test-alert.*$
do:
call_workflow: notify
with:
message: 'The alert url is {{ .ctx.alert_url }}'
functions:
api:
driver: web
rawAction: request
parameters:
header:
Accept: application/vnd.pagerduty+json;version=2
Content-Type: application/json; charset=utf-8
Authorization: Token token={{ .sysData.API_KEY }}
retry: 2
snooze:
target:
system: pagerduty
function: api
parameters:
URL: https://api.pagerduty.com/incidents/{{ .ctx.alert_Id }}/snooze
method: POST
content:
duration: $ctx.duration
export:
incident: $data.json.incident
description: >
snooze pagerduty incident
meta:
inputs:
- name: alert_Id
description: The ID of the incident to be snoozed
- name: duration
description: For how long the incident should be snoozed, a number of seconds
exports:
- name: incident
description: On success, returns the updated incident object
notes:
- See below for example
- example: |
---
rules:
- when:
source:
system: pagerduty
trigger: alert
if_match:
json:
title: :regex:test-alert
do:
call_function: pagerduty.snooze
with:
# alert_Id is exported from the event
duration: 1200
getEscalationPolicies:
target:
system: pagerduty
function: api
parameters:
URL: https://api.pagerduty.com/escalation_policies
method: GET
form:
limit: "100"
offset: $ctx.offset,"0"
query: $?ctx.tag_name
export:
partial_result: |
{{- $policies := list }}
{{- range .data.json.escalation_policies }}
{{- if contains (default "" $.ctx.schedule_pattern | lower) (lower .summary) }}
{{- $policies = append $policies . }}
{{- end }}
{{- end }}
{{- return $policies }}
partial_result_ids+: |
{{- $ids := list }}
{{- range .data.json.escalation_policies }}
{{- if contains (default "" $.ctx.schedule_pattern | lower) (lower .summary) }}
{{- $ids = append $ids .id }}
{{- end }}
{{- end }}
{{- return $ids }}
offset: '{{ add (default 0 .ctx.offset) (len .data.json.escalation_policies) }}'
EOL: '{{ .data.json.more | not }}'
getEscalationPolicyIDsByTag:
target:
system: pagerduty
function: api
parameters:
URL: https://api.pagerduty.com/tags/{{ .ctx.tag_id }}/escalation_policies
method: GET
form:
limit: "100"
offset: $ctx.offset,"0"
export:
partial_result: |
{{ $ids := list }}
{{ range .data.json.escalation_policies }}
{{ $ids = append $ids .id }}
{{ end }}
{{ return $ids }}
offset: '{{ add (default 0 .ctx.offset) (len .data.json.escalation_policies) }}'
EOL: '{{ .data.json.more | not }}'
getTagId:
target:
system: pagerduty
function: api
parameters:
URL: https://api.pagerduty.com/tags
method: GET
form:
query: $ctx.tag_name
export:
tag_id: $data.json.tags.0.id
whoisoncall:
target:
system: pagerduty
function: api
parameters:
URL: https://api.pagerduty.com/oncalls
method: GET
form: |
:yaml:---
earliest: "true"
limit: "100"
{{- with .ctx.escalation_policy_ids }}
"escalation_policy_ids[]": {{ toJson . }}
{{- end }}
{{- with .ctx.offset }}
offset: "{{ . }}"
{{- end }}
{{- with .ctx.time_shift }}
since: "{{ now | date_modify . | ISO8601 }}"
until: "{{ now | date_modify . | ISO8601 }}"
{{- end }}
export:
partial_result: $?data.json.oncalls
offset: '{{ add (default 0 .ctx.offset) (len .data.json.oncalls) }}'
EOL: '{{ .data.json.more | not }}'
meta:
description:
- This function gets the current on-call persons for the given schedule.
- highlight: This function only fetches first 100 schedules when listing. Use `pagerduty_whoisoncall`_ workflow instead.
type: important
inputs:
- name: escalation_policy_ids
description: An array of IDs of the escalation policies; if missing, list all.
exports:
- name: result
description: a list of data structure contains the schedule details. See `API <https://developer.pagerduty.com/api-reference/reference/REST/openapiv3.json/paths/~1oncalls/get>`_ for detail.
notes:
- See below for example
- example: |
---
workflows:
until:
- $?ctx.EOL
steps:
- call_function: pagerduty.whoisoncall
no_export:
- offset
- EOL
workflows:
pagerduty_whoisoncall:
description: get pagerduty on call table
steps:
- call_function: pagerduty.getTagId
- until:
- $?ctx.EOL
steps:
- call_function: pagerduty.getEscalationPolicyIDsByTag
export:
result+: $ctx.partial_result
no_export:
- offset
- EOL
- result
- partial_result
export:
escalation_policy_ids: $ctx.result
- if:
- $?ctx.fuzzy
steps:
- until:
- $?ctx.EOL
steps:
- call_function: pagerduty.getEscalationPolicies
export:
result+: $ctx.partial_result
result_ids+: $ctx.partial_result_ids
no_export:
- offset
- EOL
- result
- result_ids
- partial_result
- partial_result_ids
export:
escalation_policy_ids+: $ctx.result_ids
- if:
- $?ctx.escalation_policy_ids
steps:
- iterate: '{{ .ctx.escalation_policy_ids | uniq | chunk 20 | return }}'
iterate_as: escalation_policy_ids
steps:
- until:
- $?ctx.EOL
steps:
- call_function: pagerduty.whoisoncall
export:
result+: $ctx.partial_result
export:
oncalls+: $ctx.result
no_export:
- offset
- EOL
- result
- partial_result
export:
on_call_table: |
{{- $table := dict }}
{{- range (default (list) .ctx.oncalls) }}
{{- if and (int .escalation_level | eq 1) (contains (default "" $.ctx.schedule_pattern | lower) (lower .escalation_policy.summary)) }}
{{- $users := default (list) ( index $table .escalation_policy.summary ) }}
{{- $users = append $users .user.summary }}
{{- $_ := set $table .escalation_policy.summary $users }}
{{- end }}
{{- end }}
{{ return $table }}
no_export:
- oncalls
- escalation_policy_ids
- tag_id
meta:
description:
- This workflow wraps around multiple api calls to :code:`pagerduty` and produce a `on_call_table` datastructure.
inputs:
- name: tag_name
description: Optional, the keyword used for filtering the on tags
- name: schedule_pattern
description: Optional, the keyword used for filtering the on-call escalation policies.
exports:
- name: on_call_table
description: A map from on call schedule names to lists of users.
notes:
- This is usually used for showing the on-call table in response to slash commands.
- For example
- example: |
---
workflows:
show_on_calls:
with:
alert_system: pagerduty
no_export:
- '*'
steps:
- call: '{{ .ctx.alert_system }}_whoisoncall'
- call: notify
with:
notify*:
- reply
response_type: in_channel
blocks:
- type: section
text:
type: mrkdn
text: |
*===== On call users ======*
{{- range $name, $users := .ctx.on_call_table }}
*{{ $name }}*: {{ join ", " $users }}
{{- end }}