Skip to content

Commit

Permalink
kvm-vm: Improve output
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed Mar 29, 2024
1 parent d870e93 commit 17e5684
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Monitoring Plugins:
* infomaniak-swiss-backup-products: Improve output
* jitsi-\*: Add new parameters `--insecure` `--no-proxy`
* journald-query: Remove hard-coded `--boot` parameter from query
* kvm-vm: Improve output
* librenms-version: Fetches info from local SQLite using new librenms library
* logfile: Add new parameters `--insecure` `--no-proxy` `--timeout`
* metabase-stats: Add new parameters `--insecure` `--no-proxy` `--timeout`
Expand Down
12 changes: 10 additions & 2 deletions check-plugins/kvm-vm/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ Usage Examples
.. code-block:: bash
./kvm-vm
Output:

.. code-block:: text
VMs: 1 running, 15 shut_off
VMs: 5 running, 1 shutoff
ID ! VM Name ! State
---+-------------+---------
2 ! nextcloud ! running
9 ! mon02 ! running
10 ! infra02 ! running
11 ! mon01 ! shutoff
13 ! mailstore01 ! running
States
Expand Down
48 changes: 36 additions & 12 deletions check-plugins/kvm-vm/kvm-vm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
STATE_UNKNOWN, STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2023112901'
__version__ = '2024032901'

DESCRIPTION = 'Check VMs on a KVM host using "virsh list".'

Expand Down Expand Up @@ -58,7 +58,9 @@ def main():
sys.exit(STATE_UNKNOWN)

# execute the shell command and return its result and exit code
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec('virsh list --all'))
stdout, stderr, retc = lib.base.coe(
lib.shell.shell_exec('virsh list --all'),
)
if stderr or retc != 0:
lib.base.oao('{}'.format(stderr), STATE_WARN)
# we got a VM list and their states - strip first two header and last two empty rows
Expand All @@ -67,13 +69,19 @@ def main():
# init some vars
perfdata = ''
state = STATE_OK
table_data = []

# count the VM states
running, idle, paused, in_shutdown, shut_off, crashed, pmsuspended = 0, 0, 0, 0, 0, 0, 0
running, idle, paused, in_shutdown, shut_off, crashed, pmsuspended = 0, 0, 0, 0, 0, 0, 0 # pylint: disable=C0301
if vm_list:
for vm in vm_list:
vm = ' '.join(vm.replace('shut off', 'shut_off').replace('in shutdown', 'in_shutdown').split())
vm = ' '.join(vm.replace('shut off', 'shut_off').replace('in shutdown', 'in_shutdown').split()) # pylint: disable=C0301
vm_id, vm_name, vm_state = vm.split(' ')
table_data.append({
'vm_id': vm_id,
'vm_name': vm_name,
'vm_state': vm_state,
})

running += 1 if vm_state == 'running' else 0
idle += 1 if vm_state == 'idle' else 0
Expand Down Expand Up @@ -107,14 +115,30 @@ def main():
msg = msg[:-2]
else:
msg = 'No VMs running.'

perfdata += lib.base.get_perfdata('vm_running', running, None, None, None, 0, None)
perfdata += lib.base.get_perfdata('vm_idle', idle, None, None, None, 0, None)
perfdata += lib.base.get_perfdata('vm_paused', paused, None, None, None, 0, None)
perfdata += lib.base.get_perfdata('vm_in_shutdown', in_shutdown, None, None, None, 0, None)
perfdata += lib.base.get_perfdata('vm_shut_off', shut_off, None, None, None, 0, None)
perfdata += lib.base.get_perfdata('vm_crashed', crashed, None, None, None, 0, None)
perfdata += lib.base.get_perfdata('vm_pmsuspended', pmsuspended, None, None, None, 0, None)
msg += '\n\n'

if table_data:
msg += lib.base.get_table(
table_data,
[
'vm_id',
'vm_name',
'vm_state',
],
header=[
'ID',
'VM Name',
'State',
]
)

perfdata += lib.base.get_perfdata('vm_running', running, None, None, None, 0, None) # pylint: disable=C0301
perfdata += lib.base.get_perfdata('vm_idle', idle, None, None, None, 0, None) # pylint: disable=C0301
perfdata += lib.base.get_perfdata('vm_paused', paused, None, None, None, 0, None) # pylint: disable=C0301
perfdata += lib.base.get_perfdata('vm_in_shutdown', in_shutdown, None, None, None, 0, None) # pylint: disable=C0301
perfdata += lib.base.get_perfdata('vm_shut_off', shut_off, None, None, None, 0, None) # pylint: disable=C0301
perfdata += lib.base.get_perfdata('vm_crashed', crashed, None, None, None, 0, None) # pylint: disable=C0301
perfdata += lib.base.get_perfdata('vm_pmsuspended', pmsuspended, None, None, None, 0, None) # pylint: disable=C0301

# over and out
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
Expand Down

0 comments on commit 17e5684

Please sign in to comment.