blob: 7e2b1c89705682d04124f470b1d4ad6ac02fcbb0 [file] [log] [blame]
Michal Kobus211ee922019-04-15 17:44:06 +02001import os
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +01002
3RESOLVED_STATUSES = ('UP', 'OK', 'resolved')
4
5
Michal Kobus211ee922019-04-15 17:44:06 +02006def create_file(file_path):
7 if not os.path.exists(file_path):
8 with open(file_path, 'w+'):
9 return file_path
10
11
Michal Kobuse7589f72020-09-11 14:29:37 +020012def _format_subject(alert, add_links=False):
13 if add_links:
14 severity = alert['labels']['severity'].upper()
15 name = alert['labels']['alertname']
16 return '[{}] {}'.format(severity, name)
17
Michal Kobus23ba2f72018-12-03 17:11:20 +010018 subject = alert['annotations']['summary']
Michal Kobusba987052018-11-30 13:01:08 +010019 host = alert['labels'].get('host')
20 if host is not None:
Michal Kobus23ba2f72018-12-03 17:11:20 +010021 return '[{}] {}'.format(host, subject)
22 return subject
Michal Kobusba987052018-11-30 13:01:08 +010023
24
Michal Kobus23ba2f72018-12-03 17:11:20 +010025def _remove_advice(description):
26 return description.split(' Modify the')[0]
27
28
Michal Kobuse7589f72020-09-11 14:29:37 +020029def _get_prometheus_link(alert):
30 # switch from Console to Graph tab to show trends
31 condition_url = alert['generatorURL'].replace('g0.tab=1', 'g0.tab=0')
32
Michal Kobus043c5992020-09-30 16:26:08 +020033 # list alert firing instances in the cluster
34 # via query: ALERTS{alertname="<alertname>",alertstate="firing"}
Michal Kobuse7589f72020-09-11 14:29:37 +020035 alert_query = ('g1.range_input=1h&'
Michal Kobus043c5992020-09-30 16:26:08 +020036 'g1.expr=ALERTS%7Balertname%3D%22'
37 '{}'
38 '%22%2Calertstate%3D%22firing%22%7D&'
39 'g1.tab=0').format(alert['labels']['alertname'])
Michal Kobuse7589f72020-09-11 14:29:37 +020040 return '{}&{}'.format(condition_url, alert_query)
Michal Kobus23ba2f72018-12-03 17:11:20 +010041
42
Michal Kobuse7589f72020-09-11 14:29:37 +020043def _format_description(alert, add_links=False):
44 description_old = _remove_advice(alert['annotations']['description'])
45
46 if not add_links:
47 return description_old
48
49 msg = ('To check trends of the alert underlying condition metric '
50 ' and alert occurrence click on Prometheus UI link')
51
52 prometheus_url = _get_prometheus_link(alert)
53 return '{}\n\n{}:\n{}'.format(description_old, msg, prometheus_url)
54
55
Michal Kobus28b37f92022-06-14 11:06:24 +020056def alert_fields_and_action(alert, add_links=False, cluster_id=None):
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010057 fields = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010058
Michal Kobus28b37f92022-06-14 11:06:24 +020059 if cluster_id is not None:
60 if alert['labels'].get('cluster_id') is None:
61 alert['labels']['cluster_id'] = cluster_id
62
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010063 if alert['status'] in RESOLVED_STATUSES:
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010064 action = 'close_case'
Michal Kobusafbf4d02018-11-28 14:18:05 +010065 fields.append(alert['labels'])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010066 else:
Michal Kobusafbf4d02018-11-28 14:18:05 +010067 action = 'create_case'
Michal Kobusba987052018-11-30 13:01:08 +010068 # Order of keys matters
Michal Kobuse7589f72020-09-11 14:29:37 +020069 fields.append(_format_subject(alert, add_links))
70 fields.append(_format_description(alert, add_links))
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010071 fields.append(alert['labels'])
72 return fields, action
Michal Kobus2e85ef82021-06-24 18:01:43 +020073
74
75def is_true(name):
76 if isinstance(name, str):
77 return name.lower() == 'true'
78 return name is True
Ryan Zhang532052d2022-05-09 10:23:34 -070079
80def get_env_or_secret(env, secret):
81 # returns the value of the env var if present,
82 # otherwise docker swarm secret if present,
83 # null when neither of the two present.
84 value = os.getenv(env, 'null')
85 if value != 'null':
86 return value
87 fpath = f'/run/secrets/{secret}'
88 exist = os.path.exists(fpath)
89 if exist:
90 with open(fpath) as file:
91 value = file.read().rstrip('\n')
92 return value