error handling

This commit is contained in:
Cyberes 2023-04-21 23:54:18 -06:00
parent b32f46f4fd
commit 8232fc2472
1 changed files with 64 additions and 46 deletions

View File

@ -1,8 +1,11 @@
#!/usr/bin/env python3
import argparse
import sys
import traceback
import psutil
import checker.nagios as nagios
from checker.markdown import list_to_markdown_table
# Parse the bandwidth limit argument
@ -12,6 +15,8 @@ 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.')
args = parser.parse_args()
def main():
data = []
warn_value = (args.bandwidth * args.warn / 100)
crit_value = (args.bandwidth * args.critical / 100)
@ -37,6 +42,7 @@ for interface, stats in net_io_counters.items():
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 = []
@ -47,9 +53,11 @@ for i in range(len(data)):
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'
@ -67,3 +75,13 @@ 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)