diff --git a/check_bandwidth.py b/check_bandwidth.py index 590799d..cba0e32 100755 --- a/check_bandwidth.py +++ b/check_bandwidth.py @@ -29,6 +29,12 @@ else: ignore_re = None +def get_interface_data(interface: str, data: list): + for y in data: + if y[0] == interface: + return y + + def get_network_traffic(interface): net_io = psutil.net_io_counters(pernic=True) if interface in net_io: @@ -99,21 +105,30 @@ def main(): else: ok.append(interface) state = 'ok' - data[i][2] = f'[{state.upper()}]' + data[i][4] = f'[{state.upper()}]' perf_data.append(f'{interface}={round(bandwidth_utilization, 2)}Mbps;{warn_value};{crit_value};{f"0;{args.max};" if args.max else ""} ') - if len(ok): - print(f'OK: {", ".join(ok)}') - if len(warn): - print(f'WARNING: {", ".join(warn)}') - if len(critical): - print(f'CRITICAL: {", ".join(critical)}') + # Print the status + if exit_code == nagios.CRITICAL: + status = 'CRITICAL' + listed_interfaces = [*critical, *warn] + elif exit_code == nagios.WARNING: + status = 'WARNING' + listed_interfaces = warn + else: + status = 'OK' + listed_interfaces = ok - data = [(x[0], f'{round(x[1], 2)} Mbps', x[2]) for x in data] + listed_glances = [] + for interface in listed_interfaces: + listed_glances.append(f'{interface}: {round(get_interface_data(interface, data)[3], 2)}Mbps') + print(f'{status} - {", ".join(listed_glances)}') + + data = [(x[0], f'{round(x[3], 2)} Mbps', x[4]) for x in data] data.insert(0, ('Interface', 'Bandwidth', 'State')) print(list_to_markdown_table(data, align='left', seperator='!', borders=False)) - print(f' |{"".join(perf_data)}') + print(f'|{"".join(perf_data)}') sys.exit(exit_code)