-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalertmanager.py
More file actions
40 lines (31 loc) · 1.54 KB
/
Copy pathalertmanager.py
File metadata and controls
40 lines (31 loc) · 1.54 KB
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
import requests, json
class AlertManager(object):
def getAlerts(url, verify=True):
raw = AlertManager.getRaw(url, verify=verify)
result = {}
for instance in raw:
if instance['receiver']['name'] == "autoup":
data = dict(
triggers=[],
inhibitors=[],
)
for alert in instance['alerts']:
count = alert['annotations']['count'] if 'count' in alert['annotations'] else "-1"
data[f'{alert["labels"]["autoup"]}s'].append(dict(
name=alert['labels']['alertname'],
title=alert['annotations']['title'],
origin=alert['labels'].get('origin'),
count=count,
))
data['trigger_severity'] = sum(int(float(trigger['count'])) for trigger in data['triggers']) # string -> float -> int
result[instance['labels']['instance']] = data
return result
def getRaw(url, verify=True):
try:
response = requests.get(f"{url}/api/v2/alerts/groups", verify=verify)
response.raise_for_status() # Raise an HTTPError for bad status codes
alerts = response.json() # Assuming the response is JSON
return alerts
except requests.exceptions.RequestException as e:
print(f"Error fetching alerts from {url}: {e}")
return None