blob: de02814889789a7b28d2a757abe8176fb3d6c2d4 [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
16
17RESOLVED_STATUSES = ('UP', 'OK', 'resolved')
18
19
Michal Kobusba987052018-11-30 13:01:08 +010020def _format_subject(alert, env):
21 subject = '[{}]'.format(alert['labels'].get('environment', env))
22
23 host = alert['labels'].get('host')
24 if host is not None:
25 subject = '{}[{}]'.format(subject, host)
26
27 return '{} {}'.format(subject, alert['annotations']['summary'])
28
29
30def alert_fields_and_action(alert, env):
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010031 fields = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010032
33 if alert['status'] in RESOLVED_STATUSES:
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010034 action = 'close_case'
Michal Kobusafbf4d02018-11-28 14:18:05 +010035 fields.append(alert['labels'])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010036 else:
Michal Kobusafbf4d02018-11-28 14:18:05 +010037 action = 'create_case'
Michal Kobusba987052018-11-30 13:01:08 +010038 # Order of keys matters
39 fields.append(_format_subject(alert, env))
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010040 fields.append(alert['annotations']['description'])
41 fields.append(alert['status'])
42 fields.append(alert['labels'])
43 return fields, action