handle glitches on check opnsense traffic for host
This commit is contained in:
parent
319851ea94
commit
9f9cef96fe
|
@ -18,9 +18,12 @@ def main():
|
||||||
parser.add_argument('--opnsense', required=True, help='OPNsense hostname or IP address.')
|
parser.add_argument('--opnsense', required=True, help='OPNsense hostname or IP address.')
|
||||||
parser.add_argument('--key', required=True, help='OPNsense API key.')
|
parser.add_argument('--key', required=True, help='OPNsense API key.')
|
||||||
parser.add_argument('--secret', required=True, help='OPNsense API secret.')
|
parser.add_argument('--secret', required=True, help='OPNsense API secret.')
|
||||||
parser.add_argument('--interface', required=True, help='Interface to check (e.g., lan). Can be something like "lan,wan"')
|
parser.add_argument('--interface', required=True,
|
||||||
|
help='Interface to check (e.g., lan). Can be something like "lan,wan"')
|
||||||
parser.add_argument('--host', required=True, help='Address of the host to check.')
|
parser.add_argument('--host', required=True, help='Address of the host to check.')
|
||||||
parser.add_argument('--duration', default=10, type=int, help='How many seconds to gather statistics.')
|
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.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
check_result = {}
|
check_result = {}
|
||||||
|
@ -30,9 +33,11 @@ def main():
|
||||||
|
|
||||||
# Map interface names to their internal names
|
# Map interface names to their internal names
|
||||||
interfaces_mapping = requests.get(f'https://{args.opnsense}/api/diagnostics/traffic/interface',
|
interfaces_mapping = requests.get(f'https://{args.opnsense}/api/diagnostics/traffic/interface',
|
||||||
headers={'Accept': 'application/json'}, auth=(args.key, args.secret), verify=False, timeout=10)
|
headers={'Accept': 'application/json'}, auth=(args.key, args.secret),
|
||||||
|
verify=False, timeout=10)
|
||||||
if interfaces_mapping.status_code != 200:
|
if interfaces_mapping.status_code != 200:
|
||||||
print(f'UNKNOWN: unable to query OPNsense API for interface mappings: {interfaces_mapping.status_code}\n{interfaces_mapping.text}')
|
print(
|
||||||
|
f'UNKNOWN: unable to query OPNsense API for interface mappings: {interfaces_mapping.status_code}\n{interfaces_mapping.text}')
|
||||||
sys.exit(nagios.UNKNOWN)
|
sys.exit(nagios.UNKNOWN)
|
||||||
interfaces_mapping = interfaces_mapping.json()['interfaces']
|
interfaces_mapping = interfaces_mapping.json()['interfaces']
|
||||||
interfaces_to_check = set(args.interface.split(','))
|
interfaces_to_check = set(args.interface.split(','))
|
||||||
|
@ -51,7 +56,8 @@ def main():
|
||||||
traffic_data = []
|
traffic_data = []
|
||||||
for _ in range(args.duration):
|
for _ in range(args.duration):
|
||||||
response = requests.get(f'https://{args.opnsense}/api/diagnostics/traffic/top/{interface}',
|
response = requests.get(f'https://{args.opnsense}/api/diagnostics/traffic/top/{interface}',
|
||||||
headers={'Accept': 'application/json'}, auth=(args.key, args.secret), verify=False, timeout=10)
|
headers={'Accept': 'application/json'}, auth=(args.key, args.secret), verify=False,
|
||||||
|
timeout=10)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
print(f'UNKNOWN: unable to query OPNsense API for {interface}: {response.status_code}\n{response.text}')
|
print(f'UNKNOWN: unable to query OPNsense API for {interface}: {response.status_code}\n{response.text}')
|
||||||
sys.exit(nagios.UNKNOWN)
|
sys.exit(nagios.UNKNOWN)
|
||||||
|
@ -60,17 +66,31 @@ def main():
|
||||||
traffic_data.append(item)
|
traffic_data.append(item)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
if not len(traffic_data):
|
if not len(traffic_data) and args.fail_empty:
|
||||||
print('UNKNOWN: Interface or host not found in OPNsense API response.')
|
print('UNKNOWN: Interface or host not found in OPNsense API response. Raw response:')
|
||||||
|
print(traffic_data)
|
||||||
sys.exit(nagios.UNKNOWN)
|
sys.exit(nagios.UNKNOWN)
|
||||||
|
elif not len(traffic_data):
|
||||||
|
traffic_data = {
|
||||||
|
'rate_bits_in': 0,
|
||||||
|
'rate_bits_out': 0,
|
||||||
|
'cumulative_bytes_in': 0,
|
||||||
|
'cumulative_bytes_out': 0,
|
||||||
|
'details': 0
|
||||||
|
}
|
||||||
|
|
||||||
check_result[name] = {
|
try:
|
||||||
'rate_in': np.average([x['rate_bits_in'] for x in traffic_data]),
|
check_result[name] = {
|
||||||
'rate_out': np.average([x['rate_bits_out'] for x in traffic_data]),
|
'rate_in': np.average([x['rate_bits_in'] for x in traffic_data]),
|
||||||
'cumulative_in': np.average([x['cumulative_bytes_in'] for x in traffic_data]),
|
'rate_out': np.average([x['rate_bits_out'] for x in traffic_data]),
|
||||||
'cumulative_out': np.average([x['cumulative_bytes_out'] for x in traffic_data]),
|
'cumulative_in': np.average([x['cumulative_bytes_in'] for x in traffic_data]),
|
||||||
'connections': int(np.average([len(x['details']) for x in traffic_data]))
|
'cumulative_out': np.average([x['cumulative_bytes_out'] for x in traffic_data]),
|
||||||
}
|
'connections': int(np.average([len(x['details']) for x in traffic_data]))
|
||||||
|
}
|
||||||
|
except:
|
||||||
|
print('Failed to parse traffic data.')
|
||||||
|
print(traffic_data)
|
||||||
|
sys.exit(nagios.UNKNOWN)
|
||||||
|
|
||||||
# TODO: figure out status
|
# TODO: figure out status
|
||||||
print('OK: no metrics defined.')
|
print('OK: no metrics defined.')
|
||||||
|
@ -105,7 +125,8 @@ def main():
|
||||||
|
|
||||||
output_table = [
|
output_table = [
|
||||||
('Host', 'Interface', 'Rate In', 'Rate Out', 'Cumulative In', 'Cumulative Out', 'Connections', 'Status'),
|
('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)
|
(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(list_to_markdown_table(output_table, align='left', seperator='!', borders=False))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue