check_bandwidth: don't print all interfaces if ok

This commit is contained in:
Cyberes 2023-07-06 10:15:32 -06:00
parent 6076d794e1
commit 7d383347f4
1 changed files with 21 additions and 12 deletions

View File

@ -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
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)