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."""
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
38 log
= logging
.getLogger(__name__
)
44 "critical": "critical",
45 "indeterminate": "critical"}
55 class Alarming(object):
56 """Carries out alarming requests and responses via Aodh API."""
59 """Create the OpenStack alarming instance."""
60 # Initialize configuration and notifications
61 config
= Config
.instance()
64 self
._database
_manager
= DatabaseManager()
65 self
._auth
_manager
= AuthManager()
67 # Use the Response class to generate valid json response messages
68 self
._response
= OpenStack_Response()
70 # Initializer a producer to send responses back to SO
71 self
._producer
= KafkaProducer("alarm_response")
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
)
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']
82 if metric_name
not in METRIC_MAPPINGS
.keys():
83 log
.warning("This metric is not supported.")
86 # Check for the required metric
87 metric_id
= self
.check_for_metric(auth_token
, metric_endpoint
, metric_name
, resource_id
)
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
,
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
100 log
.warning("The required Gnocchi metric does not exist.")
103 except Exception as exc
:
104 log
.warning("Failed to create the alarm: %s", exc
)
107 def alarming(self
, message
, vim_uuid
):
108 """Consume info from the message bus to manage alarms."""
110 values
= json
.loads(message
.value
)
112 values
= yaml
.safe_load(message
.value
)
114 log
.info("OpenStack alarm action required.")
116 auth_token
= Common
.get_auth_token(vim_uuid
)
118 alarm_endpoint
= Common
.get_endpoint("alarming", vim_uuid
)
119 metric_endpoint
= Common
.get_endpoint("metric", vim_uuid
)
121 vim_account
= self
._auth
_manager
.get_credentials(vim_uuid
)
122 vim_config
= json
.loads(vim_account
.config
)
124 if message
.key
== "create_alarm_request":
125 # Configure/Update an alarm
126 alarm_details
= values
['alarm_create_request']
128 alarm_id
, alarm_status
= self
.configure_alarm(
129 alarm_endpoint
, metric_endpoint
, auth_token
, alarm_details
, vim_config
)
131 # Generate a valid response message, send via producer
132 if alarm_status
is True:
133 log
.info("Alarm successfully created")
134 self
._database
_manager
.save_alarm(alarm_id
,
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()
144 resp_message
= self
._response
.generate_response(
145 'create_alarm_response', status
=alarm_status
,
147 cor_id
=alarm_details
['correlation_id'])
148 log
.info("Response Message: %s", resp_message
)
149 self
._producer
.create_alarm_response(
150 'create_alarm_response', resp_message
)
152 log
.exception("Response creation failed:")
154 elif message
.key
== "list_alarm_request":
155 # Check for a specified: alarm_name, resource_uuid, severity
156 # and generate the appropriate list
157 list_details
= values
['alarm_list_request']
159 alarm_list
= self
.list_alarms(
160 alarm_endpoint
, auth_token
, list_details
)
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(
169 'list_alarm_response', resp_message
)
171 log
.exception("Failed to send a valid response back.")
173 elif message
.key
== "delete_alarm_request":
174 request_details
= values
['alarm_delete_request']
175 alarm_id
= request_details
['alarm_uuid']
177 resp_status
= self
.delete_alarm(
178 alarm_endpoint
, auth_token
, alarm_id
)
180 # Generate and send a response message
182 resp_message
= self
._response
.generate_response(
183 'delete_alarm_response', alarm_id
=alarm_id
,
185 cor_id
=request_details
['correlation_id'])
186 log
.info("Response message: %s", resp_message
)
187 self
._producer
.delete_alarm_response(
188 'delete_alarm_response', resp_message
)
190 log
.exception("Failed to create delete response: ")
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']
196 response
= self
.update_alarm_state(
197 alarm_endpoint
, auth_token
, alarm_id
)
199 # Log if an alarm was reset
201 log
.info("Acknowledged the alarm and cleared it.")
203 log
.warning("Failed to acknowledge/clear the alarm.")
205 elif message
.key
== "update_alarm_request":
206 # Update alarm configurations
207 alarm_details
= values
['alarm_update_request']
209 alarm_id
, status
= self
.update_alarm(
210 alarm_endpoint
, auth_token
, alarm_details
, vim_config
)
212 # Generate a response for an update request
214 resp_message
= self
._response
.generate_response(
215 'update_alarm_response', alarm_id
=alarm_id
,
216 cor_id
=alarm_details
['correlation_id'],
218 log
.info("Response message: %s", resp_message
)
219 self
._producer
.update_alarm_response(
220 'update_alarm_response', resp_message
)
222 log
.exception("Failed to send an update response: ")
225 log
.debug("Unknown key, no action will be performed")
229 def delete_alarm(self
, endpoint
, auth_token
, alarm_id
):
230 """Delete alarm function."""
231 url
= "{}/v2/alarms/%s".format(endpoint
) % alarm_id
234 result
= Common
.perform_request(
235 url
, auth_token
, req_type
="delete")
236 if str(result
.status_code
) == "404":
237 log
.info("Alarm doesn't exist: %s", result
.status_code
)
238 # If status code is 404 alarm did not exist
244 log
.exception("Failed to delete alarm %s :", alarm_id
)
247 def list_alarms(self
, endpoint
, auth_token
, list_details
):
248 """Generate the requested list of alarms."""
249 url
= "{}/v2/alarms/".format(endpoint
)
250 a_list
, name_list
, sev_list
, res_list
= [], [], [], []
252 # TODO(mcgoughh): for now resource_id is a mandatory field
253 # Check for a resource id
255 resource
= list_details
['resource_uuid']
256 except KeyError as exc
:
257 log
.warning("Resource id not specified for list request: %s", exc
)
260 # Checking what fields are specified for a list request
262 name
= list_details
['alarm_name'].lower()
263 except KeyError as exc
:
264 log
.info("Alarm name isn't specified.")
268 severity
= list_details
['severity'].lower()
269 sev
= SEVERITIES
[severity
]
270 except KeyError as exc
:
271 log
.info("Severity is unspecified/incorrectly configured")
274 # Perform the request to get the desired list
276 result
= Common
.perform_request(
277 url
, auth_token
, req_type
="get")
279 if result
is not None:
280 # Get list based on resource id
281 for alarm
in json
.loads(result
.text
):
282 rule
= alarm
['gnocchi_resources_threshold_rule']
283 if resource
== rule
['resource_id']:
284 res_list
.append(alarm
)
286 log
.info("No alarms for this resource")
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.",
293 for alarm
in json
.loads(result
.text
):
294 if name
== alarm
['name']:
295 name_list
.append(alarm
)
296 for alarm
in json
.loads(result
.text
):
297 if sev
== alarm
['severity']:
298 sev_list
.append(alarm
)
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']:
305 name_list
.append(alarm
)
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']:
311 sev_list
.append(alarm
)
312 a_list
= list(set(sev_list
).intersection(res_list
))
314 log
.info("Returning an entire list of alarms.")
317 log
.info("There are no alarms!")
319 except Exception as exc
:
320 log
.info("Failed to generate required list: %s", exc
)
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."""
327 url
= "{}/v2/alarms/%s/state".format(endpoint
) % alarm_id
328 payload
= json
.dumps("ok")
331 Common
.perform_request(
332 url
, auth_token
, req_type
="put", payload
=payload
)
335 log
.exception("Unable to update alarm state: ")
338 def update_alarm(self
, endpoint
, auth_token
, values
, vim_config
):
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']
343 # Gets current configurations about the alarm
345 result
= Common
.perform_request(
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']
351 metric_name
= [key
for key
, value
in six
.iteritems(METRIC_MAPPINGS
) if value
== rule
['metric']][0]
352 except Exception as exc
:
353 log
.exception("Failed to retrieve existing alarm info. Can only update OSM alarms.")
356 # Generates and check payload configuration for alarm update
357 if 'granularity' in vim_config
and 'granularity' not in values
:
358 values
['granularity'] = vim_config
['granularity']
359 payload
= self
.check_payload(values
, metric_name
, resource_id
,
360 alarm_name
, alarm_state
=alarm_state
)
362 # Updates the alarm configurations with the valid payload
363 if payload
is not None:
365 update_alarm
= Common
.perform_request(
366 url
, auth_token
, req_type
="put", payload
=payload
)
368 return json
.loads(update_alarm
.text
)['alarm_id'], True
369 except Exception as exc
:
370 log
.exception("Alarm update could not be performed: ")
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."""
377 cfg
= Config
.instance()
378 # Check state and severity
380 severity
= 'critical'
381 if 'severity' in values
:
382 severity
= values
['severity'].lower()
384 if severity
== "indeterminate":
385 alarm_state
= "insufficient data"
386 if alarm_state
is None:
389 statistic
= values
['statistic'].lower()
391 granularity
= cfg
.OS_DEFAULT_GRANULARITY
392 if 'granularity' in values
:
393 granularity
= values
['granularity']
395 resource_type
= 'generic'
396 if 'resource_type' in values
:
397 resource_type
= values
['resource_type'].lower()
399 # Try to configure the payload for the update/create request
400 # Can only update: threshold, operation, statistic and
401 # the severity of the alarm
402 rule
= {'threshold': values
['threshold_value'],
403 'comparison_operator': values
['operation'].lower(),
404 'metric': METRIC_MAPPINGS
[metric_name
],
405 'resource_id': resource_id
,
406 'resource_type': resource_type
,
407 'aggregation_method': STATISTICS
[statistic
],
408 'granularity': granularity
, }
409 payload
= json
.dumps({'state': alarm_state
,
411 'severity': SEVERITIES
[severity
],
412 'type': 'gnocchi_resources_threshold',
413 'gnocchi_resources_threshold_rule': rule
,
414 'alarm_actions': [cfg
.OS_NOTIFIER_URI
], })
416 except KeyError as exc
:
417 log
.warning("Alarm is not configured correctly: %s", exc
)
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
425 alarm_state
= Common
.perform_request(
426 url
, auth_token
, req_type
="get")
427 return json
.loads(alarm_state
.text
)
428 except Exception as exc
:
429 log
.warning("Failed to get the state of the alarm:%s", exc
)
432 def check_for_metric(self
, auth_token
, metric_endpoint
, m_name
, r_id
):
433 """Check for the alarm metric."""
435 url
= "{}/v1/metric?sort=name:asc".format(metric_endpoint
)
436 result
= Common
.perform_request(
437 url
, auth_token
, req_type
="get")
439 metrics_partial
= json
.loads(result
.text
)
440 for metric
in metrics_partial
:
441 metric_list
.append(metric
)
443 while len(json
.loads(result
.text
)) > 0:
444 last_metric_id
= metrics_partial
[-1]['id']
445 url
= "{}/v1/metric?sort=name:asc&marker={}".format(metric_endpoint
, last_metric_id
)
446 result
= Common
.perform_request(
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
)
453 for metric
in metric_list
:
454 name
= metric
['name']
455 resource
= metric
['resource_id']
456 if name
== METRIC_MAPPINGS
[m_name
] and resource
== r_id
:
457 metric_id
= metric
['id']
458 log
.info("The required metric exists, an alarm will be created.")
460 except Exception as exc
:
461 log
.info("Desired Gnocchi metric not found:%s", exc
)