Fix bug 1258: metrics not properly collected in some Openstacks 23/9823/4
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 9 Oct 2020 15:30:03 +0000 (15:30 +0000)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 28 Oct 2020 14:20:13 +0000 (14:20 +0000)
Change-Id: I5d77e7882a784252217118deb447215a2eac8ae4
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osm_mon/collector/vnf_collectors/openstack.py

index bd2a617..df0424b 100644 (file)
@@ -232,17 +232,36 @@ class GnocchiBackend(OpenstackBackend):
         try:
             aggregation = METRIC_AGGREGATORS.get(openstack_metric_name)
 
-            measures = self.client.metric.get_measures(openstack_metric_name,
-                                                       aggregation=aggregation,
-                                                       start=time.time() - 1200,
-                                                       resource_id=resource_id)
-            # measures[-1][0] is the time of the reporting interval
-            # measures[-1][1] is the durcation of the reporting interval
-            # measures[-1][2] is the value of the metric
-            if measures:
-                value = measures[-1][2]
+            try:
+                measures = self.client.metric.get_measures(openstack_metric_name,
+                                                           aggregation=aggregation,
+                                                           start=time.time() - 1200,
+                                                           resource_id=resource_id)
+                if measures:
+                    value = measures[-1][2]
+            except gnocchiclient.exceptions.NotFound as e:
+                # CPU metric in previous Openstack versions do not support rate:mean aggregation method
+                if openstack_metric_name == "cpu":
+                    log.debug("No metric %s found for instance %s: %s", openstack_metric_name, resource_id, e)
+                    log.debug("Retrying to get metric %s for instance %s without aggregation",
+                              openstack_metric_name, resource_id)
+                    measures = self.client.metric.get_measures(openstack_metric_name,
+                                                               resource_id=resource_id,
+                                                               limit=1)
+                else:
+                    raise e
+                # measures[-1] is the last measure
+                # measures[-2] is the previous measure
+                # measures[x][2] is the value of the metric
+                if measures and len(measures) >= 2:
+                    value = measures[-1][2] - measures[-2][2]
+            if value:
+                # measures[-1][0] is the time of the reporting interval
+                # measures[-1][1] is the duration of the reporting interval
                 if aggregation:
                     # If this is an aggregate, we need to divide the total over the reported time period.
+                    # Even if the aggregation method is not supported by Openstack, the code will execute it
+                    # because aggregation is specified in METRIC_AGGREGATORS
                     value = value / measures[-1][1]
                 if openstack_metric_name in METRIC_MULTIPLIERS:
                     value = value * METRIC_MULTIPLIERS[openstack_metric_name]