def test_collect(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ignore_ssl=True, ) collector.content = _succeed(mock.Mock()) with contextlib.ExitStack() as stack: stack.enter_context(mock.patch.object(collector, '_vmware_get_vms')).return_value = _succeed(True) stack.enter_context( mock.patch.object(collector, '_vmware_get_vm_perf_manager_metrics') ).return_value = _succeed(True) stack.enter_context(mock.patch.object(collector, '_vmware_get_datastores')).return_value = _succeed(True) stack.enter_context(mock.patch.object(collector, '_vmware_get_hosts')).return_value = _succeed(True) stack.enter_context(mock.patch.object(collector, '_vmware_disconnect')).return_value = _succeed(True) metrics = yield collector.collect() assert metrics[0].name == 'vmware_vm_power_state' assert metrics[-1].name == 'vmware_vm_snapshot_timestamp_seconds'
def test_collect_deferred_error_works(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ignore_ssl=True, ) collector.content = _succeed(mock.Mock()) @defer.inlineCallbacks def _fake_get_vms(*args, **kwargs): yield None raise RuntimeError('An error has occurred') with contextlib.ExitStack() as stack: stack.enter_context(mock.patch.object(collector, '_vmware_get_vms')).side_effect = _fake_get_vms stack.enter_context( mock.patch.object(collector, '_vmware_get_vm_perf_manager_metrics') ).return_value = _succeed(None) stack.enter_context(mock.patch.object(collector, '_vmware_get_datastores')).return_value = _succeed(None) stack.enter_context(mock.patch.object(collector, '_vmware_get_hosts')).return_value = _succeed(None) stack.enter_context(mock.patch.object(collector, '_vmware_disconnect')).return_value = _succeed(None) with pytest.raises(defer.FirstError): yield collector.collect()
def test_counter_ids(): counter = mock.Mock() counter.groupInfo.key = 'a' counter.nameInfo.key = 'b' counter.rollupType = 'c' counter.key = 1 content = mock.Mock() content.perfManager.perfCounter = [counter] collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) collector.content = content result = yield collector.counter_ids assert result == {'a.b.c': 1}
def test_vmware_connect(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, 5000, ignore_ssl=True, ) with mock.patch('vmware_exporter.vmware_exporter.connect') as connect: yield collector.connection call_kwargs = connect.SmartConnect.call_args[1] assert call_kwargs['host'] == '127.0.0.1' assert call_kwargs['user'] == 'root' assert call_kwargs['pwd'] == 'password' assert call_kwargs['sslContext'] is not None
def test_no_error_onempty_vms(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': False, 'hosts': False, 'snapshots': False, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ignore_ssl=True, ) metrics = collector._create_metric_containers() metric_1 = mock.Mock() metric_1.id.counterId = 10 metric_1.value = [9] metric_2 = mock.Mock() metric_2.id.counterId = 1 metric_2.value = [1] ent_1 = mock.Mock() ent_1.value = [metric_1, metric_2] ent_1.entity = vim.ManagedObject('vm:1') content = mock.Mock() content.perfManager.QueryStats.return_value = [ent_1] collector.content = _succeed(content) collector.__dict__['counter_ids'] = _succeed({ 'cpu.ready.summation': 1, 'cpu.maxlimited.summation': 2, 'cpu.usage.average': 3, 'cpu.usagemhz.average': 4, 'disk.usage.average': 5, 'disk.read.average': 6, 'disk.write.average': 7, 'mem.usage.average': 8, 'net.received.average': 9, 'net.transmitted.average': 10, }) collector.__dict__['vm_labels'] = _succeed({'': []}) collector.__dict__['vm_inventory'] = _succeed({'': {}}) # Try to test for querySpec=[] # threads.deferToThread(content.perfManager.QueryStats, querySpec=specs), # TypeError Required field "querySpec" not provided (not @optional) yield collector._vmware_get_vm_perf_manager_metrics(metrics) assert metrics['vmware_vm_power_state'].samples == []
def test_collect_datastore(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) collector.content = _succeed(mock.Mock()) collector.__dict__['datastore_labels'] = _succeed({ 'datastore-1': ['datastore-1', 'dc', 'ds_cluster'], }) metrics = collector._create_metric_containers() with mock.patch.object(collector, 'batch_fetch_properties') as batch_fetch_properties: batch_fetch_properties.return_value = _succeed({ 'datastore-1': { 'name': 'datastore-1', 'summary.capacity': 0, 'summary.freeSpace': 0, 'host': ['host-1'], 'vm': ['vm-1'], 'summary.accessible': True, 'summary.maintenanceMode': 'normal', } }) yield collector._vmware_get_datastores(metrics) assert _check_properties(batch_fetch_properties.call_args[0][1]) assert metrics['vmware_datastore_capacity_size'].samples[0][1] == { 'ds_name': 'datastore-1', 'dc_name': 'dc', 'ds_cluster': 'ds_cluster' } assert metrics['vmware_datastore_capacity_size'].samples[0][2] == 0.0 assert metrics['vmware_datastore_maintenance_mode'].samples[0][1] == { 'ds_name': 'datastore-1', 'dc_name': 'dc', 'ds_cluster': 'ds_cluster', 'mode': 'normal' } assert metrics['vmware_datastore_maintenance_mode'].samples[0][2] == 1.0 assert metrics['vmware_datastore_accessible'].samples[0][1] == { 'ds_name': 'datastore-1', 'dc_name': 'dc', 'ds_cluster': 'ds_cluster' } assert metrics['vmware_datastore_accessible'].samples[0][2] == 1.0
def test_metrics_without_hostaccess(): boot_time = EPOCH + datetime.timedelta(seconds=60) disk = mock.Mock() disk.diskPath = '/boot' disk.capacity = 100 disk.freeSpace = 50 collect_only = { 'vms': True, 'vmguests': True, 'datastores': False, 'hosts': False, 'snapshots': False, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) metrics = collector._create_metric_containers() collector.content = _succeed(mock.Mock()) collector.__dict__['host_labels'] = _succeed({'': []}) with mock.patch.object(collector, 'batch_fetch_properties') as batch_fetch_properties: batch_fetch_properties.return_value = _succeed({ 'vm-1': { 'name': 'vm-x', 'runtime.host': vim.ManagedObject('notfound:1'), 'runtime.powerState': 'poweredOn', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, 'runtime.bootTime': boot_time, 'guest.disk': [disk], 'guest.toolsStatus': 'toolsOk', 'guest.toolsVersion': '10336', 'guest.toolsVersionStatus2': 'guestToolsUnmanaged', } }) assert collector.vm_labels.result == {'vm-1': ['vm-x']} yield collector._vmware_get_vms(metrics) # 113 AssertionError {'partition': '/boot'} vs {'host_name': '/boot'} assert metrics['vmware_vm_guest_disk_capacity'].samples[0][1] == { 'vm_name': 'vm-x', 'partition': '/boot', 'host_name': 'n/a', 'cluster_name': 'n/a', 'dc_name': 'n/a', } # Fail due to expected labels ['vm-1', 'host-1', 'dc', 'cluster-1'] # but found ['vm-1'] assert metrics['vmware_vm_power_state'].samples[0][1] == { 'vm_name': 'vm-x', 'host_name': 'n/a', 'cluster_name': 'n/a', 'dc_name': 'n/a', }
def test_vmware_disconnect(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) # Mock that we have a connection connection = object() collector.connection = connection with mock.patch('vmware_exporter.vmware_exporter.connect') as connect: yield collector._vmware_disconnect() connect.Disconnect.assert_called_with(connection)
def test_collect_hosts(batch_fetch_properties): content = mock.Mock() boot_time = EPOCH + datetime.timedelta(seconds=60) batch_fetch_properties.return_value = { 'host-1': { 'id': 'host:1', 'name': 'host-1', 'runtime.powerState': 'poweredOn', 'runtime.bootTime': boot_time, 'runtime.connectionState': 'connected', 'runtime.inMaintenanceMode': True, 'summary.quickStats.overallCpuUsage': 100, 'summary.hardware.numCpuCores': 12, 'summary.hardware.cpuMhz': 1000, 'summary.quickStats.overallMemoryUsage': 1024, 'summary.hardware.memorySize': 2048 * 1024 * 1024, }, 'host-2': { 'id': 'host:2', 'name': 'host-2', 'runtime.powerState': 'poweredOff', } } collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) inventory = { 'host:1': { 'dc': 'dc', 'cluster': 'cluster', }, 'host:2': { 'dc': 'dc', 'cluster': 'cluster', } } metrics = collector._create_metric_containers() collector._vmware_get_hosts(content, metrics, inventory) assert _check_properties(batch_fetch_properties.call_args[0][2]) assert metrics['vmware_host_memory_max'].samples[0][1] == { 'host_name': 'host-1', 'dc_name': 'dc', 'cluster_name': 'cluster' } assert metrics['vmware_host_memory_max'].samples[0][2] == 2048 # In our test data we hava a host that is powered down - we should have its # power_state metric but not any others. assert len(metrics['vmware_host_power_state'].samples) == 2 assert len(metrics['vmware_host_memory_max'].samples) == 1
def test_vmware_get_inventory(): content = mock.Mock(spec=vim.ServiceInstanceContent) # Compute case 1 host_1 = mock.Mock(spec=vim.HostSystem) host_1._moId = 'host:1' host_1.name = 'host-1' host_1.summary.config.name = 'host-1.' folder_1 = mock.Mock(spec=vim.ComputeResource) folder_1.host = [host_1] # Computer case 2 host_2 = mock.Mock(spec=vim.HostSystem) host_2._moId = 'host:2' host_2.name = 'host-2' host_2.summary.config.name = 'host-2.' folder_2 = vim.ClusterComputeResource('computer-cluster:1') folder_2.__dict__['name'] = 'compute-cluster-1' folder_2.__dict__['host'] = [host_2] # Folders case host_3 = mock.Mock(spec=vim.HostSystem) host_3._moId = 'host:3' host_3.name = 'host-3' host_3.summary.config.name = 'host-3.' folder_3 = mock.Mock(spec=vim.ComputeResource) folder_3.host = [host_3] folder_4 = vim.Folder('folder:4') folder_4.__dict__['name'] = 'folder-4' folder_4.__dict__['childEntity'] = [folder_3] folder_5 = vim.Folder('folder:5') folder_5.__dict__['name'] = 'folder-5' folder_5.__dict__['childEntity'] = [folder_4] # Datastore case 1 datastore_1 = vim.Datastore('datastore:1') datastore_1.__dict__['name'] = 'datastore-1' # Datastore case 2 datastore_2 = vim.Datastore('datastore:2') datastore_2.__dict__['name'] = 'datastore-2' datastore_2_folder = vim.StoragePod('storagepod:1') datastore_2_folder.__dict__['childEntity'] = [datastore_2] datastore_2_folder.__dict__['name'] = 'datastore2-folder' data_center_1 = mock.Mock(spec=vim.Datacenter) data_center_1.name = 'dc-1' data_center_1_hostfolder = mock.Mock(spec=vim.Folder) data_center_1_hostfolder.childEntity = [folder_1, folder_2, folder_5] data_center_1.hostFolder = data_center_1_hostfolder dc1_datastoreFolder = mock.Mock(spec=vim.Folder) dc1_datastoreFolder.childEntity = [datastore_1, datastore_2_folder] data_center_1.datastoreFolder = dc1_datastoreFolder rootFolder1 = mock.Mock(spec=vim.Folder) rootFolder1.childEntity = [data_center_1] content.rootFolder = rootFolder1 collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ignore_ssl=True, ) collector.content = content with contextlib.ExitStack() as stack: # We have to disable the LazyObject magic on pyvmomi classes so that we can use them as fakes stack.enter_context(mock.patch.object(vim.Folder, 'name', None)) stack.enter_context(mock.patch.object(vim.Folder, 'childEntity', None)) stack.enter_context(mock.patch.object(vim.ClusterComputeResource, 'name', None)) stack.enter_context(mock.patch.object(vim.ClusterComputeResource, 'host', None)) stack.enter_context(mock.patch.object(vim.Datastore, 'name', None)) stack.enter_context(mock.patch.object(vim.StoragePod, 'childEntity', None)) stack.enter_context(mock.patch.object(vim.StoragePod, 'name', None)) host = yield collector.host_labels ds = yield collector.datastore_labels assert host == { 'host:1': ['host-1', 'dc-1', ''], 'host:2': ['host-2', 'dc-1', 'compute-cluster-1'], 'host:3': ['host-3', 'dc-1', ''], } assert ds == { 'datastore-1': ['datastore-1', 'dc-1', ''], 'datastore-2': ['datastore-2', 'dc-1', 'datastore2-folder'], }
def test_vmware_get_inventory(): content = mock.Mock() # Compute case 1 host_1 = mock.Mock() host_1._moId = 'host:1' host_1.name = 'host-1' folder_1 = mock.Mock() folder_1.host = [host_1] # Computer case 2 host_2 = mock.Mock() host_2._moId = 'host:2' host_2.name = 'host-2' host_2.summary.config.name = 'host-2.' folder_2 = vim.ClusterComputeResource('computer-cluster:1') folder_2.__dict__['name'] = 'compute-cluster-1' folder_2.__dict__['host'] = [host_2] # Datastore case 1 datastore_1 = vim.Datastore('datastore:1') datastore_1.__dict__['name'] = 'datastore-1' # Datastore case 2 datastore_2 = vim.Datastore('datastore:2') datastore_2.__dict__['name'] = 'datastore-2' datastore_2_folder = mock.Mock() datastore_2_folder.childEntity = [datastore_2] datastore_2_folder.name = 'datastore2-folder' data_center_1 = mock.Mock() data_center_1.name = 'dc-1' data_center_1.hostFolder.childEntity = [folder_1, folder_2] data_center_1.datastoreFolder.childEntity = [ datastore_1, datastore_2_folder ] content.rootFolder.childEntity = [data_center_1] collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ignore_ssl=True, ) with contextlib.ExitStack() as stack: # We have to disable the LazyObject magic on pyvmomi classes so that we can use them as fakes stack.enter_context( mock.patch.object(vim.ClusterComputeResource, 'name', None)) stack.enter_context( mock.patch.object(vim.ClusterComputeResource, 'host', None)) stack.enter_context(mock.patch.object(vim.Datastore, 'name', None)) host, ds = collector._vmware_get_inventory(content) assert host == { 'host:1': { 'name': 'host-1', 'dc': 'dc-1', 'cluster': '', }, 'host:2': { 'name': 'host-2', 'dc': 'dc-1', 'cluster': 'compute-cluster-1', } } assert ds == { 'datastore-1': { 'dc': 'dc-1', 'ds_cluster': '', }, 'datastore-2': { 'dc': 'dc-1', 'ds_cluster': 'datastore2-folder', } }
def test_collect_datastore(batch_fetch_properties): content = mock.Mock() batch_fetch_properties.return_value = { 'datastore-1': { 'name': 'datastore-1', 'summary.capacity': 0, 'summary.freeSpace': 0, 'host': ['host-1'], 'vm': ['vm-1'], 'summary.accessible': True, 'summary.maintenanceMode': 'normal', } } collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) inventory = { 'datastore-1': { 'dc': 'dc', 'ds_cluster': 'ds_cluster', } } metrics = collector._create_metric_containers() collector._vmware_get_datastores(content, metrics, inventory) assert _check_properties(batch_fetch_properties.call_args[0][2]) assert metrics['vmware_datastore_capacity_size'].samples[0][1] == { 'ds_name': 'datastore-1', 'dc_name': 'dc', 'ds_cluster': 'ds_cluster' } assert metrics['vmware_datastore_capacity_size'].samples[0][2] == 0.0 assert metrics['vmware_datastore_maintenance_mode'].samples[0][1] == { 'ds_name': 'datastore-1', 'dc_name': 'dc', 'ds_cluster': 'ds_cluster', 'mode': 'normal' } assert metrics['vmware_datastore_maintenance_mode'].samples[0][2] == 1.0 assert metrics['vmware_datastore_accessible'].samples[0][1] == { 'ds_name': 'datastore-1', 'dc_name': 'dc', 'ds_cluster': 'ds_cluster' } assert metrics['vmware_datastore_accessible'].samples[0][2] == 1.0
def test_collect_vm_perf(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) metrics = collector._create_metric_containers() metric_1 = mock.Mock() metric_1.id.counterId = 10 metric_1.value = [9] metric_2 = mock.Mock() metric_2.id.counterId = 1 metric_2.value = [1] ent_1 = mock.Mock() ent_1.value = [metric_1, metric_2] ent_1.entity = vim.ManagedObject('vm:1') content = mock.Mock() content.perfManager.QueryStats.return_value = [ent_1] collector.content = _succeed(content) collector.__dict__['counter_ids'] = _succeed({ 'cpu.ready.summation': 1, 'cpu.maxlimited.summation': 2, 'cpu.usage.average': 3, 'cpu.usagemhz.average': 4, 'disk.usage.average': 5, 'disk.read.average': 6, 'disk.write.average': 7, 'mem.usage.average': 8, 'net.received.average': 9, 'net.transmitted.average': 10, }) collector.__dict__['vm_labels'] = _succeed({ 'vm:1': ['vm-1', 'host-1', 'dc', 'cluster-1'], }) collector.__dict__['vm_inventory'] = _succeed({ 'vm:1': { 'name': 'vm-1', 'obj': vim.ManagedObject('vm-1'), 'runtime.powerState': 'poweredOn', }, 'vm:2': { 'name': 'vm-2', 'obj': vim.ManagedObject('vm-2'), 'runtime.powerState': 'poweredOff', }, }) yield collector._vmware_get_vm_perf_manager_metrics(metrics) # General VM metrics assert metrics['vmware_vm_net_transmitted_average'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_net_transmitted_average'].samples[0][2] == 9.0
def test_collect_hosts(): boot_time = EPOCH + datetime.timedelta(seconds=60) collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) collector.content = _succeed(mock.Mock()) collector.__dict__['host_labels'] = _succeed({ 'host:1': ['host-1', 'dc', 'cluster'], 'host:2': ['host-1', 'dc', 'cluster'], }) metrics = collector._create_metric_containers() with mock.patch.object(collector, 'batch_fetch_properties') as batch_fetch_properties: batch_fetch_properties.return_value = _succeed({ 'host:1': { 'id': 'host:1', 'name': 'host-1', 'runtime.powerState': 'poweredOn', 'runtime.bootTime': boot_time, 'runtime.connectionState': 'connected', 'runtime.inMaintenanceMode': True, 'summary.quickStats.overallCpuUsage': 100, 'summary.hardware.numCpuCores': 12, 'summary.hardware.cpuMhz': 1000, 'summary.quickStats.overallMemoryUsage': 1024, 'summary.hardware.memorySize': 2048 * 1024 * 1024, 'summary.config.product.version': '6.0.0', 'summary.config.product.build': '6765062', }, 'host:2': { 'id': 'host:2', 'name': 'host-2', 'runtime.powerState': 'poweredOff', } }) yield collector._vmware_get_hosts(metrics) assert _check_properties(batch_fetch_properties.call_args[0][1]) assert metrics['vmware_host_memory_max'].samples[0][1] == { 'host_name': 'host-1', 'dc_name': 'dc', 'cluster_name': 'cluster' } assert metrics['vmware_host_memory_max'].samples[0][2] == 2048 assert metrics['vmware_host_num_cpu'].samples[0][2] == 12 assert metrics['vmware_host_product_info'].samples[0][1] == { 'host_name': 'host-1', 'dc_name': 'dc', 'cluster_name': 'cluster', 'version': '6.0.0', 'build': '6765062', } assert metrics['vmware_host_product_info'].samples[0][2] == 1 # In our test data we hava a host that is powered down - we should have its # power_state metric but not any others. assert len(metrics['vmware_host_power_state'].samples) == 2 assert len(metrics['vmware_host_memory_max'].samples) == 1
def test_collect_vms(): boot_time = EPOCH + datetime.timedelta(seconds=60) snapshot_1 = mock.Mock() snapshot_1.createTime = EPOCH + datetime.timedelta(seconds=60) snapshot_1.name = 'snapshot_1' snapshot_1.childSnapshotList = [] snapshot_2 = mock.Mock() snapshot_2.createTime = EPOCH + datetime.timedelta(seconds=120) snapshot_2.name = 'snapshot_2' snapshot_2.childSnapshotList = [snapshot_1] snapshot = mock.Mock() snapshot.rootSnapshotList = [snapshot_2] disk = mock.Mock() disk.diskPath = '/boot' disk.capacity = 100 disk.freeSpace = 50 collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } # Test runtime.host not found collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) collector.content = _succeed(mock.Mock()) collector.__dict__['host_labels'] = _succeed({'': []}) with mock.patch.object(collector, 'batch_fetch_properties') as batch_fetch_properties: batch_fetch_properties.return_value = _succeed({ 'vm-1': { 'name': 'vm-1', 'runtime.host': vim.ManagedObject('notfound:1'), 'runtime.powerState': 'poweredOn', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, 'runtime.bootTime': boot_time, 'snapshot': snapshot, 'guest.disk': [disk], 'guest.toolsStatus': 'toolsOk', 'guest.toolsVersion': '10336', 'guest.toolsVersionStatus2': 'guestToolsUnmanaged', } }) assert collector.vm_labels.result == {'vm-1': ['vm-1']} # Reset variables collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) collector.content = _succeed(mock.Mock()) collector.__dict__['host_labels'] = _succeed({ 'host-1': ['host-1', 'dc', 'cluster-1'], }) metrics = collector._create_metric_containers() with mock.patch.object(collector, 'batch_fetch_properties') as batch_fetch_properties: batch_fetch_properties.return_value = _succeed({ 'vm-1': { 'name': 'vm-1', 'runtime.host': vim.ManagedObject('host-1'), 'runtime.powerState': 'poweredOn', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, 'runtime.bootTime': boot_time, 'snapshot': snapshot, 'guest.disk': [disk], 'guest.toolsStatus': 'toolsOk', 'guest.toolsVersion': '10336', 'guest.toolsVersionStatus2': 'guestToolsUnmanaged', }, 'vm-2': { 'name': 'vm-2', 'runtime.powerState': 'poweredOff', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, 'runtime.bootTime': boot_time, 'snapshot': snapshot, 'guest.disk': [disk], 'guest.toolsStatus': 'toolsOk', 'guest.toolsVersion': '10336', 'guest.toolsVersionStatus2': 'guestToolsUnmanaged', }, 'vm-3': { 'name': 'vm-3', 'runtime.host': vim.ManagedObject('host-1'), 'runtime.powerState': 'poweredOff', 'summary.config.numCpu': 1, 'summary.config.memorySizeMB': 1024, 'runtime.bootTime': boot_time, 'snapshot': snapshot, 'guest.disk': [disk], 'guest.toolsStatus': 'toolsOk', 'guest.toolsVersion': '10336', 'guest.toolsVersionStatus2': 'guestToolsUnmanaged', }, }) yield collector._vmware_get_vms(metrics) assert _check_properties(batch_fetch_properties.call_args[0][1]) assert collector.vm_labels.result == { 'vm-1': ['vm-1', 'host-1', 'dc', 'cluster-1'], 'vm-2': ['vm-2'], 'vm-3': ['vm-3', 'host-1', 'dc', 'cluster-1'], } # Assert that vm-3 skipped #69/#70 assert metrics['vmware_vm_power_state'].samples[1][1] == { 'vm_name': 'vm-3', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } # General VM metrics assert metrics['vmware_vm_power_state'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_power_state'].samples[0][2] == 1.0 assert metrics['vmware_vm_boot_timestamp_seconds'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_boot_timestamp_seconds'].samples[0][2] == 60 # Disk info (vmguest) assert metrics['vmware_vm_guest_disk_capacity'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'partition': '/boot', } assert metrics['vmware_vm_guest_disk_capacity'].samples[0][2] == 100 # VM tools info (vmguest) assert metrics['vmware_vm_guest_tools_running_status'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'tools_status': 'toolsOk', } assert metrics['vmware_vm_guest_tools_running_status'].samples[0][2] == 1.0 assert metrics['vmware_vm_guest_tools_version'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'tools_version': '10336', } assert metrics['vmware_vm_guest_tools_version'].samples[0][2] == 1.0 assert metrics['vmware_vm_guest_tools_version_status'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'tools_version_status': 'guestToolsUnmanaged', } assert metrics['vmware_vm_guest_tools_version_status'].samples[0][2] == 1.0 # Snapshots assert metrics['vmware_vm_snapshots'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_snapshots'].samples[0][2] == 2 assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'vm_snapshot_name': 'snapshot_2', } assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[0][2] == 120 assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[1][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'vm_snapshot_name': 'snapshot_1', } assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[1][2] == 60 # Max Memory assert metrics['vmware_vm_memory_max'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_memory_max'].samples[0][2] == 1024
def test_no_error_onempty_vms(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': False, 'hosts': False, 'snapshots': False, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, 5000, ignore_ssl=True, ) metrics = collector._create_metric_containers() metric_1 = mock.Mock() metric_1.id.counterId = 10 metric_1.value = [9] metric_2 = mock.Mock() metric_2.id.counterId = 1 metric_2.value = [1] metric_3 = mock.Mock() metric_3.id.counterId = 13 metric_3.value = [618] metric_4 = mock.Mock() metric_4.id.counterId = 18 metric_4.value = [5] ent_1 = mock.Mock() ent_1.value = [metric_1, metric_2, metric_3, metric_4] ent_1.entity = vim.ManagedObject('vm:1') content = mock.Mock() content.perfManager.QueryStats.return_value = [ent_1] collector.content = _succeed(content) collector.__dict__['counter_ids'] = _succeed({ 'cpu.ready.summation': 1, 'cpu.maxlimited.summation': 2, 'cpu.usage.average': 3, 'cpu.usagemhz.average': 4, 'disk.usage.average': 5, 'disk.read.average': 6, 'disk.write.average': 7, 'mem.usage.average': 8, 'net.received.average': 9, 'net.transmitted.average': 10, 'cpu.costop.summation': 11, 'cpu.idle.summation': 12, 'cpu.demand.average': 13, 'mem.consumed.average': 14, 'mem.active.average': 15, 'mem.swapped.average': 16, 'mem.vmmemctl.average': 17, 'disk.maxTotalLatency.latest': 18, 'net.multicastRx.summation': 19, 'net.multicastTx.summation': 20, 'net.broadcastTx.summation': 21, 'net.broadcastRx.summation': 22, 'net.droppedRx.summation': 23, 'net.droppedTx.summation': 24, }) collector.__dict__['vm_labels'] = _succeed({'': []}) collector.__dict__['vm_inventory'] = _succeed({'': {}}) # Try to test for querySpec=[] # threads.deferToThread(content.perfManager.QueryStats, querySpec=specs), # TypeError Required field "querySpec" not provided (not @optional) yield collector._vmware_get_vm_perf_manager_metrics(metrics) assert metrics['vmware_vm_power_state'].samples == []
def test_collect_vms(batch_fetch_properties): content = mock.Mock() boot_time = EPOCH + datetime.timedelta(seconds=60) snapshot_1 = mock.Mock() snapshot_1.createTime = EPOCH + datetime.timedelta(seconds=60) snapshot_1.name = 'snapshot_1' snapshot_1.childSnapshotList = [] snapshot_2 = mock.Mock() snapshot_2.createTime = EPOCH + datetime.timedelta(seconds=120) snapshot_2.name = 'snapshot_2' snapshot_2.childSnapshotList = [snapshot_1] snapshot = mock.Mock() snapshot.rootSnapshotList = [snapshot_2] disk = mock.Mock() disk.diskPath = '/boot' disk.capacity = 100 disk.freeSpace = 50 batch_fetch_properties.return_value = { 'vm-1': { 'name': 'vm-1', 'runtime.host': vim.ManagedObject('host-1'), 'runtime.powerState': 'poweredOn', 'summary.config.numCpu': 1, 'runtime.bootTime': boot_time, 'snapshot': snapshot, 'guest.disk': [disk], } } collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, ) inventory = { 'host-1': { 'name': 'host-1', 'dc': 'dc', 'cluster': 'cluster-1', } } metrics = collector._create_metric_containers() collector._labels = {} with mock.patch.object(collector, '_vmware_get_vm_perf_manager_metrics'): yield collector._vmware_get_vms(content, metrics, inventory) assert _check_properties(batch_fetch_properties.call_args[0][2]) # General VM metrics assert metrics['vmware_vm_power_state'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_power_state'].samples[0][2] == 1.0 assert metrics['vmware_vm_boot_timestamp_seconds'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_boot_timestamp_seconds'].samples[0][2] == 60 # Disk info (vmguest) assert metrics['vmware_vm_guest_disk_capacity'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'partition': '/boot', } assert metrics['vmware_vm_guest_disk_capacity'].samples[0][2] == 100 # Snapshots assert metrics['vmware_vm_snapshots'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_snapshots'].samples[0][2] == 2 assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'vm_snapshot_name': 'snapshot_2', } assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[0][2] == 120 assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[1][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', 'vm_snapshot_name': 'snapshot_1', } assert metrics['vmware_vm_snapshot_timestamp_seconds'].samples[1][2] == 60
def test_collect_host_perf(): collect_only = { 'vms': False, 'vmguests': False, 'datastores': False, 'hosts': True, 'snapshots': False, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, 5000, ) metrics = collector._create_metric_containers() metric_1 = mock.Mock() metric_1.id.counterId = 2 metric_1.value = [3600] metric_2 = mock.Mock() metric_2.id.counterId = 6 metric_2.value = [3700] metric_3 = mock.Mock() metric_3.id.counterId = 17 metric_3.value = [1024] metric_4 = mock.Mock() metric_4.id.counterId = 20 metric_4.value = [10] ent_1 = mock.Mock() ent_1.value = [metric_1, metric_2, metric_3, metric_4] ent_1.entity = vim.ManagedObject('host:1') content = mock.Mock() content.perfManager.QueryStats.return_value = [ent_1] collector.content = _succeed(content) collector.__dict__['counter_ids'] = _succeed({ 'cpu.costop.summation': 1, 'cpu.demand.average': 2, 'cpu.idle.summation': 3, 'cpu.ready.summation': 4, 'cpu.swapwait.summation': 5, 'cpu.usage.average': 6, 'cpu.usagemhz.average': 7, 'cpu.used.summation': 8, 'disk.read.average': 9, 'disk.write.average': 10, 'mem.active.average': 11, 'mem.latency.average': 12, 'mem.swapin.average': 13, 'mem.swapinRate.average': 14, 'mem.swapout.average': 15, 'mem.swapoutRate.average': 16, 'mem.vmmemctl.average': 17, 'net.bytesRx.average': 18, 'net.bytesTx.average': 19, 'net.droppedRx.summation': 20, 'net.droppedTx.summation': 21, 'net.errorsRx.summation': 22, 'net.errorsTx.summation': 23, 'net.usage.average': 24, }) collector.__dict__['host_labels'] = _succeed({ 'host:1': ['host-1', 'dc', 'cluster-1'], }) collector.__dict__['host_system_inventory'] = _succeed({ 'host:1': { 'name': 'host-1', 'obj': vim.ManagedObject('host-1'), 'runtime.powerState': 'poweredOn', }, }) yield collector._vmware_get_host_perf_manager_metrics(metrics) # General Host metrics assert metrics['vmware_host_cpu_demand_average'].samples[0][1] == { 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_host_cpu_demand_average'].samples[0][2] == 3600.0 assert metrics['vmware_host_cpu_usage_average'].samples[0][1] == { 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_host_cpu_usage_average'].samples[0][2] == 3700.0 assert metrics['vmware_host_mem_vmmemctl_average'].samples[0][1] == { 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_host_mem_vmmemctl_average'].samples[0][2] == 1024.0 assert metrics['vmware_host_net_droppedRx_summation'].samples[0][1] == { 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_host_net_droppedRx_summation'].samples[0][2] == 10.0
def test_collect_vm_perf(): collect_only = { 'vms': True, 'vmguests': True, 'datastores': True, 'hosts': True, 'snapshots': True, } collector = VmwareCollector( '127.0.0.1', 'root', 'password', collect_only, 5000, ) metrics = collector._create_metric_containers() metric_1 = mock.Mock() metric_1.id.counterId = 10 metric_1.value = [9] metric_2 = mock.Mock() metric_2.id.counterId = 1 metric_2.value = [1] metric_3 = mock.Mock() metric_3.id.counterId = 13 metric_3.value = [618] metric_4 = mock.Mock() metric_4.id.counterId = 18 metric_4.value = [5] ent_1 = mock.Mock() ent_1.value = [metric_1, metric_2, metric_3, metric_4] ent_1.entity = vim.ManagedObject('vm:1') content = mock.Mock() content.perfManager.QueryStats.return_value = [ent_1] collector.content = _succeed(content) collector.__dict__['counter_ids'] = _succeed({ 'cpu.ready.summation': 1, 'cpu.maxlimited.summation': 2, 'cpu.usage.average': 3, 'cpu.usagemhz.average': 4, 'disk.usage.average': 5, 'disk.read.average': 6, 'disk.write.average': 7, 'mem.usage.average': 8, 'net.received.average': 9, 'net.transmitted.average': 10, 'cpu.costop.summation': 11, 'cpu.idle.summation': 12, 'cpu.demand.average': 13, 'mem.consumed.average': 14, 'mem.active.average': 15, 'mem.swapped.average': 16, 'mem.vmmemctl.average': 17, 'disk.maxTotalLatency.latest': 18, 'net.multicastRx.summation': 19, 'net.multicastTx.summation': 20, 'net.broadcastTx.summation': 21, 'net.broadcastRx.summation': 22, 'net.droppedRx.summation': 23, 'net.droppedTx.summation': 24, }) collector.__dict__['vm_labels'] = _succeed({ 'vm:1': ['vm-1', 'host-1', 'dc', 'cluster-1'], }) collector.__dict__['vm_inventory'] = _succeed({ 'vm:1': { 'name': 'vm-1', 'obj': vim.ManagedObject('vm-1'), 'runtime.powerState': 'poweredOn', }, 'vm:2': { 'name': 'vm-2', 'obj': vim.ManagedObject('vm-2'), 'runtime.powerState': 'poweredOff', }, }) yield collector._vmware_get_vm_perf_manager_metrics(metrics) # General VM metrics assert metrics['vmware_vm_net_transmitted_average'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_net_transmitted_average'].samples[0][2] == 9.0 assert metrics['vmware_vm_cpu_demand_average'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_cpu_demand_average'].samples[0][2] == 618.0 assert metrics['vmware_vm_disk_maxTotalLatency_latest'].samples[0][1] == { 'vm_name': 'vm-1', 'host_name': 'host-1', 'cluster_name': 'cluster-1', 'dc_name': 'dc', } assert metrics['vmware_vm_disk_maxTotalLatency_latest'].samples[0][ 2] == 5.0