blob: d2c713b2059ff17d1c86e0936dccdd4380a49b26 [file] [log] [blame]
Michal Kobus211ee922019-04-15 17:44:06 +02001import os
2
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +01003
4RESOLVED_STATUSES = ('UP', 'OK', 'resolved')
5
6
Michal Kobus211ee922019-04-15 17:44:06 +02007def 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 Kobus23ba2f72018-12-03 17:11:20 +010013def _format_subject(alert):
14 subject = alert['annotations']['summary']
Michal Kobusba987052018-11-30 13:01:08 +010015 host = alert['labels'].get('host')
16 if host is not None:
Michal Kobus23ba2f72018-12-03 17:11:20 +010017 return '[{}] {}'.format(host, subject)
18 return subject
Michal Kobusba987052018-11-30 13:01:08 +010019
20
Michal Kobus23ba2f72018-12-03 17:11:20 +010021def _remove_advice(description):
22 return description.split(' Modify the')[0]
23
24
25def _format_description(alert):
26 return _remove_advice(alert['annotations']['description'])
27
28
29def alert_fields_and_action(alert):
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010030 fields = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010031
32 if alert['status'] in RESOLVED_STATUSES:
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010033 action = 'close_case'
Michal Kobusafbf4d02018-11-28 14:18:05 +010034 fields.append(alert['labels'])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010035 else:
Michal Kobusafbf4d02018-11-28 14:18:05 +010036 action = 'create_case'
Michal Kobusba987052018-11-30 13:01:08 +010037 # Order of keys matters
Michal Kobus23ba2f72018-12-03 17:11:20 +010038 fields.append(_format_subject(alert))
39 fields.append(_format_description(alert))
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010040 fields.append(alert['labels'])
41 return fields, action