Skip to content

Commit 9d49c2a

Browse files
committed
Refactored Events V1 and V2
1 parent 324c8ec commit 9d49c2a

6 files changed

Lines changed: 88 additions & 103 deletions

File tree

EVENTS_API_V1/Ack/ack_incident.py

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

5-
6-
SUBDOMAIN='pdt-dank'
7-
API_ACCESS_KEY='HjEs6A6KozribnKqm1tX'
8-
6+
SERVICE_KEY = "" # ENTER EVENTS V1 API INTEGRATION KEY HERE
7+
INCIDENT_KEY = "" # ENTER INCIDENT KEY
98

109
def trigger_incident():
11-
headers = {
12-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
13-
'Content-type': 'application/json',
10+
# Triggers a PagerDuty incident without a previously generated incident key
11+
# Uses Events V1 API - documentation: https://v2.developer.pagerduty.com/docs/trigger-events
12+
13+
payload = { # Payload is built with the least amount of fields required to trigger an incident
14+
"service_key": SERVICE_KEY,
15+
"event_type": "acknowledge",
16+
"incident_key": INCIDENT_KEY,
17+
"description": "Example alert on host1.example.com"
1418
}
15-
payload = json.dumps({
16-
"service_key": "e05fd2270e2c4c028f3ae2388bbc09eb",
17-
"incident_key": "18d16696da65420c9f8d5cfa38b17a27",
18-
"event_type": "acknowledge",
19-
"description": "Andrew now working on the problem.",
20-
"details": {
21-
"work started": "2010-06-10 05:43"
22-
}
23-
})
24-
r = requests.post(
25-
'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
26-
headers=headers,
27-
data=payload,
28-
)
29-
print r.status_code
30-
print r.text
31-
trigger_incident()
19+
20+
response = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
21+
data=json.dumps(payload))
22+
23+
if response.json()["status"] == "success":
24+
print ('Incident Acknowledged')
25+
else:
26+
print response.text # print error message if not successful
27+
28+
if __name__ == '__main__':
29+
trigger_incident()
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
#!/usr/bin/env python
2+
23
import json
34
import requests
45

5-
6-
SUBDOMAIN='pdt-dank'
7-
API_ACCESS_KEY='HjEs6A6KozribnKqm1tX'
8-
6+
SERVICE_KEY = "" # ENTER EVENTS V1 API INTEGRATION KEY HERE
7+
INCIDENT_KEY = "" # ENTER INCIDENT KEY
98

109
def trigger_incident():
11-
headers = {
12-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
13-
'Content-type': 'application/json',
10+
# Triggers a PagerDuty incident without a previously generated incident key
11+
# Uses Events V1 API - documentation: https://v2.developer.pagerduty.com/docs/trigger-events
12+
13+
payload = { # Payload is built with the least amount of fields required to trigger an incident
14+
"service_key": SERVICE_KEY,
15+
"event_type": "resolve",
16+
"incident_key": INCIDENT_KEY,
17+
"description": "Example alert on host1.example.com"
1418
}
15-
payload = json.dumps({
16-
"service_key": "e05fd2270e2c4c028f3ae2388bbc09eb",
17-
"incident_key": "18d16696da65420c9f8d5cfa38b17a27",
18-
"event_type": "resolve",
19-
"description": "Andrew fixed the problem.",
20-
"details": {
21-
"fixed at": "2010-06-10 06:00"
22-
}
23-
})
24-
r = requests.post(
25-
'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
26-
headers=headers,
27-
data=payload,
28-
)
29-
print r.status_code
30-
print r.text
31-
trigger_incident()
19+
20+
response = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
21+
data=json.dumps(payload))
22+
23+
if response.json()["status"] == "success":
24+
print ('Incident Resolved')
25+
else:
26+
print response.text # print error message if not successful
27+
28+
if __name__ == '__main__':
29+
trigger_incident()
Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
#!/usr/bin/env python
2+
23
import json
34
import requests
45

