Skip to content

Commit b9b17e7

Browse files
committed
refactored events v2 api trigger/ack/resolve scripts
1 parent cf459e6 commit b9b17e7

File tree

4 files changed

+90
-111
lines changed

4 files changed

+90
-111
lines changed

EVENTS_API_v2/ack/ack_incident.py

100644100755
Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
#!/usr/bin/env python
2+
23
import json
34
import requests
45

6+
ROUTING_KEY = "257e6698a97046e19d15a44efb515106" # ENTER EVENTS V2 API INTEGRATION KEY HERE
7+
INCIDENT_KEY = "455408afbf924327949920a87426758c" # ENTER INCIDENT KEY HERE
58

6-
SUBDOMAIN = "" # Enter your subdomain here
7-
API_ACCESS_KEY = "" # Enter your subdomain's API access key here
8-
9+
def trigger_incident():
10+
# Triggers a PagerDuty incident without a previously generated incident key
11+
# Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
912

10-
def ack_incident():
11-
"""Acknowledges a triggered incident using the customer's API access key and incident key."""
12-
13-
headers = {
14-
'Accept': 'application/vnd.pagerduty+json;version=2',
15-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
16-
'Content-type': 'application/json'
13+
header = {
14+
"Content-Type": "application/json"
1715
}
1816

19-
payload = json.dumps({
20-
"service_key": "", # Enter the service key here
21-
"incident_key": "", # Enter the incident key here
22-
"event_type": "acknowledge",
23-
"description": "Andrew now working on the problem.", # Enter your own description
24-
"details": {
25-
"work started": "2010-06-10 05:43"
26-
}
27-
})
28-
29-
r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
30-
headers=headers,
31-
data=payload,
32-
)
33-
34-
print r.status_code
35-
print r.text
17+
payload = { # Payload is built with the least amount of fields required to trigger an incident
18+
"routing_key": ROUTING_KEY,
19+
"event_action": "acknowledge",
20+
"dedup_key": INCIDENT_KEY
21+
}
3622

23+
response = requests.post('https://events.pagerduty.com/v2/enqueue',
24+
data=json.dumps(payload),
25+
headers=header)
26+
27+
if response.json()["status"] == "success":
28+
print ('Incident Acknowledged ')
29+
else:
30+
print response.text # print error message if not successful
3731

3832
if __name__ == '__main__':
39-
ack_incident()
33+
trigger_incident()

EVENTS_API_v2/resolve/resolve_incident.py

100644100755
Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
#!/usr/bin/env python
2+
23
import json
34
import requests
45

6+
ROUTING_KEY = "257e6698a97046e19d15a44efb515106" # ENTER EVENTS V2 API INTEGRATION KEY HERE
7+
INCIDENT_KEY = "455408afbf924327949920a87426758c" # ENTER INCIDENT KEY HERE
58

6-
SUBDOMAIN = "" # Enter your subdomain here
7-
API_ACCESS_KEY = "" # Enter your subdomain's API access key here
8-
9+
def trigger_incident():
10+
# Triggers a PagerDuty incident without a previously generated incident key
11+
# Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
912

10-
def resolve_incident():
11-
"""Resolves a PagerDuty incident using customer's API access key and incident key."""
12-
13-
headers = {
14-
'Accept': 'application/vnd.pagerduty+json;version=2',
15-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
16-
'Content-type': 'application/json',
13+
header = {
14+
"Content-Type": "application/json"
1715
}
18-
19-
payload = json.dumps({
20-
"service_key": "", # Enter service key here
21-
"incident_key": "", # Enter incident key here
22-
"event_type": "resolve",
23-
"description": "Andrew fixed the problem.", # Enter personalized description
24-
"details": {
25-
"fixed at": "2010-06-10 06:00"
26-
}
27-
})
28-
29-
r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
30-
headers=headers,
31-
data=payload,
32-
)
3316

34-
print r.status_code
35-
print r.text
17+
payload = { # Payload is built with the least amount of fields required to trigger an incident
18+
"routing_key": ROUTING_KEY,
19+
"event_action": "resolve",
20+
"dedup_key": INCIDENT_KEY
21+
}
3622

23+
response = requests.post('https://events.pagerduty.com/v2/enqueue',
24+
data=json.dumps(payload),
25+
headers=header)
26+
27+
if response.json()["status"] == "success":
28+
print ('Incident Resolved ')
29+
else:
30+
print response.text # print error message if not successful
3731

