Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [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 |
| 17 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 18 | |
| 19 | RESOLVED_STATUSES = ('UP', 'OK', 'resolved') |
| 20 | |
| 21 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame^] | 22 | def 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 Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 28 | def _format_subject(alert): |
| 29 | subject = alert['annotations']['summary'] |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 30 | host = alert['labels'].get('host') |
| 31 | if host is not None: |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 32 | return '[{}] {}'.format(host, subject) |
| 33 | return subject |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 34 | |
| 35 | |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 36 | def _remove_advice(description): |
| 37 | return description.split(' Modify the')[0] |
| 38 | |
| 39 | |
| 40 | def _format_description(alert): |
| 41 | return _remove_advice(alert['annotations']['description']) |
| 42 | |
| 43 | |
| 44 | def alert_fields_and_action(alert): |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 45 | fields = [] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 46 | |
| 47 | if alert['status'] in RESOLVED_STATUSES: |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 48 | action = 'close_case' |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 49 | fields.append(alert['labels']) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 50 | else: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 51 | action = 'create_case' |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 52 | # Order of keys matters |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 53 | fields.append(_format_subject(alert)) |
| 54 | fields.append(_format_description(alert)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 55 | fields.append(alert['labels']) |
| 56 | return fields, action |