Fix variable return bugs in kvm plugin
This patch ensures that monitorstack connects to the system URI for libvirt. Without this, a regular user may connect to libvirt without full access to the existing virtual machines. It also adds the gathered variables to the output dictionatry so they are returned when the plugin runs. Tests have been adjusted to match the new changes, including a new constructor that takes the system connection URI into consideration. Closes-Bug: 1684235 Change-Id: Ieacc69ee495268b3b20974eb2f01ff1d2c9195c1
This commit is contained in:
parent
95fb4b1983
commit
928b34243a
|
@ -12,8 +12,6 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Get metrics from a KVM hypervisor."""
|
||||
|
||||
import platform
|
||||
import socket
|
||||
|
||||
import click
|
||||
|
@ -43,23 +41,33 @@ def cli(ctx):
|
|||
output = {
|
||||
'measurement_name': 'kvm',
|
||||
'meta': {
|
||||
'platform': platform.platform(),
|
||||
'kvm_host_id': abs(hash(socket.getfqdn()))
|
||||
}
|
||||
}
|
||||
conn = libvirt.openReadOnly()
|
||||
|
||||
# Open a read-only connection to libvirt
|
||||
conn = libvirt.openReadOnly("qemu:///system")
|
||||
|
||||
try:
|
||||
variables = output['variables'] = dict()
|
||||
variables = dict()
|
||||
|
||||
# Get all of the KVM instances on this host.
|
||||
domains = conn.listDomainsID()
|
||||
variables['kvm_vms'] = len(domains)
|
||||
variables['kvm_total_vcpus'] = conn.getCPUMap()[0]
|
||||
variables['kvm_scheduled_vcpus'] = 0
|
||||
|
||||
# Loop through each instance to gather additional data.
|
||||
for domain in domains:
|
||||
variables['kvm_scheduled_vcpus'] += conn.lookupByID(
|
||||
domain
|
||||
).maxVcpus()
|
||||
|
||||
# Return the data.
|
||||
output['variables'] = variables
|
||||
|
||||
except Exception as exp:
|
||||
# We may have picked up an exception while querying libvirt for data.
|
||||
output['exit_code'] = 1
|
||||
output['message'] = '{} failed -- {}'.format(
|
||||
COMMAND_NAME,
|
||||
|
|
|
@ -39,6 +39,9 @@ class LibvirtStub(object):
|
|||
class openReadOnly(object): # noqa
|
||||
"""Stubbed openReadOnly class."""
|
||||
|
||||
def __init__(self, connection_uri): # noqa
|
||||
pass
|
||||
|
||||
def close(self, *args, **kwargs): # noqa
|
||||
pass
|
||||
|
||||
|
@ -63,6 +66,9 @@ class LibvirtStubFailed(object):
|
|||
class openReadOnly(object): # noqa
|
||||
"""Stubbed openReadOnly class."""
|
||||
|
||||
def __init__(self, connection_uri): # noqa
|
||||
pass
|
||||
|
||||
def close(self, *args, **kwargs): # noqa
|
||||
pass
|
||||
|
||||
|
@ -94,7 +100,6 @@ class TestKvm(unittest.TestCase):
|
|||
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue