diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2023-05-19 17:54:59 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2023-05-19 17:54:59 +0200 |
commit | a5e82e7c7889fb0163e6f9b659e97c88c502981b (patch) | |
tree | 5279bba57ab94074ac0c5faa312b35b9d2e8d8da /plugins | |
parent | 1e58d00c444c8eb2a03c557f6c4aa65d1718360d (diff) | |
download | monitoring_custom-a5e82e7c7889fb0163e6f9b659e97c88c502981b.tar.gz |
Diffstat (limited to 'plugins')
-rwxr-xr-x | plugins/check_zpool | 52 |
1 files changed, 52 insertions, 0 deletions
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"]}') |