Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 1 | # Copyright 2018: Mirantis Inc. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 16 | import os |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 17 | |
| 18 | RESOLVED_STATUSES = ('UP', 'OK', 'resolved') |
| 19 | |
| 20 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 21 | def create_file(file_path): |
| 22 | if not os.path.exists(file_path): |
| 23 | with open(file_path, 'w+'): |
| 24 | return file_path |
| 25 | |
| 26 | |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 27 | def _format_subject(alert, add_links=False): |
| 28 | if add_links: |
| 29 | severity = alert['labels']['severity'].upper() |
| 30 | name = alert['labels']['alertname'] |
| 31 | return '[{}] {}'.format(severity, name) |
| 32 | |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 33 | subject = alert['annotations']['summary'] |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 34 | host = alert['labels'].get('host') |
| 35 | if host is not None: |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 36 | return '[{}] {}'.format(host, subject) |
| 37 | return subject |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 38 | |
| 39 | |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 40 | def _remove_advice(description): |
| 41 | return description.split(' Modify the')[0] |
| 42 | |
| 43 | |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 44 | def _get_prometheus_link(alert): |
| 45 | # switch from Console to Graph tab to show trends |
| 46 | condition_url = alert['generatorURL'].replace('g0.tab=1', 'g0.tab=0') |
| 47 | |
Michal Kobus | 043c599 | 2020-09-30 16:26:08 +0200 | [diff] [blame^] | 48 | # list alert firing instances in the cluster |
| 49 | # via query: ALERTS{alertname="<alertname>",alertstate="firing"} |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 50 | alert_query = ('g1.range_input=1h&' |
Michal Kobus | 043c599 | 2020-09-30 16:26:08 +0200 | [diff] [blame^] | 51 | 'g1.expr=ALERTS%7Balertname%3D%22' |
| 52 | '{}' |
| 53 | '%22%2Calertstate%3D%22firing%22%7D&' |
| 54 | 'g1.tab=0').format(alert['labels']['alertname']) |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 55 | return '{}&{}'.format(condition_url, alert_query) |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 56 | |
| 57 | |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 58 | def _format_description(alert, add_links=False): |
| 59 | description_old = _remove_advice(alert['annotations']['description']) |
| 60 | |
| 61 | if not add_links: |
| 62 | return description_old |
| 63 | |
| 64 | msg = ('To check trends of the alert underlying condition metric ' |
| 65 | ' and alert occurrence click on Prometheus UI link') |
| 66 | |
| 67 | prometheus_url = _get_prometheus_link(alert) |
| 68 | return '{}\n\n{}:\n{}'.format(description_old, msg, prometheus_url) |
| 69 | |
| 70 | |
| 71 | def alert_fields_and_action(alert, add_links=False): |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 72 | fields = [] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 73 | |
| 74 | if alert['status'] in RESOLVED_STATUSES: |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 75 | action = 'close_case' |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 76 | fields.append(alert['labels']) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 77 | else: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 78 | action = 'create_case' |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 79 | # Order of keys matters |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 80 | fields.append(_format_subject(alert, add_links)) |
| 81 | fields.append(_format_description(alert, add_links)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 82 | fields.append(alert['labels']) |
| 83 | return fields, action |