From: Benjamin Diaz Date: Tue, 19 Feb 2019 23:49:39 +0000 (-0300) Subject: Adds support for Ceilometer API metric collection X-Git-Tag: v6.0.0~25 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=8721c5ce76869719af22fb238a56fec612f76289;p=osm%2FMON.git Adds support for Ceilometer API metric collection Change-Id: Id0e841c465ea5772d08413dacdf490a1fc109dff Signed-off-by: Benjamin Diaz --- diff --git a/debian/python3-osm-mon.postinst b/debian/python3-osm-mon.postinst index 8ab760d..1600da3 100644 --- a/debian/python3-osm-mon.postinst +++ b/debian/python3-osm-mon.postinst @@ -35,4 +35,5 @@ pip3 install pyyaml==3.* pip3 install prometheus_client==0.4.* pip3 install gnocchiclient==7.0.* pip3 install pyvcloud==19.1.1 +pip3 install python-ceilometerclient==2.9.* echo "Installation of python dependencies finished" diff --git a/osm_mon/collector/vnf_collectors/openstack.py b/osm_mon/collector/vnf_collectors/openstack.py index ba7097e..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 @@ -58,7 +60,8 @@ class OpenstackCollector(BaseVimCollector): 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) @@ -76,6 +79,18 @@ 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) @@ -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') diff --git a/requirements.txt b/requirements.txt index 1ebe677..7e4c081 100644 --- a/requirements.txt +++ b/requirements.txt @@ -31,5 +31,6 @@ prometheus_client==0.4.* gnocchiclient==7.0.* pymysql==0.9.* pyvcloud==19.1.* +python-ceilometerclient==2.9.* git+https://osm.etsi.org/gerrit/osm/common.git@v5.0#egg=osm-common git+https://osm.etsi.org/gerrit/osm/N2VC.git@v5.0#egg=n2vc diff --git a/setup.py b/setup.py index 3792510..ab5f600 100644 --- a/setup.py +++ b/setup.py @@ -63,6 +63,7 @@ setup( "prometheus_client==0.4.*", "gnocchiclient==7.0.*", "pyvcloud==19.1.1", + "python-ceilometerclient==2.9.*", "osm-common", "n2vc" ],