From 27fa1b66e12b732e41d23d5b616076ffe2b91ee8 Mon Sep 17 00:00:00 2001 From: Jed Reynolds Date: Fri, 28 Feb 2020 12:22:35 -0800 Subject: [PATCH] ws-sta-monitor: figuring out regex --- py-json/ws-sta-monitor.py | 149 +++++++++++++++++++++++++++++--------- 1 file changed, 116 insertions(+), 33 deletions(-) diff --git a/py-json/ws-sta-monitor.py b/py-json/ws-sta-monitor.py index 70cee82e..2d775d28 100755 --- a/py-json/ws-sta-monitor.py +++ b/py-json/ws-sta-monitor.py @@ -16,6 +16,7 @@ if sys.version_info[0] != 3: import argparse import json import logging +import traceback import time from time import sleep import websocket @@ -31,14 +32,18 @@ from LANforge import LFUtils from LANforge.LFUtils import NA ignore=[ - "scan finished", - "scan started" + ": scan finished", + ": scan started", "CTRL-EVENT-SCAN-STARTED", - "-EVENT-SSID-TEMP-DISABLED", + "SCAN-STARTED", + "SSID-TEMP-DISABLED", + "CTRL-EVENT-REGDOM-CHANGE", + "CTRL-EVENT-SUBNET-STATUS-UPDATE", "new station", "del station", "ping", "deleted-alert", + "regulatory domain change", ] interesting=[ "Trying to authenticate", @@ -51,19 +56,17 @@ interesting=[ rebank = { "ifname" : re.compile("IFNAME=(\S+)") } +websock = None + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def main(): - host = "ct524-debbie.jbr.candelatech.com" + global websock + host = "localhost" base_url = "ws://%s:8081"%host - websock = None # open websocket - start_websocket(base_url, websock) - if (websock is not None): - print ("Started websocket") - else: - print ("Failed to start websocket, bye.") - exit(1) + websock = start_websocket(base_url, websock) + # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- def sock_filter(wsock, text): @@ -71,6 +74,9 @@ def sock_filter(wsock, text): global interesting global rebank debug = 0 + station_name = None + resource = None + for test in ignore: if (test in text): if (debug): @@ -79,48 +85,123 @@ def sock_filter(wsock, text): try: message = json.loads(text) - if (("time" in message) and ("timestamp" in message)): + except Exception as ex: + print ("Json Exception: ", repr(ex)) + traceback.print_exc() + + try: + # big generic filter for wifi-message or details keys + try: + if ("details" in message.keys()): + for test in ignore: + if (test in message["details"]): + return; + except KeyError: + print ("Message lacks key 'details'") + try: + if ("wifi-event" in message.keys()): + for test in ignore: + #print (" is ",test, " in ", message["wifi-event"]) + if (test in message["wifi-event"]): + return; + except KeyError: + print("Message lacks key 'wifi-event'" ) + + if (("time" in message.keys()) and ("timestamp" in message.keys())): return - if ("event_type" in message): + + if ("name" in message.keys()): + station_name = message["name"] + if ("resource" in message.keys()): + resource = "1.", message["resource"] + + if ("event_type" in message.keys()): + match_result = re.match(r'Port (\S+)', message["details"]) + if (match_result is not None): + station_name = match_result.group(1) + if (message["is_alert"]): - print ("alert: ", message["details"]); + print ("alert: ", message["details"]) + LFUtils.debug_printer.pprint(message) return else: - print ("event: ", message["details"]); + print ("event: ", message["details"]) + LFUtils.debug_printer.pprint(message) + if (" IP change from " in message["details"]): + if (" to 0.0.0.0" in messsage["details"]): + print ("e: %s.%s lost IP address",[resource,station_name]) + else: + print ("e: %s.%s gained IP address",[resource,station_name]) + if ("Link DOWN" in message["details"]): + return # duplicates alert return - if ("wifi-event" in message): - for test in ignore: - if (test in message["wifi-event"]): - if (debug): - print (" ignoring ",text) - return; - #group = rebank["ifname"].match(message["wifi-event"]).group() - #if (group): - # print ("IFname: ",group) + if ("wifi-event" in message.keys()): + if ((station_name is None) or (resource is None)): + try: + print ("Searching for station name:") + match_result = re.match(r'^(1\.\d+):\s+(\S+)\s+\(phy', message["wifi-event"]) + if (match_result is not None): + LFUtils.debug_printer.pprint(match_result) + LFUtils.debug_printer.pprint(match_result.group) + resource = match_result.group(1) + station_name = match_result.group(2) + print ("good!") + else: + match_result = re.match(r'(1\.\d+):\s+IFNAME=(\S+)\s+', message["wifi-event"]) + LFUtils.debug_printer.pprint(match_result) + LFUtils.debug_printer.pprint(match_result.group) + if (match_result is not None): + resource = match_result.group(1) + station_name = match_result.group(2) + print ("ok!") + else: + station_name = 'no-sta' + resource_name = 'no-resource' + print ("bleh!") + except Exception as ex2: + print ("No regex match:") + print(repr(ex2)) + traceback.print_exc() + sleep(1) + + print ("Determined station name: as %s/%s"%[resource, station_name]) if ("disconnected" in message["wifi-event"]): - print ("Station down") + print ("Station %s.%s down"%[resource,station_name]) return if ("Trying to authenticate" in message["wifi-event"]): - print ("station authenticating") + print ("station %s.%s authenticating"%[resource,station_name]) return - print ("wifi-event: ", message["wifi-event"]) + print ("w: ", message["wifi-event"]) else: print ("\nUnhandled: ") LFUtils.debug_printer.pprint(message) + except KeyError as kerr: + print ("# ----- Bad Key: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") + print ("input: ",text) + print (repr(kerr)) + traceback.print_exc() + print ("# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") + sleep(1) + return except json.JSONDecodeError as derr: print ("# ----- Decode err: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") - print (text) - print (derr) + print ("input: ",text) + print (repr(derr)) + traceback.print_exc() print ("# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") + sleep(1) return except Exception as ex: - print ("# ----- Not JSON: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") - print (text) - print (ex) + print ("# ----- Exception: ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") + print(repr(ex)) + print ("input: ",text) + LFUtils.debug_printer.pprint(message) + traceback.print_exc() print ("# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----") + sleep(1) return # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- @@ -136,13 +217,14 @@ def m_open(wsock): #ping = json.loads(); wsock.send('{"text":"ping"}') thread.start_new_thread(run, ()) + print ("started websocket client") # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- def m_close(wsock): LFUtils.debug_printer.pprint(wsock) # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -def start_websocket(uri, *websock): +def start_websocket(uri, websock): #websocket.enableTrace(True) websock = websocket.WebSocketApp(uri, on_message = sock_filter, @@ -150,6 +232,7 @@ def start_websocket(uri, *websock): on_close = m_close) websock.on_open = m_open websock.run_forever() + return websock # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----