| # Copyright 2018: Mirantis Inc. |
| # All Rights Reserved. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| # not use this file except in compliance with the License. You may obtain |
| # a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations |
| # under the License. |
| |
| |
| RESOLVED_STATUSES = ('UP', 'OK', 'resolved') |
| |
| |
| def _format_subject(alert): |
| subject = alert['annotations']['summary'] |
| host = alert['labels'].get('host') |
| if host is not None: |
| return '[{}] {}'.format(host, subject) |
| return subject |
| |
| |
| def _remove_advice(description): |
| return description.split(' Modify the')[0] |
| |
| |
| def _format_description(alert): |
| return _remove_advice(alert['annotations']['description']) |
| |
| |
| def alert_fields_and_action(alert): |
| fields = [] |
| |
| if alert['status'] in RESOLVED_STATUSES: |
| action = 'close_case' |
| fields.append(alert['labels']) |
| else: |
| action = 'create_case' |
| # Order of keys matters |
| fields.append(_format_subject(alert)) |
| fields.append(_format_description(alert)) |
| fields.append(alert['labels']) |
| return fields, action |