Skip to content

Commit

Permalink
feat(adding_host_perf): add collect host perf counter and tests (#150)
Browse files Browse the repository at this point in the history
* add collect host perf counter and tests

* Fix flake8 blank line

* Fix flake8 blank line

* fix whitespace

* fix blank lines
  • Loading branch information
alecorps authored and pryorda committed Oct 9, 2019
1 parent 994a75d commit 3c51754
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
116 changes: 116 additions & 0 deletions tests/unit/test_vmware_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,119 @@ def test_collect_hosts():
assert len(metrics['vmware_host_memory_max'].samples) == 1


@pytest_twisted.inlineCallbacks
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,
)

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


@pytest_twisted.inlineCallbacks
def test_collect_datastore():
collect_only = {
Expand Down Expand Up @@ -620,6 +733,9 @@ def test_collect():
).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_get_host_perf_manager_metrics')
).return_value = _succeed(True)
stack.enter_context(mock.patch.object(collector, '_vmware_disconnect')).return_value = _succeed(True)
metrics = yield collector.collect()

Expand Down
81 changes: 81 additions & 0 deletions vmware_exporter/vmware_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,87 @@ def _vmware_get_vm_perf_manager_metrics(self, vm_metrics):

logging.info('FIN: _vmware_get_vm_perf_manager_metrics')

@defer.inlineCallbacks
def _vmware_get_host_perf_manager_metrics(self, host_metrics):
logging.info('START: _vmware_get_host_perf_manager_metrics')

host_systems, counter_info = yield parallelize(self.host_system_inventory, self.counter_ids)

# List of performance counter we want
perf_list = [
'cpu.costop.summation',
'cpu.demand.average',
'cpu.idle.summation',
'cpu.ready.summation',
'cpu.swapwait.summation',
'cpu.usage.average',
'cpu.usagemhz.average',
'cpu.used.summation',
'disk.read.average',
'disk.write.average',
'mem.active.average',
'mem.latency.average',
'mem.swapin.average',
'mem.swapinRate.average',
'mem.swapout.average',
'mem.swapoutRate.average',
'mem.vmmemctl.average',
'net.bytesRx.average',
'net.bytesTx.average',
'net.droppedRx.summation',
'net.droppedTx.summation',
'net.errorsRx.summation',
'net.errorsTx.summation',
'net.usage.average',
]

# Prepare gauges
for p in perf_list:
p_metric = 'vmware_host_' + p.replace('.', '_')
host_metrics[p_metric] = GaugeMetricFamily(
p_metric,
p_metric,
labels=['host_name', 'dc_name', 'cluster_name'])

metrics = []
metric_names = {}
for perf_metric in perf_list:
perf_metric_name = 'vmware_host_' + perf_metric.replace('.', '_')
counter_key = counter_info[perf_metric]
metrics.append(vim.PerformanceManager.MetricId(
counterId=counter_key,
instance=''
))
metric_names[counter_key] = perf_metric_name

specs = []
for host in host_systems.values():
if host.get('runtime.powerState') != 'poweredOn':
continue
specs.append(vim.PerformanceManager.QuerySpec(
maxSample=1,
entity=host['obj'],
metricId=metrics,
intervalId=20
))

content = yield self.content

if len(specs) > 0:
results, labels = yield parallelize(
threads.deferToThread(content.perfManager.QueryStats, querySpec=specs),
self.host_labels,
)

for ent in results:
for metric in ent.value:
host_metrics[metric_names[metric.id.counterId]].add_metric(
labels[ent.entity._moId],
float(sum(metric.value)),
)

logging.info('FIN: _vmware_get_host_perf_manager_metrics')

@defer.inlineCallbacks
def _vmware_get_vms(self, metrics):
"""
Expand Down

0 comments on commit 3c51754

Please sign in to comment.