+
+ def collect_metrics(self, metric_list: List[Dict]) -> List[Dict]:
+ log.debug("collect_metrics")
+ self._reload_connection()
+
+ metric_results = []
+ log.info(metric_list)
+ for metric in metric_list:
+ server = metric["vm_id"]
+ metric_name = metric["metric"]
+ metric_mapping = METRIC_MAPPINGS.get(metric_name)
+ if not metric_mapping:
+ # log.info(f"Metric {metric_name} not available in Azure")
+ continue
+ azure_metric_name = metric_mapping["metricname"]
+ azure_aggregation = metric_mapping["aggregation"]
+ end = datetime.datetime.now()
+ init = end - datetime.timedelta(minutes=5)
+ try:
+ metrics_data = self.conn_monitor.metrics.list(
+ server,
+ timespan="{}/{}".format(init, end),
+ interval="PT1M",
+ metricnames=azure_metric_name,
+ aggregation=azure_aggregation,
+ )
+ except Exception as e:
+ log.error(e)
+ continue
+ total = 0
+ n_metrics = 0
+ for item in metrics_data.value:
+ log.info("{} ({})".format(item.name.localized_value, item.unit))
+ for timeserie in item.timeseries:
+ for data in timeserie.data:
+ if azure_aggregation == "Average":
+ val = data.average
+ elif azure_aggregation == "Total":
+ val = data.total
+ else:
+ val = None
+ log.info("{}: {}".format(data.time_stamp, val))
+ if val is not None:
+ total += val
+ n_metrics += 1
+ if n_metrics > 0:
+ value = total / n_metrics
+ log.info(f"value = {value}")
+ metric["value"] = value
+ metric_results.append(metric)
+ else:
+ log.info("No metric available")
+
+ return metric_results