Example #1
0
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
Example #2
0
def test_collect_vm_perf():
    content = mock.Mock()

    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 = {'vm:1': ['vm-1', 'host-1', 'dc', 'cluster-1']}

    vms = {
        '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',
        }
    }

    metric_1 = mock.Mock()
    metric_1.id.counterId = 9
    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.perfManager.QueryStats.return_value = [ent_1]

    with mock.patch.object(collector,
                           '_vmware_perf_metrics') as _vmware_perf_metrics:
        _vmware_perf_metrics.return_value = {
            'cpu.ready.summation': 1,
            'cpu.usage.average': 2,
            'cpu.usagemhz.average': 3,
            'disk.usage.average': 4,
            'disk.read.average': 5,
            'disk.write.average': 6,
            'mem.usage.average': 7,
            'net.received.average': 8,
            'net.transmitted.average': 9,
        }

        yield collector._vmware_get_vm_perf_manager_metrics(
            content, vms, metrics, inventory)

    # 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