From d25dfd1b877861f89f0c38c439e64f2fbc33773c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Tue, 24 Jan 2023 20:19:14 -0700 Subject: [PATCH] Resurrect lqTools.py. Not yet compatible with OnAStick mode. https://github.com/LibreQoE/LibreQoS/issues/52 --- src/lqTools.py | 152 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/lqTools.py diff --git a/src/lqTools.py b/src/lqTools.py new file mode 100644 index 00000000..f7f8d568 --- /dev/null +++ b/src/lqTools.py @@ -0,0 +1,152 @@ +#!/usr/bin/python3 + +import csv +import io +import ipaddress +import json +import os +import os.path +import subprocess +import warnings +import argparse +from ispConfig import interfaceA, interfaceB, enableActualShellCommands + +def shell(command): + if enableActualShellCommands: + logging.info(command) + commands = command.split(' ') + proc = subprocess.Popen(commands, stdout=subprocess.PIPE) + for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): # or another encoding + print(line) + else: + print(command) + +def safeShell(command): + safelyRan = True + if enableActualShellCommands: + commands = command.split(' ') + proc = subprocess.Popen(commands, stdout=subprocess.PIPE) + for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): # or another encoding + #logging.info(line) + print(line) + if ("RTNETLINK answers" in line) or ("We have an error talking to the kernel" in line): + safelyRan = False + else: + print(command) + safelyRan = True + return safelyRan + +def getQdiscForIPaddress(ipAddress): + qDiscID = '' + foundQdisc = False + with open('statsByCircuit.json', 'r') as j: + subscriberCircuits = json.loads(j.read()) + for circuit in subscriberCircuits: + for device in circuit['devices']: + for ipv4 in device['ipv4s']: + strippedIPv4 = ipv4.replace('/32','') + if strippedIPv4 == ipAddress: + qDiscID = circuit['classid'] + foundQdisc = True + for ipv6 in device['ipv6s']: + if ipv6 == ipAddress: + qDiscID = circuit['qdisc'] + foundQdisc = True + if foundQdisc: + return qDiscID + else: + return None + +def printStatsFromIP(ipAddress): + qDiscID = getQdiscForIPaddress(ipAddress) + if qDiscID != None: + interfaces = [interfaceA, interfaceB] + for interface in interfaces: + command = 'tc -s qdisc show dev ' + interface + ' parent ' + qDiscID + commands = command.split(' ') + proc = subprocess.Popen(commands, stdout=subprocess.PIPE) + for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): # or another encoding + print(line.replace('\n','')) + else: + print("Invalid IP address provided") + +def printCircuitClassInfo(ipAddress): + qDiscID = getQdiscForIPaddress(ipAddress) + if qDiscID != None: + print("IP: " + ipAddress + " | Class ID: " + qDiscID) + print() + theClassID = '' + interfaces = [interfaceA, interfaceB] + downloadMin = '' + downloadMax = '' + uploadMin = '' + uploadMax = '' + cburst = '' + burst = '' + for interface in interfaces: + command = 'tc class show dev ' + interface + ' classid ' + qDiscID + commands = command.split(' ') + proc = subprocess.Popen(commands, stdout=subprocess.PIPE) + for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): # or another encoding + if "htb" in line: + listOfThings = line.split(" ") + if interface == interfaceA: + downloadMin = line.split(' rate ')[1].split(' ')[0] + downloadMax = line.split(' ceil ')[1].split(' ')[0] + burst = line.split(' burst ')[1].split(' ')[0] + cburst = line.split(' cburst ')[1].replace('\n','') + else: + uploadMin = line.split(' rate ')[1].split(' ')[0] + uploadMax = line.split(' ceil ')[1].split(' ')[0] + print("Download rate/ceil: " + downloadMin + "/" + downloadMax) + print("Upload rate/ceil: " + uploadMin + "/" + uploadMax) + print("burst/cburst: " + burst + "/" + cburst) + else: + print("Invalid IP address provided") + +def findClassIDForCircuitByIP(data, inputIP, classID): + for node in data: + if 'circuits' in data[node]: + for circuit in data[node]['circuits']: + for device in circuit['devices']: + if device['ipv4s']: + for ipv4 in device['ipv4s']: + if ipv4 == inputIP: + classID = circuit['qdisc'] + if device['ipv6s']: + for ipv6 in device['ipv6s']: + if inputIP == ipv6: + classID = circuit['qdisc'] + # Recursive call this function for children nodes attached to this node + if 'children' in data[node]: + classID = findClassIDForCircuitByIP(data[node]['children'], inputIP, classID) + return classID + +def findClassIDForCircuitByID(data, inputID, classID): + for node in data: + if 'circuits' in data[node]: + for circuit in data[node]['circuits']: + if circuit['circuitID'] == inputID: + classID = circuit['qdisc'] + # Recursive call this function for children nodes attached to this node + if 'children' in data[node]: + classID = findClassIDForCircuitByID(data[node]['children'], inputID, classID) + return classID + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers(dest='command') + + planFromIP = subparsers.add_parser('show-active-plan-from-ip', help="Provide tc class info by IP",) + planFromIP.add_argument('ip', type=str,) + statsFromIP = subparsers.add_parser('tc-statistics-from-ip', help="Provide tc qdisc stats by IP",) + statsFromIP.add_argument('ip', type=str,) + + args = parser.parse_args() + + if (args.command == 'tc-statistics-from-ip'): + printStatsFromIP(args.ip) + elif (args.command == 'show-active-plan-from-ip'): + printCircuitClassInfo(args.ip) + else: + print("Invalid parameters. Use --help to learn more.") -- knotmirror 0.0.1