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
from subprocess import PIPE
import io
import decimal
import json
from ispConfig import fqOrCAKE, interfaceA, interfaceB, influxDBBucket, influxDBOrg, influxDBtoken, influxDBurl
from datetime import date, datetime, timedelta
import decimal
from influxdb_client import InfluxDBClient, Point, Dialect
import subprocess
from datetime import datetime
from influxdb_client import InfluxDBClient, Point
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):
interfaces = [interfaceA, interfaceB]
for interface in interfaces:
command = 'tc -j -s qdisc show dev ' + interface
commands = command.split(' ')
tcShowResults = subprocess.run(commands, stdout=subprocess.PIPE).stdout.decode('utf-8')
tcShowResults = getInterfaceStats(interface)
if interface == interfaceA:
interfaceAjson = json.loads(tcShowResults)
interfaceAjson = tcShowResults
else:
interfaceBjson = json.loads(tcShowResults)
interfaceBjson = tcShowResults
for device in devices:
if 'timeQueried' in device:
device['priorQueryTime'] = device['timeQueried']
@ -29,10 +37,10 @@ def getDeviceStats(devices):
jsonVersion = interfaceAjson
else:
jsonVersion = interfaceBjson
for element in jsonVersion:
if "parent" in element:
parentFixed = '0x' + element['parent'].split(':')[0] + ':' + '0x' + element['parent'].split(':')[1]
if parentFixed == device['qdisc']:
element = jsonVersion[device['qdisc']] if device['qdisc'] in jsonVersion else False
if element:
drops = int(element['drops'])
packets = int(element['packets'])
bytesSent = int(element['bytes'])
@ -44,6 +52,7 @@ def getDeviceStats(devices):
if 'bytesSentUpload' in device:
device['priorQueryBytesUpload'] = device['bytesSentUpload']
device['bytesSentUpload'] = bytesSent
device['timeQueried'] = datetime.now().isoformat()
for device in devices:
if 'priorQueryTime' in device: