blob: e9c221b7feda0c5df3871ebe6e67e4f727910944 [file] [log] [blame]
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +01001# 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
17
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 Kobus23ba2f72018-12-03 17:11:20 +010028def _format_subject(alert):
29 subject = alert['annotations']['summary']
Michal Kobusba987052018-11-30 13:01:08 +010030 host = alert['labels'].get('host')
31 if host is not None:
Michal Kobus23ba2f72018-12-03 17:11:20 +010032 return '[{}] {}'.format(host, subject)
33 return subject
Michal Kobusba987052018-11-30 13:01:08 +010034
35
Michal Kobus23ba2f72018-12-03 17:11:20 +010036def _remove_advice(description):
37 return description.split(' Modify the')[0]
38
39
40def _format_description(alert):
41 return _remove_advice(alert['annotations']['description'])
42
43
44def alert_fields_and_action(alert):
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010045 fields = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010046
47 if alert['status'] in RESOLVED_STATUSES:
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010048 action = 'close_case'
Michal Kobusafbf4d02018-11-28 14:18:05 +010049 fields.append(alert['labels'])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010050 else:
Michal Kobusafbf4d02018-11-28 14:18:05 +010051 action = 'create_case'
Michal Kobusba987052018-11-30 13:01:08 +010052 # Order of keys matters
Michal Kobus23ba2f72018-12-03 17:11:20 +010053 fields.append(_format_subject(alert))
54 fields.append(_format_description(alert))
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010055 fields.append(alert['labels'])
56 return fields, action