X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcollector%2Fvnf_collectors%2Fopenstack.py;h=60b387cf2e4d0a5887b1195687154618444f771a;hb=73dbb4e243f47afef0d1bb61988608e256939e87;hp=c11f63fd3ddc081c9b74526ec38b618df79b0973;hpb=6b45f219dc6bbb1dde1d3cb88eb51372ee81d359;p=osm%2FMON.git diff --git a/osm_mon/collector/vnf_collectors/openstack.py b/osm_mon/collector/vnf_collectors/openstack.py index c11f63f..60b387c 100644 --- a/osm_mon/collector/vnf_collectors/openstack.py +++ b/osm_mon/collector/vnf_collectors/openstack.py @@ -19,22 +19,20 @@ # For those usages not covered by the Apache License, Version 2.0 please # contact: bdiaz@whitestack.com or glavado@whitestack.com ## -import datetime -import json import logging from enum import Enum from typing import List import gnocchiclient.exceptions -from ceilometerclient.v2 import client as ceilometer_client +from ceilometerclient import client as ceilometer_client +from ceilometerclient.exc import HTTPException from gnocchiclient.v1 import client as gnocchi_client -from keystoneauth1 import session -from keystoneauth1.identity import v3 from keystoneclient.v3 import client as keystone_client +from keystoneauth1.exceptions.catalog import EndpointNotFound from neutronclient.v2_0 import client as neutron_client from osm_mon.collector.metric import Metric -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.openstack import OpenstackUtils from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector from osm_mon.collector.vnf_metric import VnfMetric from osm_mon.core.common_db import CommonDbClient @@ -67,30 +65,31 @@ class MetricType(Enum): class OpenstackCollector(BaseVimCollector): def __init__(self, config: Config, vim_account_id: str): super().__init__(config, vim_account_id) - self.conf = config self.common_db = CommonDbClient(config) - self.backend = self._get_backend(vim_account_id) + vim_account = self.common_db.get_vim_account(vim_account_id) + self.backend = self._get_backend(vim_account) - def _build_keystone_client(self, vim_account_id: str) -> keystone_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) + def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client: + sess = OpenstackUtils.get_session(vim_account) return keystone_client.Client(session=sess) def _get_resource_uuid(self, nsr_id: str, vnf_member_index: str, vdur_name: str) -> str: vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name) return vdur['vim-id'] - def _get_granularity(self, vim_account_id: str): - creds = CollectorUtils.get_credentials(vim_account_id) - vim_config = json.loads(creds.config) - if 'granularity' in vim_config: - return int(vim_config['granularity']) - else: - return int(self.conf.get('openstack', 'default_granularity')) - def collect(self, vnfr: dict) -> List[Metric]: nsr_id = vnfr['nsr-id-ref'] vnf_member_index = vnfr['member-vnf-index-ref'] vnfd = self.common_db.get_vnfd(vnfr['vnfd-id']) + + # Populate extra tags for metrics + tags = {} + tags['ns_name'] = self.common_db.get_nsr(nsr_id)['name'] + if vnfr['_admin']['projects_read']: + tags['project_id'] = vnfr['_admin']['projects_read'][0] + else: + tags['project_id'] = '' + metrics = [] for vdur in vnfr['vdur']: # This avoids errors when vdur records have not been completely filled @@ -113,19 +112,27 @@ class OpenstackCollector(BaseVimCollector): "Was it recently deleted?", vdur['name'], vnf_member_index, nsr_id) continue - value = self.backend.collect_metric(metric_type, openstack_metric_name, resource_id, interface_name) - if value is not None: - metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, value) - metrics.append(metric) + try: + value = self.backend.collect_metric(metric_type, openstack_metric_name, resource_id, + interface_name) + if value is not None: + if interface_name: + tags['interface'] = interface_name + metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, value, tags) + metrics.append(metric) + except Exception: + log.exception("Error collecting metric %s for vdu %s" % (metric_name, vdur['name'])) return metrics - def _get_backend(self, vim_account_id: str): - keystone = self._build_keystone_client(vim_account_id) - if keystone.services.list('gnocchi'): - granularity = self._get_granularity(vim_account_id) - return GnocchiBackend(vim_account_id, granularity) - elif keystone.services.list('ceilometer'): - return CeilometerBackend(vim_account_id) + def _get_backend(self, vim_account: dict): + try: + ceilometer = CeilometerBackend(vim_account) + ceilometer.client.capabilities.get() + return ceilometer + except (HTTPException, EndpointNotFound): + gnocchi = GnocchiBackend(vim_account) + gnocchi.client.metric.list(limit=1) + return gnocchi def _get_metric_type(self, metric_name: str, interface_name: str) -> MetricType: if metric_name not in INTERFACE_METRICS: @@ -140,32 +147,19 @@ class OpenstackBackend: def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str): pass - @staticmethod - def get_session(vim_account_id: str): - creds = CollectorUtils.get_credentials(vim_account_id) - verify_ssl = CollectorUtils.is_verify_ssl(creds) - auth = v3.Password(auth_url=creds.url, - username=creds.user, - password=creds.password, - project_name=creds.tenant_name, - project_domain_id='default', - user_domain_id='default') - return session.Session(auth=auth, verify=verify_ssl) - class GnocchiBackend(OpenstackBackend): - def __init__(self, vim_account_id: str, granularity: int): - self.client = self._build_gnocchi_client(vim_account_id) - self.neutron = self._build_neutron_client(vim_account_id) - self.granularity = granularity + def __init__(self, vim_account: dict): + self.client = self._build_gnocchi_client(vim_account) + self.neutron = self._build_neutron_client(vim_account) - def _build_gnocchi_client(self, vim_account_id: str) -> gnocchi_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) + def _build_gnocchi_client(self, vim_account: dict) -> gnocchi_client.Client: + sess = OpenstackUtils.get_session(vim_account) return gnocchi_client.Client(session=sess) - def _build_neutron_client(self, vim_account_id: str) -> neutron_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) + def _build_neutron_client(self, vim_account: dict) -> neutron_client.Client: + sess = OpenstackUtils.get_session(vim_account) return neutron_client.Client(session=sess) def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str): @@ -182,8 +176,6 @@ class GnocchiBackend(OpenstackBackend): raise Exception('Unknown metric type %s' % metric_type.value) def _collect_interface_one_metric(self, metric_name, resource_id, interface_name): - delta = 10 * self.granularity - start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta) ports = self.neutron.list_ports(name=interface_name, device_id=resource_id) if not ports or not ports['ports']: raise Exception( @@ -194,23 +186,19 @@ class GnocchiBackend(OpenstackBackend): interfaces = self.client.resource.search(resource_type='instance_network_interface', query={'=': {'name': tap_name}}) measures = self.client.metric.get_measures(metric_name, - start=start_date, resource_id=interfaces[0]['id'], - granularity=self.granularity) + limit=1) return measures[-1][2] if measures else None def _collect_interface_all_metric(self, openstack_metric_name, resource_id): - delta = 10 * self.granularity - start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta) total_measure = None interfaces = self.client.resource.search(resource_type='instance_network_interface', query={'=': {'instance_id': resource_id}}) for interface in interfaces: try: measures = self.client.metric.get_measures(openstack_metric_name, - start=start_date, resource_id=interface['id'], - granularity=self.granularity) + limit=1) if measures: if not total_measure: total_measure = 0.0 @@ -222,14 +210,11 @@ class GnocchiBackend(OpenstackBackend): return total_measure def _collect_instance_metric(self, openstack_metric_name, resource_id): - delta = 10 * self.granularity - start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta) value = None try: measures = self.client.metric.get_measures(openstack_metric_name, - start=start_date, resource_id=resource_id, - granularity=self.granularity) + limit=1) if measures: value = measures[-1][2] except gnocchiclient.exceptions.NotFound as e: @@ -239,12 +224,12 @@ class GnocchiBackend(OpenstackBackend): class CeilometerBackend(OpenstackBackend): - def __init__(self, vim_account_id: str): - self.client = self._build_ceilometer_client(vim_account_id) + def __init__(self, vim_account: dict): + self.client = self._build_ceilometer_client(vim_account) - def _build_ceilometer_client(self, vim_account_id: str) -> ceilometer_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) - return ceilometer_client.Client(session=sess) + def _build_ceilometer_client(self, vim_account: dict) -> ceilometer_client.Client: + sess = OpenstackUtils.get_session(vim_account) + return ceilometer_client.Client("2", session=sess) def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str): if metric_type != MetricType.INSTANCE: