From 7d383347f45178f0dce642603261db28bc5b287c Mon Sep 17 00:00:00 2001 From: Cyberes Date: Thu, 6 Jul 2023 10:15:32 -0600 Subject: [PATCH] check_bandwidth: don't print all interfaces if ok --- check_bandwidth.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/check_bandwidth.py b/check_bandwidth.py index d84519f..13c1c82 100755 --- a/check_bandwidth.py +++ b/check_bandwidth.py @@ -8,15 +8,20 @@ import traceback import psutil import checker.nagios as nagios +from checker import list_to_markdown_table, print_icinga2_check_status from checker.linuxfabric.base import get_state -from checker import list_to_markdown_table, dict_to_perfdata, print_icinga2_check_status parser = argparse.ArgumentParser(description='Check network interface bandwidth utilization.') -parser.add_argument('--bandwidth', type=float, default=0, help='Bandwidth speed in Mbps. Used to calculate percentage. Default is 0 which disables warning and critical levels.') -parser.add_argument('--critical', type=int, default=75, help='Critical if percent of bandwidth usage is greater than or equal to this.') -parser.add_argument('--warn', type=int, default=50, help='Warning if percent of bandwidth usage is greater than or equal to this.') -parser.add_argument('--max', type=int, default=None, help='Set the max value the bandwidth can be. Useful for graphs and whatever.') -parser.add_argument('--ignore', nargs='*', default=['lo'], help='Interface names to ignore, separated by a space. Default: lo') +parser.add_argument('--bandwidth', type=float, default=0, + help='Bandwidth speed in Mbps. Used to calculate percentage. Default is 0 which disables warning and critical levels.') +parser.add_argument('--critical', type=int, default=75, + help='Critical if percent of bandwidth usage is greater than or equal to this.') +parser.add_argument('--warn', type=int, default=50, + help='Warning if percent of bandwidth usage is greater than or equal to this.') +parser.add_argument('--max', type=int, default=None, + help='Set the max value the bandwidth can be. Useful for graphs and whatever.') +parser.add_argument('--ignore', nargs='*', default=['lo'], + help='Interface names to ignore, separated by a space. Default: lo') parser.add_argument('--ignore-re', default=None, help='Regex matching interface names to ignore.') args = parser.parse_args() @@ -124,17 +129,21 @@ def main(): listed_interfaces = [*critical, *warn] elif exit_code == nagios.WARNING: listed_interfaces = warn - else: - listed_interfaces = ok - listed_glances = [] - for interface in listed_interfaces: - listed_glances.append(f'{interface}: {round(get_interface_data(interface, data)[3], 2)} Mbps') + if exit_code != nagios.STATE_OK: + listed_glances = [] + for interface in listed_interfaces: + listed_glances.append(f'{interface}: {round(get_interface_data(interface, data)[3], 2)} Mbps') + glance_data = ", ".join(listed_glances) + else: + glance_data = 'all interfaces are ok' data = [(x[0], f'{round(x[3], 2)} Mbps', x[4]) for x in data] data.insert(0, ('Interface', 'Bandwidth', 'State')) - print_icinga2_check_status(f'{", ".join(listed_glances)}\n{list_to_markdown_table(data, align="left", seperator="!", borders=False)}', exit_code, perfdata) + print_icinga2_check_status( + f'{glance_data}\n{list_to_markdown_table(data, align="left", seperator="!", borders=False)}', + exit_code, perfdata) sys.exit(exit_code)