Adds support for Ceilometer API metric collection
[osm/MON.git] / osm_mon / collector / vnf_collectors / openstack.py
index ba7097e..4cf798f 100644 (file)
@@ -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')