7e6347f8df99417733a4f8432cb7fa01014d449c
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
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
22 """Carry out alarming requests via Aodh API."""
26 from io
import UnsupportedOperation
30 from osm_mon
.core
.auth
import AuthManager
31 from osm_mon
.core
.database
import DatabaseManager
32 from osm_mon
.core
.settings
import Config
33 from osm_mon
.plugins
.OpenStack
.Gnocchi
.metric_handler
import METRIC_MAPPINGS
34 from osm_mon
.plugins
.OpenStack
.common
import Common
35 from osm_mon
.plugins
.OpenStack
.response
import OpenStackResponseBuilder
37 log
= logging
.getLogger(__name__
)
43 "critical": "critical",
44 "indeterminate": "critical"}
54 class OpenstackAlarmHandler(object):
55 """Carries out alarming requests and responses via Aodh API."""
58 """Create the OpenStack alarming instance."""
59 self
._database
_manager
= DatabaseManager()
60 self
._auth
_manager
= AuthManager()
61 self
._cfg
= Config
.instance()
63 # Use the Response class to generate valid json response messages
64 self
._response
= OpenStackResponseBuilder()
66 def handle_message(self
, key
: str, values
: dict, vim_uuid
: str):
68 Processes alarm request message depending on it's key
69 :param key: Kafka message key
70 :param values: Dict containing alarm request data. Follows models defined in core.models.
71 :param vim_uuid: UUID of the VIM to handle the alarm request.
72 :return: Dict containing alarm response data. Follows models defined in core.models.
75 log
.info("OpenStack alarm action required.")
77 verify_ssl
= self
._auth
_manager
.is_verify_ssl(vim_uuid
)
79 auth_token
= Common
.get_auth_token(vim_uuid
, verify_ssl
=verify_ssl
)
81 alarm_endpoint
= Common
.get_endpoint("alarming", vim_uuid
, verify_ssl
=verify_ssl
)
82 metric_endpoint
= Common
.get_endpoint("metric", vim_uuid
, verify_ssl
=verify_ssl
)
84 vim_account
= self
._auth
_manager
.get_credentials(vim_uuid
)
85 vim_config
= json
.loads(vim_account
.config
)
87 if key
== "create_alarm_request":
88 alarm_details
= values
['alarm_create_request']
92 metric_name
= alarm_details
['metric_name'].lower()
93 resource_id
= alarm_details
['resource_uuid']
95 self
.check_for_metric(auth_token
, metric_endpoint
, metric_name
, resource_id
, verify_ssl
)
97 alarm_id
= self
.configure_alarm(
98 alarm_endpoint
, auth_token
, alarm_details
, vim_config
, verify_ssl
)
100 log
.info("Alarm successfully created")
101 self
._database
_manager
.save_alarm(alarm_id
,
103 alarm_details
['threshold_value'],
104 alarm_details
['operation'].lower(),
105 alarm_details
['metric_name'].lower(),
106 alarm_details
['vdu_name'].lower(),
107 alarm_details
['vnf_member_index'],
108 alarm_details
['ns_id'].lower()
111 except Exception as e
:
112 log
.exception("Error creating alarm")
115 return self
._response
.generate_response('create_alarm_response',
116 cor_id
=alarm_details
['correlation_id'],
120 elif key
== "list_alarm_request":
121 list_details
= values
['alarm_list_request']
124 alarm_list
= self
.list_alarms(
125 alarm_endpoint
, auth_token
, list_details
, verify_ssl
)
126 except Exception as e
:
127 log
.exception("Error listing alarms")
130 return self
._response
.generate_response('list_alarm_response',
131 cor_id
=list_details
['correlation_id'],
132 alarm_list
=alarm_list
)
134 elif key
== "delete_alarm_request":
135 request_details
= values
['alarm_delete_request']
136 alarm_id
= request_details
['alarm_uuid']
140 alarm_endpoint
, auth_token
, alarm_id
, verify_ssl
)
142 except Exception as e
:
143 log
.exception("Error deleting alarm")
146 return self
._response
.generate_response('delete_alarm_response',
147 cor_id
=request_details
['correlation_id'],
151 elif key
== "acknowledge_alarm_request":
153 alarm_id
= values
['ack_details']['alarm_uuid']
155 self
.update_alarm_state(
156 alarm_endpoint
, auth_token
, alarm_id
, verify_ssl
)
158 log
.info("Acknowledged the alarm and cleared it.")
159 except Exception as e
:
160 log
.exception("Error acknowledging alarm")
165 elif key
== "update_alarm_request":
166 # Update alarm configurations
167 alarm_details
= values
['alarm_update_request']
171 alarm_id
= self
.update_alarm(
172 alarm_endpoint
, auth_token
, alarm_details
, vim_config
, verify_ssl
)
174 except Exception as e
:
175 log
.exception("Error updating alarm")
178 return self
._response
.generate_response('update_alarm_response',
179 cor_id
=alarm_details
['correlation_id'],
184 raise UnsupportedOperation("Unknown key {}, no action will be performed.".format(key
))
186 def configure_alarm(self
, alarm_endpoint
, auth_token
, values
, vim_config
, verify_ssl
):
187 """Create requested alarm in Aodh."""
188 url
= "{}/v2/alarms/".format(alarm_endpoint
)
190 # Check if the desired alarm is supported
191 alarm_name
= values
['alarm_name'].lower()
192 metric_name
= values
['metric_name'].lower()
193 resource_id
= values
['resource_uuid']
195 if metric_name
not in METRIC_MAPPINGS
.keys():
196 raise KeyError("Metric {} is not supported.".format(metric_name
))
198 if 'granularity' in vim_config
and 'granularity' not in values
:
199 values
['granularity'] = vim_config
['granularity']
200 payload
= self
.check_payload(values
, metric_name
, resource_id
,
202 new_alarm
= Common
.perform_request(
203 url
, auth_token
, req_type
="post", payload
=payload
, verify_ssl
=verify_ssl
)
204 return json
.loads(new_alarm
.text
)['alarm_id']
206 def delete_alarm(self
, endpoint
, auth_token
, alarm_id
, verify_ssl
):
207 """Delete alarm function."""
208 url
= "{}/v2/alarms/%s".format(endpoint
) % alarm_id
210 result
= Common
.perform_request(
211 url
, auth_token
, req_type
="delete", verify_ssl
=verify_ssl
)
212 if str(result
.status_code
) == "404":
213 raise ValueError("Alarm {} doesn't exist".format(alarm_id
))
215 def list_alarms(self
, endpoint
, auth_token
, list_details
, verify_ssl
):
216 """Generate the requested list of alarms."""
217 url
= "{}/v2/alarms/".format(endpoint
)
218 a_list
, name_list
, sev_list
, res_list
= [], [], [], []
220 # TODO(mcgoughh): for now resource_id is a mandatory field
221 # Check for a resource id
223 resource
= list_details
['resource_uuid']
224 name
= list_details
['alarm_name'].lower()
225 severity
= list_details
['severity'].lower()
226 sev
= SEVERITIES
[severity
]
227 except KeyError as e
:
228 log
.warning("Missing parameter for alarm list request: %s", e
)
231 # Perform the request to get the desired list
233 result
= Common
.perform_request(
234 url
, auth_token
, req_type
="get", verify_ssl
=verify_ssl
)
236 if result
is not None:
237 # Get list based on resource id
238 for alarm
in json
.loads(result
.text
):
239 rule
= alarm
['gnocchi_resources_threshold_rule']
240 if resource
== rule
['resource_id']:
241 res_list
.append(alarm
['alarm_id'])
243 # Generate specified listed if requested
244 if name
is not None and sev
is not None:
245 log
.info("Return a list of %s alarms with %s severity.",
247 for alarm
in json
.loads(result
.text
):
248 if name
== alarm
['name']:
249 name_list
.append(alarm
['alarm_id'])
250 for alarm
in json
.loads(result
.text
):
251 if sev
== alarm
['severity']:
252 sev_list
.append(alarm
['alarm_id'])
253 name_sev_list
= list(set(name_list
).intersection(sev_list
))
254 a_list
= list(set(name_sev_list
).intersection(res_list
))
255 elif name
is not None:
256 log
.info("Returning a %s list of alarms.", name
)
257 for alarm
in json
.loads(result
.text
):
258 if name
== alarm
['name']:
259 name_list
.append(alarm
['alarm_id'])
260 a_list
= list(set(name_list
).intersection(res_list
))
261 elif sev
is not None:
262 log
.info("Returning %s severity alarm list.", sev
)
263 for alarm
in json
.loads(result
.text
):
264 if sev
== alarm
['severity']:
265 sev_list
.append(alarm
['alarm_id'])
266 a_list
= list(set(sev_list
).intersection(res_list
))
268 log
.info("Returning an entire list of alarms.")
271 log
.info("There are no alarms!")
273 for alarm
in json
.loads(result
.text
):
274 if alarm
['alarm_id'] in a_list
:
275 response_list
.append(alarm
)
278 except Exception as e
:
279 log
.exception("Failed to generate alarm list: ")
282 def update_alarm_state(self
, endpoint
, auth_token
, alarm_id
, verify_ssl
):
283 """Set the state of an alarm to ok when ack message is received."""
284 url
= "{}/v2/alarms/%s/state".format(endpoint
) % alarm_id
285 payload
= json
.dumps("ok")
287 result
= Common
.perform_request(
288 url
, auth_token
, req_type
="put", payload
=payload
, verify_ssl
=verify_ssl
)
290 return json
.loads(result
.text
)
292 def update_alarm(self
, endpoint
, auth_token
, values
, vim_config
, verify_ssl
):
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']
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]
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
)
312 # Updates the alarm configurations with the valid payload
313 update_alarm
= Common
.perform_request(
314 url
, auth_token
, req_type
="put", payload
=payload
, verify_ssl
=verify_ssl
)
316 return json
.loads(update_alarm
.text
)['alarm_id']
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
324 severity
= 'critical'
325 if 'severity' in values
:
326 severity
= values
['severity'].lower()
328 if severity
== "indeterminate":
329 alarm_state
= "insufficient data"
330 if alarm_state
is None:
333 statistic
= values
['statistic'].lower()
335 granularity
= cfg
.OS_DEFAULT_GRANULARITY
336 if 'granularity' in values
:
337 granularity
= values
['granularity']
339 resource_type
= 'generic'
340 if 'resource_type' in values
:
341 resource_type
= values
['resource_type'].lower()
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
,
355 'severity': SEVERITIES
[severity
],
356 'type': 'gnocchi_resources_threshold',
357 'gnocchi_resources_threshold_rule': rule
,
358 'alarm_actions': [cfg
.OS_NOTIFIER_URI
],
359 'repeat_actions': True}, sort_keys
=True)
362 def get_alarm_state(self
, endpoint
, auth_token
, alarm_id
):
363 """Get the state of the alarm."""
364 url
= "{}/v2/alarms/%s/state".format(endpoint
) % alarm_id
366 alarm_state
= Common
.perform_request(
367 url
, auth_token
, req_type
="get")
368 return json
.loads(alarm_state
.text
)
370 def check_for_metric(self
, auth_token
, metric_endpoint
, metric_name
, resource_id
, verify_ssl
):
372 Checks if resource has a specific metric. If not, throws exception.
373 :param verify_ssl: Boolean flag to set SSL cert validation
374 :param auth_token: OpenStack auth token
375 :param metric_endpoint: OpenStack metric endpoint
376 :param metric_name: Metric name
377 :param resource_id: Resource UUID
378 :return: Metric details from resource
379 :raise Exception: Could not retrieve metric from resource
382 url
= "{}/v1/resource/generic/{}".format(metric_endpoint
, resource_id
)
383 result
= Common
.perform_request(
384 url
, auth_token
, req_type
="get", verify_ssl
=verify_ssl
)
385 resource
= json
.loads(result
.text
)
386 metrics_dict
= resource
['metrics']
387 return metrics_dict
[METRIC_MAPPINGS
[metric_name
]]
388 except Exception as e
:
389 log
.exception("Desired Gnocchi metric not found:", e
)