check opnsense traffic: fix metrics

This commit is contained in:
Cyberes 2023-05-06 15:55:46 -06:00
parent f4184c2c43
commit cf2a48ca6e
1 changed files with 62 additions and 26 deletions

View File

@ -23,9 +23,10 @@ def main():
parser.add_argument('--duration', default=10, type=int, help='How many seconds to gather statistics.')
parser.add_argument('--fail-empty', action='store_true', help='If the API did not return any data, fail with UNKNOWN. Otherwise, assume that there was no traffic.')
parser.add_argument('--bandwidth', type=float, required=True, help='Bandwidth speed in Mbps. Used to calculate percentage.')
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('--bandwidth-critical', type=int, default=75, help='Critical if percent of bandwidth usage is greater than or equal to this.')
parser.add_argument('--bandwidth-warn', type=int, default=50, help='Warning if percent of bandwidth usage is greater than or equal to this.')
parser.add_argument('--conn-critical', type=int, default=-1, help='Set critical level for number of connections. Default: -1 (disabled).')
parser.add_argument('--conn-warn', type=int, default=-1, help='Set warning level for number of connections. Default: -1 (disabled).')
args = parser.parse_args()
check_result = {}
@ -98,8 +99,8 @@ def main():
print(traffic_data)
sys.exit(nagios.UNKNOWN)
warn_value = (args.bandwidth * args.warn / 100)
crit_value = (args.bandwidth * args.critical / 100)
warn_b_value = (args.bandwidth * args.bandwidth_warn / 100) * 1e+6
crit_b_value = (args.bandwidth * args.bandwidth_critical / 100) * 1e+6
exit_code = nagios.OK
critical = []
@ -107,33 +108,68 @@ def main():
ok = []
perf_data = []
for name, data in check_result.items():
# TODO: figure out status
if -1 >= crit_value:
critical.append(name)
status = '[CRITICAL]'
output_table = [
('Host', 'Interface', 'Rate In', 'Rate Out', 'Cumulative In', 'Cumulative Out', 'Connections', 'Status')
]
def check_b(name, value):
nonlocal exit_code
if value >= crit_b_value:
critical.append((name, filesize(value)))
exit_code = nagios.CRITICAL
elif -1 >= warn_value:
warn.append(name)
status = '[WARNING]'
return '[CRITICAL]', nagios.CRITICAL
elif value >= warn_b_value:
warn.append((name, filesize(value)))
exit_code = nagios.WARNING
return '[WARNING]', nagios.WARNING
else:
ok.append(name)
ok.append((name, filesize(value)))
return '[OK]', nagios.OK
for name, data in check_result.items():
status = '[OK]'
perf_data.append(f'\'{name}_rate_in\'={int(data["rate_in"])}B;{warn_value};{crit_value};0;')
perf_data.append(f'\'{name}_rate_out\'={int(data["rate_out"])}B;{warn_value};{crit_value};0;')
perf_data.append(f'\'{name}_cumulative_in\'={int(data["cumulative_in"])}B;{warn_value};{crit_value};0;')
perf_data.append(f'\'{name}_cumulative_out\'={int(data["cumulative_out"])}B;{warn_value};{crit_value};0;')
perf_data.append(f'\'{name}_connections\'={int(data["connections"])}B;{warn_value};{crit_value};0;')
in_status, in_rc = check_b('rate_in', data['rate_in'])
if in_rc >= exit_code:
status = in_status
out_status, out_rc = check_b('rate_out', data['rate_out'])
if out_rc >= exit_code:
status = out_status
if data['connections'] >= args.conn_critical > 0:
critical.append(('connections', data['connections']))
exit_code = nagios.CRITICAL
status = '[CRITICAL]'
elif data['connections'] >= args.conn_warn > 0:
warn.append(('connections', data['connections']))
exit_code = nagios.WARNING
status = '[WARNING]'
else:
ok.append(('connections', data['connections']))
perf_data.append(f'\'{name}_rate_in\'={int(data["rate_in"])}B;{warn_b_value};{crit_b_value};0;')
perf_data.append(f'\'{name}_rate_out\'={int(data["rate_out"])}B;{warn_b_value};{crit_b_value};0;')
perf_data.append(f'\'{name}_cumulative_in\'={int(data["cumulative_in"])}B;{warn_b_value};{crit_b_value};0;')
perf_data.append(f'\'{name}_cumulative_out\'={int(data["cumulative_out"])}B;{warn_b_value};{crit_b_value};0;')
perf_data.append(f'\'{name}_connections\'={int(data["connections"])}B;{warn_b_value};{crit_b_value};0;')
output_table.append((args.host, name, filesize(data['rate_in']), filesize(data['rate_out']), filesize(data['cumulative_in']), filesize(data['cumulative_out']), data['connections'], status))
if len(critical):
x = ['CRITICAL: ']
for i in critical:
x.append(f'{i[0]}: {i[1]}, ')
print(''.join(x).strip(', '))
if len(warn):
x = ['WARN: ']
for i in warn:
x.append(f'{i[0]}: {i[1]}')
print(''.join(x).strip(', '))
if not len(warn) and not len(critical):
print(f'OK: bandwidth is below {args.bandwidth} Mbps.')
output_table = [
('Host', 'Interface', 'Rate In', 'Rate Out', 'Cumulative In', 'Cumulative Out', 'Connections', 'Status'),
(args.host, name, filesize(data['rate_in']), filesize(data['rate_out']), filesize(data['cumulative_in']),
filesize(data['cumulative_out']), data['connections'], status)
]
print(list_to_markdown_table(output_table, align='left', seperator='!', borders=False))
print(f'| {" ".join(perf_data)}')
sys.exit(exit_code)