| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 1 | # Copyright 2017 Intel Research and Development Ireland Limited |
| 2 | # ************************************************************* |
| 3 | |
| 4 | # This file is part of OSM Monitoring module |
| 5 | # All Rights Reserved to Intel Corporation |
| 6 | |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. You may obtain |
| 9 | # a copy of the License at |
| 10 | |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | # License for the specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | |
| 19 | # For those usages not covered by the Apache License, Version 2.0 please |
| 20 | # contact: helena.mcgough@intel.com or adrian.hoban@intel.com |
| 21 | ## |
| 22 | """Carry out alarming requests via Aodh API.""" |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 23 | |
| 24 | import json |
| Helena McGough | 4bebd12 | 2017-09-26 09:42:47 +0100 | [diff] [blame] | 25 | import logging |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 26 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 27 | import six |
| Benjamin Diaz | 821a62e | 2018-04-10 21:57:17 -0300 | [diff] [blame] | 28 | import yaml |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 29 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 30 | from osm_mon.core.auth import AuthManager |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 31 | from osm_mon.core.database import DatabaseManager |
| 32 | from osm_mon.core.message_bus.producer import KafkaProducer |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 33 | from osm_mon.core.settings import Config |
| Benjamin Diaz | ffe5a8e | 2018-05-17 19:02:24 -0300 | [diff] [blame^] | 34 | from osm_mon.plugins.OpenStack.Gnocchi.metrics import METRIC_MAPPINGS |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 35 | from osm_mon.plugins.OpenStack.common import Common |
| Helena McGough | effeb7c | 2017-11-23 15:54:08 +0000 | [diff] [blame] | 36 | from osm_mon.plugins.OpenStack.response import OpenStack_Response |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 37 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 38 | log = logging.getLogger(__name__) |
| 39 | |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 40 | SEVERITIES = { |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 41 | "warning": "low", |
| 42 | "minor": "low", |
| 43 | "major": "moderate", |
| 44 | "critical": "critical", |
| 45 | "indeterminate": "critical"} |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 46 | |
| Helena McGough | 0e7809a | 2017-09-18 11:31:05 +0100 | [diff] [blame] | 47 | STATISTICS = { |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 48 | "average": "mean", |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 49 | "minimum": "min", |
| 50 | "maximum": "max", |
| 51 | "count": "count", |
| 52 | "sum": "sum"} |
| Helena McGough | 0e7809a | 2017-09-18 11:31:05 +0100 | [diff] [blame] | 53 | |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 54 | |
| 55 | class Alarming(object): |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 56 | """Carries out alarming requests and responses via Aodh API.""" |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 57 | |
| 58 | def __init__(self): |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 59 | """Create the OpenStack alarming instance.""" |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 60 | # Initialize configuration and notifications |
| 61 | config = Config.instance() |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 62 | config.read_environ() |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 63 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 64 | self._database_manager = DatabaseManager() |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 65 | self._auth_manager = AuthManager() |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 66 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 67 | # Use the Response class to generate valid json response messages |
| 68 | self._response = OpenStack_Response() |
| 69 | |
| 70 | # Initializer a producer to send responses back to SO |
| 71 | self._producer = KafkaProducer("alarm_response") |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 72 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 73 | def configure_alarm(self, alarm_endpoint, metric_endpoint, auth_token, values, vim_config): |
| 74 | """Create requested alarm in Aodh.""" |
| 75 | url = "{}/v2/alarms/".format(alarm_endpoint) |
| 76 | |
| 77 | # Check if the desired alarm is supported |
| 78 | alarm_name = values['alarm_name'].lower() |
| 79 | metric_name = values['metric_name'].lower() |
| 80 | resource_id = values['resource_uuid'] |
| 81 | |
| 82 | if metric_name not in METRIC_MAPPINGS.keys(): |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 83 | log.warning("This metric is not supported.") |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 84 | return None, False |
| 85 | |
| 86 | # Check for the required metric |
| 87 | metric_id = self.check_for_metric(auth_token, metric_endpoint, metric_name, resource_id) |
| 88 | |
| 89 | try: |
| 90 | if metric_id is not None: |
| 91 | # Create the alarm if metric is available |
| 92 | if 'granularity' in vim_config and 'granularity' not in values: |
| 93 | values['granularity'] = vim_config['granularity'] |
| 94 | payload = self.check_payload(values, metric_name, resource_id, |
| 95 | alarm_name) |
| 96 | new_alarm = Common.perform_request( |
| 97 | url, auth_token, req_type="post", payload=payload) |
| 98 | return json.loads(new_alarm.text)['alarm_id'], True |
| 99 | else: |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 100 | log.warning("The required Gnocchi metric does not exist.") |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 101 | return None, False |
| 102 | |
| 103 | except Exception as exc: |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 104 | log.warning("Failed to create the alarm: %s", exc) |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 105 | return None, False |
| 106 | |
| Benjamin Diaz | b726161 | 2018-05-11 18:00:16 -0300 | [diff] [blame] | 107 | def alarming(self, message, vim_uuid): |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 108 | """Consume info from the message bus to manage alarms.""" |
| Benjamin Diaz | 821a62e | 2018-04-10 21:57:17 -0300 | [diff] [blame] | 109 | try: |
| 110 | values = json.loads(message.value) |
| 111 | except ValueError: |
| 112 | values = yaml.safe_load(message.value) |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 113 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 114 | log.info("OpenStack alarm action required.") |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 115 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 116 | auth_token = Common.get_auth_token(vim_uuid) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 117 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 118 | alarm_endpoint = Common.get_endpoint("alarming", vim_uuid) |
| 119 | metric_endpoint = Common.get_endpoint("metric", vim_uuid) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 120 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 121 | vim_account = self._auth_manager.get_credentials(vim_uuid) |
| 122 | vim_config = json.loads(vim_account.config) |
| 123 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 124 | if message.key == "create_alarm_request": |
| 125 | # Configure/Update an alarm |
| 126 | alarm_details = values['alarm_create_request'] |
| 127 | |
| 128 | alarm_id, alarm_status = self.configure_alarm( |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 129 | alarm_endpoint, metric_endpoint, auth_token, alarm_details, vim_config) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 130 | |
| 131 | # Generate a valid response message, send via producer |
| Benjamin Diaz | b726161 | 2018-05-11 18:00:16 -0300 | [diff] [blame] | 132 | if alarm_status is True: |
| 133 | log.info("Alarm successfully created") |
| Benjamin Diaz | ffe5a8e | 2018-05-17 19:02:24 -0300 | [diff] [blame^] | 134 | self._database_manager.save_alarm(alarm_id, |
| 135 | vim_uuid, |
| 136 | alarm_details['threshold_value'], |
| 137 | alarm_details['operation'].lower(), |
| 138 | alarm_details['metric_name'].lower(), |
| 139 | alarm_details['vdu_name'].lower(), |
| 140 | alarm_details['vnf_member_index'].lower(), |
| 141 | alarm_details['ns_id'].lower() |
| 142 | ) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 143 | try: |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 144 | resp_message = self._response.generate_response( |
| 145 | 'create_alarm_response', status=alarm_status, |
| 146 | alarm_id=alarm_id, |
| 147 | cor_id=alarm_details['correlation_id']) |
| 148 | log.info("Response Message: %s", resp_message) |
| 149 | self._producer.create_alarm_response( |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 150 | 'create_alarm_response', resp_message) |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 151 | except Exception: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 152 | log.exception("Response creation failed:") |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 153 | |
| 154 | elif message.key == "list_alarm_request": |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 155 | # Check for a specified: alarm_name, resource_uuid, severity |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 156 | # and generate the appropriate list |
| 157 | list_details = values['alarm_list_request'] |
| 158 | |
| 159 | alarm_list = self.list_alarms( |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 160 | alarm_endpoint, auth_token, list_details) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 161 | |
| 162 | try: |
| 163 | # Generate and send a list response back |
| 164 | resp_message = self._response.generate_response( |
| 165 | 'list_alarm_response', alarm_list=alarm_list, |
| 166 | cor_id=list_details['correlation_id']) |
| 167 | log.info("Response Message: %s", resp_message) |
| 168 | self._producer.list_alarm_response( |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 169 | 'list_alarm_response', resp_message) |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 170 | except Exception: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 171 | log.exception("Failed to send a valid response back.") |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 172 | |
| 173 | elif message.key == "delete_alarm_request": |
| 174 | request_details = values['alarm_delete_request'] |
| 175 | alarm_id = request_details['alarm_uuid'] |
| 176 | |
| 177 | resp_status = self.delete_alarm( |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 178 | alarm_endpoint, auth_token, alarm_id) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 179 | |
| 180 | # Generate and send a response message |
| 181 | try: |
| 182 | resp_message = self._response.generate_response( |
| 183 | 'delete_alarm_response', alarm_id=alarm_id, |
| 184 | status=resp_status, |
| 185 | cor_id=request_details['correlation_id']) |
| 186 | log.info("Response message: %s", resp_message) |
| 187 | self._producer.delete_alarm_response( |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 188 | 'delete_alarm_response', resp_message) |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 189 | except Exception: |
| 190 | log.exception("Failed to create delete response: ") |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 191 | |
| 192 | elif message.key == "acknowledge_alarm": |
| 193 | # Acknowledge that an alarm has been dealt with by the SO |
| 194 | alarm_id = values['ack_details']['alarm_uuid'] |
| 195 | |
| 196 | response = self.update_alarm_state( |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 197 | alarm_endpoint, auth_token, alarm_id) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 198 | |
| 199 | # Log if an alarm was reset |
| 200 | if response is True: |
| 201 | log.info("Acknowledged the alarm and cleared it.") |
| 202 | else: |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 203 | log.warning("Failed to acknowledge/clear the alarm.") |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 204 | |
| 205 | elif message.key == "update_alarm_request": |
| 206 | # Update alarm configurations |
| 207 | alarm_details = values['alarm_update_request'] |
| 208 | |
| 209 | alarm_id, status = self.update_alarm( |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 210 | alarm_endpoint, auth_token, alarm_details, vim_config) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 211 | |
| 212 | # Generate a response for an update request |
| 213 | try: |
| 214 | resp_message = self._response.generate_response( |
| 215 | 'update_alarm_response', alarm_id=alarm_id, |
| 216 | cor_id=alarm_details['correlation_id'], |
| 217 | status=status) |
| 218 | log.info("Response message: %s", resp_message) |
| 219 | self._producer.update_alarm_response( |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 220 | 'update_alarm_response', resp_message) |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 221 | except Exception: |
| 222 | log.exception("Failed to send an update response: ") |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 223 | |
| 224 | else: |
| 225 | log.debug("Unknown key, no action will be performed") |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 226 | |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 227 | return |
| 228 | |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 229 | def delete_alarm(self, endpoint, auth_token, alarm_id): |
| 230 | """Delete alarm function.""" |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 231 | url = "{}/v2/alarms/%s".format(endpoint) % alarm_id |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 232 | |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 233 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 234 | result = Common.perform_request( |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 235 | url, auth_token, req_type="delete") |
| 236 | if str(result.status_code) == "404": |
| Helena McGough | 4bebd12 | 2017-09-26 09:42:47 +0100 | [diff] [blame] | 237 | log.info("Alarm doesn't exist: %s", result.status_code) |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 238 | # If status code is 404 alarm did not exist |
| 239 | return False |
| 240 | else: |
| 241 | return True |
| 242 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 243 | except Exception: |
| 244 | log.exception("Failed to delete alarm %s :", alarm_id) |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 245 | return False |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 246 | |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 247 | def list_alarms(self, endpoint, auth_token, list_details): |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 248 | """Generate the requested list of alarms.""" |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 249 | url = "{}/v2/alarms/".format(endpoint) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 250 | a_list, name_list, sev_list, res_list = [], [], [], [] |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 251 | |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 252 | # TODO(mcgoughh): for now resource_id is a mandatory field |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 253 | # Check for a resource id |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 254 | try: |
| 255 | resource = list_details['resource_uuid'] |
| 256 | except KeyError as exc: |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 257 | log.warning("Resource id not specified for list request: %s", exc) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 258 | return None |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 259 | |
| 260 | # Checking what fields are specified for a list request |
| 261 | try: |
| 262 | name = list_details['alarm_name'].lower() |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 263 | except KeyError as exc: |
| 264 | log.info("Alarm name isn't specified.") |
| 265 | name = None |
| 266 | |
| 267 | try: |
| 268 | severity = list_details['severity'].lower() |
| 269 | sev = SEVERITIES[severity] |
| 270 | except KeyError as exc: |
| 271 | log.info("Severity is unspecified/incorrectly configured") |
| 272 | sev = None |
| 273 | |
| 274 | # Perform the request to get the desired list |
| 275 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 276 | result = Common.perform_request( |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 277 | url, auth_token, req_type="get") |
| 278 | |
| 279 | if result is not None: |
| 280 | # Get list based on resource id |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 281 | for alarm in json.loads(result.text): |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 282 | rule = alarm['gnocchi_resources_threshold_rule'] |
| 283 | if resource == rule['resource_id']: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 284 | res_list.append(alarm) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 285 | if not res_list: |
| 286 | log.info("No alarms for this resource") |
| 287 | return a_list |
| 288 | |
| 289 | # Generate specified listed if requested |
| 290 | if name is not None and sev is not None: |
| 291 | log.info("Return a list of %s alarms with %s severity.", |
| 292 | name, sev) |
| 293 | for alarm in json.loads(result.text): |
| 294 | if name == alarm['name']: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 295 | name_list.append(alarm) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 296 | for alarm in json.loads(result.text): |
| 297 | if sev == alarm['severity']: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 298 | sev_list.append(alarm) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 299 | name_sev_list = list(set(name_list).intersection(sev_list)) |
| 300 | a_list = list(set(name_sev_list).intersection(res_list)) |
| 301 | elif name is not None: |
| 302 | log.info("Returning a %s list of alarms.", name) |
| 303 | for alarm in json.loads(result.text): |
| 304 | if name == alarm['name']: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 305 | name_list.append(alarm) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 306 | a_list = list(set(name_list).intersection(res_list)) |
| 307 | elif sev is not None: |
| 308 | log.info("Returning %s severity alarm list.", sev) |
| 309 | for alarm in json.loads(result.text): |
| 310 | if sev == alarm['severity']: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 311 | sev_list.append(alarm) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 312 | a_list = list(set(sev_list).intersection(res_list)) |
| 313 | else: |
| 314 | log.info("Returning an entire list of alarms.") |
| 315 | a_list = res_list |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 316 | else: |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 317 | log.info("There are no alarms!") |
| 318 | |
| 319 | except Exception as exc: |
| 320 | log.info("Failed to generate required list: %s", exc) |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 321 | return None |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 322 | |
| 323 | return a_list |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 324 | |
| 325 | def update_alarm_state(self, endpoint, auth_token, alarm_id): |
| 326 | """Set the state of an alarm to ok when ack message is received.""" |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 327 | url = "{}/v2/alarms/%s/state".format(endpoint) % alarm_id |
| 328 | payload = json.dumps("ok") |
| 329 | |
| 330 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 331 | Common.perform_request( |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 332 | url, auth_token, req_type="put", payload=payload) |
| 333 | return True |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 334 | except Exception: |
| 335 | log.exception("Unable to update alarm state: ") |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 336 | return False |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 337 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 338 | def update_alarm(self, endpoint, auth_token, values, vim_config): |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 339 | """Get alarm name for an alarm configuration update.""" |
| 340 | # Get already existing alarm details |
| 341 | url = "{}/v2/alarms/%s".format(endpoint) % values['alarm_uuid'] |
| 342 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 343 | # Gets current configurations about the alarm |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 344 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 345 | result = Common.perform_request( |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 346 | url, auth_token, req_type="get") |
| 347 | alarm_name = json.loads(result.text)['name'] |
| 348 | rule = json.loads(result.text)['gnocchi_resources_threshold_rule'] |
| 349 | alarm_state = json.loads(result.text)['state'] |
| 350 | resource_id = rule['resource_id'] |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 351 | metric_name = [key for key, value in six.iteritems(METRIC_MAPPINGS) if value == rule['metric']][0] |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 352 | except Exception as exc: |
| Benjamin Diaz | b726161 | 2018-05-11 18:00:16 -0300 | [diff] [blame] | 353 | log.exception("Failed to retrieve existing alarm info. Can only update OSM alarms.") |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 354 | return None, False |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 355 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 356 | # Generates and check payload configuration for alarm update |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 357 | if 'granularity' in vim_config and 'granularity' not in values: |
| 358 | values['granularity'] = vim_config['granularity'] |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 359 | payload = self.check_payload(values, metric_name, resource_id, |
| 360 | alarm_name, alarm_state=alarm_state) |
| 361 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 362 | # Updates the alarm configurations with the valid payload |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 363 | if payload is not None: |
| 364 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 365 | update_alarm = Common.perform_request( |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 366 | url, auth_token, req_type="put", payload=payload) |
| 367 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 368 | return json.loads(update_alarm.text)['alarm_id'], True |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 369 | except Exception as exc: |
| Benjamin Diaz | b726161 | 2018-05-11 18:00:16 -0300 | [diff] [blame] | 370 | log.exception("Alarm update could not be performed: ") |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 371 | return None, False |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 372 | |
| 373 | def check_payload(self, values, metric_name, resource_id, |
| 374 | alarm_name, alarm_state=None): |
| 375 | """Check that the payload is configuration for update/create alarm.""" |
| 376 | try: |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 377 | cfg = Config.instance() |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 378 | # Check state and severity |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 379 | |
| 380 | severity = 'critical' |
| 381 | if 'severity' in values: |
| 382 | severity = values['severity'].lower() |
| 383 | |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 384 | if severity == "indeterminate": |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 385 | alarm_state = "insufficient data" |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 386 | if alarm_state is None: |
| 387 | alarm_state = "ok" |
| 388 | |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 389 | statistic = values['statistic'].lower() |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 390 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 391 | granularity = cfg.OS_DEFAULT_GRANULARITY |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 392 | if 'granularity' in values: |
| 393 | granularity = values['granularity'] |
| 394 | |
| 395 | resource_type = 'generic' |
| 396 | if 'resource_type' in values: |
| 397 | resource_type = values['resource_type'].lower() |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 398 | |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 399 | # Try to configure the payload for the update/create request |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 400 | # Can only update: threshold, operation, statistic and |
| 401 | # the severity of the alarm |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 402 | rule = {'threshold': values['threshold_value'], |
| 403 | 'comparison_operator': values['operation'].lower(), |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 404 | 'metric': METRIC_MAPPINGS[metric_name], |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 405 | 'resource_id': resource_id, |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 406 | 'resource_type': resource_type, |
| 407 | 'aggregation_method': STATISTICS[statistic], |
| 408 | 'granularity': granularity, } |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 409 | payload = json.dumps({'state': alarm_state, |
| 410 | 'name': alarm_name, |
| 411 | 'severity': SEVERITIES[severity], |
| 412 | 'type': 'gnocchi_resources_threshold', |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 413 | 'gnocchi_resources_threshold_rule': rule, |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 414 | 'alarm_actions': [cfg.OS_NOTIFIER_URI], }) |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 415 | return payload |
| 416 | except KeyError as exc: |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 417 | log.warning("Alarm is not configured correctly: %s", exc) |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 418 | return None |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 419 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 420 | def get_alarm_state(self, endpoint, auth_token, alarm_id): |
| 421 | """Get the state of the alarm.""" |
| 422 | url = "{}/v2/alarms/%s/state".format(endpoint) % alarm_id |
| 423 | |
| 424 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 425 | alarm_state = Common.perform_request( |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 426 | url, auth_token, req_type="get") |
| 427 | return json.loads(alarm_state.text) |
| 428 | except Exception as exc: |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 429 | log.warning("Failed to get the state of the alarm:%s", exc) |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 430 | return None |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 431 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 432 | def check_for_metric(self, auth_token, metric_endpoint, m_name, r_id): |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 433 | """Check for the alarm metric.""" |
| 434 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 435 | url = "{}/v1/metric?sort=name:asc".format(metric_endpoint) |
| 436 | result = Common.perform_request( |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 437 | url, auth_token, req_type="get") |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 438 | metric_list = [] |
| 439 | metrics_partial = json.loads(result.text) |
| 440 | for metric in metrics_partial: |
| 441 | metric_list.append(metric) |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 442 | |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 443 | while len(json.loads(result.text)) > 0: |
| 444 | last_metric_id = metrics_partial[-1]['id'] |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 445 | url = "{}/v1/metric?sort=name:asc&marker={}".format(metric_endpoint, last_metric_id) |
| 446 | result = Common.perform_request( |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 447 | url, auth_token, req_type="get") |
| 448 | if len(json.loads(result.text)) > 0: |
| 449 | metrics_partial = json.loads(result.text) |
| 450 | for metric in metrics_partial: |
| 451 | metric_list.append(metric) |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 452 | metric_id = None |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 453 | for metric in metric_list: |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 454 | name = metric['name'] |
| 455 | resource = metric['resource_id'] |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 456 | if name == METRIC_MAPPINGS[m_name] and resource == r_id: |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 457 | metric_id = metric['id'] |
| 458 | log.info("The required metric exists, an alarm will be created.") |
| 459 | return metric_id |
| 460 | except Exception as exc: |
| 461 | log.info("Desired Gnocchi metric not found:%s", exc) |
| 462 | return None |