bundlewrap/bundles/vmhost/files/check_vm_status
Franziska Kunsmann 5639da4954
Some checks failed
bundlewrap/pipeline/head There was a failure building this commit
bundles/vmhost: add QEMU VM STATUS check
2020-11-21 20:58:55 +01:00

47 lines
926 B
Python

#!/usr/bin/env python3
from subprocess import check_output
from sys import exit
try:
result = check_output(['virsh', '--readonly', 'list', '--all']).decode('utf-8').split('\n')
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[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]))
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)