From a5e82e7c7889fb0163e6f9b659e97c88c502981b Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Fri, 19 May 2023 17:54:59 +0200 Subject: check_zpool --- plugins/check_zpool | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 plugins/check_zpool diff --git a/plugins/check_zpool b/plugins/check_zpool new file mode 100755 index 0000000..14713ca --- /dev/null +++ b/plugins/check_zpool @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import re +import subprocess +import sys + +STATUS = ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN'] + +def nagios_return(status: int, message: str, perfdata: dict = {}): + perfdata_str = '' + for data in perfdata: + perfdata_str += f' {data}={perfdata[data]}' + + print(f'{STATUS[status]}: {message} | {perfdata_str}') + exit(status) + +def parse_data(data): + parts = re.split(r'(?:\n|^)\s*(\w*):\s*', data.strip(), re.MULTILINE)[1:] + parsed = dict(zip(parts[::2], parts[1::2])) + return { + **parsed, + 'config': parse_config(parsed.get('config', '')) + } + + +def parse_config(data): + lines = [v.strip().split() for v in data.splitlines() if v.strip()] + if lines: + return [ + dict(zip(lines[0], v)) + for v in lines[1:] + ] + return [] + + +if len(sys.argv) != 2: + nagios_return(3, 'Invalid arguments') + +zpool = sys.argv[1] + +data = {} + +try: + ret = subprocess.run(['zpool', 'status', zpool], capture_output=True) + data = parse_data(ret.stdout.decode('ascii')) +except: + nagios_return(3, f'{zpool}: Command execution failed') + +if data['state'] != 'ONLINE': + nagios_return(2, f'{zpool} is {data["state"]}') + +nagios_return(0, f'{zpool} is {data["state"]}') -- cgit v1.2.3