2020-11-21 19:58:55 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from subprocess import check_output
|
|
|
|
from sys import exit
|
|
|
|
|
|
|
|
try:
|
2020-12-29 09:14:26 +00:00
|
|
|
result = check_output(['virsh', '--readonly', 'list', '--all']).decode('utf-8').splitlines()
|
2020-11-21 19:58:55 +00:00
|
|
|
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()
|
|
|
|
|
2020-12-29 09:14:26 +00:00
|
|
|
if info[1].startswith('EXCLUDE_FROM_MONITORING_'):
|
|
|
|
continue
|
|
|
|
|
2020-11-21 19:58:55 +00:00
|
|
|
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]))
|
|
|
|
|
|
|
|
|
2020-11-21 20:01:37 +00:00
|
|
|
print('{} running, {} stopped, {} crashed'.format(len(running), len(stopped), len(crashed)))
|
|
|
|
|
2020-11-21 19:58:55 +00:00
|
|
|
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)
|