From a5e0b57e4d954cb5c8b8b9b146ec5144c2b13667 Mon Sep 17 00:00:00 2001 From: alecorps <31779520+alecorps@users.noreply.github.com> Date: Thu, 10 Oct 2019 05:42:11 +0200 Subject: [PATCH] fix(vm_cpu_usage): add vm max cpu usage metric and test (#147) * add vm max cpu usage metric and test * fix test issue --- tests/unit/test_vmware_exporter.py | 13 +++++++++++++ vmware_exporter/vmware_exporter.py | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/tests/unit/test_vmware_exporter.py b/tests/unit/test_vmware_exporter.py index 90ea741..093d167 100644 --- a/tests/unit/test_vmware_exporter.py +++ b/tests/unit/test_vmware_exporter.py @@ -84,6 +84,7 @@ def test_collect_vms(): 'runtime.powerState': 'poweredOn', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, + 'runtime.maxCpuUsage': 2400, 'summary.config.template': False, 'runtime.bootTime': boot_time, 'snapshot': snapshot, @@ -160,6 +161,7 @@ def test_collect_vms(): 'runtime.powerState': 'poweredOn', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, + 'runtime.maxCpuUsage': 2400, 'summary.config.template': False, 'runtime.bootTime': boot_time, 'snapshot': snapshot, @@ -173,6 +175,7 @@ def test_collect_vms(): 'runtime.powerState': 'poweredOff', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, + 'runtime.maxCpuUsage': 2400, 'summary.config.template': False, 'runtime.bootTime': boot_time, 'snapshot': snapshot, @@ -187,6 +190,7 @@ def test_collect_vms(): 'runtime.powerState': 'poweredOff', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, + 'runtime.maxCpuUsage': 2400, 'summary.config.template': False, 'runtime.bootTime': boot_time, 'snapshot': snapshot, @@ -303,6 +307,14 @@ def test_collect_vms(): } assert metrics['vmware_vm_memory_max'].samples[0][2] == 1024 + # Max Cpu + assert metrics['vmware_vm_max_cpu_usage'].samples[0][1] == { + 'vm_name': 'vm-1', + 'host_name': 'host-1', + 'cluster_name': 'cluster-1', + 'dc_name': 'dc', + } + assert metrics['vmware_vm_max_cpu_usage'].samples[0][2] == 2400 assert metrics['vmware_vm_template'].samples[0][2] == 0.0 @@ -341,6 +353,7 @@ def test_metrics_without_hostaccess(): 'runtime.powerState': 'poweredOn', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, + 'runtime.maxCpuUsage': 2400, 'summary.config.template': False, 'runtime.bootTime': boot_time, 'guest.disk': [disk], diff --git a/vmware_exporter/vmware_exporter.py b/vmware_exporter/vmware_exporter.py index 5c0f8c0..de22e3c 100755 --- a/vmware_exporter/vmware_exporter.py +++ b/vmware_exporter/vmware_exporter.py @@ -64,6 +64,10 @@ def _create_metric_containers(self): 'vmware_vm_memory_max', 'VMWare VM Memory Max availability in Mbytes', labels=['vm_name', 'host_name', 'dc_name', 'cluster_name']), + 'vmware_vm_max_cpu_usage': GaugeMetricFamily( + 'vmware_vm_max_cpu_usage', + 'VMWare VM Cpu Max availability in hz', + labels=['vm_name', 'host_name', 'dc_name', 'cluster_name']), 'vmware_vm_template': GaugeMetricFamily( 'vmware_vm_template', 'VMWare VM Template (true / false)', @@ -354,6 +358,7 @@ def vm_inventory(self): 'runtime.bootTime', 'summary.config.numCpu', 'summary.config.memorySizeMB', + 'runtime.maxCpuUsage', 'summary.config.template', ]) @@ -765,6 +770,9 @@ def _vmware_get_vms(self, metrics): if 'summary.config.memorySizeMB' in row: metrics['vmware_vm_memory_max'].add_metric(labels, row['summary.config.memorySizeMB']) + if 'runtime.maxCpuUsage' in row: + metrics['vmware_vm_max_cpu_usage'].add_metric(labels, row['runtime.maxCpuUsage']) + if 'summary.config.template' in row: metrics['vmware_vm_template'].add_metric(labels, row['summary.config.template'])