icinga2-checks/check_bandwidth.py

94 lines
3.4 KiB
Python
Raw Normal View History

2023-04-21 23:54:18 -06:00
#!/usr/bin/env python3
import argparse
2023-04-21 23:54:18 -06:00
import sys
import traceback
2023-04-21 23:54:18 -06:00
import psutil
2023-04-21 23:54:18 -06:00
import checker.nagios as nagios
2023-04-21 23:54:18 -06:00
from checker.markdown import list_to_markdown_table
parser = argparse.ArgumentParser(description='Check network interface bandwidth utilization.')
parser.add_argument('--bandwidth', type=float, required=True, help='Bandwidth speed in Mbps.')
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.')
2023-04-21 23:54:18 -06:00
parser.add_argument('--max', type=int, default=None, help='Set the max value the bandwidth can be. Useful for graphs and whatever.')
2023-04-26 13:28:01 -06:00
parser.add_argument('--ignore', default=None, help='Interface names to ignore.')
2023-04-21 23:54:18 -06:00
args = parser.parse_args()
2023-04-26 13:28:01 -06:00
ignore_interfaces = args.ignore.split(',') if args.ignore else []
2023-04-21 23:54:18 -06:00
def main():
data = []
warn_value = (args.bandwidth * args.warn / 100)
crit_value = (args.bandwidth * args.critical / 100)
# Get network interface statistics
net_io_counters = psutil.net_io_counters(pernic=True)
# Calculate bandwidth utilization for each interface
for interface, stats in net_io_counters.items():
2023-04-26 13:28:01 -06:00
if interface in ignore_interfaces:
continue
2023-04-21 23:54:18 -06:00
# Get the number of bytes sent and received
bytes_sent = stats.bytes_sent
bytes_recv = stats.bytes_recv
# Wait for 1 second
psutil.cpu_percent(interval=1)
# Get the number of bytes sent and received after 1 second
new_stats = psutil.net_io_counters(pernic=True)[interface]
new_bytes_sent = new_stats.bytes_sent
new_bytes_recv = new_stats.bytes_recv
# Calculate the bandwidth utilization in bits per second
bandwidth_utilization = (8 * (new_bytes_sent - bytes_sent + new_bytes_recv - bytes_recv)) / (1 * 1000 * 1000)
data.append([interface, bandwidth_utilization, 'none'])
exit_code = nagios.OK
critical = []
warn = []
ok = []
perf_data = []
for i in range(len(data)):
interface = data[i][0]
bandwidth_utilization = data[i][1]
if bandwidth_utilization >= crit_value:
critical.append(interface)
2023-04-21 23:54:18 -06:00
state = 'critical'
2023-04-21 23:54:18 -06:00
exit_code = nagios.CRITICAL
elif bandwidth_utilization >= warn_value:
warn.append(interface)
2023-04-21 23:54:18 -06:00
state = 'warning'
2023-04-21 23:54:20 -06:00
if exit_code < nagios.WARNING:
exit_code = nagios.WARNING
2023-04-21 23:54:18 -06:00
else:
ok.append(interface)
2023-04-21 23:54:18 -06:00
state = 'ok'
data[i][2] = f'[{state.upper()}]'
2023-04-21 23:54:18 -06:00
perf_data.append(f'{interface}={round(bandwidth_utilization, 2)}MB;{warn_value};{crit_value};{f"0;{args.max};" if args.max else ""} ')
2023-04-21 23:54:18 -06:00
if len(ok):
print(f'OK: {", ".join(ok)}')
if len(warn):
print(f'WARNING: {", ".join(warn)}')
if len(critical):
print(f'CRITICAL: {", ".join(critical)}')
data = [(x[0], f'{round(x[1], 2)} Mbps', x[2]) for x in data]
data.insert(0, ('Interface', 'Bandwidth', 'State'))
2023-04-21 23:54:18 -06:00
print(list_to_markdown_table(data, align='left', seperator='!', borders=False))
2023-04-21 23:54:18 -06:00
print(f' |{"".join(perf_data)}')
sys.exit(exit_code)
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f'UNKNOWN: exception "{e}"')
print(traceback.format_exc())
sys.exit(nagios.UNKNOWN)