7b135477e7bc130709703be50a4224f0cbd1bcf5
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 OpenStack metric requests via Gnocchi API."""
29 from core
.message_bus
.producer
import KafkaProducer
31 from kafka
import KafkaConsumer
33 from plugins
.OpenStack
.common
import Common
34 from plugins
.OpenStack
.response
import OpenStack_Response
36 __author__
= "Helena McGough"
39 "average_memory_utilization": "memory.percent",
40 "disk_read_ops": "disk.disk_ops",
41 "disk_write_ops": "disk.disk_ops",
42 "disk_read_bytes": "disk.disk_octets",
43 "disk_write_bytes": "disk.disk_octets",
44 "packets_dropped": "interface.if_dropped",
45 "packets_received": "interface.if_packets",
46 "packets_sent": "interface.if_packets",
47 "cpu_utilization": "cpu.percent",
59 class Metrics(object):
60 """OpenStack metric requests performed via the Gnocchi API."""
63 """Initialize the metric actions."""
64 self
._common
= Common()
66 # TODO(mcgoughh): Initialize a generic consumer object to consume
67 # message from the SO. This is hardcoded for now
68 server
= {'server': 'localhost:9092', 'topic': 'metric_request'}
69 self
._consumer
= KafkaConsumer(server
['topic'],
71 bootstrap_servers
=server
['server'])
73 # Use the Response class to generate valid json response messages
74 self
._response
= OpenStack_Response()
76 # Initializer a producer to send responses back to SO
77 self
._producer
= KafkaProducer("metric_response")
79 def metric_calls(self
):
80 """Consume info from the message bus to manage metric requests."""
81 # Consumer check for metric messages
82 for message
in self
._consumer
:
83 # Check if this plugin should carry out this request
84 values
= json
.loads(message
.value
)
85 vim_type
= values
['vim_type'].lower()
87 if vim_type
== "openstack":
88 # Generate auth_token and endpoint
89 auth_token
, endpoint
= self
.authenticate()
91 if message
.key
== "create_metric_request":
93 metric_details
= values
['metric_create']
94 metric_id
, resource_id
, status
= self
.configure_metric(
95 endpoint
, auth_token
, metric_details
)
97 # Generate and send a create metric response
99 resp_message
= self
._response
.generate_response(
100 'create_metric_response', status
=status
,
101 cor_id
=values
['correlation_id'],
102 metric_id
=metric_id
, r_id
=resource_id
)
103 self
._producer
.create_metrics_resp(
104 'create_metric_response', resp_message
,
106 except Exception as exc
:
107 log
.warn("Failed to create response: %s", exc
)
109 elif message
.key
== "read_metric_data_request":
110 # Read all metric data related to a specified metric
111 timestamps
, metric_data
= self
.read_metric_data(
112 endpoint
, auth_token
, values
)
114 # Generate and send a response message
116 resp_message
= self
._response
.generate_response(
117 'read_metric_data_response',
118 m_id
=values
['metric_uuid'],
119 m_name
=values
['metric_name'],
120 r_id
=values
['resource_uuid'],
121 cor_id
=values
['correlation_id'],
122 times
=timestamps
, metrics
=metric_data
)
123 self
._producer
.read_metric_data_response(
124 'read_metric_data_response', resp_message
,
126 except Exception as exc
:
127 log
.warn("Failed to send read metric response:%s", exc
)
129 elif message
.key
== "delete_metric_request":
130 # delete the specified metric in the request
131 metric_id
= values
['metric_uuid']
132 status
= self
.delete_metric(
133 endpoint
, auth_token
, metric_id
)
135 # Generate and send a response message
137 resp_message
= self
._response
.generate_response(
138 'delete_metric_response', m_id
=metric_id
,
139 m_name
=values
['metric_name'],
140 status
=status
, r_id
=values
['resource_uuid'],
141 cor_id
=values
['correlation_id'])
142 self
._producer
.delete_metric_response(
143 'delete_metric_response', resp_message
,
145 except Exception as exc
:
146 log
.warn("Failed to send delete response:%s", exc
)
148 elif message
.key
== "update_metric_request":
149 # Gnocchi doesn't support configuration updates
150 # Log and send a response back to this effect
151 log
.warn("Gnocchi doesn't support metric configuration\
153 req_details
= values
['metric_create']
154 metric_name
= req_details
['metric_name']
155 resource_id
= req_details
['resource_uuid']
156 metric_id
= self
.get_metric_id(
157 endpoint
, auth_token
, metric_name
, resource_id
)
159 # Generate and send a response message
161 resp_message
= self
._response
.generate_response(
162 'update_metric_response', status
=False,
163 cor_id
=values
['correlation_id'],
164 r_id
=resource_id
, m_id
=metric_id
)
165 self
._producer
.update_metric_response(
166 'update_metric_response', resp_message
,
168 except Exception as exc
:
169 log
.warn("Failed to send an update response:%s", exc
)
171 elif message
.key
== "list_metric_request":
172 list_details
= values
['metrics_list_request']
174 metric_list
= self
.list_metrics(
175 endpoint
, auth_token
, list_details
)
177 # Generate and send a response message
179 resp_message
= self
._response
.generate_response(
180 'list_metric_response', m_list
=metric_list
,
181 cor_id
=list_details
['correlation_id'])
182 self
._producer
.list_metric_response(
183 'list_metric_response', resp_message
,
185 except Exception as exc
:
186 log
.warn("Failed to send a list response:%s", exc
)
189 log
.warn("Unknown key, no action will be performed.")
191 log
.debug("Message is not for this OpenStack.")
195 def configure_metric(self
, endpoint
, auth_token
, values
):
196 """Create the new metric in Gnocchi."""
198 resource_id
= values
['resource_uuid']
200 log
.warn("Resource is not defined correctly.")
201 return None, None, False
203 # Check/Normalize metric name
204 metric_name
, norm_name
= self
.get_metric_name(values
)
205 if norm_name
is None:
206 log
.warn("This metric is not supported by this plugin.")
207 return None, resource_id
, False
209 # Check for an existing metric for this resource
210 metric_id
= self
.get_metric_id(
211 endpoint
, auth_token
, metric_name
, resource_id
)
213 if metric_id
is None:
214 # Try appending metric to existing resource
216 base_url
= "{}/v1/resource/generic/%s/metric"
217 res_url
= base_url
.format(endpoint
) % resource_id
218 payload
= {metric_name
: {'archive_policy_name': 'high',
219 'unit': values
['metric_unit']}}
220 result
= self
._common
._perform
_request
(
221 res_url
, auth_token
, req_type
="post",
222 payload
=json
.dumps(payload
))
223 # Get id of newly created metric
224 for row
in json
.loads(result
.text
):
225 if row
['name'] == metric_name
:
226 metric_id
= row
['id']
227 log
.info("Appended metric to existing resource.")
229 return metric_id
, resource_id
, True
230 except Exception as exc
:
231 # Gnocchi version of resource does not exist creating a new one
232 log
.info("Failed to append metric to existing resource:%s",
235 url
= "{}/v1/resource/generic".format(endpoint
)
236 metric
= {'name': metric_name
,
237 'archive_policy_name': 'high',
238 'unit': values
['metric_unit'], }
240 resource_payload
= json
.dumps({'id': resource_id
,
242 metric_name
: metric
}})
244 resource
= self
._common
._perform
_request
(
245 url
, auth_token
, req_type
="post",
246 payload
=resource_payload
)
248 # Return the newly created resource_id for creating alarms
249 new_resource_id
= json
.loads(resource
.text
)['id']
250 log
.info("Created new resource for metric: %s",
253 metric_id
= self
.get_metric_id(
254 endpoint
, auth_token
, metric_name
, new_resource_id
)
256 return metric_id
, new_resource_id
, True
257 except Exception as exc
:
258 log
.warn("Failed to create a new resource:%s", exc
)
259 return None, None, False
262 log
.info("This metric already exists for this resource.")
264 return metric_id
, resource_id
, False
266 def delete_metric(self
, endpoint
, auth_token
, metric_id
):
268 url
= "{}/v1/metric/%s".format(endpoint
) % (metric_id
)
271 result
= self
._common
._perform
_request
(
272 url
, auth_token
, req_type
="delete")
273 if str(result
.status_code
) == "404":
274 log
.warn("Failed to delete the metric.")
278 except Exception as exc
:
279 log
.warn("Failed to carry out delete metric request:%s", exc
)
282 def list_metrics(self
, endpoint
, auth_token
, values
):
283 """List all metrics."""
284 url
= "{}/v1/metric/".format(endpoint
)
286 # Check for a specified list
288 # Check if the metric_name was specified for the list
289 metric_name
= values
['metric_name'].lower()
290 if metric_name
not in METRIC_MAPPINGS
.keys():
291 log
.warn("This metric is not supported, won't be listed.")
293 except KeyError as exc
:
294 log
.info("Metric name is not specified: %s", exc
)
298 resource
= values
['resource_uuid']
299 except KeyError as exc
:
300 log
.info("Resource is not specified:%s", exc
)
304 result
= self
._common
._perform
_request
(
305 url
, auth_token
, req_type
="get")
306 metrics
= json
.loads(result
.text
)
308 if metrics
is not None:
309 # Format the list response
310 if metric_name
is not None and resource
is not None:
311 metric_list
= self
.response_list(
312 metrics
, metric_name
=metric_name
, resource
=resource
)
313 log
.info("Returning an %s resource list for %s metrics",
314 metric_name
, resource
)
315 elif metric_name
is not None:
316 metric_list
= self
.response_list(
317 metrics
, metric_name
=metric_name
)
318 log
.info("Returning a list of %s metrics", metric_name
)
319 elif resource
is not None:
320 metric_list
= self
.response_list(
321 metrics
, resource
=resource
)
322 log
.info("Return a list of %s resource metrics", resource
)
324 metric_list
= self
.response_list(metrics
)
325 log
.info("Returning a complete list of metrics")
329 log
.info("There are no metrics available")
331 except Exception as exc
:
332 log
.warn("Failed to generate any metric list. %s", exc
)
335 def get_metric_id(self
, endpoint
, auth_token
, metric_name
, resource_id
):
336 """Check if the desired metric already exists for the resource."""
337 url
= "{}/v1/resource/generic/%s".format(endpoint
) % resource_id
340 # Try return the metric id if it exists
341 result
= self
._common
._perform
_request
(
342 url
, auth_token
, req_type
="get")
343 return json
.loads(result
.text
)['metrics'][metric_name
]
345 log
.info("Metric doesn't exist. No metric_id available")
348 def get_metric_name(self
, values
):
349 """Check metric name configuration and normalize."""
351 # Normalize metric name
352 metric_name
= values
['metric_name'].lower()
353 return metric_name
, METRIC_MAPPINGS
[metric_name
]
355 log
.info("Metric name %s is invalid.", metric_name
)
356 return metric_name
, None
358 def read_metric_data(self
, endpoint
, auth_token
, values
):
359 """Collectd metric measures over a specified time period."""
363 # Try and collect measures
364 metric_id
= values
['metric_uuid']
365 collection_unit
= values
['collection_unit'].upper()
366 collection_period
= values
['collection_period']
368 # Define the start and end time based on configurations
369 stop_time
= time
.strftime("%Y-%m-%d") + "T" + time
.strftime("%X")
370 end_time
= int(round(time
.time() * 1000))
371 if collection_unit
== 'YEAR':
372 diff
= PERIOD_MS
[collection_unit
]
374 diff
= collection_period
* PERIOD_MS
[collection_unit
]
375 s_time
= (end_time
- diff
)/1000.0
376 start_time
= datetime
.datetime
.fromtimestamp(s_time
).strftime(
377 '%Y-%m-%dT%H:%M:%S.%f')
378 base_url
= "{}/v1/metric/%(0)s/measures?start=%(1)s&stop=%(2)s"
379 url
= base_url
.format(endpoint
) % {
380 "0": metric_id
, "1": start_time
, "2": stop_time
}
382 # Perform metric data request
383 metric_data
= self
._common
._perform
_request
(
384 url
, auth_token
, req_type
="get")
386 # Generate a list of the requested timestamps and data
387 for r
in json
.loads(metric_data
.text
):
388 timestamp
= r
[0].replace("T", " ")
389 timestamps
.append(timestamp
)
392 return timestamps
, data
393 except Exception as exc
:
394 log
.warn("Failed to gather specified measures: %s", exc
)
395 return timestamps
, data
397 def authenticate(self
):
398 """Generate an authentication token and endpoint for metric request."""
400 # Check for a tenant_id
401 auth_token
= self
._common
._authenticate
()
402 endpoint
= self
._common
.get_endpoint("metric")
403 return auth_token
, endpoint
404 except Exception as exc
:
405 log
.warn("Authentication to Keystone failed: %s", exc
)
409 def response_list(self
, metric_list
, metric_name
=None, resource
=None):
410 """Create the appropriate lists for a list response."""
411 resp_list
, name_list
, res_list
= [], [], []
413 # Create required lists
414 for row
in metric_list
:
415 # Only list OSM metrics
416 if row
['name'] in METRIC_MAPPINGS
.keys():
417 metric
= {"metric_name": row
['name'],
418 "metric_uuid": row
['id'],
419 "metric_unit": row
['unit'],
420 "resource_uuid": row
['resource_id']}
421 resp_list
.append(str(metric
))
422 # Generate metric_name specific list
423 if metric_name
is not None:
424 if row
['name'] == metric_name
:
425 metric
= {"metric_name": row
['name'],
426 "metric_uuid": row
['id'],
427 "metric_unit": row
['unit'],
428 "resource_uuid": row
['resource_id']}
429 name_list
.append(str(metric
))
430 # Generate resource specific list
431 if resource
is not None:
432 if row
['resource_id'] == resource
:
433 metric
= {"metric_name": row
['name'],
434 "metric_uuid": row
['id'],
435 "metric_unit": row
['unit'],
436 "resource_uuid": row
['resource_id']}
437 res_list
.append(str(metric
))
439 # Join required lists
440 if metric_name
is not None and resource
is not None:
441 return list(set(res_list
).intersection(name_list
))
442 elif metric_name
is not None:
444 elif resource
is not None:
445 return list(set(res_list
).intersection(resp_list
))