From 5639da4954934ed4ef466734c816b1db750742a8 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sat, 21 Nov 2020 20:58:55 +0100 Subject: [PATCH] bundles/vmhost: add QEMU VM STATUS check --- bundles/vmhost/files/check_vm_status | 47 ++++++++++++++++++++++++++++ bundles/vmhost/items.py | 5 +++ bundles/vmhost/metadata.py | 9 ++++++ 3 files changed, 61 insertions(+) create mode 100644 bundles/vmhost/files/check_vm_status create mode 100644 bundles/vmhost/items.py diff --git a/bundles/vmhost/files/check_vm_status b/bundles/vmhost/files/check_vm_status new file mode 100644 index 0000000..cb39f57 --- /dev/null +++ b/bundles/vmhost/files/check_vm_status @@ -0,0 +1,47 @@ +#!/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) diff --git a/bundles/vmhost/items.py b/bundles/vmhost/items.py new file mode 100644 index 0000000..52ad0ec --- /dev/null +++ b/bundles/vmhost/items.py @@ -0,0 +1,5 @@ +files = { + '/usr/local/share/icinga/plugins/check_vm_status': { + 'mode': '0755', + }, +} diff --git a/bundles/vmhost/metadata.py b/bundles/vmhost/metadata.py index cdacb66..424bad9 100644 --- a/bundles/vmhost/metadata.py +++ b/bundles/vmhost/metadata.py @@ -7,4 +7,13 @@ defaults = { 'qemu-system-x86': {}, }, }, + 'icinga2_api': { + 'vmhost': { + 'services': { + 'QEMU VM STATUS': { + 'command_on_monitored_host': 'sudo /usr/local/share/icinga/plugins/check_vm_status', + }, + }, + }, + }, }