move tc data to dicts, stop large amount of iterations at scale

This commit is contained in:
Warren 2022-09-12 12:56:29 +12:00
parent 0bd190aacd
commit 47ef3ee0b0

View File

@ -1,26 +1,34 @@
import os
import subprocess import subprocess
from subprocess import PIPE
import io
import decimal
import json import json
from ispConfig import fqOrCAKE, interfaceA, interfaceB, influxDBBucket, influxDBOrg, influxDBtoken, influxDBurl import subprocess
from datetime import date, datetime, timedelta from datetime import datetime
import decimal
from influxdb_client import InfluxDBClient, Point, Dialect from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS from influxdb_client.client.write_api import SYNCHRONOUS
import dateutil.parser
from ispConfig import interfaceA, interfaceB, influxDBBucket, influxDBOrg, influxDBtoken, influxDBurl
def getInterfaceStats(interface):
command = 'tc -j -s qdisc show dev ' + interface
jsonAr = json.loads(subprocess.run(command.split(' '), stdout=subprocess.PIPE).stdout.decode('utf-8'))
jsonDict = {}
for element in filter(lambda e: 'parent' in e, jsonAr):
flowID = ':'.join(map(lambda p: f'0x{p}', element['parent'].split(':')[0:2]))
jsonDict[flowID] = element
del jsonAr
return jsonDict
def getDeviceStats(devices): def getDeviceStats(devices):
interfaces = [interfaceA, interfaceB] interfaces = [interfaceA, interfaceB]
for interface in interfaces: for interface in interfaces:
command = 'tc -j -s qdisc show dev ' + interface tcShowResults = getInterfaceStats(interface)
commands = command.split(' ')
tcShowResults = subprocess.run(commands, stdout=subprocess.PIPE).stdout.decode('utf-8')
if interface == interfaceA: if interface == interfaceA:
interfaceAjson = json.loads(tcShowResults) interfaceAjson = tcShowResults
else: else:
interfaceBjson = json.loads(tcShowResults) interfaceBjson = tcShowResults
for device in devices: for device in devices:
if 'timeQueried' in device: if 'timeQueried' in device:
device['priorQueryTime'] = device['timeQueried'] device['priorQueryTime'] = device['timeQueried']
@ -29,10 +37,10 @@ def getDeviceStats(devices):
jsonVersion = interfaceAjson jsonVersion = interfaceAjson
else: else:
jsonVersion = interfaceBjson jsonVersion = interfaceBjson
for element in jsonVersion:
if "parent" in element: element = jsonVersion[device['qdisc']] if device['qdisc'] in jsonVersion else False
parentFixed = '0x' + element['parent'].split(':')[0] + ':' + '0x' + element['parent'].split(':')[1]
if parentFixed == device['qdisc']: if element:
drops = int(element['drops']) drops = int(element['drops'])
packets = int(element['packets']) packets = int(element['packets'])
bytesSent = int(element['bytes']) bytesSent = int(element['bytes'])
@ -44,6 +52,7 @@ def getDeviceStats(devices):
if 'bytesSentUpload' in device: if 'bytesSentUpload' in device:
device['priorQueryBytesUpload'] = device['bytesSentUpload'] device['priorQueryBytesUpload'] = device['bytesSentUpload']
device['bytesSentUpload'] = bytesSent device['bytesSentUpload'] = bytesSent
device['timeQueried'] = datetime.now().isoformat() device['timeQueried'] = datetime.now().isoformat()
for device in devices: for device in devices:
if 'priorQueryTime' in device: if 'priorQueryTime' in device: