blob: b7978fb47b1107c6ee099142265fb23b3d98fc2a [file] [log] [blame]
Helena McGoughcda5f2f2017-09-12 08:30:02 +01001# 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 McGoughf358b4f2017-08-23 10:11:42 +000023
24import json
Helena McGoughfe92f842017-11-17 14:57:08 +000025
Helena McGough4bebd122017-09-26 09:42:47 +010026import logging
Helena McGoughc85d9842017-08-29 09:52:56 +000027
Helena McGougheffeb7c2017-11-23 15:54:08 +000028from osm_mon.core.message_bus.producer import KafkaProducer
Helena McGoughc85d9842017-08-29 09:52:56 +000029
Helena McGougheffeb7c2017-11-23 15:54:08 +000030from osm_mon.plugins.OpenStack.response import OpenStack_Response
31from osm_mon.plugins.OpenStack.settings import Config
Helena McGoughcda5f2f2017-09-12 08:30:02 +010032
Helena McGoughfe92f842017-11-17 14:57:08 +000033log = logging.getLogger(__name__)
34
Helena McGoughd00ff822017-09-20 17:42:22 +010035ALARM_NAMES = {
36 "average_memory_usage_above_threshold": "average_memory_utilization",
37 "disk_read_ops": "disk_read_ops",
38 "disk_write_ops": "disk_write_ops",
39 "disk_read_bytes": "disk_read_bytes",
40 "disk_write_bytes": "disk_write_bytes",
41 "net_packets_dropped": "packets_dropped",
42 "packets_in_above_threshold": "packets_received",
43 "packets_out_above_threshold": "packets_sent",
44 "cpu_utilization_above_threshold": "cpu_utilization"}
Helena McGoughc2763192017-09-07 13:14:30 +000045
46SEVERITIES = {
Helena McGoughd00ff822017-09-20 17:42:22 +010047 "warning": "low",
48 "minor": "low",
49 "major": "moderate",
50 "critical": "critical",
51 "indeterminate": "critical"}
Helena McGoughf358b4f2017-08-23 10:11:42 +000052
Helena McGough0e7809a2017-09-18 11:31:05 +010053STATISTICS = {
Helena McGoughd00ff822017-09-20 17:42:22 +010054 "average": "avg",
55 "minimum": "min",
56 "maximum": "max",
57 "count": "count",
58 "sum": "sum"}
Helena McGough0e7809a2017-09-18 11:31:05 +010059
Helena McGoughf358b4f2017-08-23 10:11:42 +000060
61class Alarming(object):
Helena McGoughcda5f2f2017-09-12 08:30:02 +010062 """Carries out alarming requests and responses via Aodh API."""
Helena McGoughf358b4f2017-08-23 10:11:42 +000063
64 def __init__(self):
Helena McGoughcda5f2f2017-09-12 08:30:02 +010065 """Create the OpenStack alarming instance."""
Helena McGoughfe92f842017-11-17 14:57:08 +000066 # Initialize configuration and notifications
67 config = Config.instance()
68 config.read_environ("aodh")
Helena McGoughf358b4f2017-08-23 10:11:42 +000069
Helena McGoughfe92f842017-11-17 14:57:08 +000070 # Initialise authentication for API requests
71 self.auth_token = None
72 self.endpoint = None
73 self.common = None
Helena McGoughc85d9842017-08-29 09:52:56 +000074
Helena McGoughcda5f2f2017-09-12 08:30:02 +010075 # Use the Response class to generate valid json response messages
76 self._response = OpenStack_Response()
77
78 # Initializer a producer to send responses back to SO
79 self._producer = KafkaProducer("alarm_response")
Helena McGoughc85d9842017-08-29 09:52:56 +000080
Helena McGoughfe92f842017-11-17 14:57:08 +000081 def alarming(self, message, common, auth_token):
Helena McGoughc85d9842017-08-29 09:52:56 +000082 """Consume info from the message bus to manage alarms."""
Helena McGoughfe92f842017-11-17 14:57:08 +000083 values = json.loads(message.value)
84 self.common = common
Helena McGoughc2763192017-09-07 13:14:30 +000085
Helena McGoughfe92f842017-11-17 14:57:08 +000086 log.info("OpenStack alarm action required.")
Helena McGoughc2763192017-09-07 13:14:30 +000087
Helena McGoughfe92f842017-11-17 14:57:08 +000088 # Generate and auth_token and endpoint for request
89 if auth_token is not None:
90 if self.auth_token != auth_token:
91 log.info("Auth_token for alarming set by access_credentials.")
92 self.auth_token = auth_token
Helena McGoughc85d9842017-08-29 09:52:56 +000093 else:
Helena McGoughfe92f842017-11-17 14:57:08 +000094 log.info("Auth_token has not been updated.")
95 else:
96 log.info("Using environment variables to set auth_token for Aodh.")
97 self.auth_token = self.common._authenticate()
98
99 if self.endpoint is None:
100 log.info("Generating a new endpoint for Aodh.")
101 self.endpoint = self.common.get_endpoint("alarming")
102
103 if message.key == "create_alarm_request":
104 # Configure/Update an alarm
105 alarm_details = values['alarm_create_request']
106
107 alarm_id, alarm_status = self.configure_alarm(
108 self.endpoint, self.auth_token, alarm_details)
109
110 # Generate a valid response message, send via producer
111 try:
112 if alarm_status is True:
113 log.info("Alarm successfully created")
114
115 resp_message = self._response.generate_response(
116 'create_alarm_response', status=alarm_status,
117 alarm_id=alarm_id,
118 cor_id=alarm_details['correlation_id'])
119 log.info("Response Message: %s", resp_message)
120 self._producer.create_alarm_response(
Helena McGough94f93f72017-11-23 17:29:54 +0000121 'create_alarm_response', resp_message,
Helena McGoughfe92f842017-11-17 14:57:08 +0000122 'alarm_response')
123 except Exception as exc:
124 log.warn("Response creation failed: %s", exc)
125
126 elif message.key == "list_alarm_request":
127 # Check for a specifed: alarm_name, resource_uuid, severity
128 # and generate the appropriate list
129 list_details = values['alarm_list_request']
130
131 alarm_list = self.list_alarms(
132 self.endpoint, self.auth_token, list_details)
133
134 try:
135 # Generate and send a list response back
136 resp_message = self._response.generate_response(
137 'list_alarm_response', alarm_list=alarm_list,
138 cor_id=list_details['correlation_id'])
139 log.info("Response Message: %s", resp_message)
140 self._producer.list_alarm_response(
141 'list_alarm_response', resp_message,
142 'alarm_response')
143 except Exception as exc:
144 log.warn("Failed to send a valid response back.")
145
146 elif message.key == "delete_alarm_request":
147 request_details = values['alarm_delete_request']
148 alarm_id = request_details['alarm_uuid']
149
150 resp_status = self.delete_alarm(
151 self.endpoint, self.auth_token, alarm_id)
152
153 # Generate and send a response message
154 try:
155 resp_message = self._response.generate_response(
156 'delete_alarm_response', alarm_id=alarm_id,
157 status=resp_status,
158 cor_id=request_details['correlation_id'])
159 log.info("Response message: %s", resp_message)
160 self._producer.delete_alarm_response(
161 'delete_alarm_response', resp_message,
162 'alarm_response')
163 except Exception as exc:
164 log.warn("Failed to create delete reponse:%s", exc)
165
166 elif message.key == "acknowledge_alarm":
167 # Acknowledge that an alarm has been dealt with by the SO
168 alarm_id = values['ack_details']['alarm_uuid']
169
170 response = self.update_alarm_state(
171 self.endpoint, self.auth_token, alarm_id)
172
173 # Log if an alarm was reset
174 if response is True:
175 log.info("Acknowledged the alarm and cleared it.")
176 else:
177 log.warn("Failed to acknowledge/clear the alarm.")
178
179 elif message.key == "update_alarm_request":
180 # Update alarm configurations
181 alarm_details = values['alarm_update_request']
182
183 alarm_id, status = self.update_alarm(
184 self.endpoint, self.auth_token, alarm_details)
185
186 # Generate a response for an update request
187 try:
188 resp_message = self._response.generate_response(
189 'update_alarm_response', alarm_id=alarm_id,
190 cor_id=alarm_details['correlation_id'],
191 status=status)
192 log.info("Response message: %s", resp_message)
193 self._producer.update_alarm_response(
194 'update_alarm_response', resp_message,
195 'alarm_response')
196 except Exception as exc:
197 log.warn("Failed to send an update response:%s", exc)
198
199 else:
200 log.debug("Unknown key, no action will be performed")
Helena McGoughc85d9842017-08-29 09:52:56 +0000201
Helena McGoughf358b4f2017-08-23 10:11:42 +0000202 return
203
Helena McGoughc85d9842017-08-29 09:52:56 +0000204 def configure_alarm(self, endpoint, auth_token, values):
Helena McGoughc2763192017-09-07 13:14:30 +0000205 """Create requested alarm in Aodh."""
206 url = "{}/v2/alarms/".format(endpoint)
Helena McGoughc85d9842017-08-29 09:52:56 +0000207
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100208 # Check if the desired alarm is supported
Helena McGoughd00ff822017-09-20 17:42:22 +0100209 alarm_name = values['alarm_name'].lower()
210 metric_name = values['metric_name'].lower()
211 resource_id = values['resource_uuid']
212
213 if alarm_name not in ALARM_NAMES.keys():
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100214 log.warn("This alarm is not supported, by a valid metric.")
215 return None, False
Helena McGoughd00ff822017-09-20 17:42:22 +0100216 if ALARM_NAMES[alarm_name] != metric_name:
217 log.warn("This is not the correct metric for this alarm.")
218 return None, False
219
220 # Check for the required metric
221 metric_id = self.check_for_metric(auth_token, metric_name, resource_id)
Helena McGoughc85d9842017-08-29 09:52:56 +0000222
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100223 try:
Helena McGoughd00ff822017-09-20 17:42:22 +0100224 if metric_id is not None:
225 # Create the alarm if metric is available
226 payload = self.check_payload(values, metric_name, resource_id,
227 alarm_name)
Helena McGoughfe92f842017-11-17 14:57:08 +0000228 new_alarm = self.common._perform_request(
Helena McGoughd00ff822017-09-20 17:42:22 +0100229 url, auth_token, req_type="post", payload=payload)
230 return json.loads(new_alarm.text)['alarm_id'], True
231 else:
232 log.warn("The required Gnocchi metric does not exist.")
233 return None, False
Helena McGoughc85d9842017-08-29 09:52:56 +0000234
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100235 except Exception as exc:
Helena McGoughd00ff822017-09-20 17:42:22 +0100236 log.warn("Failed to create the alarm: %s", exc)
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100237 return None, False
Helena McGoughf358b4f2017-08-23 10:11:42 +0000238
Helena McGoughc2763192017-09-07 13:14:30 +0000239 def delete_alarm(self, endpoint, auth_token, alarm_id):
240 """Delete alarm function."""
241 url = "{}/v2/alarms/%s".format(endpoint) % (alarm_id)
Helena McGoughf358b4f2017-08-23 10:11:42 +0000242
Helena McGoughc2763192017-09-07 13:14:30 +0000243 try:
Helena McGoughfe92f842017-11-17 14:57:08 +0000244 result = self.common._perform_request(
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100245 url, auth_token, req_type="delete")
246 if str(result.status_code) == "404":
Helena McGough4bebd122017-09-26 09:42:47 +0100247 log.info("Alarm doesn't exist: %s", result.status_code)
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100248 # If status code is 404 alarm did not exist
249 return False
250 else:
251 return True
252
Helena McGoughc2763192017-09-07 13:14:30 +0000253 except Exception as exc:
254 log.warn("Failed to delete alarm: %s because %s.", alarm_id, exc)
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100255 return False
Helena McGoughc2763192017-09-07 13:14:30 +0000256
Helena McGoughf1527682017-09-22 10:55:22 +0100257 def list_alarms(self, endpoint, auth_token, list_details):
Helena McGoughc2763192017-09-07 13:14:30 +0000258 """Generate the requested list of alarms."""
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100259 url = "{}/v2/alarms/".format(endpoint)
Helena McGoughf1527682017-09-22 10:55:22 +0100260 a_list, name_list, sev_list, res_list = [], [], [], []
Helena McGoughc2763192017-09-07 13:14:30 +0000261
Helena McGoughf1527682017-09-22 10:55:22 +0100262 # TODO(mcgoughh): for now resource_id is a mandatory field
Helena McGoughfe92f842017-11-17 14:57:08 +0000263 # Check for a reqource is
264 try:
265 resource = list_details['resource_uuid']
266 except KeyError as exc:
267 log.warn("Resource id not specified for list request: %s", exc)
268 return None
Helena McGoughf1527682017-09-22 10:55:22 +0100269
270 # Checking what fields are specified for a list request
271 try:
272 name = list_details['alarm_name'].lower()
273 if name not in ALARM_NAMES.keys():
274 log.warn("This alarm is not supported, won't be used!")
275 name = None
276 except KeyError as exc:
277 log.info("Alarm name isn't specified.")
278 name = None
279
280 try:
281 severity = list_details['severity'].lower()
282 sev = SEVERITIES[severity]
283 except KeyError as exc:
284 log.info("Severity is unspecified/incorrectly configured")
285 sev = None
286
287 # Perform the request to get the desired list
288 try:
Helena McGoughfe92f842017-11-17 14:57:08 +0000289 result = self.common._perform_request(
Helena McGoughf1527682017-09-22 10:55:22 +0100290 url, auth_token, req_type="get")
291
292 if result is not None:
293 # Get list based on resource id
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100294 for alarm in json.loads(result.text):
Helena McGoughf1527682017-09-22 10:55:22 +0100295 rule = alarm['gnocchi_resources_threshold_rule']
296 if resource == rule['resource_id']:
297 res_list.append(str(alarm))
298 if not res_list:
299 log.info("No alarms for this resource")
300 return a_list
301
302 # Generate specified listed if requested
303 if name is not None and sev is not None:
304 log.info("Return a list of %s alarms with %s severity.",
305 name, sev)
306 for alarm in json.loads(result.text):
307 if name == alarm['name']:
308 name_list.append(str(alarm))
309 for alarm in json.loads(result.text):
310 if sev == alarm['severity']:
311 sev_list.append(str(alarm))
312 name_sev_list = list(set(name_list).intersection(sev_list))
313 a_list = list(set(name_sev_list).intersection(res_list))
314 elif name is not None:
315 log.info("Returning a %s list of alarms.", name)
316 for alarm in json.loads(result.text):
317 if name == alarm['name']:
318 name_list.append(str(alarm))
319 a_list = list(set(name_list).intersection(res_list))
320 elif sev is not None:
321 log.info("Returning %s severity alarm list.", sev)
322 for alarm in json.loads(result.text):
323 if sev == alarm['severity']:
324 sev_list.append(str(alarm))
325 a_list = list(set(sev_list).intersection(res_list))
326 else:
327 log.info("Returning an entire list of alarms.")
328 a_list = res_list
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100329 else:
Helena McGoughf1527682017-09-22 10:55:22 +0100330 log.info("There are no alarms!")
331
332 except Exception as exc:
333 log.info("Failed to generate required list: %s", exc)
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100334 return None
Helena McGoughf1527682017-09-22 10:55:22 +0100335
336 return a_list
Helena McGoughc2763192017-09-07 13:14:30 +0000337
338 def update_alarm_state(self, endpoint, auth_token, alarm_id):
339 """Set the state of an alarm to ok when ack message is received."""
Helena McGoughc2763192017-09-07 13:14:30 +0000340 url = "{}/v2/alarms/%s/state".format(endpoint) % alarm_id
341 payload = json.dumps("ok")
342
343 try:
Helena McGoughfe92f842017-11-17 14:57:08 +0000344 self.common._perform_request(
Helena McGoughc2763192017-09-07 13:14:30 +0000345 url, auth_token, req_type="put", payload=payload)
346 return True
347 except Exception as exc:
348 log.warn("Unable to update alarm state: %s", exc)
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100349 return False
Helena McGoughc2763192017-09-07 13:14:30 +0000350
351 def update_alarm(self, endpoint, auth_token, values):
352 """Get alarm name for an alarm configuration update."""
353 # Get already existing alarm details
354 url = "{}/v2/alarms/%s".format(endpoint) % values['alarm_uuid']
355
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100356 # Gets current configurations about the alarm
Helena McGoughc2763192017-09-07 13:14:30 +0000357 try:
Helena McGoughfe92f842017-11-17 14:57:08 +0000358 result = self.common._perform_request(
Helena McGoughc2763192017-09-07 13:14:30 +0000359 url, auth_token, req_type="get")
360 alarm_name = json.loads(result.text)['name']
361 rule = json.loads(result.text)['gnocchi_resources_threshold_rule']
362 alarm_state = json.loads(result.text)['state']
363 resource_id = rule['resource_id']
364 metric_name = rule['metric']
365 except Exception as exc:
366 log.warn("Failed to retreive existing alarm info: %s.\
Helena McGoughd00ff822017-09-20 17:42:22 +0100367 Can only update OSM alarms.", exc)
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100368 return None, False
Helena McGoughc2763192017-09-07 13:14:30 +0000369
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100370 # Generates and check payload configuration for alarm update
Helena McGoughc2763192017-09-07 13:14:30 +0000371 payload = self.check_payload(values, metric_name, resource_id,
372 alarm_name, alarm_state=alarm_state)
373
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100374 # Updates the alarm configurations with the valid payload
Helena McGoughc2763192017-09-07 13:14:30 +0000375 if payload is not None:
376 try:
Helena McGoughfe92f842017-11-17 14:57:08 +0000377 update_alarm = self.common._perform_request(
Helena McGoughc2763192017-09-07 13:14:30 +0000378 url, auth_token, req_type="put", payload=payload)
379
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100380 return json.loads(update_alarm.text)['alarm_id'], True
Helena McGoughc2763192017-09-07 13:14:30 +0000381 except Exception as exc:
382 log.warn("Alarm update could not be performed: %s", exc)
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100383 return None, False
384 return None, False
Helena McGoughc2763192017-09-07 13:14:30 +0000385
386 def check_payload(self, values, metric_name, resource_id,
387 alarm_name, alarm_state=None):
388 """Check that the payload is configuration for update/create alarm."""
389 try:
390 # Check state and severity
Helena McGoughd00ff822017-09-20 17:42:22 +0100391 severity = values['severity'].lower()
392 if severity == "indeterminate":
Helena McGoughc2763192017-09-07 13:14:30 +0000393 alarm_state = "insufficient data"
Helena McGoughc2763192017-09-07 13:14:30 +0000394 if alarm_state is None:
395 alarm_state = "ok"
396
Helena McGoughd00ff822017-09-20 17:42:22 +0100397 statistic = values['statistic'].lower()
Helena McGoughc2763192017-09-07 13:14:30 +0000398 # Try to configure the payload for the update/create request
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100399 # Can only update: threshold, operation, statistic and
400 # the severity of the alarm
Helena McGoughc2763192017-09-07 13:14:30 +0000401 rule = {'threshold': values['threshold_value'],
402 'comparison_operator': values['operation'].lower(),
403 'metric': metric_name,
404 'resource_id': resource_id,
405 'resource_type': 'generic',
Helena McGough94f93f72017-11-23 17:29:54 +0000406 'aggregation_method': STATISTICS[statistic], }
Helena McGoughc2763192017-09-07 13:14:30 +0000407 payload = json.dumps({'state': alarm_state,
408 'name': alarm_name,
409 'severity': SEVERITIES[severity],
410 'type': 'gnocchi_resources_threshold',
Helena McGough94f93f72017-11-23 17:29:54 +0000411 'gnocchi_resources_threshold_rule': rule,
412 'alarm_actions': ['http://localhost:8662'], })
Helena McGoughc2763192017-09-07 13:14:30 +0000413 return payload
414 except KeyError as exc:
415 log.warn("Alarm is not configured correctly: %s", exc)
416 return None
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100417
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100418 def get_alarm_state(self, endpoint, auth_token, alarm_id):
419 """Get the state of the alarm."""
420 url = "{}/v2/alarms/%s/state".format(endpoint) % alarm_id
421
422 try:
Helena McGoughfe92f842017-11-17 14:57:08 +0000423 alarm_state = self.common._perform_request(
Helena McGoughcda5f2f2017-09-12 08:30:02 +0100424 url, auth_token, req_type="get")
425 return json.loads(alarm_state.text)
426 except Exception as exc:
427 log.warn("Failed to get the state of the alarm:%s", exc)
428 return None
Helena McGoughd00ff822017-09-20 17:42:22 +0100429
430 def check_for_metric(self, auth_token, m_name, r_id):
431 """Check for the alarm metric."""
432 try:
Helena McGoughfe92f842017-11-17 14:57:08 +0000433 endpoint = self.common.get_endpoint("metric")
Helena McGoughd00ff822017-09-20 17:42:22 +0100434
435 url = "{}/v1/metric/".format(endpoint)
Helena McGoughfe92f842017-11-17 14:57:08 +0000436 metric_list = self.common._perform_request(
Helena McGoughd00ff822017-09-20 17:42:22 +0100437 url, auth_token, req_type="get")
438
439 for metric in json.loads(metric_list.text):
440 name = metric['name']
441 resource = metric['resource_id']
442 if (name == m_name and resource == r_id):
443 metric_id = metric['id']
444 log.info("The required metric exists, an alarm will be created.")
445 return metric_id
446 except Exception as exc:
447 log.info("Desired Gnocchi metric not found:%s", exc)
448 return None