From 989d222392fead6a53f6b1ff0412f77864c2cf2e Mon Sep 17 00:00:00 2001 From: Cyberes Date: Fri, 23 Feb 2024 16:42:47 -0700 Subject: [PATCH] check_zfs_zpool: use capacity for perfdata --- check_zfs_zpool.py | 60 +++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/check_zfs_zpool.py b/check_zfs_zpool.py index b54dba4..f01963f 100755 --- a/check_zfs_zpool.py +++ b/check_zfs_zpool.py @@ -10,7 +10,6 @@ from checker import nagios, dict_to_perfdata from checker.markdown import list_to_markdown_table from checker.nagios import state_to_txt from checker.units import filesize -from checker.zfs import zfs_get_free # TODO: add perfdata @@ -138,24 +137,11 @@ def get_zpool_zfs_properties(pool_name: str): return zfs_properties -def main(): - parser = argparse.ArgumentParser(description='Check ZFS pool status') - parser.add_argument('--pool-name', required=True, help='Name of the ZFS pool to check.') - parser.add_argument('--check-type', required=True, choices=['status', 'cache', 'log'], help='What to check.') - parser.add_argument('--warning-free', type=int, default=65, - help='Warning level for free space percentage (default: 65)') - parser.add_argument('--critical-free', type=int, default=80, - help='Critical level for free space percentage (default: 80)') - parser.add_argument('--warning-frag', type=int, default=50, - help='Warning level for fragmentation percentage (default: 50)') - parser.add_argument('--critical-frag', type=int, default=75, - help='Critical level for fragmentation percentage (default: 75)') - args = parser.parse_args() - - args.warning_free = percent_to_float(f'{args.warning_free}%') - args.critical_free = percent_to_float(f'{args.critical_free}%') - args.warning_frag = percent_to_float(f'{args.warning_frag}%') - args.critical_frag = percent_to_float(f'{args.critical_frag}%') +def main(args): + warning_free_float = percent_to_float(f'{args.warning_free}%') + critical_free_float = percent_to_float(f'{args.critical_free}%') + warning_frag_float = percent_to_float(f'{args.warning_frag}%') + critical_frag_float = percent_to_float(f'{args.critical_frag}%') if args.check_type == 'status': vdev_devices = [x for x in get_vdev_info(args.pool_name, 'pool') if not x['pool']] @@ -171,10 +157,10 @@ def main(): pool_status['fragmentation'] = percent_to_float(f"{pool_status['fragmentation']}%") # Check for critical - if pool_status['capacity'] >= args.critical_free: + if pool_status['capacity'] >= critical_free_float: exit_code = nagios.CRITICAL issues.append('capacity') - elif pool_status['fragmentation'] >= args.critical_frag: + elif pool_status['fragmentation'] >= critical_frag_float: exit_code = nagios.CRITICAL issues.append('fragmentation') elif pool_status['health'] != 'ONLINE': @@ -183,10 +169,10 @@ def main(): # Check for warnings if exit_code == nagios.OK: - if pool_status['capacity'] >= args.warning_free: + if pool_status['capacity'] >= warning_free_float: exit_code = nagios.WARNING issues.append('capacity') - elif pool_status['fragmentation'] >= args.warning_frag: + elif pool_status['fragmentation'] >= warning_frag_float: exit_code = nagios.WARNING issues.append('fragmentation') @@ -199,8 +185,8 @@ def main(): print('OK - pool', args.pool_name, 'is healthy') # Build the table - critical, warning, states = check_vdev_devices(vdev_devices, args.critical_free, args.warning_free, - args.critical_frag, args.warning_frag) + critical, warning, states = check_vdev_devices(vdev_devices, critical_free_float, warning_free_float, + critical_frag_float, warning_frag_float) table_data = [ ('Device', 'Size', 'Alloc', 'Free', 'Frag', 'Cap', 'Health', 'State'), (args.pool_name, filesize(pool_status['size'], spaces=False, formatter=False), @@ -218,16 +204,15 @@ def main(): table_data.append((device['device'], device['size'], device['alloc'], device['free'], device['frag'], device['cap'], device['health'], states[device['device']])) - zpool_size, zpool_free = zfs_get_free(args.pool_name) perf_data = { - 'free': { - 'value': zpool_size, 'warn': int(zpool_size * args.warning_free), 'crit': int(zpool_size * args.critical_free), 'min': 0, 'unit': 'GB' + 'capacity': { + 'value': int(float(float_to_percent(pool_status['capacity']).strip('%'))), 'warn': args.warning_free, 'crit': args.critical_free, 'min': 0, 'unit': '%' }, 'size': { - 'value': zpool_free, 'warn': int(zpool_size * args.warning_free), 'crit': int(zpool_size * args.critical_free), 'min': 0, 'unit': 'GB' + 'value': pool_status['size'], 'min': 0 }, 'fragmentation': { - 'value': int(float(float_to_percent(pool_status['fragmentation']).strip('%'))), 'warn': float_to_percent(args.warning_frag), 'crit': float_to_percent(args.critical_frag), 'min': 0, 'unit': '%' + 'value': int(float(float_to_percent(pool_status['fragmentation']).strip('%'))), 'warn': float_to_percent(warning_frag_float).strip('%'), 'crit': float_to_percent(critical_frag_float).strip('%'), 'min': 0, 'unit': '%' } } perf_data_str = dict_to_perfdata(perf_data) @@ -242,8 +227,8 @@ def main(): print('UNKNOWN - no devices found') sys.exit(nagios.UNKNOWN) table_data = [('Device', 'Size', 'Alloc', 'Free', 'Frag', 'Cap', 'Health', 'State')] - critical, warning, states = check_vdev_devices(vdev_devices, args.critical_free, args.warning_free, - args.critical_frag, args.warning_frag) + critical, warning, states = check_vdev_devices(vdev_devices, critical_free_float, warning_free_float, + critical_frag_float, warning_frag_float) for device in vdev_devices: if device['frag'] != '-': @@ -312,8 +297,17 @@ def main(): if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Check ZFS pool status') + parser.add_argument('--pool-name', required=True, help='Name of the ZFS pool to check.') + parser.add_argument('--check-type', required=True, choices=['status', 'cache', 'log'], help='What to check.') + parser.add_argument('--warning-free', type=int, default=65, help='Warning level for free space percentage (default: 65)') + parser.add_argument('--critical-free', type=int, default=80, help='Critical level for free space percentage (default: 80)') + parser.add_argument('--warning-frag', type=int, default=50, help='Warning level for fragmentation percentage (default: 50)') + parser.add_argument('--critical-frag', type=int, default=75, help='Critical level for fragmentation percentage (default: 75)') + args = parser.parse_args() + try: - main() + main(args) except Exception as e: print(f'UNKNOWN: exception "{e}"') import traceback