5-
6-
SUBDOMAIN='pdt-dank'
7-
API_ACCESS_KEY='HjEs6A6KozribnKqm1tX'
8-
6+
SERVICE_KEY = "" # ENTER EVENTS V1 API INTEGRATION KEY HERE
7+
INCIDENT_KEY = "" # ENTER INCIDENT KEY
98

109
def trigger_incident():
11-
headers = {
12-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
13-
'Content-type': 'application/json',
10+
# Triggers a PagerDuty incident without a previously generated incident key
11+
# Uses Events V1 API - documentation: https://v2.developer.pagerduty.com/docs/trigger-events
12+
13+
payload = { # Payload is built with the least amount of fields required to trigger an incident
14+
"service_key": SERVICE_KEY,
15+
"event_type": "trigger",
16+
"incident_key": INCIDENT_KEY,
17+
"description": "Example alert on host1.example.com"
1418
}
15-
payload = json.dumps({
16-
"service_key": "e05fd2270e2c4c028f3ae2388bbc09eb",
17-
"incident_key": "srv01/HTTP",
18-
"event_type": "trigger",
19-
"description": "FAILURE for production/HTTP on machine srv01.acme.com",
20-
"client": "Sample Monitoring Service",
21-
"client_url": "https://monitoring.service.com",
22-
"details": {
23-
"ping time": "1500ms",
24-
"load avg": 0.75
25-
}
26-
})
27-
r = requests.post(
28-
'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
29-
headers=headers,
30-
data=payload,
31-
)
32-
print r.status_code
33-
print r.text
34-
trigger_incident()
19+
20+
response = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
21+
data=json.dumps(payload))
22+
23+
if response.json()["status"] == "success":
24+
print ('Incident Triggered')
25+
else:
26+
print response.text # print error message if not successful
27+
28+
if __name__ == '__main__':
29+
trigger_incident()
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
#!/usr/bin/env python
2+
23
import json
34
import requests
45

5-
6-
SUBDOMAIN='pdt-dank'
7-
API_ACCESS_KEY='HjEs6A6KozribnKqm1tX'
8-
6+
SERVICE_KEY = "" # ENTER EVENTS V1 API INTEGRATION KEY HERE
97

108
def trigger_incident():
11-
headers = {
12-
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
13-
'Content-type': 'application/json',
9+
# Triggers a PagerDuty incident without a previously generated incident key
10+
# Uses Events V1 API - documentation: https://v2.developer.pagerduty.com/docs/trigger-events
11+
12+
payload = { # Payload is built with the least amount of fields required to trigger an incident
13+
"service_key": SERVICE_KEY,
14+
"event_type": "trigger",
15+
"description": "Example alert on host1.example.com"
1416
}
15-
payload = json.dumps({
16-
"service_key": "e05fd2270e2c4c028f3ae2388bbc09eb",
17-
"event_type": "trigger",
18-
"description": "FAILURE for production/HTTP on machine srv01.acme.com",
19-
"client": "Sample Monitoring Service",
20-
"client_url": "https://monitoring.service.com",
21-
"details": {
22-
"ping time": "1500ms",
23-
"load avg": 0.75
24-
}
25-
})
26-
r = requests.post(
27-
'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
28-
headers=headers,
29-
data=payload,
30-
)
31-
print r.status_code
32-
print r.text
33-
trigger_incident()
17+
18+
response = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json',
19+
data=json.dumps(payload))
20+
21+
if response.json()["status"] == "success":
22+
print ('Incident created with with incident / alert key of ' + '"' + response.json()['incident_key'] + '"')
23+
else:
24+
print response.text # print error message if not successful
25+
26+
if __name__ == '__main__':
27+
trigger_incident()

EVENTS_API_v2/trigger/trigger_with_incident_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def trigger_incident():
3030
headers=header)
3131

3232
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
33+
print ('Incident Created')
3434
else:
3535
print response.text # print error message if not successful
3636

EVENTS_API_v2/trigger/trigger_without_incident_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def trigger_incident():
2828
headers=header)
2929

3030
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
31+
print ('Incident created with with dedup key (also known as incident / alert key) of ' + '"' + response.json()['dedup_key'] + '"')
3232
else:
3333
print response.text # print error message if not successful
3434

0 commit comments

Comments
 (0)