From 4d26c9e97ba8438d127cc46bacb580e5a3f460b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verch=C3=A8re?= Date: Thu, 1 Jun 2017 18:22:41 +0200 Subject: [PATCH] Refs #3. Start adding more VM metrics --- vmware_exporter.py | 61 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/vmware_exporter.py b/vmware_exporter.py index 50d55e7..4de52a6 100755 --- a/vmware_exporter.py +++ b/vmware_exporter.py @@ -110,9 +110,12 @@ def collect(self): print("[{0}] Start collecting vcenter metrics".format(datetime.utcnow().replace(tzinfo=pytz.utc))) - # Get VMWare VM Informations + # Get VMWare Content Information content = self.si.RetrieveContent() + # Get performance metrics counter information + counter_info = self._vmware_perf_metrics(content) + # Fill Snapshots (count and age) vm_counts, vm_ages = self._vmware_get_snapshots(content) for v in vm_counts: @@ -128,7 +131,7 @@ def collect(self): self._vmware_get_datastores(content, metrics) # Fill VM Informations - self._vmware_get_vms(content, metrics) + self._vmware_get_vms(content, metrics, counter_info) # Fill Hosts Informations self._vmware_get_hosts(content, metrics) @@ -184,6 +187,19 @@ def _vmware_connect(self): return None + def _vmware_perf_metrics(self, content): + # create a mapping from performance stats to their counterIDs + # counter_info: [performance stat => counterId] + # performance stat example: cpu.usagemhz.LATEST + counter_info = {} + for c in content.perfManager.perfCounter: + prefix = c.groupInfo.key + counter_full = "{}.{}.{}".format(c.groupInfo.key, + c.nameInfo.key,c.rollupType) + counter_info[counter_full] = c.key + return counter_info + + def _vmware_list_snapshots_recursively(self, snapshots): """ Get snapshots from a VM list, recursively @@ -247,19 +263,58 @@ def _vmware_get_datastores(self, content, ds_metrics): ds_metrics['vmware_datastore_vms'].add_metric([summary.name], len(ds.vm)) - def _vmware_get_vms(self, content, vm_metrics): + def _vmware_get_vms(self, content, vm_metrics, counter_info): """ Get VM information """ + + # List of performance counter we want + perf_list = [ + 'cpu.usage.average', + 'disk.usage.average', + 'disk.read.average', + 'disk.write.average', + 'mem.usage.average', + 'net.received.average', + 'net.transmitted.average', + ] + + # Prepare gauges + for p in perf_list: + p_metric = 'vmware_vm_' + p.replace('.', '_') + vm_metrics[p_metric] = GaugeMetricFamily( + p_metric, + p_metric, + labels=['vm_name']) + for vm in self._vmware_get_obj(content, [vim.VirtualMachine]): summary = vm.summary + power_state = 1 if summary.runtime.powerState == 'poweredOn' else 0 vm_metrics['vmware_vm_power_state'].add_metric([vm.name], power_state) + + # Get metrics for poweredOn vms only if power_state: if summary.runtime.bootTime: vm_metrics['vmware_vm_boot_timestamp_seconds'].add_metric([vm.name], self._to_unix_timestamp(summary.runtime.bootTime)) + for p in perf_list: + p_metric = 'vmware_vm_' + p.replace('.', '_') + counter_key = counter_info[p] + metric_id = vim.PerformanceManager.MetricId( + counterId=counter_key, + instance='') + spec = vim.PerformanceManager.QuerySpec( + maxSample=1, + entity=vm, + metricId=[metric_id], + intervalId=20) + result = content.perfManager.QueryStats(querySpec=[spec]) + vm_metrics[p_metric].add_metric([vm.name], + float(sum(result[0].value[0].value))) + + def _vmware_get_hosts(self, content, host_metrics): """