Bug 1976: MON fails to collect disk metrics fixed
[osm/MON.git] / osm_mon / collector / vnf_collectors / openstack.py
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
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: bdiaz@whitestack.com or glavado@whitestack.com
21 ##
22 from enum import Enum
23 import logging
24 import time
25 from typing import List
26
27 from ceilometerclient import client as ceilometer_client
28 from ceilometerclient.exc import HTTPException
29 import gnocchiclient.exceptions
30 from gnocchiclient.v1 import client as gnocchi_client
31 from keystoneauth1.exceptions.catalog import EndpointNotFound
32 from keystoneclient.v3 import client as keystone_client
33 from neutronclient.v2_0 import client as neutron_client
34
35 from osm_mon.collector.metric import Metric
36 from osm_mon.collector.utils.openstack import OpenstackUtils
37 from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector
38 from osm_mon.collector.vnf_metric import VnfMetric
39 from osm_mon.core.common_db import CommonDbClient
40 from osm_mon.core.config import Config
41
42
43 log = logging.getLogger(__name__)
44
45 METRIC_MAPPINGS = {
46 "average_memory_utilization": "memory.usage",
47 "disk_read_ops": "disk.read.requests.rate",
48 "disk_write_ops": "disk.write.requests.rate",
49 "disk_read_bytes": "disk.read.bytes.rate",
50 "disk_write_bytes": "disk.write.bytes.rate",
51 "packets_in_dropped": "network.outgoing.packets.drop",
52 "packets_out_dropped": "network.incoming.packets.drop",
53 "packets_received": "network.incoming.packets.rate",
54 "packets_sent": "network.outgoing.packets.rate",
55 "cpu_utilization": "cpu",
56 }
57
58 # Metrics which have new names in Rocky and higher releases
59 METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES = {
60 "disk_read_ops": "disk.device.read.requests",
61 "disk_write_ops": "disk.device.write.requests",
62 "disk_read_bytes": "disk.device.read.bytes",
63 "disk_write_bytes": "disk.device.write.bytes",
64 "packets_received": "network.incoming.packets",
65 "packets_sent": "network.outgoing.packets"
66 }
67
68 METRIC_MULTIPLIERS = {"cpu": 0.0000001}
69
70 METRIC_AGGREGATORS = {"cpu": "rate:mean"}
71
72 INTERFACE_METRICS = [
73 "packets_in_dropped",
74 "packets_out_dropped",
75 "packets_received",
76 "packets_sent",
77 ]
78
79 INSTANCE_DISK = [
80 "disk_read_ops",
81 "disk_write_ops",
82 "disk_read_bytes",
83 "disk_write_bytes",
84 ]
85
86
87 class MetricType(Enum):
88 INSTANCE = "instance"
89 INTERFACE_ALL = "interface_all"
90 INTERFACE_ONE = "interface_one"
91 INSTANCEDISK = 'instancedisk'
92
93
94 class OpenstackCollector(BaseVimCollector):
95 def __init__(self, config: Config, vim_account_id: str, vim_session: object):
96 super().__init__(config, vim_account_id)
97 self.common_db = CommonDbClient(config)
98 vim_account = self.common_db.get_vim_account(vim_account_id)
99 self.backend = self._get_backend(vim_account, vim_session)
100
101 def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
102 sess = OpenstackUtils.get_session(vim_account)
103 return keystone_client.Client(session=sess)
104
105 def _get_resource_uuid(
106 self, nsr_id: str, vnf_member_index: str, vdur_name: str
107 ) -> str:
108 vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name)
109 return vdur["vim-id"]
110
111 def collect(self, vnfr: dict) -> List[Metric]:
112 nsr_id = vnfr["nsr-id-ref"]
113 vnf_member_index = vnfr["member-vnf-index-ref"]
114 vnfd = self.common_db.get_vnfd(vnfr["vnfd-id"])
115 # Populate extra tags for metrics
116 tags = {}
117 tags["ns_name"] = self.common_db.get_nsr(nsr_id)["name"]
118 if vnfr["_admin"]["projects_read"]:
119 tags["project_id"] = vnfr["_admin"]["projects_read"][0]
120 else:
121 tags["project_id"] = ""
122
123 metrics = []
124
125 for vdur in vnfr["vdur"]:
126 # This avoids errors when vdur records have not been completely filled
127 if "name" not in vdur:
128 continue
129 vdu = next(filter(lambda vdu: vdu["id"] == vdur["vdu-id-ref"], vnfd["vdu"]))
130 if "monitoring-parameter" in vdu:
131 for param in vdu["monitoring-parameter"]:
132 metric_name = param["performance-metric"]
133 openstack_metric_name = METRIC_MAPPINGS[metric_name]
134 metric_type = self._get_metric_type(metric_name)
135 try:
136 resource_id = self._get_resource_uuid(
137 nsr_id, vnf_member_index, vdur["name"]
138 )
139 except ValueError:
140 log.warning(
141 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
142 "Was it recently deleted?",
143 vdur["name"],
144 vnf_member_index,
145 nsr_id,
146 )
147 continue
148 try:
149 log.info(
150 "Collecting metric type: %s and metric_name: %s and resource_id %s and ",
151 metric_type,
152 metric_name,
153 resource_id,
154 )
155 value = self.backend.collect_metric(
156 metric_type, openstack_metric_name, resource_id
157 )
158
159 if value is None and metric_name in METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES:
160 # Reattempting metric collection with new metric names.
161 # Some metric names have changed in newer Openstack releases
162 log.info(
163 "Reattempting metric collection for type: %s and name: %s and resource_id %s",
164 metric_type,
165 metric_name,
166 resource_id
167 )
168 openstack_metric_name = METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES[metric_name]
169 value = self.backend.collect_metric(
170 metric_type, openstack_metric_name, resource_id
171 )
172 if value is not None:
173 log.info("value: %s", value)
174 metric = VnfMetric(
175 nsr_id,
176 vnf_member_index,
177 vdur["name"],
178 metric_name,
179 value,
180 tags,
181 )
182 metrics.append(metric)
183 else:
184 log.info("metric value is empty")
185 except Exception as e:
186 log.exception(
187 "Error collecting metric %s for vdu %s"
188 % (metric_name, vdur["name"])
189 )
190 log.info("Error in metric collection: %s" % e)
191 return metrics
192
193 def _get_backend(self, vim_account: dict, vim_session: object):
194 try:
195 gnocchi = GnocchiBackend(vim_account, vim_session)
196 gnocchi.client.metric.list(limit=1)
197 log.info("Using gnocchi backend to collect metric")
198 return gnocchi
199 except (HTTPException, EndpointNotFound):
200 ceilometer = CeilometerBackend(vim_account, vim_session)
201 ceilometer.client.capabilities.get()
202 log.info("Using ceilometer backend to collect metric")
203 return ceilometer
204
205 def _get_metric_type(self, metric_name: str) -> MetricType:
206 if metric_name not in INTERFACE_METRICS:
207 if metric_name not in INSTANCE_DISK:
208 return MetricType.INSTANCE
209 else:
210 return MetricType.INSTANCEDISK
211 else:
212 return MetricType.INTERFACE_ALL
213
214
215 class OpenstackBackend:
216 def collect_metric(
217 self, metric_type: MetricType, metric_name: str, resource_id: str
218 ):
219 pass
220
221
222 class GnocchiBackend(OpenstackBackend):
223 def __init__(self, vim_account: dict, vim_session: object):
224 self.client = self._build_gnocchi_client(vim_account, vim_session)
225 self.neutron = self._build_neutron_client(vim_account, vim_session)
226
227 def _build_gnocchi_client(self, vim_account: dict, vim_session: object) -> gnocchi_client.Client:
228 return gnocchi_client.Client(session=vim_session)
229
230 def _build_neutron_client(self, vim_account: dict, vim_session: object) -> neutron_client.Client:
231 return neutron_client.Client(session=vim_session)
232
233 def collect_metric(
234 self, metric_type: MetricType, metric_name: str, resource_id: str
235 ):
236 if metric_type == MetricType.INTERFACE_ALL:
237 return self._collect_interface_all_metric(metric_name, resource_id)
238
239 elif metric_type == MetricType.INSTANCE:
240 return self._collect_instance_metric(metric_name, resource_id)
241
242 elif metric_type == MetricType.INSTANCEDISK:
243 return self._collect_instance_disk_metric(metric_name, resource_id)
244
245 else:
246 raise Exception("Unknown metric type %s" % metric_type.value)
247
248 def _collect_interface_all_metric(self, openstack_metric_name, resource_id):
249 total_measure = None
250 interfaces = self.client.resource.search(
251 resource_type="instance_network_interface",
252 query={"=": {"instance_id": resource_id}},
253 )
254 for interface in interfaces:
255 try:
256 measures = self.client.metric.get_measures(
257 openstack_metric_name, resource_id=interface["id"], limit=1
258 )
259 if measures:
260 if not total_measure:
261 total_measure = 0.0
262 total_measure += measures[-1][2]
263 except (gnocchiclient.exceptions.NotFound, TypeError) as e:
264 # Gnocchi in some Openstack versions raise TypeError instead of NotFound
265 log.debug(
266 "No metric %s found for interface %s: %s",
267 openstack_metric_name,
268 interface["id"],
269 e,
270 )
271 return total_measure
272
273 def _collect_instance_disk_metric(self, openstack_metric_name, resource_id):
274 value = None
275 instances = self.client.resource.search(
276 resource_type='instance_disk',
277 query={'=': {'instance_id': resource_id}},
278 )
279 for instance in instances:
280 try:
281 measures = self.client.metric.get_measures(
282 openstack_metric_name, resource_id=instance['id'], limit=1
283 )
284 if measures:
285 value = measures[-1][2]
286
287 except gnocchiclient.exceptions.NotFound as e:
288 log.debug("No metric %s found for instance disk %s: %s", openstack_metric_name,
289 instance['id'], e)
290 return value
291
292 def _collect_instance_metric(self, openstack_metric_name, resource_id):
293 value = None
294 try:
295 aggregation = METRIC_AGGREGATORS.get(openstack_metric_name)
296
297 try:
298 measures = self.client.metric.get_measures(
299 openstack_metric_name,
300 aggregation=aggregation,
301 start=time.time() - 1200,
302 resource_id=resource_id,
303 )
304 if measures:
305 value = measures[-1][2]
306 except (
307 gnocchiclient.exceptions.NotFound,
308 gnocchiclient.exceptions.BadRequest,
309 TypeError,
310 ) as e:
311 # CPU metric in previous Openstack versions do not support rate:mean aggregation method
312 # Gnocchi in some Openstack versions raise TypeError instead of NotFound or BadRequest
313 if openstack_metric_name == "cpu":
314 log.debug(
315 "No metric %s found for instance %s: %s",
316 openstack_metric_name,
317 resource_id,
318 e,
319 )
320 log.info(
321 "Retrying to get metric %s for instance %s without aggregation",
322 openstack_metric_name,
323 resource_id,
324 )
325 measures = self.client.metric.get_measures(
326 openstack_metric_name, resource_id=resource_id, limit=1
327 )
328 else:
329 raise e
330 # measures[-1] is the last measure
331 # measures[-2] is the previous measure
332 # measures[x][2] is the value of the metric
333 if measures and len(measures) >= 2:
334 value = measures[-1][2] - measures[-2][2]
335 if value:
336 # measures[-1][0] is the time of the reporting interval
337 # measures[-1][1] is the duration of the reporting interval
338 if aggregation:
339 # If this is an aggregate, we need to divide the total over the reported time period.
340 # Even if the aggregation method is not supported by Openstack, the code will execute it
341 # because aggregation is specified in METRIC_AGGREGATORS
342 value = value / measures[-1][1]
343 if openstack_metric_name in METRIC_MULTIPLIERS:
344 value = value * METRIC_MULTIPLIERS[openstack_metric_name]
345 except gnocchiclient.exceptions.NotFound as e:
346 log.debug(
347 "No metric %s found for instance %s: %s",
348 openstack_metric_name,
349 resource_id,
350 e,
351 )
352 return value
353
354
355 class CeilometerBackend(OpenstackBackend):
356 def __init__(self, vim_account: dict, vim_session: object):
357 self.client = self._build_ceilometer_client(vim_account, vim_session)
358
359 def _build_ceilometer_client(self, vim_account: dict, vim_session: object) -> ceilometer_client.Client:
360 return ceilometer_client.Client("2", session=vim_session)
361
362 def collect_metric(
363 self, metric_type: MetricType, metric_name: str, resource_id: str
364 ):
365 if metric_type != MetricType.INSTANCE:
366 raise NotImplementedError(
367 "Ceilometer backend only support instance metrics"
368 )
369 measures = self.client.samples.list(
370 meter_name=metric_name,
371 limit=1,
372 q=[{"field": "resource_id", "op": "eq", "value": resource_id}],
373 )
374 return measures[0].counter_volume if measures else None