blob: 9715545d2cec43d8a4b11584c91f7ad8d3a7e239 [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
56def alert_fields_and_action(alert, add_links=False):
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010057 fields = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010058
59 if alert['status'] in RESOLVED_STATUSES:
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010060 action = 'close_case'
Michal Kobusafbf4d02018-11-28 14:18:05 +010061 fields.append(alert['labels'])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010062 else:
Michal Kobusafbf4d02018-11-28 14:18:05 +010063 action = 'create_case'
Michal Kobusba987052018-11-30 13:01:08 +010064 # Order of keys matters
Michal Kobuse7589f72020-09-11 14:29:37 +020065 fields.append(_format_subject(alert, add_links))
66 fields.append(_format_description(alert, add_links))
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010067 fields.append(alert['labels'])
68 return fields, action