Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 1 | import os |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 2 | |
| 3 | RESOLVED_STATUSES = ('UP', 'OK', 'resolved') |
| 4 | |
| 5 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 6 | def 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 Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 12 | def _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 Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 18 | subject = alert['annotations']['summary'] |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 19 | host = alert['labels'].get('host') |
| 20 | if host is not None: |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 21 | return '[{}] {}'.format(host, subject) |
| 22 | return subject |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 23 | |
| 24 | |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 25 | def _remove_advice(description): |
| 26 | return description.split(' Modify the')[0] |
| 27 | |
| 28 | |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 29 | def _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 Kobus | 043c599 | 2020-09-30 16:26:08 +0200 | [diff] [blame] | 33 | # list alert firing instances in the cluster |
| 34 | # via query: ALERTS{alertname="<alertname>",alertstate="firing"} |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 35 | alert_query = ('g1.range_input=1h&' |
Michal Kobus | 043c599 | 2020-09-30 16:26:08 +0200 | [diff] [blame] | 36 | 'g1.expr=ALERTS%7Balertname%3D%22' |
| 37 | '{}' |
| 38 | '%22%2Calertstate%3D%22firing%22%7D&' |
| 39 | 'g1.tab=0').format(alert['labels']['alertname']) |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 40 | return '{}&{}'.format(condition_url, alert_query) |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 41 | |
| 42 | |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 43 | def _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 Kobus | 28b37f9 | 2022-06-14 11:06:24 +0200 | [diff] [blame] | 56 | def alert_fields_and_action(alert, add_links=False, cluster_id=None): |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 57 | fields = [] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 58 | |
Michal Kobus | 28b37f9 | 2022-06-14 11:06:24 +0200 | [diff] [blame] | 59 | if cluster_id is not None: |
| 60 | if alert['labels'].get('cluster_id') is None: |
| 61 | alert['labels']['cluster_id'] = cluster_id |
| 62 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 63 | if alert['status'] in RESOLVED_STATUSES: |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 64 | action = 'close_case' |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 65 | fields.append(alert['labels']) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 66 | else: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 67 | action = 'create_case' |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 68 | # Order of keys matters |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 69 | fields.append(_format_subject(alert, add_links)) |
| 70 | fields.append(_format_description(alert, add_links)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 71 | fields.append(alert['labels']) |
| 72 | return fields, action |
Michal Kobus | 2e85ef8 | 2021-06-24 18:01:43 +0200 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def is_true(name): |
| 76 | if isinstance(name, str): |
| 77 | return name.lower() == 'true' |
| 78 | return name is True |