| 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.""" |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 60 | self._database_manager = DatabaseManager() |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 61 | self._auth_manager = AuthManager() |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 62 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 63 | # Use the Response class to generate valid json response messages |
| 64 | self._response = OpenStack_Response() |
| 65 | |
| 66 | # Initializer a producer to send responses back to SO |
| 67 | self._producer = KafkaProducer("alarm_response") |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 68 | |
| Benjamin Diaz | b726161 | 2018-05-11 18:00:16 -0300 | [diff] [blame] | 69 | def alarming(self, message, vim_uuid): |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 70 | """ |
| 71 | Processes alarm request message depending on it's key |
| 72 | :param message: Message containing key and value attributes. This last one can be in JSON or YAML format. |
| 73 | :param vim_uuid: UUID of the VIM to handle the alarm request. |
| 74 | :return: |
| 75 | """ |
| Benjamin Diaz | 821a62e | 2018-04-10 21:57:17 -0300 | [diff] [blame] | 76 | try: |
| 77 | values = json.loads(message.value) |
| 78 | except ValueError: |
| 79 | values = yaml.safe_load(message.value) |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 80 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 81 | log.info("OpenStack alarm action required.") |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 82 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 83 | auth_token = Common.get_auth_token(vim_uuid) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 84 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 85 | alarm_endpoint = Common.get_endpoint("alarming", vim_uuid) |
| 86 | metric_endpoint = Common.get_endpoint("metric", vim_uuid) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 87 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 88 | vim_account = self._auth_manager.get_credentials(vim_uuid) |
| 89 | vim_config = json.loads(vim_account.config) |
| 90 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 91 | if message.key == "create_alarm_request": |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 92 | alarm_details = values['alarm_create_request'] |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 93 | alarm_id = None |
| 94 | status = False |
| 95 | try: |
| 96 | metric_name = alarm_details['metric_name'].lower() |
| 97 | resource_id = alarm_details['resource_uuid'] |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 98 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 99 | self.check_for_metric(auth_token, metric_endpoint, metric_name, resource_id) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 100 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 101 | alarm_id = self.configure_alarm( |
| 102 | alarm_endpoint, auth_token, alarm_details, vim_config) |
| 103 | |
| Benjamin Diaz | b726161 | 2018-05-11 18:00:16 -0300 | [diff] [blame] | 104 | log.info("Alarm successfully created") |
| Benjamin Diaz | ffe5a8e | 2018-05-17 19:02:24 -0300 | [diff] [blame] | 105 | self._database_manager.save_alarm(alarm_id, |
| 106 | vim_uuid, |
| 107 | alarm_details['threshold_value'], |
| 108 | alarm_details['operation'].lower(), |
| 109 | alarm_details['metric_name'].lower(), |
| 110 | alarm_details['vdu_name'].lower(), |
| Benjamin Diaz | 453aabf | 2018-08-22 17:31:58 -0300 | [diff] [blame] | 111 | alarm_details['vnf_member_index'], |
| Benjamin Diaz | ffe5a8e | 2018-05-17 19:02:24 -0300 | [diff] [blame] | 112 | alarm_details['ns_id'].lower() |
| 113 | ) |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 114 | status = True |
| 115 | except Exception as e: |
| 116 | log.exception("Error creating alarm") |
| 117 | raise e |
| 118 | finally: |
| 119 | self._generate_and_send_response('create_alarm_response', |
| 120 | alarm_details['correlation_id'], |
| 121 | status=status, |
| 122 | alarm_id=alarm_id) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 123 | |
| 124 | elif message.key == "list_alarm_request": |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 125 | list_details = values['alarm_list_request'] |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 126 | alarm_list = None |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 127 | try: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 128 | alarm_list = self.list_alarms( |
| 129 | alarm_endpoint, auth_token, list_details) |
| 130 | except Exception as e: |
| 131 | log.exception("Error listing alarms") |
| 132 | raise e |
| 133 | finally: |
| 134 | self._generate_and_send_response('list_alarm_response', |
| 135 | list_details['correlation_id'], |
| 136 | alarm_list=alarm_list) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 137 | |
| 138 | elif message.key == "delete_alarm_request": |
| 139 | request_details = values['alarm_delete_request'] |
| 140 | alarm_id = request_details['alarm_uuid'] |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 141 | status = False |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 142 | try: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 143 | self.delete_alarm( |
| 144 | alarm_endpoint, auth_token, alarm_id) |
| 145 | status = True |
| 146 | except Exception as e: |
| 147 | log.exception("Error deleting alarm") |
| 148 | raise e |
| 149 | finally: |
| 150 | self._generate_and_send_response('delete_alarm_response', |
| 151 | request_details['correlation_id'], |
| 152 | status=status, |
| 153 | alarm_id=alarm_id) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 154 | |
| 155 | elif message.key == "acknowledge_alarm": |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 156 | try: |
| 157 | alarm_id = values['ack_details']['alarm_uuid'] |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 158 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 159 | self.update_alarm_state( |
| 160 | alarm_endpoint, auth_token, alarm_id) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 161 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 162 | log.info("Acknowledged the alarm and cleared it.") |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 163 | except Exception as e: |
| 164 | log.exception("Error acknowledging alarm") |
| 165 | raise e |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 166 | |
| 167 | elif message.key == "update_alarm_request": |
| 168 | # Update alarm configurations |
| 169 | alarm_details = values['alarm_update_request'] |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 170 | alarm_id = None |
| 171 | status = False |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 172 | try: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 173 | alarm_id = self.update_alarm( |
| 174 | alarm_endpoint, auth_token, alarm_details, vim_config) |
| 175 | status = True |
| 176 | except Exception as e: |
| 177 | log.exception("Error updating alarm") |
| 178 | raise e |
| 179 | finally: |
| Benjamin Diaz | 326907a | 2018-06-18 14:21:46 -0300 | [diff] [blame] | 180 | self._generate_and_send_response('update_alarm_response', |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 181 | alarm_details['correlation_id'], |
| 182 | status=status, |
| 183 | alarm_id=alarm_id) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 184 | |
| 185 | else: |
| 186 | log.debug("Unknown key, no action will be performed") |
| Helena McGough | c85d984 | 2017-08-29 09:52:56 +0000 | [diff] [blame] | 187 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 188 | def configure_alarm(self, alarm_endpoint, auth_token, values, vim_config): |
| 189 | """Create requested alarm in Aodh.""" |
| 190 | url = "{}/v2/alarms/".format(alarm_endpoint) |
| 191 | |
| 192 | # Check if the desired alarm is supported |
| 193 | alarm_name = values['alarm_name'].lower() |
| 194 | metric_name = values['metric_name'].lower() |
| 195 | resource_id = values['resource_uuid'] |
| 196 | |
| 197 | if metric_name not in METRIC_MAPPINGS.keys(): |
| 198 | raise KeyError("Metric {} is not supported.".format(metric_name)) |
| 199 | |
| 200 | if 'granularity' in vim_config and 'granularity' not in values: |
| 201 | values['granularity'] = vim_config['granularity'] |
| 202 | payload = self.check_payload(values, metric_name, resource_id, |
| 203 | alarm_name) |
| 204 | new_alarm = Common.perform_request( |
| 205 | url, auth_token, req_type="post", payload=payload) |
| 206 | return json.loads(new_alarm.text)['alarm_id'] |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 207 | |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 208 | def delete_alarm(self, endpoint, auth_token, alarm_id): |
| 209 | """Delete alarm function.""" |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 210 | url = "{}/v2/alarms/%s".format(endpoint) % alarm_id |
| Helena McGough | f358b4f | 2017-08-23 10:11:42 +0000 | [diff] [blame] | 211 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 212 | result = Common.perform_request( |
| 213 | url, auth_token, req_type="delete") |
| 214 | if str(result.status_code) == "404": |
| 215 | raise ValueError("Alarm {} doesn't exist".format(alarm_id)) |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 216 | |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 217 | def list_alarms(self, endpoint, auth_token, list_details): |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 218 | """Generate the requested list of alarms.""" |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 219 | url = "{}/v2/alarms/".format(endpoint) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 220 | a_list, name_list, sev_list, res_list = [], [], [], [] |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 221 | |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 222 | # TODO(mcgoughh): for now resource_id is a mandatory field |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 223 | # Check for a resource id |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 224 | try: |
| 225 | resource = list_details['resource_uuid'] |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 226 | name = list_details['alarm_name'].lower() |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 227 | severity = list_details['severity'].lower() |
| 228 | sev = SEVERITIES[severity] |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 229 | except KeyError as e: |
| 230 | log.warning("Missing parameter for alarm list request: %s", e) |
| 231 | raise e |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 232 | |
| 233 | # Perform the request to get the desired list |
| 234 | try: |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 235 | result = Common.perform_request( |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 236 | url, auth_token, req_type="get") |
| 237 | |
| 238 | if result is not None: |
| 239 | # Get list based on resource id |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 240 | for alarm in json.loads(result.text): |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 241 | rule = alarm['gnocchi_resources_threshold_rule'] |
| 242 | if resource == rule['resource_id']: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 243 | res_list.append(alarm['alarm_id']) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 244 | |
| 245 | # Generate specified listed if requested |
| 246 | if name is not None and sev is not None: |
| 247 | log.info("Return a list of %s alarms with %s severity.", |
| 248 | name, sev) |
| 249 | for alarm in json.loads(result.text): |
| 250 | if name == alarm['name']: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 251 | name_list.append(alarm['alarm_id']) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 252 | for alarm in json.loads(result.text): |
| 253 | if sev == alarm['severity']: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 254 | sev_list.append(alarm['alarm_id']) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 255 | name_sev_list = list(set(name_list).intersection(sev_list)) |
| 256 | a_list = list(set(name_sev_list).intersection(res_list)) |
| 257 | elif name is not None: |
| 258 | log.info("Returning a %s list of alarms.", name) |
| 259 | for alarm in json.loads(result.text): |
| 260 | if name == alarm['name']: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 261 | name_list.append(alarm['alarm_id']) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 262 | a_list = list(set(name_list).intersection(res_list)) |
| 263 | elif sev is not None: |
| 264 | log.info("Returning %s severity alarm list.", sev) |
| 265 | for alarm in json.loads(result.text): |
| 266 | if sev == alarm['severity']: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 267 | sev_list.append(alarm['alarm_id']) |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 268 | a_list = list(set(sev_list).intersection(res_list)) |
| 269 | else: |
| 270 | log.info("Returning an entire list of alarms.") |
| 271 | a_list = res_list |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 272 | else: |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 273 | log.info("There are no alarms!") |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 274 | response_list = [] |
| 275 | for alarm in json.loads(result.text): |
| 276 | if alarm['alarm_id'] in a_list: |
| 277 | response_list.append(alarm) |
| 278 | return response_list |
| Helena McGough | f152768 | 2017-09-22 10:55:22 +0100 | [diff] [blame] | 279 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 280 | except Exception as e: |
| 281 | log.exception("Failed to generate alarm list: ") |
| 282 | raise e |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 283 | |
| 284 | def update_alarm_state(self, endpoint, auth_token, alarm_id): |
| 285 | """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] | 286 | url = "{}/v2/alarms/%s/state".format(endpoint) % alarm_id |
| 287 | payload = json.dumps("ok") |
| 288 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 289 | Common.perform_request( |
| 290 | url, auth_token, req_type="put", payload=payload) |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 291 | |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 292 | def update_alarm(self, endpoint, auth_token, values, vim_config): |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 293 | """Get alarm name for an alarm configuration update.""" |
| 294 | # Get already existing alarm details |
| 295 | url = "{}/v2/alarms/%s".format(endpoint) % values['alarm_uuid'] |
| 296 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 297 | # Gets current configurations about the alarm |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 298 | result = Common.perform_request( |
| 299 | url, auth_token, req_type="get") |
| 300 | alarm_name = json.loads(result.text)['name'] |
| 301 | rule = json.loads(result.text)['gnocchi_resources_threshold_rule'] |
| 302 | alarm_state = json.loads(result.text)['state'] |
| 303 | resource_id = rule['resource_id'] |
| 304 | 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] | 305 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 306 | # Generates and check payload configuration for alarm update |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 307 | if 'granularity' in vim_config and 'granularity' not in values: |
| 308 | values['granularity'] = vim_config['granularity'] |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 309 | payload = self.check_payload(values, metric_name, resource_id, |
| 310 | alarm_name, alarm_state=alarm_state) |
| 311 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 312 | # Updates the alarm configurations with the valid payload |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 313 | update_alarm = Common.perform_request( |
| 314 | url, auth_token, req_type="put", payload=payload) |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 315 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 316 | return json.loads(update_alarm.text)['alarm_id'] |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 317 | |
| 318 | def check_payload(self, values, metric_name, resource_id, |
| 319 | alarm_name, alarm_state=None): |
| 320 | """Check that the payload is configuration for update/create alarm.""" |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 321 | cfg = Config.instance() |
| 322 | # Check state and severity |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 323 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 324 | severity = 'critical' |
| 325 | if 'severity' in values: |
| 326 | severity = values['severity'].lower() |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 327 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 328 | if severity == "indeterminate": |
| 329 | alarm_state = "insufficient data" |
| 330 | if alarm_state is None: |
| 331 | alarm_state = "ok" |
| Helena McGough | c276319 | 2017-09-07 13:14:30 +0000 | [diff] [blame] | 332 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 333 | statistic = values['statistic'].lower() |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 334 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 335 | granularity = cfg.OS_DEFAULT_GRANULARITY |
| 336 | if 'granularity' in values: |
| 337 | granularity = values['granularity'] |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 338 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 339 | resource_type = 'generic' |
| 340 | if 'resource_type' in values: |
| 341 | resource_type = values['resource_type'].lower() |
| Benjamin Diaz | 0e57d11 | 2018-03-25 14:43:52 -0300 | [diff] [blame] | 342 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 343 | # Try to configure the payload for the update/create request |
| 344 | # Can only update: threshold, operation, statistic and |
| 345 | # the severity of the alarm |
| 346 | rule = {'threshold': values['threshold_value'], |
| 347 | 'comparison_operator': values['operation'].lower(), |
| 348 | 'metric': METRIC_MAPPINGS[metric_name], |
| 349 | 'resource_id': resource_id, |
| 350 | 'resource_type': resource_type, |
| 351 | 'aggregation_method': STATISTICS[statistic], |
| 352 | 'granularity': granularity, } |
| 353 | payload = json.dumps({'state': alarm_state, |
| 354 | 'name': alarm_name, |
| 355 | 'severity': SEVERITIES[severity], |
| 356 | 'type': 'gnocchi_resources_threshold', |
| 357 | 'gnocchi_resources_threshold_rule': rule, |
| 358 | 'alarm_actions': [cfg.OS_NOTIFIER_URI], }, sort_keys=True) |
| 359 | return payload |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 360 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 361 | def get_alarm_state(self, endpoint, auth_token, alarm_id): |
| 362 | """Get the state of the alarm.""" |
| 363 | url = "{}/v2/alarms/%s/state".format(endpoint) % alarm_id |
| 364 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 365 | alarm_state = Common.perform_request( |
| 366 | url, auth_token, req_type="get") |
| 367 | return json.loads(alarm_state.text) |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 368 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 369 | def check_for_metric(self, auth_token, metric_endpoint, metric_name, resource_id): |
| 370 | """ |
| 371 | Checks if resource has a specific metric. If not, throws exception. |
| 372 | :param auth_token: OpenStack auth token |
| 373 | :param metric_endpoint: OpenStack metric endpoint |
| 374 | :param metric_name: Metric name |
| 375 | :param resource_id: Resource UUID |
| 376 | :return: Metric details from resource |
| 377 | :raise Exception: Could not retrieve metric from resource |
| 378 | """ |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 379 | try: |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 380 | url = "{}/v1/resource/generic/{}".format(metric_endpoint, resource_id) |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 381 | result = Common.perform_request( |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 382 | url, auth_token, req_type="get") |
| gcalvino | a2c06b1 | 2018-05-22 10:00:16 +0200 | [diff] [blame] | 383 | resource = json.loads(result.text) |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 384 | metrics_dict = resource['metrics'] |
| 385 | return metrics_dict[METRIC_MAPPINGS[metric_name]] |
| 386 | except Exception as e: |
| 387 | log.exception("Desired Gnocchi metric not found:", e) |
| 388 | raise e |
| 389 | |
| Benjamin Diaz | 326907a | 2018-06-18 14:21:46 -0300 | [diff] [blame] | 390 | def _generate_and_send_response(self, key, correlation_id, **kwargs): |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 391 | try: |
| 392 | resp_message = self._response.generate_response( |
| Benjamin Diaz | 326907a | 2018-06-18 14:21:46 -0300 | [diff] [blame] | 393 | key, cor_id=correlation_id, **kwargs) |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 394 | log.info("Response Message: %s", resp_message) |
| Benjamin Diaz | 326907a | 2018-06-18 14:21:46 -0300 | [diff] [blame] | 395 | self._producer.publish_alarm_response( |
| 396 | key, resp_message) |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 397 | except Exception as e: |
| 398 | log.exception("Response creation failed:") |
| 399 | raise e |