Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 1 | import os |
| 2 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 3 | |
| 4 | RESOLVED_STATUSES = ('UP', 'OK', 'resolved') |
| 5 | |
| 6 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 7 | def create_file(file_path): |
| 8 | if not os.path.exists(file_path): |
| 9 | with open(file_path, 'w+'): |
| 10 | return file_path |
| 11 | |
| 12 | |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 13 | def _format_subject(alert): |
| 14 | subject = alert['annotations']['summary'] |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 15 | host = alert['labels'].get('host') |
| 16 | if host is not None: |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 17 | return '[{}] {}'.format(host, subject) |
| 18 | return subject |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 19 | |
| 20 | |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 21 | def _remove_advice(description): |
| 22 | return description.split(' Modify the')[0] |
| 23 | |
| 24 | |
| 25 | def _format_description(alert): |
| 26 | return _remove_advice(alert['annotations']['description']) |
| 27 | |
| 28 | |
| 29 | def alert_fields_and_action(alert): |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 30 | fields = [] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 31 | |
| 32 | if alert['status'] in RESOLVED_STATUSES: |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 33 | action = 'close_case' |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 34 | fields.append(alert['labels']) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 35 | else: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 36 | action = 'create_case' |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 37 | # Order of keys matters |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 38 | fields.append(_format_subject(alert)) |
| 39 | fields.append(_format_description(alert)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 40 | fields.append(alert['labels']) |
| 41 | return fields, action |