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 | |
| 16 | import hashlib |
| 17 | import logging |
| 18 | import os |
| 19 | import uuid |
| 20 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 21 | from prometheus_client import Counter, Gauge |
| 22 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 23 | import requests |
| 24 | |
| 25 | from simple_salesforce import Salesforce |
| 26 | from simple_salesforce import exceptions as sf_exceptions |
| 27 | |
| 28 | |
| 29 | STATE_MAP = { |
| 30 | 'OK': '060 Informational', |
| 31 | 'UP': '060 Informational', |
| 32 | 'UNKNOWN': '070 Unknown', |
| 33 | 'WARNING': '080 Warning', |
| 34 | 'MINOR': '080 Warning', |
| 35 | 'MAJOR': '090 Critical', |
| 36 | 'CRITICAL': '090 Critical', |
| 37 | 'DOWN': '090 Critical', |
| 38 | 'UNREACHABLE': '090 Critical', |
| 39 | } |
| 40 | |
| 41 | CONFIG_FIELD_MAP = { |
| 42 | 'auth_url': 'instance', |
| 43 | 'username': 'username', |
| 44 | 'password': 'password', |
| 45 | 'organization_id': 'organizationId', |
| 46 | 'environment_id': 'environment_id', |
| 47 | 'sandbox_enabled': 'domain', |
| 48 | } |
| 49 | |
| 50 | logger = logging.getLogger(__name__) |
| 51 | |
| 52 | |
| 53 | def sf_auth_retry(method): |
| 54 | def wrapper(self, *args, **kwargs): |
| 55 | try: |
| 56 | return method(self, *args, **kwargs) |
| 57 | except sf_exceptions.SalesforceExpiredSession: |
| 58 | logger.warning('Salesforce session expired.') |
| 59 | self._auth() |
| 60 | return method(self, *args, **kwargs) |
| 61 | return wrapper |
| 62 | |
| 63 | |
| 64 | class SfNotifierError(Exception): |
Michal Kobus | ee36c42 | 2018-11-26 15:02:31 +0100 | [diff] [blame] | 65 | pass |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 66 | |
| 67 | |
| 68 | class SalesforceClient(object): |
| 69 | |
| 70 | def __init__(self, config): |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 71 | self.metrics = { |
| 72 | 'sf_auth_ok': Gauge('sf_auth_ok', 'sf-notifier'), |
| 73 | 'sf_error_count': Counter('sf_error_count', 'sf-notifier'), |
| 74 | 'sf_request_count': Counter('sf_request_count', 'sf-notifier') |
| 75 | } |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 76 | self.session = requests.Session() |
| 77 | self.config = self._validate_config(config) |
| 78 | self.environment = self.config.pop('environment_id') |
| 79 | self._auth() |
| 80 | self._registered_alerts = {} |
| 81 | |
Michal Kobus | ee36c42 | 2018-11-26 15:02:31 +0100 | [diff] [blame] | 82 | @staticmethod |
| 83 | def _validate_config(config): |
| 84 | kwargs = {} |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 85 | |
| 86 | for param, field in CONFIG_FIELD_MAP.iteritems(): |
| 87 | setting_var = param.upper() |
| 88 | env_var = 'SFDC_{}'.format(setting_var) |
| 89 | kwargs[field] = os.environ.get( |
Michal Kobus | ee36c42 | 2018-11-26 15:02:31 +0100 | [diff] [blame] | 90 | env_var, config.get(setting_var)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 91 | |
| 92 | if field == 'domain': |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 93 | if kwargs[field] in ['true', 'True', True]: |
Michal Kobus | ee36c42 | 2018-11-26 15:02:31 +0100 | [diff] [blame] | 94 | kwargs[field] = 'test' |
| 95 | else: |
| 96 | del kwargs[field] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 97 | continue |
| 98 | |
| 99 | if kwargs[field] is None: |
| 100 | msg = ('Invalid config: missing "{}" field or "{}" environment' |
| 101 | ' variable.').format(param, env_var) |
| 102 | logger.error(msg) |
| 103 | raise SfNotifierError(msg) |
| 104 | return kwargs |
| 105 | |
| 106 | def _auth(self): |
Michal Kobus | ee36c42 | 2018-11-26 15:02:31 +0100 | [diff] [blame] | 107 | kwargs = {'session': self.session} |
| 108 | kwargs.update(self.config) |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 109 | try: |
| 110 | self.sf = Salesforce(**kwargs) |
| 111 | except sf_exceptions.SalesforceAuthenticationFailed: |
| 112 | logger.error('Salesforce authentication failure.') |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 113 | self.metrics['sf_auth_ok'].set(0) |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 114 | return |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 115 | logger.info('Salesforce authentication successful.') |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 116 | self.metrics['sf_auth_ok'].set(1) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 117 | |
Michal Kobus | ee36c42 | 2018-11-26 15:02:31 +0100 | [diff] [blame] | 118 | @staticmethod |
| 119 | def _get_alert_id(labels): |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 120 | alert_id_data = '' |
| 121 | for key in sorted(labels): |
| 122 | alert_id_data += labels[key].replace(".", "\\.") |
| 123 | return hashlib.sha256(alert_id_data).hexdigest() |
| 124 | |
| 125 | @sf_auth_retry |
| 126 | def _create_case(self, subject, body, labels, alert_id): |
| 127 | |
| 128 | if alert_id in self._registered_alerts: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 129 | logger.warning('Duplicate case for alert: {}.'.format(alert_id)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 130 | return 1, self._registered_alerts[alert_id]['Id'] |
| 131 | |
| 132 | severity = labels.get('severity', 'unknown').upper() |
| 133 | payload = { |
| 134 | 'Subject': subject, |
| 135 | 'Description': body, |
| 136 | 'IsMosAlert__c': 'true', |
| 137 | 'Alert_Priority__c': STATE_MAP.get(severity, '070 Unknown'), |
| 138 | 'Alert_Host__c': labels.get('host') or labels.get( |
| 139 | 'instance', 'UNKNOWN' |
| 140 | ), |
| 141 | 'Alert_Service__c': labels.get('service', 'UNKNOWN'), |
| 142 | 'Environment2__c': self.environment, |
| 143 | 'Alert_ID__c': alert_id, |
| 144 | } |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 145 | logger.info('Try to create case: {}.'.format(payload)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 146 | try: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 147 | self.metrics['sf_request_count'].inc() |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 148 | case = self.sf.Case.create(payload) |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 149 | logger.info('Created case: {}.'.format(case)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 150 | except sf_exceptions.SalesforceMalformedRequest as ex: |
| 151 | msg = ex.content[0]['message'] |
| 152 | err_code = ex.content[0]['errorCode'] |
| 153 | |
| 154 | if err_code == 'DUPLICATE_VALUE': |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 155 | logger.warning('Duplicate case: {}.'.format(msg)) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 156 | case_id = msg.split()[-1] |
| 157 | self._registered_alerts[alert_id] = {'Id': case_id} |
| 158 | return 1, case_id |
| 159 | else: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 160 | self.metrics['sf_error_count'].inc() |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 161 | raise |
| 162 | |
| 163 | self._registered_alerts[alert_id] = {'Id': case['id']} |
| 164 | return 0, case['id'] |
| 165 | |
| 166 | @sf_auth_retry |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 167 | def _close_case(self, case_id): |
| 168 | logger.info('Try to close case: {}.'.format(case_id)) |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 169 | self.metrics['sf_request_count'].inc() |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 170 | update = self.sf.Case.update( |
| 171 | case_id, |
| 172 | {'Status': 'Auto-solved', 'Alert_ID__c': uuid.uuid4().hex} |
| 173 | ) |
| 174 | logger.info('Closed case: {}.'.format(case_id)) |
| 175 | return update |
| 176 | |
| 177 | @sf_auth_retry |
| 178 | def _create_feed_item(self, subject, body, case_id): |
| 179 | feed_item = {'Title': subject, 'ParentId': case_id, 'Body': body} |
| 180 | return self.sf.FeedItem.create(feed_item) |
| 181 | |
| 182 | @sf_auth_retry |
| 183 | def _get_case_by_alert_id(self, alert_id): |
| 184 | logger.info('Try to get case by alert ID: {}.'.format(alert_id)) |
| 185 | |
| 186 | if alert_id in self._registered_alerts: |
| 187 | return self._registered_alerts[alert_id] |
| 188 | try: |
| 189 | return self.sf.Case.get_by_custom_id('Alert_ID__c', alert_id) |
| 190 | except sf_exceptions.SalesforceResourceNotFound: |
Michal Kobus | ba98705 | 2018-11-30 13:01:08 +0100 | [diff] [blame] | 191 | if self._registered_alerts.get(alert_id): |
| 192 | del self._registered_alerts[alert_id] |
| 193 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 194 | logger.warning('Alert ID: {} was already solved.'.format(alert_id)) |
| 195 | |
| 196 | def create_case(self, subject, body, status, labels): |
| 197 | alert_id = self._get_alert_id(labels) |
| 198 | |
| 199 | error_code, case_id = self._create_case(subject, body, |
| 200 | labels, alert_id) |
| 201 | |
| 202 | response = {'case_id': case_id, 'alert_id': alert_id, |
| 203 | 'status': 'created'} |
| 204 | |
| 205 | if error_code != 2: |
| 206 | self._create_feed_item(subject, body, case_id) |
| 207 | if error_code == 1: |
| 208 | response['status'] = 'duplicate' |
| 209 | return response |
| 210 | |
| 211 | def close_case(self, labels): |
| 212 | alert_id = self._get_alert_id(labels) |
| 213 | case = self._get_case_by_alert_id(alert_id) |
| 214 | |
| 215 | response = {'alert_id': alert_id, 'status': 'resolved'} |
| 216 | |
| 217 | if case is None: |
| 218 | return response |
| 219 | |
| 220 | if self._registered_alerts.get(alert_id): |
| 221 | del self._registered_alerts[alert_id] |
| 222 | |
| 223 | response['case_id'] = case['Id'] |
| 224 | response['closed'] = self._close_case(case['Id']) |
| 225 | return response |