error handling
This commit is contained in:
parent
b32f46f4fd
commit
8232fc2472
|
@ -1,8 +1,11 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
|
|
||||||
|
import checker.nagios as nagios
|
||||||
from checker.markdown import list_to_markdown_table
|
from checker.markdown import list_to_markdown_table
|
||||||
|
|
||||||
# Parse the bandwidth limit argument
|
# Parse the bandwidth limit argument
|
||||||
|
@ -12,58 +15,73 @@ parser.add_argument('--critical', type=int, default=75, help='Critical if percen
|
||||||
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('--warn', type=int, default=50, help='Warning if percent of bandwidth usage is greater than or equal to this.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
data = []
|
|
||||||
warn_value = (args.bandwidth * args.warn / 100)
|
|
||||||
crit_value = (args.bandwidth * args.critical / 100)
|
|
||||||
|
|
||||||
# Get network interface statistics
|
def main():
|
||||||
net_io_counters = psutil.net_io_counters(pernic=True)
|
data = []
|
||||||
|
warn_value = (args.bandwidth * args.warn / 100)
|
||||||
|
crit_value = (args.bandwidth * args.critical / 100)
|
||||||
|
|
||||||
# Calculate bandwidth utilization for each interface
|
# Get network interface statistics
|
||||||
for interface, stats in net_io_counters.items():
|
net_io_counters = psutil.net_io_counters(pernic=True)
|
||||||
# Get the number of bytes sent and received
|
|
||||||
bytes_sent = stats.bytes_sent
|
|
||||||
bytes_recv = stats.bytes_recv
|
|
||||||
|
|
||||||
# Wait for 1 second
|
# Calculate bandwidth utilization for each interface
|
||||||
psutil.cpu_percent(interval=1)
|
for interface, stats in net_io_counters.items():
|
||||||
|
# Get the number of bytes sent and received
|
||||||
|
bytes_sent = stats.bytes_sent
|
||||||
|
bytes_recv = stats.bytes_recv
|
||||||
|
|
||||||
# Get the number of bytes sent and received after 1 second
|
# Wait for 1 second
|
||||||
new_stats = psutil.net_io_counters(pernic=True)[interface]
|
psutil.cpu_percent(interval=1)
|
||||||
new_bytes_sent = new_stats.bytes_sent
|
|
||||||
new_bytes_recv = new_stats.bytes_recv
|
|
||||||
|
|
||||||
# Calculate the bandwidth utilization in bits per second
|
# Get the number of bytes sent and received after 1 second
|
||||||
bandwidth_utilization = (8 * (new_bytes_sent - bytes_sent + new_bytes_recv - bytes_recv)) / (1 * 1000 * 1000)
|
new_stats = psutil.net_io_counters(pernic=True)[interface]
|
||||||
data.append([interface, bandwidth_utilization, 'none'])
|
new_bytes_sent = new_stats.bytes_sent
|
||||||
|
new_bytes_recv = new_stats.bytes_recv
|
||||||
|
|
||||||
critical = []
|
# Calculate the bandwidth utilization in bits per second
|
||||||
warn = []
|
bandwidth_utilization = (8 * (new_bytes_sent - bytes_sent + new_bytes_recv - bytes_recv)) / (1 * 1000 * 1000)
|
||||||
ok = []
|
data.append([interface, bandwidth_utilization, 'none'])
|
||||||
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)
|
|
||||||
data[i][2] = 'critical'
|
|
||||||
elif bandwidth_utilization >= warn_value:
|
|
||||||
warn.append(interface)
|
|
||||||
data[i][2] = 'warning'
|
|
||||||
else:
|
|
||||||
ok.append(interface)
|
|
||||||
data[i][2] = 'ok'
|
|
||||||
perf_data.append(f'{interface}={round(bandwidth_utilization, 2)}MB;{warn_value};{crit_value}; ')
|
|
||||||
|
|
||||||
if len(ok):
|
exit_code = nagios.OK
|
||||||
print(f'OK: {", ".join(ok)}')
|
critical = []
|
||||||
if len(warn):
|
warn = []
|
||||||
print(f'WARNING: {", ".join(warn)}')
|
ok = []
|
||||||
if len(critical):
|
perf_data = []
|
||||||
print(f'CRITICAL: {", ".join(critical)}')
|
for i in range(len(data)):
|
||||||
|
interface = data[i][0]
|
||||||
|
bandwidth_utilization = data[i][1]
|
||||||
|
if bandwidth_utilization >= crit_value:
|
||||||
|
critical.append(interface)
|
||||||
|
data[i][2] = 'critical'
|
||||||
|
exit_code = nagios.CRITICAL
|
||||||
|
elif bandwidth_utilization >= warn_value:
|
||||||
|
warn.append(interface)
|
||||||
|
data[i][2] = 'warning'
|
||||||
|
exit_code = nagios.WARNING
|
||||||
|
else:
|
||||||
|
ok.append(interface)
|
||||||
|
data[i][2] = 'ok'
|
||||||
|
perf_data.append(f'{interface}={round(bandwidth_utilization, 2)}MB;{warn_value};{crit_value}; ')
|
||||||
|
|
||||||
data = [(x[0], f'{round(x[1], 2)} Mbps', x[2]) for x in data]
|
if len(ok):
|
||||||
data.insert(0, ('Interface', 'Bandwidth', 'State'))
|
print(f'OK: {", ".join(ok)}')
|
||||||
print(list_to_markdown_table(data, align='left'))
|
if len(warn):
|
||||||
|
print(f'WARNING: {", ".join(warn)}')
|
||||||
|
if len(critical):
|
||||||
|
print(f'CRITICAL: {", ".join(critical)}')
|
||||||
|
|
||||||
print(f' |{"".join(perf_data)}')
|
data = [(x[0], f'{round(x[1], 2)} Mbps', x[2]) for x in data]
|
||||||
|
data.insert(0, ('Interface', 'Bandwidth', 'State'))
|
||||||
|
print(list_to_markdown_table(data, align='left'))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in New Issue