From c2f2b5f3db07a3ce9a169386d14b8eb8387f41c7 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Fri, 23 Feb 2024 10:57:26 -0700 Subject: [PATCH] add perfdata to check_zfs_zpool --- check_zfs_zpool.py | 19 +++++++++++++++++-- checker/zfs.py | 9 +++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 checker/zfs.py diff --git a/check_zfs_zpool.py b/check_zfs_zpool.py index 2d9fbe8..b54dba4 100755 --- a/check_zfs_zpool.py +++ b/check_zfs_zpool.py @@ -6,10 +6,11 @@ import sys import zfslib as zfs -from checker import nagios +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 @@ -217,7 +218,21 @@ def main(): table_data.append((device['device'], device['size'], device['alloc'], device['free'], device['frag'], device['cap'], device['health'], states[device['device']])) - print(list_to_markdown_table(table_data, align='center', seperator='!', borders=False)) + 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' + }, + 'size': { + 'value': zpool_free, 'warn': int(zpool_size * args.warning_free), 'crit': int(zpool_size * args.critical_free), 'min': 0, 'unit': 'GB' + }, + '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': '%' + } + } + perf_data_str = dict_to_perfdata(perf_data) + + print(list_to_markdown_table(table_data, align='center', seperator='!', borders=False), '|', perf_data_str) sys.exit(exit_code) elif args.check_type in ['cache', 'log']: diff --git a/checker/zfs.py b/checker/zfs.py new file mode 100644 index 0000000..d36af14 --- /dev/null +++ b/checker/zfs.py @@ -0,0 +1,9 @@ +import subprocess + + +def zfs_get_free(zpool: str): + size = int(subprocess.check_output(f"zpool get -p -H size | grep {zpool} | awk '{{ print $3}}'", shell=True).decode().strip('\n')) + size_gb = int(size / 1e+9) + free = int(subprocess.check_output(f"zpool get -p -H free | grep {zpool} | awk '{{ print $3}}'", shell=True).decode().strip('\n')) + free_gb = int(free / 1e+9) + return size_gb, free_gb