3832
if __name__ == '__main__':
39-
resolve_incident()
33+
trigger_incident()
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
11
#!/usr/bin/env python
2+
23
import json
34
import requests
45

5-
6-
SUBDOMAIN = "" # Enter your subdomain here
7-
API_ACCESS_KEY = "" # Enter your subdomain's API access key here
8-
6+
ROUTING_KEY = "" # ENTER EVENTS V2 API INTEGRATION KEY HERE
7+
INCIDENT_KEY = "" # ENTER INCIDENT KEY HERE
98

109
def trigger_incident():
11-
"""Triggers an incident with a previously generated incident key."""
10+
# Triggers a PagerDuty incident without a previously generated incident key
11+
# Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
1212

13-
headers = {
14-
'Accept': 'application/vnd.pagerduty+json;version=2',
15-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
16-
'Content-type': 'application/json',
13+
header = {
14+
"Content-Type": "application/json"
1715
}
1816

19-
payload = json.dumps({
20-
"service_key": "", # Enter service key here
21-
"incident_key": "srv01/HTTP",
22-
"event_type": "trigger",
23-
"description": "FAILURE for production/HTTP on machine srv01.acme.com",
24-
"client": "Sample Monitoring Service",
25-
"client_url": "https://monitoring.service.com",
26-
"details": {
27-
"ping time": "1500ms",
28-
"load avg": 0.75
17+
payload = { # Payload is built with the least amount of fields required to trigger an incident
18+
"routing_key": ROUTING_KEY,
19+
"event_action": "trigger",
20+
"dedup_key": INCIDENT_KEY,
21+
"payload": {
22+
"summary": "Example alert on host1.example.com",
23+
"source": "monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003",
24+
"severity": "critical"
2925
}
30-
})
31-
32-
r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
33-
headers=headers,
34-
data=payload,
35-
)
36-
37-
print r.status_code
38-
print r.text
26+
}
3927

28+
response = requests.post('https://events.pagerduty.com/v2/enqueue',
29+
data=json.dumps(payload),
30+
headers=header)
31+
32+
if response.json()["status"] == "success":
33+
print ('Incident created with with dedup key (also known as incident / alert key) of ' + '"' + response.json()['dedup_key'] + '"') #print dedup key if successful
34+
else:
35+
print response.text # print error message if not successful
4036

4137
if __name__ == '__main__':
4238
trigger_incident()
Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
#!/usr/bin/env python
2+
23
import json
34
import requests
45

5-
6-
SUBDOMAIN = "" # Enter your subdomain here
7-
API_ACCESS_KEY = "" # Enter your subdomain's API access key here
8-
6+
ROUTING_KEY = "" # ENTER EVENTS V2 API INTEGRATION KEY HERE
97

108
def trigger_incident():
11-
"""Triggers a PagerDuty incident without a previously generated incident key."""
9+
# Triggers a PagerDuty incident without a previously generated incident key
10+
# Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
1211

13-
headers = {
14-
'Accept': 'application/vnd.pagerduty+json;version=2',
15-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
16-
'Content-type': 'application/json',
12+
header = {
13+
"Content-Type": "application/json"
1714
}
1815

19-
payload = json.dumps({
20-
"service_key": "", # Enter service key here
21-
"event_type": "trigger",
22-
"description": "FAILURE for production/HTTP on machine srv01.acme.com",
23-
"client": "Sample Monitoring Service",
24-
"client_url": "https://monitoring.service.com",
25-
"details": {
26-
"ping time": "1500ms",
27-
"load avg": 0.75
16+
payload = { # Payload is built with the least amount of fields required to trigger an incident
17+
"routing_key": ROUTING_KEY,
18+
"event_action": "trigger",
19+
"payload": {
20+
"summary": "Example alert on host1.example.com",
21+
"source": "monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003",
22+
"severity": "critical"
2823
}
29-
})
30-
31-
r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
32-
headers=headers,
33-
data=payload,
34-
)
35-
36-
print r.status_code
37-
print r.text
24+
}
3825

26+
response = requests.post('https://events.pagerduty.com/v2/enqueue',
27+
data=json.dumps(payload),
28+
headers=header)
29+
30+
if response.json()["status"] == "success":
31+
print ('Incident created with with dedup key (also known as incident / alert key) of ' + '"' + response.json()['dedup_key'] + '"') #print dedup key if successful
32+
else:
33+
print response.text # print error message if not successful
3934

4035
if __name__ == '__main__':
4136
trigger_incident()

0 commit comments

Comments
 (0)