X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcollector%2Fvnf_collectors%2Fopenstack.py;h=4cf798f2f3326e2e564b38f5303db2d78650bd93;hb=8721c5ce76869719af22fb238a56fec612f76289;hp=8dbab5c8b0f0ae5ed00ebcd0e5f3579d627106b3;hpb=b525e6c8619d494d4e254def394cf5b62de4df4a;p=osm%2FMON.git diff --git a/osm_mon/collector/vnf_collectors/openstack.py b/osm_mon/collector/vnf_collectors/openstack.py index 8dbab5c..4cf798f 100644 --- a/osm_mon/collector/vnf_collectors/openstack.py +++ b/osm_mon/collector/vnf_collectors/openstack.py @@ -26,7 +26,9 @@ from typing import List import gnocchiclient.exceptions from gnocchiclient.v1 import client as gnocchi_client +from ceilometerclient.v2 import client as ceilometer_client from keystoneauth1 import session +from keystoneauth1.exceptions import EndpointNotFound from keystoneauth1.identity import v3 from osm_mon.collector.metric import Metric @@ -34,7 +36,7 @@ from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector from osm_mon.collector.vnf_metric import VnfMetric from osm_mon.core.auth import AuthManager from osm_mon.core.common_db import CommonDbClient -from osm_mon.core.settings import Config +from osm_mon.core.config import Config log = logging.getLogger(__name__) @@ -52,12 +54,14 @@ METRIC_MAPPINGS = { class OpenstackCollector(BaseVimCollector): - def __init__(self, vim_account_id: str): - super().__init__(vim_account_id) - self.common_db = CommonDbClient() - self.auth_manager = AuthManager() + def __init__(self, config: Config, vim_account_id: str): + super().__init__(config, vim_account_id) + self.conf = config + self.common_db = CommonDbClient(config) + self.auth_manager = AuthManager(config) self.granularity = self._get_granularity(vim_account_id) - self.gnocchi_client = self._build_gnocchi_client(vim_account_id) + self.backend = self._get_backend(vim_account_id) + self.client = self._build_client(vim_account_id) def _get_resource_uuid(self, nsr_id, vnf_member_index, vdur_name) -> str: vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name) @@ -75,14 +79,25 @@ class OpenstackCollector(BaseVimCollector): sess = session.Session(auth=auth, verify=verify_ssl) return gnocchi_client.Client(session=sess) + def _build_ceilometer_client(self, vim_account_id: str) -> ceilometer_client.Client: + creds = self.auth_manager.get_credentials(vim_account_id) + verify_ssl = self.auth_manager.is_verify_ssl(vim_account_id) + 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') + sess = session.Session(auth=auth, verify=verify_ssl) + return ceilometer_client.Client(session=sess) + def _get_granularity(self, vim_account_id: str): creds = self.auth_manager.get_credentials(vim_account_id) vim_config = json.loads(creds.config) if 'granularity' in vim_config: return int(vim_config['granularity']) else: - cfg = Config.instance() - return cfg.OS_DEFAULT_GRANULARITY + return int(self.conf.get('openstack', 'default_granularity')) def collect(self, vnfr: dict) -> List[Metric]: nsr_id = vnfr['nsr-id-ref'] @@ -99,19 +114,51 @@ class OpenstackCollector(BaseVimCollector): if 'monitoring-param' in vdu: for param in vdu['monitoring-param']: metric_name = param['nfvi-metric'] - gnocchi_metric_name = METRIC_MAPPINGS[metric_name] - delta = 10 * self.granularity - start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta) + openstack_metric_name = METRIC_MAPPINGS[metric_name] resource_id = self._get_resource_uuid(nsr_id, vnf_member_index, vdur['name']) - try: - measures = self.gnocchi_client.metric.get_measures(gnocchi_metric_name, - start=start_date, - resource_id=resource_id, - granularity=self.granularity) + if self.backend == 'ceilometer': + measures = self.client.samples.list(meter_name=openstack_metric_name, limit=1, q=[ + {'field': 'resource_id', 'op': 'eq', 'value': resource_id}]) if len(measures): - metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, measures[-1][2]) + metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, + measures[0].counter_volume) metrics.append(metric) - except gnocchiclient.exceptions.NotFound as e: - log.debug("No metric found: %s", e) - pass + elif self.backend == 'gnocchi': + delta = 10 * self.granularity + start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta) + try: + measures = self.client.metric.get_measures(openstack_metric_name, + start=start_date, + resource_id=resource_id, + granularity=self.granularity) + if len(measures): + metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, measures[-1][2]) + metrics.append(metric) + except gnocchiclient.exceptions.NotFound as e: + log.debug("No metric found: %s", e) + pass + else: + raise Exception('Unknown metric backend: %s', self.backend) return metrics + + def _build_client(self, vim_account_id): + if self.backend == 'ceilometer': + return self._build_ceilometer_client(vim_account_id) + elif self.backend == 'gnocchi': + return self._build_gnocchi_client(vim_account_id) + else: + raise Exception('Unknown metric backend: %s', self.backend) + + def _get_backend(self, vim_account_id): + try: + gnocchi = self._build_gnocchi_client(vim_account_id) + gnocchi.resource.list(limit=1) + return 'gnocchi' + except EndpointNotFound: + try: + ceilometer = self._build_ceilometer_client(vim_account_id) + ceilometer.resources.list(limit=1) + return 'ceilometer' + except Exception: + log.exception('Error trying to determine metric backend') + raise Exception('Could not determine metric backend')