blob: 1c99a0a7aadcd00474e1c83811b41657be7d66c2 [file] [log] [blame]
Michal Kobusaa3accf2019-06-05 12:25:09 +02001# 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 Kobus211ee922019-04-15 17:44:06 +020016import os
Michal Kobuse7589f72020-09-11 14:29:37 +020017import urllib
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010018
19RESOLVED_STATUSES = ('UP', 'OK', 'resolved')
20
21
Michal Kobus211ee922019-04-15 17:44:06 +020022def create_file(file_path):
23 if not os.path.exists(file_path):
24 with open(file_path, 'w+'):
25 return file_path
26
27
Michal Kobuse7589f72020-09-11 14:29:37 +020028def _format_subject(alert, add_links=False):
29 if add_links:
30 severity = alert['labels']['severity'].upper()
31 name = alert['labels']['alertname']
32 return '[{}] {}'.format(severity, name)
33
Michal Kobus23ba2f72018-12-03 17:11:20 +010034 subject = alert['annotations']['summary']
Michal Kobusba987052018-11-30 13:01:08 +010035 host = alert['labels'].get('host')
36 if host is not None:
Michal Kobus23ba2f72018-12-03 17:11:20 +010037 return '[{}] {}'.format(host, subject)
38 return subject
Michal Kobusba987052018-11-30 13:01:08 +010039
40
Michal Kobus23ba2f72018-12-03 17:11:20 +010041def _remove_advice(description):
42 return description.split(' Modify the')[0]
43
44
Michal Kobuse7589f72020-09-11 14:29:37 +020045def _get_prometheus_link(alert):
46 # switch from Console to Graph tab to show trends
47 condition_url = alert['generatorURL'].replace('g0.tab=1', 'g0.tab=0')
48
49 # list alert instances in the cluster
50 # because of Python formatting - double braces were used
51 alert_query = ('g1.range_input=1h&'
52 'g1.expr=ALERTS{{alertname="{alertname}",'
53 'alertstate="firing"}}&'
54 'g1.tab=0')
55 alert_query = alert_query.format(**alert['labels'])
56 alert_query = urllib.quote(alert_query)
57 return '{}&{}'.format(condition_url, alert_query)
Michal Kobus23ba2f72018-12-03 17:11:20 +010058
59
Michal Kobuse7589f72020-09-11 14:29:37 +020060def _format_description(alert, add_links=False):
61 description_old = _remove_advice(alert['annotations']['description'])
62
63 if not add_links:
64 return description_old
65
66 msg = ('To check trends of the alert underlying condition metric '
67 ' and alert occurrence click on Prometheus UI link')
68
69 prometheus_url = _get_prometheus_link(alert)
70 return '{}\n\n{}:\n{}'.format(description_old, msg, prometheus_url)
71
72
73def alert_fields_and_action(alert, add_links=False):
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010074 fields = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010075
76 if alert['status'] in RESOLVED_STATUSES:
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010077 action = 'close_case'
Michal Kobusafbf4d02018-11-28 14:18:05 +010078 fields.append(alert['labels'])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010079 else:
Michal Kobusafbf4d02018-11-28 14:18:05 +010080 action = 'create_case'
Michal Kobusba987052018-11-30 13:01:08 +010081 # Order of keys matters
Michal Kobuse7589f72020-09-11 14:29:37 +020082 fields.append(_format_subject(alert, add_links))
83 fields.append(_format_description(alert, add_links))
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010084 fields.append(alert['labels'])
85 return fields, action