cea12baa9df700731217555914e71b5ef63e86f1
[osm/MON.git] / osm_mon / plugins / OpenStack / Aodh / alarming.py
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."""
23
24 import json
25 import logging
26
27 import six
28 import yaml
29
30 from osm_mon.core.auth import AuthManager
31 from osm_mon.core.database import DatabaseManager
32 from osm_mon.core.message_bus.producer import KafkaProducer
33 from osm_mon.core.settings import Config
34 from osm_mon.plugins.OpenStack.Gnocchi.metrics import METRIC_MAPPINGS
35 from osm_mon.plugins.OpenStack.common import Common
36 from osm_mon.plugins.OpenStack.response import OpenStack_Response
37
38 log = logging.getLogger(__name__)
39
40 SEVERITIES = {
41 "warning": "low",
42 "minor": "low",
43 "major": "moderate",
44 "critical": "critical",
45 "indeterminate": "critical"}
46
47 STATISTICS = {
48 "average": "mean",
49 "minimum": "min",
50 "maximum": "max",
51 "count": "count",
52 "sum": "sum"}
53
54
55 class Alarming(object):
56 """Carries out alarming requests and responses via Aodh API."""
57
58 def __init__(self):
59 """Create the OpenStack alarming instance."""
60 self._database_manager = DatabaseManager()
61 self._auth_manager = AuthManager()
62
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")
68
69 def alarming(self, message, vim_uuid):
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 """
76 try:
77 values = json.loads(message.value)
78 except ValueError:
79 values = yaml.safe_load(message.value)
80
81 log.info("OpenStack alarm action required.")
82
83 auth_token = Common.get_auth_token(vim_uuid)
84
85 alarm_endpoint = Common.get_endpoint("alarming", vim_uuid)
86 metric_endpoint = Common.get_endpoint("metric", vim_uuid)
87
88 vim_account = self._auth_manager.get_credentials(vim_uuid)
89 vim_config = json.loads(vim_account.config)
90
91 if message.key == "create_alarm_request":
92 alarm_details = values['alarm_create_request']
93 alarm_id = None
94 status = False
95 try:
96 metric_name = alarm_details['metric_name'].lower()
97 resource_id = alarm_details['resource_uuid']
98
99 self.check_for_metric(auth_token, metric_endpoint, metric_name, resource_id)
100
101 alarm_id = self.configure_alarm(
102 alarm_endpoint, auth_token, alarm_details, vim_config)
103
104 log.info("Alarm successfully created")
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(),
111 alarm_details['vnf_member_index'],
112 alarm_details['ns_id'].lower()
113 )
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)
123
124 elif message.key == "list_alarm_request":
125 list_details = values['alarm_list_request']
126 alarm_list = None
127 try:
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)
137
138 elif message.key == "delete_alarm_request":
139 request_details = values['alarm_delete_request']
140 alarm_id = request_details['alarm_uuid']
141 status = False
142 try:
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)
154
155 elif message.key == "acknowledge_alarm":
156 try:
157 alarm_id = values['ack_details']['alarm_uuid']
158
159 self.update_alarm_state(
160 alarm_endpoint, auth_token, alarm_id)
161
162 log.info("Acknowledged the alarm and cleared it.")
163 except Exception as e:
164 log.exception("Error acknowledging alarm")
165 raise e
166
167 elif message.key == "update_alarm_request":
168 # Update alarm configurations
169 alarm_details = values['alarm_update_request']
170 alarm_id = None
171 status = False
172 try:
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:
180 self._generate_and_send_response('update_alarm_response',
181 alarm_details['correlation_id'],
182 status=status,
183 alarm_id=alarm_id)
184
185 else:
186 log.debug("Unknown key, no action will be performed")
187
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']
207
208 def delete_alarm(self, endpoint, auth_token, alarm_id):
209 """Delete alarm function."""
210 url = "{}/v2/alarms/%s".format(endpoint) % alarm_id
211
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))
216
217 def list_alarms(self, endpoint, auth_token, list_details):
218 """Generate the requested list of alarms."""
219 url = "{}/v2/alarms/".format(endpoint)
220 a_list, name_list, sev_list, res_list = [], [], [], []
221
222 # TODO(mcgoughh): for now resource_id is a mandatory field
223 # Check for a resource id
224 try:
225 resource = list_details['resource_uuid']
226 name = list_details['alarm_name'].lower()
227 severity = list_details['severity'].lower()
228 sev = SEVERITIES[severity]
229 except KeyError as e:
230 log.warning("Missing parameter for alarm list request: %s", e)
231 raise e
232
233 # Perform the request to get the desired list
234 try:
235 result = Common.perform_request(
236 url, auth_token, req_type="get")
237
238 if result is not None:
239 # Get list based on resource id
240 for alarm in json.loads(result.text):
241 rule = alarm['gnocchi_resources_threshold_rule']
242 if resource == rule['resource_id']:
243 res_list.append(alarm['alarm_id'])
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']:
251 name_list.append(alarm['alarm_id'])
252 for alarm in json.loads(result.text):
253 if sev == alarm['severity']:
254 sev_list.append(alarm['alarm_id'])
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']:
261 name_list.append(alarm['alarm_id'])
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']:
267 sev_list.append(alarm['alarm_id'])
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
272 else:
273 log.info("There are no alarms!")
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
279
280 except Exception as e:
281 log.exception("Failed to generate alarm list: ")
282 raise e
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."""
286 url = "{}/v2/alarms/%s/state".format(endpoint) % alarm_id
287 payload = json.dumps("ok")
288
289 Common.perform_request(
290 url, auth_token, req_type="put", payload=payload)
291
292 def update_alarm(self, endpoint, auth_token, values, vim_config):
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
297 # Gets current configurations about the alarm
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]
305
306 # Generates and check payload configuration for alarm update
307 if 'granularity' in vim_config and 'granularity' not in values:
308 values['granularity'] = vim_config['granularity']
309 payload = self.check_payload(values, metric_name, resource_id,
310 alarm_name, alarm_state=alarm_state)
311
312 # Updates the alarm configurations with the valid payload
313 update_alarm = Common.perform_request(
314 url, auth_token, req_type="put", payload=payload)
315
316 return json.loads(update_alarm.text)['alarm_id']
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."""
321 cfg = Config.instance()
322 # Check state and severity
323
324 severity = 'critical'
325 if 'severity' in values:
326 severity = values['severity'].lower()
327
328 if severity == "indeterminate":
329 alarm_state = "insufficient data"
330 if alarm_state is None:
331 alarm_state = "ok"
332
333 statistic = values['statistic'].lower()
334
335 granularity = cfg.OS_DEFAULT_GRANULARITY
336 if 'granularity' in values:
337 granularity = values['granularity']
338
339 resource_type = 'generic'
340 if 'resource_type' in values:
341 resource_type = values['resource_type'].lower()
342
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
360
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
365 alarm_state = Common.perform_request(
366 url, auth_token, req_type="get")
367 return json.loads(alarm_state.text)
368
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 """
379 try:
380 url = "{}/v1/resource/generic/{}".format(metric_endpoint, resource_id)
381 result = Common.perform_request(
382 url, auth_token, req_type="get")
383 resource = json.loads(result.text)
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
390 def _generate_and_send_response(self, key, correlation_id, **kwargs):
391 try:
392 resp_message = self._response.generate_response(
393 key, cor_id=correlation_id, **kwargs)
394 log.info("Response Message: %s", resp_message)
395 self._producer.publish_alarm_response(
396 key, resp_message)
397 except Exception as e:
398 log.exception("Response creation failed:")
399 raise e