Browse Source

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
Major Hayden 2 years ago
parent
commit
928b34243a
No account linked to committer's email address
2 changed files with 19 additions and 6 deletions
  1. 13
    5
      monitorstack/plugins/kvm.py
  2. 6
    1
      tests/test_plugin_kvm.py

+ 13
- 5
monitorstack/plugins/kvm.py View File

@@ -12,8 +12,6 @@
12 12
 # See the License for the specific language governing permissions and
13 13
 # limitations under the License.
14 14
 """Get metrics from a KVM hypervisor."""
15
-
16
-import platform
17 15
 import socket
18 16
 
19 17
 import click
@@ -43,23 +41,33 @@ def cli(ctx):
43 41
     output = {
44 42
         'measurement_name': 'kvm',
45 43
         'meta': {
46
-            'platform': platform.platform(),
47 44
             'kvm_host_id': abs(hash(socket.getfqdn()))
48 45
         }
49 46
     }
50
-    conn = libvirt.openReadOnly()
47
+
48
+    # Open a read-only connection to libvirt
49
+    conn = libvirt.openReadOnly("qemu:///system")
50
+
51 51
     try:
52
-        variables = output['variables'] = dict()
52
+        variables = dict()
53
+
54
+        # Get all of the KVM instances on this host.
53 55
         domains = conn.listDomainsID()
54 56
         variables['kvm_vms'] = len(domains)
55 57
         variables['kvm_total_vcpus'] = conn.getCPUMap()[0]
56 58
         variables['kvm_scheduled_vcpus'] = 0
59
+
60
+        # Loop through each instance to gather additional data.
57 61
         for domain in domains:
58 62
             variables['kvm_scheduled_vcpus'] += conn.lookupByID(
59 63
                 domain
60 64
             ).maxVcpus()
61 65
 
66
+        # Return the data.
67
+        output['variables'] = variables
68
+
62 69
     except Exception as exp:
70
+        # We may have picked up an exception while querying libvirt for data.
63 71
         output['exit_code'] = 1
64 72
         output['message'] = '{} failed -- {}'.format(
65 73
             COMMAND_NAME,

+ 6
- 1
tests/test_plugin_kvm.py View File

@@ -39,6 +39,9 @@ class LibvirtStub(object):
39 39
     class openReadOnly(object):  # noqa
40 40
         """Stubbed openReadOnly class."""
41 41
 
42
+        def __init__(self, connection_uri):  # noqa
43
+            pass
44
+
42 45
         def close(self, *args, **kwargs):  # noqa
43 46
             pass
44 47
 
@@ -63,6 +66,9 @@ class LibvirtStubFailed(object):
63 66
     class openReadOnly(object):  # noqa
64 67
         """Stubbed openReadOnly class."""
65 68
 
69
+        def __init__(self, connection_uri):  # noqa
70
+            pass
71
+
66 72
         def close(self, *args, **kwargs):  # noqa
67 73
             pass
68 74
 
@@ -94,7 +100,6 @@ class TestKvm(unittest.TestCase):
94 100
         assert variables['kvm_total_vcpus'] == 1
95 101
         assert 'kvm_scheduled_vcpus' in variables
96 102
         assert variables['kvm_scheduled_vcpus'] == 6
97
-        assert 'platform' in meta
98 103
         assert 'kvm_host_id' in meta
99 104
         assert result['exit_code'] == 0
100 105
 

Loading…
Cancel
Save