Added KVM Metric plugin (#2)

This plugin will pull metrics and meta-data from a KVM host.

Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
This commit is contained in:
Kevin Carter 2017-03-01 13:22:10 -06:00 committed by Major Hayden
parent 5aca2d2940
commit 52e06b501b
4 changed files with 140 additions and 1 deletions

View File

@ -1,2 +1,2 @@
Kevin Carter <kevin.carter@rackspace.com>
Kevin Carter <kevin@cloudnull.com>
Major Hayden <major@mhtx.net>

View File

@ -1,6 +1,7 @@
CHANGES
=======
* Added KVM Metric plugin
* Couple of updates: telegraf line protocol, dynamic imports, metadata
* Proof of concept
* Initial commit

View File

@ -0,0 +1,67 @@
#!/usr/bin/env python
# Copyright 2017, Kevin Carter <kevin@cloudnull.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import platform
import socket
import click
from monitorstack.cli import pass_context
DOC = """Get metrics from a KVM hypervisor"""
@click.command('kvm', short_help=DOC.split('\n')[0])
@pass_context
def cli(ctx):
setattr(cli, '__doc__', DOC)
# Lower level import because we only want to load this module
# when this plugin is called.
try:
import libvirt
except ImportError:
raise SystemExit('The "kvm plugin requires libvirt-python to be'
' installed".')
output = {
'measurement_name': 'kvm',
'meta': {
'platform': platform.platform(),
'kvm_host_id': abs(hash(socket.getfqdn()))
}
}
conn = libvirt.openReadOnly()
try:
variables = output['variables'] = dict()
domains = conn.listDomainsID()
variables['kvm_vms'] = len(domains)
variables['kvm_total_vcpus'] = conn.getCPUMap()[0]
variables['kvm_scheduled_vcpus'] = 0
for domain in domains:
variables['kvm_scheduled_vcpus'] += conn.lookupByID(
domain
).maxVcpus()
except Exception as exp:
output['exit_code'] = 1
output['message'] = 'kvm failed -- Error: {}'.format(exp)
else:
output['exit_code'] = 0
output['message'] = 'kvm is ok'
finally:
conn.close()
return output

71
tests/test_kvm.py Normal file
View File

@ -0,0 +1,71 @@
# Copyright 2017, Kevin Carter <kevin@cloudnull.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the base class."""
import json
import sys
from click.testing import CliRunner
from monitorstack.cli import cli
class LibvirtStub(object):
"""Stubbed libvirt class."""
class openReadOnly(object): # noqa
"""Stubbed openReadOnly class."""
def close(self, *args, **kwargs): # noqa
pass
def listDomainsID(self, *args, **kwargs): # noqa
return ['a', 'b', 'c']
def getCPUMap(self, *args, **kwargs): # noqa
return [1, 1, 1]
class lookupByID(object): # noqa
"""Stubbed lookupByID class."""
def __init__(self, *args, **kwargs):
pass
def maxVcpus(self): # noqa
return 2
class TestKvm(object):
"""Tests for the uptime monitor class."""
def test_run(self):
"""Ensure the run() method works."""
sys.modules['libvirt'] = LibvirtStub
runner = CliRunner()
result = runner.invoke(cli, ['-f', 'json', 'kvm'])
result_json = json.loads(result.output)
variables = result_json['variables']
meta = result_json['meta']
assert 'kvm_vms' in variables
assert variables['kvm_vms'] == 3
assert 'kvm_total_vcpus' in variables
assert variables['kvm_total_vcpus'] == 1
assert 'kvm_scheduled_vcpus' in variables
assert variables['kvm_scheduled_vcpus'] == 6
assert 'platform' in meta
assert 'kvm_host_id' in meta
assert result.exit_code == 0