bundlewrap/bundles/vmhost/files/check_vm_status

53 lines
1.1 KiB
Text
Raw Permalink Normal View History

#!/usr/bin/env python3
from subprocess import check_output
from sys import exit
try:
result = check_output(['virsh', '--readonly', 'list', '--all']).decode('utf-8').splitlines()
except Exception as e:
# If something fails, this means libvirt is not responding. This is
# an error.
print(repr(e))
exit(2)
running = set()
stopped = set()
crashed = set()
for vm in result[2:]:
if vm.strip() == '':
continue
info = vm.split()
if info[1].startswith('EXCLUDE_FROM_MONITORING_'):
continue
if info[2] in {'crashed', 'dying'}:
crashed.add('{}: {}'.format(info[1], info[2]))
elif info[2] in {'running'}:
running.add('{}: {}'.format(info[1], info[2]))
else:
stopped.add('{}: {}'.format(info[1], info[2]))
print('{} running, {} stopped, {} crashed'.format(len(running), len(stopped), len(crashed)))
for vm in sorted(crashed):
print(vm)
for vm in sorted(stopped):
print(vm)
for vm in sorted(running):
print(vm)
if len(crashed) > 0:
exit(2)
elif len(stopped) > 0:
exit(1)
else:
exit(0)