1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
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: bdiaz@whitestack.com or glavado@whitestack.com
25 from typing
import List
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 from prometheus_api_client
import PrometheusConnect
as prometheus_client
36 from osm_mon
.collector
.metric
import Metric
37 from osm_mon
.collector
.utils
.openstack
import OpenstackUtils
38 from osm_mon
.collector
.vnf_collectors
.base_vim
import BaseVimCollector
39 from osm_mon
.collector
.vnf_metric
import VnfMetric
40 from osm_mon
.core
.common_db
import CommonDbClient
41 from osm_mon
.core
.config
import Config
44 log
= logging
.getLogger(__name__
)
47 "average_memory_utilization": "memory.usage",
48 "disk_read_ops": "disk.read.requests.rate",
49 "disk_write_ops": "disk.write.requests.rate",
50 "disk_read_bytes": "disk.read.bytes.rate",
51 "disk_write_bytes": "disk.write.bytes.rate",
52 "packets_in_dropped": "network.outgoing.packets.drop",
53 "packets_out_dropped": "network.incoming.packets.drop",
54 "packets_received": "network.incoming.packets.rate",
55 "packets_sent": "network.outgoing.packets.rate",
56 "cpu_utilization": "cpu",
59 METRIC_MAPPINGS_FOR_PROMETHEUS_TSBD
= {
60 "cpu_utilization": "cpu",
61 "average_memory_utilization": "memory_usage",
62 "disk_read_ops": "disk_device_read_requests",
63 "disk_write_ops": "disk_device_write_requests",
64 "disk_read_bytes": "disk_device_read_bytes",
65 "disk_write_bytes": "disk_device_write_bytes",
66 "packets_in_dropped": "network_incoming_packets_drop",
67 "packets_out_dropped": "network_outgoing_packets_drop",
68 "packets_received": "network_incoming_packets",
69 "packets_sent": "network_outgoing_packets",
72 # Metrics which have new names in Rocky and higher releases
73 METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES
= {
74 "disk_read_ops": "disk.device.read.requests",
75 "disk_write_ops": "disk.device.write.requests",
76 "disk_read_bytes": "disk.device.read.bytes",
77 "disk_write_bytes": "disk.device.write.bytes",
78 "packets_received": "network.incoming.packets",
79 "packets_sent": "network.outgoing.packets",
82 METRIC_MULTIPLIERS
= {"cpu": 0.0000001}
84 METRIC_AGGREGATORS
= {"cpu": "rate:mean"}
88 "packets_out_dropped",
101 class MetricType(Enum
):
102 INSTANCE
= "instance"
103 INTERFACE_ALL
= "interface_all"
104 INTERFACE_ONE
= "interface_one"
105 INSTANCEDISK
= "instancedisk"
108 class OpenstackCollector(BaseVimCollector
):
109 def __init__(self
, config
: Config
, vim_account_id
: str, vim_session
: object):
110 super().__init
__(config
, vim_account_id
)
111 self
.common_db
= CommonDbClient(config
)
112 vim_account
= self
.common_db
.get_vim_account(vim_account_id
)
113 self
.backend
= self
._get
_backend
(vim_account
, vim_session
)
115 def _build_keystone_client(self
, vim_account
: dict) -> keystone_client
.Client
:
116 sess
= OpenstackUtils
.get_session(vim_account
)
117 return keystone_client
.Client(session
=sess
)
119 def _get_resource_uuid(
120 self
, nsr_id
: str, vnf_member_index
: str, vdur_name
: str
122 vdur
= self
.common_db
.get_vdur(nsr_id
, vnf_member_index
, vdur_name
)
123 return vdur
["vim-id"]
125 def collect(self
, vnfr
: dict) -> List
[Metric
]:
126 nsr_id
= vnfr
["nsr-id-ref"]
127 vnf_member_index
= vnfr
["member-vnf-index-ref"]
128 vnfd
= self
.common_db
.get_vnfd(vnfr
["vnfd-id"])
129 # Populate extra tags for metrics
131 tags
["ns_name"] = self
.common_db
.get_nsr(nsr_id
)["name"]
132 if vnfr
["_admin"]["projects_read"]:
133 tags
["project_id"] = vnfr
["_admin"]["projects_read"][0]
135 tags
["project_id"] = ""
139 for vdur
in vnfr
["vdur"]:
140 # This avoids errors when vdur records have not been completely filled
141 if "name" not in vdur
:
143 vdu
= next(filter(lambda vdu
: vdu
["id"] == vdur
["vdu-id-ref"], vnfd
["vdu"]))
144 if "monitoring-parameter" in vdu
:
145 for param
in vdu
["monitoring-parameter"]:
146 metric_name
= param
["performance-metric"]
147 log
.info(f
"Using an {type(self.backend)} as backend")
148 if type(self
.backend
) is PrometheusTSBDBackend
:
149 openstack_metric_name
= self
.backend
.map_metric(metric_name
)
151 openstack_metric_name
= METRIC_MAPPINGS
[metric_name
]
152 metric_type
= self
._get
_metric
_type
(metric_name
)
154 resource_id
= self
._get
_resource
_uuid
(
155 nsr_id
, vnf_member_index
, vdur
["name"]
159 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
160 "Was it recently deleted?",
168 "Collecting metric type: %s and metric_name: %s and resource_id %s and ",
173 value
= self
.backend
.collect_metric(
174 metric_type
, openstack_metric_name
, resource_id
180 in METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES
181 and type(self
.backend
) is not PrometheusTSBDBackend
183 # Reattempting metric collection with new metric names.
184 # Some metric names have changed in newer Openstack releases
186 "Reattempting metric collection for type: %s and name: %s and resource_id %s",
191 openstack_metric_name
= (
192 METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES
[
196 value
= self
.backend
.collect_metric(
197 metric_type
, openstack_metric_name
, resource_id
199 if value
is not None:
200 log
.info("value: %s", value
)
209 metrics
.append(metric
)
211 log
.info("metric value is empty")
212 except Exception as e
:
214 "Error collecting metric %s for vdu %s"
215 % (metric_name
, vdur
["name"])
217 log
.info("Error in metric collection: %s" % e
)
220 def _get_backend(self
, vim_account
: dict, vim_session
: object):
221 if vim_account
.get("prometheus-config"):
223 tsbd
= PrometheusTSBDBackend(vim_account
)
224 log
.debug("Using prometheustsbd backend to collect metric")
226 except Exception as e
:
227 log
.error(f
"Can't create prometheus client, {e}")
230 gnocchi
= GnocchiBackend(vim_account
, vim_session
)
231 gnocchi
.client
.metric
.list(limit
=1)
232 log
.debug("Using gnocchi backend to collect metric")
234 except (HTTPException
, EndpointNotFound
):
235 ceilometer
= CeilometerBackend(vim_account
, vim_session
)
236 ceilometer
.client
.capabilities
.get()
237 log
.debug("Using ceilometer backend to collect metric")
240 def _get_metric_type(self
, metric_name
: str) -> MetricType
:
241 if metric_name
not in INTERFACE_METRICS
:
242 if metric_name
not in INSTANCE_DISK
:
243 return MetricType
.INSTANCE
245 return MetricType
.INSTANCEDISK
247 return MetricType
.INTERFACE_ALL
250 class OpenstackBackend
:
252 self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str
257 class PrometheusTSBDBackend(OpenstackBackend
):
258 def __init__(self
, vim_account
: dict):
259 self
.map = self
._build
_map
(vim_account
)
260 self
.cred
= vim_account
["prometheus-config"].get("prometheus-cred")
261 self
.client
= self
._build
_prometheus
_client
(
262 vim_account
["prometheus-config"]["prometheus-url"]
265 def _build_prometheus_client(self
, url
: str) -> prometheus_client
:
266 return prometheus_client(url
, disable_ssl
=True)
268 def _build_map(self
, vim_account
: dict) -> dict:
269 custom_map
= METRIC_MAPPINGS_FOR_PROMETHEUS_TSBD
270 if "prometheus-map" in vim_account
["prometheus-config"]:
271 custom_map
.update(vim_account
["prometheus-config"]["prometheus-map"])
275 self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str
277 metric
= self
.query_metric(metric_name
, resource_id
)
278 return metric
["value"][1] if metric
else None
280 def map_metric(self
, metric_name
: str):
281 return self
.map[metric_name
]
283 def query_metric(self
, metric_name
, resource_id
=None):
284 metrics
= self
.client
.get_current_metric_value(metric_name
=metric_name
)
287 filter(lambda x
: resource_id
in x
["metric"]["resource_id"], metrics
)
293 class GnocchiBackend(OpenstackBackend
):
294 def __init__(self
, vim_account
: dict, vim_session
: object):
295 self
.client
= self
._build
_gnocchi
_client
(vim_account
, vim_session
)
296 self
.neutron
= self
._build
_neutron
_client
(vim_account
, vim_session
)
298 def _build_gnocchi_client(
299 self
, vim_account
: dict, vim_session
: object
300 ) -> gnocchi_client
.Client
:
301 return gnocchi_client
.Client(session
=vim_session
)
303 def _build_neutron_client(
304 self
, vim_account
: dict, vim_session
: object
305 ) -> neutron_client
.Client
:
306 return neutron_client
.Client(session
=vim_session
)
309 self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str
311 if metric_type
== MetricType
.INTERFACE_ALL
:
312 return self
._collect
_interface
_all
_metric
(metric_name
, resource_id
)
314 elif metric_type
== MetricType
.INSTANCE
:
315 return self
._collect
_instance
_metric
(metric_name
, resource_id
)
317 elif metric_type
== MetricType
.INSTANCEDISK
:
318 return self
._collect
_instance
_disk
_metric
(metric_name
, resource_id
)
321 raise Exception("Unknown metric type %s" % metric_type
.value
)
323 def _collect_interface_all_metric(self
, openstack_metric_name
, resource_id
):
325 interfaces
= self
.client
.resource
.search(
326 resource_type
="instance_network_interface",
327 query
={"=": {"instance_id": resource_id
}},
329 for interface
in interfaces
:
331 measures
= self
.client
.metric
.get_measures(
332 openstack_metric_name
, resource_id
=interface
["id"], limit
=1
335 if not total_measure
:
337 total_measure
+= measures
[-1][2]
338 except (gnocchiclient
.exceptions
.NotFound
, TypeError) as e
:
339 # Gnocchi in some Openstack versions raise TypeError instead of NotFound
341 "No metric %s found for interface %s: %s",
342 openstack_metric_name
,
348 def _collect_instance_disk_metric(self
, openstack_metric_name
, resource_id
):
350 instances
= self
.client
.resource
.search(
351 resource_type
="instance_disk",
352 query
={"=": {"instance_id": resource_id
}},
354 for instance
in instances
:
356 measures
= self
.client
.metric
.get_measures(
357 openstack_metric_name
, resource_id
=instance
["id"], limit
=1
360 value
= measures
[-1][2]
362 except gnocchiclient
.exceptions
.NotFound
as e
:
364 "No metric %s found for instance disk %s: %s",
365 openstack_metric_name
,
371 def _collect_instance_metric(self
, openstack_metric_name
, resource_id
):
374 aggregation
= METRIC_AGGREGATORS
.get(openstack_metric_name
)
377 measures
= self
.client
.metric
.get_measures(
378 openstack_metric_name
,
379 aggregation
=aggregation
,
380 start
=time
.time() - 1200,
381 resource_id
=resource_id
,
384 value
= measures
[-1][2]
386 gnocchiclient
.exceptions
.NotFound
,
387 gnocchiclient
.exceptions
.BadRequest
,
390 # CPU metric in previous Openstack versions do not support rate:mean aggregation method
391 # Gnocchi in some Openstack versions raise TypeError instead of NotFound or BadRequest
392 if openstack_metric_name
== "cpu":
394 "No metric %s found for instance %s: %s",
395 openstack_metric_name
,
400 "Retrying to get metric %s for instance %s without aggregation",
401 openstack_metric_name
,
404 measures
= self
.client
.metric
.get_measures(
405 openstack_metric_name
, resource_id
=resource_id
, limit
=1
409 # measures[-1] is the last measure
410 # measures[-2] is the previous measure
411 # measures[x][2] is the value of the metric
412 if measures
and len(measures
) >= 2:
413 value
= measures
[-1][2] - measures
[-2][2]
415 # measures[-1][0] is the time of the reporting interval
416 # measures[-1][1] is the duration of the reporting interval
418 # If this is an aggregate, we need to divide the total over the reported time period.
419 # Even if the aggregation method is not supported by Openstack, the code will execute it
420 # because aggregation is specified in METRIC_AGGREGATORS
421 value
= value
/ measures
[-1][1]
422 if openstack_metric_name
in METRIC_MULTIPLIERS
:
423 value
= value
* METRIC_MULTIPLIERS
[openstack_metric_name
]
424 except gnocchiclient
.exceptions
.NotFound
as e
:
426 "No metric %s found for instance %s: %s",
427 openstack_metric_name
,
434 class CeilometerBackend(OpenstackBackend
):
435 def __init__(self
, vim_account
: dict, vim_session
: object):
436 self
.client
= self
._build
_ceilometer
_client
(vim_account
, vim_session
)
438 def _build_ceilometer_client(
439 self
, vim_account
: dict, vim_session
: object
440 ) -> ceilometer_client
.Client
:
441 return ceilometer_client
.Client("2", session
=vim_session
)
444 self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str
446 if metric_type
!= MetricType
.INSTANCE
:
447 raise NotImplementedError(
448 "Ceilometer backend only support instance metrics"
450 measures
= self
.client
.samples
.list(
451 meter_name
=metric_name
,
453 q
=[{"field": "resource_id", "op": "eq", "value": resource_id
}],
455 return measures
[0].counter_volume
if measures
else None