Skip to content

Commit 8e957e7

Browse files
committed
update
1 parent 793d082 commit 8e957e7

6 files changed

Lines changed: 202 additions & 3 deletions

File tree

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
Version 0.5.0
6+
=============
7+
8+
- Opsgenie Integration.
9+
510
Version 0.4.0
611
=============
712

examples/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2022 Clivern
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.

examples/opsgenie.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2022 Clivern
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
from alertify.opsgenie import Client
24+
25+
api_key = "xxxxxxxxxxxxxxxxxxxxx"
26+
message = "SaaS monitoring detected an incident"
27+
description = "The SaaS service is currently down."
28+
priority = "P1"
29+
tags = ["monitoring", "incident"]
30+
details = {
31+
"service_name": "Cloud",
32+
"incident_time": "2023-02-19T12:00:00Z"
33+
}
34+
35+
c = Client()
36+
37+
# Trigger Incident
38+
o1 = c.trigger_incident(
39+
api_key,
40+
message,
41+
description,
42+
priority,
43+
tags,
44+
details
45+
)
46+
47+
# Fetch Alert ID with Request ID
48+
r1 = c.fetch_request(
49+
api_key,
50+
o1['requestId']
51+
)
52+
53+
# Resolve Incident
54+
o2 = c.resolve_incident(
55+
api_key,
56+
r1['data']['alertId']
57+
)
58+
59+
# Fetch Request Status
60+
r2 = c.fetch_request(
61+
api_key,
62+
o2['requestId']
63+
)

src/alertify/opsgenie/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
22+
23+
from .client import Client

src/alertify/opsgenie/client.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2022 Clivern
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
import requests
24+
import logging
25+
import json
26+
from alertify.exception import ApiError
27+
28+
29+
class Client():
30+
"""Opsgenie Client Class"""
31+
32+
def __init__(self):
33+
self._logging = logging.getLogger(__name__)
34+
35+
def trigger_incident(self, api_key, message, description, priority, tags=[], details={}):
36+
"""Trigger Incident"""
37+
data = {
38+
"message": message,
39+
"description": description,
40+
"priority": priority,
41+
"tags": tags,
42+
"details": details
43+
}
44+
45+
headers = {
46+
'Content-Type': 'application/json',
47+
'Authorization': 'GenieKey ' + api_key
48+
}
49+
50+
try:
51+
response = requests.post(
52+
'https://api.eu.opsgenie.com/v2/alerts',
53+
headers=headers,
54+
json=data
55+
)
56+
except Exception as e:
57+
raise ApiError("Failed to create opsgenie incident: {}".format(str(e)))
58+
59+
if response.status_code // 100 != 2:
60+
raise ApiError("Opsgenie respond with invalid status code {}".format(response.status_code))
61+
62+
return json.loads(response.content.decode("utf-8"))
63+
64+
def fetch_request(self, api_key, request_id):
65+
"""Fetch Alert With Request ID"""
66+
headers = {
67+
'Content-Type': 'application/json',
68+
'Authorization': 'GenieKey ' + api_key
69+
}
70+
71+
try:
72+
response = requests.get(
73+
"https://api.opsgenie.com/v2/alerts/requests/{}".format(request_id),
74+
headers=headers
75+
)
76+
except Exception as e:
77+
raise ApiError("Failed to fetch opsgenie request: {}".format(str(e)))
78+
79+
if response.status_code // 100 != 2:
80+
raise ApiError("Opsgenie respond with invalid status code {}".format(response.status_code))
81+
82+
return json.loads(response.content.decode("utf-8"))
83+
84+
def resolve_incident(self, api_key, incident_id, source="Uptimedog", note="Action executed via Alert API"):
85+
"""Resolve Incident"""
86+
data = {
87+
"source": source,
88+
"note": note
89+
}
90+
91+
headers = {
92+
'Content-Type': 'application/json',
93+
'Authorization': 'GenieKey ' + api_key
94+
}
95+
96+
try:
97+
response = requests.post(
98+
"https://api.opsgenie.com/v2/alerts/{}/close?identifierType=id".format(incident_id),
99+
data=json.dumps(data),
100+
headers=headers
101+
)
102+
except Exception as e:
103+
raise ApiError("Failed to close opsgenie incident: {}".format(str(e)))
104+
105+
if response.status_code // 100 != 2:
106+
raise ApiError("Opsgenie respond with invalid status code {}".format(response.status_code))
107+
108+
return json.loads(response.content.decode("utf-8"))

src/alertify/pagerduty/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727

2828

2929
class Client():
30-
"""Client Class"""
30+
"""Pagerduty Client Class"""
3131

3232
def __init__(self):
3333
self._logging = logging.getLogger(__name__)
3434

3535
def trigger_incident(self, routing_key, summary, source, severity, component, group, class_type, details={}):
36-
36+
"""Trigger Incident"""
3737
data = {
3838
'routing_key': routing_key,
3939
'event_action': 'trigger',
@@ -62,7 +62,7 @@ def trigger_incident(self, routing_key, summary, source, severity, component, gr
6262
return json.loads(response.content.decode("utf-8"))
6363

6464
def resolve_incident(self, routing_key, dedup_key, summary, source, severity, component, group, class_type, details={}):
65-
65+
"""Resolve Incident"""
6666
data = {
6767
'routing_key': routing_key,
6868
'event_action': 'resolve',

0 commit comments

Comments
 (0)