diff --git a/check_openwrt_clients.py b/check_openwrt_clients.py new file mode 100755 index 0000000..d0abee8 --- /dev/null +++ b/check_openwrt_clients.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +import argparse +import json +import re +import sys +import time +import traceback + +import paramiko + +from checker import nagios +from checker.result import quit_check +from checker.types import try_int, try_float + +WIFI_2 = list(range(1, 12)) +WIFI_5 = list(range(32, 178)) + + +def main(args): + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + try: + ssh.connect(args.host, username='root') + except paramiko.ssh_exception.AuthenticationException: + quit_check( + f'SSH authentication to root@{args.host} failed! Make sure your key exists on the remote system or copy it over with ssh-copy-id', + nagios.STATE_UNKNOWN) + sys.exit(nagios.STATE_UNKNOWN) + except paramiko.ssh_exception.NoValidConnectionsError: + quit_check(f'SSH connection failed to {args.host}', nagios.STATE_UNKNOWN) + + stdin, stdout, stderr = ssh.exec_command(f'iwinfo {args.interface} assoclist') + if stdout.channel.recv_exit_status() != 0: + quit_check(f'command failed:\nSTDOUT: {"".join(stdout.readlines())}\nSTDERR: {"".join(stderr.readlines())}', + nagios.STATE_UNKNOWN) + output = [str(x).strip('\n').strip(' ') for x in stdout.readlines()] + + connected_clients = 0 + for line in output: + is_header_line = bool( + re.search(r'^[\t\s\n]', line) is None and len(line) and 'No station connected' not in line + ) + if is_header_line: + connected_clients += 1 + + perfdata = { + 'connected_clients': {'value': connected_clients, 'min': 0}, + } + + quit_check( + f"{connected_clients} connected clients on {args.interface}", + nagios.STATE_OK, + perfdata=perfdata) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--host', required=True, help='The host to SSH into.') + parser.add_argument('--interface', required=True, help='The wireless interface to use (example: `wlan0`).') + args = parser.parse_args() + + try: + main(args) + except Exception as e: + print(f'UNKNOWN - exception "{e}"') + traceback.print_exc() + sys.exit(nagios.STATE_UNKNOWN)