Added first hopefully running version
This commit is contained in:
parent
d7d286556e
commit
24ea84160c
1 changed files with 80 additions and 41 deletions
|
@ -1,56 +1,95 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import os.path
|
||||
import serial
|
||||
import time
|
||||
import random
|
||||
import logging
|
||||
import argparse
|
||||
import serial
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
def get_bytes(t, iface='enp0s25'):
|
||||
with open('/sys/class/net/' + iface + '/statistics/' + t + '_bytes', 'r') as f:
|
||||
data = f.read();
|
||||
return int(data)
|
||||
|
||||
port_addr = '/dev/ttyACM0'
|
||||
def get_link_speed(iface='enp0s25'):
|
||||
ethtool=subprocess.Popen(['ethtool',iface], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
s = re.search('Speed: (?P<Speed>\d+)Mb/s', ethtool.stdout.read().decode('utf-8'))
|
||||
return int(s.group('Speed')) * 125000
|
||||
|
||||
logging.warning("Staring up")
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-v', '--verbose', action='count', default=0,
|
||||
help='increase logging verbosity')
|
||||
parser.add_argument('-s', '--serial', type=str, default='/dev/ttyACM0',
|
||||
help='serial port for arduino connection')
|
||||
parser.add_argument('-t', '--time', type=float, default=0.02,
|
||||
help='update interval for measurements')
|
||||
parser.add_argument('interfaces', metavar='interface', type=str, nargs='+',
|
||||
help='network interfaces to take into account')
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(port_addr):
|
||||
port = serial.Serial(
|
||||
port=port_addr,
|
||||
baudrate=115200,
|
||||
bytesize=serial.EIGHTBITS,
|
||||
parity=serial.PARITY_NONE,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
timeout=1,
|
||||
xonxoff=False,
|
||||
rtscts=False,
|
||||
dsrdtr=False,
|
||||
write_timeout=None,
|
||||
inter_byte_timeout=None)
|
||||
# Process verbosity
|
||||
if args.verbose == 0:
|
||||
LOGLEVEL = logging.WARNING
|
||||
elif args.verbose == 1:
|
||||
LOGLEVEL = logging.INFO
|
||||
elif args.verbose >= 2:
|
||||
LOGLEVEL = logging.DEBUG
|
||||
|
||||
logging.warning("Opening port")
|
||||
logging.basicConfig(format='%(levelname)s:%(name)s:%(asctime)s:%(message)s', level=LOGLEVEL)
|
||||
logging.info("Network utilization visualization starting up")
|
||||
|
||||
time.sleep(2)
|
||||
if not os.path.exists(args.serial):
|
||||
logging.error("Serial output not available, quitting!")
|
||||
sys.exit(1)
|
||||
else:
|
||||
logging.info("Opening serial port %s", args.serial)
|
||||
port = serial.Serial(
|
||||
port=args.serial,
|
||||
baudrate=115200,
|
||||
bytesize=serial.EIGHTBITS,
|
||||
parity=serial.PARITY_NONE,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
timeout=1,
|
||||
xonxoff=False,
|
||||
rtscts=False,
|
||||
dsrdtr=False,
|
||||
write_timeout=None,
|
||||
inter_byte_timeout=None)
|
||||
|
||||
logging.warning("Opened port")
|
||||
time.sleep(2)
|
||||
|
||||
val1 = random.randint(0,100)
|
||||
val2 = random.randint(0,100)
|
||||
logging.info("Serial port opened")
|
||||
|
||||
logging.warning("Staring with values %d and %d", val1, val2)
|
||||
sum_max = 0
|
||||
ifaces = []
|
||||
for iface in args.interfaces:
|
||||
ifaces.append({
|
||||
'name': iface,
|
||||
'rx': get_bytes('rx', iface),
|
||||
'tx': get_bytes('tx', iface)})
|
||||
sum_max += get_link_speed(iface) * args.time
|
||||
|
||||
while True:
|
||||
val1 += random.randint(-10,10)
|
||||
val2 += random.randint(-10,10)
|
||||
if val1 < 0:
|
||||
val1 = 0
|
||||
if val1 > 100:
|
||||
val1 = 100
|
||||
if val2 < 0:
|
||||
val2 = 0
|
||||
if val2 > 100:
|
||||
val2 = 100
|
||||
while True:
|
||||
sum_rx = 0;
|
||||
sum_tx = 0;
|
||||
|
||||
message = bytearray(3)
|
||||
message[0] = 0xFF
|
||||
message[1] = val1
|
||||
message[2] = val2
|
||||
for iface in ifaces:
|
||||
if_rx = get_bytes('rx', iface['name'])
|
||||
if_tx = get_bytes('tx', iface['name'])
|
||||
sum_rx += if_rx - iface['rx']
|
||||
sum_tx += if_tx - iface['tx']
|
||||
iface['rx'] = if_rx
|
||||
iface['tx'] = if_tx
|
||||
|
||||
port.write(message)
|
||||
message = bytearray(3)
|
||||
message[0] = 0xFF
|
||||
message[1] = int(sum_tx * 100 / sum_max)
|
||||
message[2] = int(sum_rx * 100 / sum_max)
|
||||
|
||||
time.sleep(0.01)
|
||||
port.write(message)
|
||||
|
||||
time.sleep(args.time)
|
||||
|
|
Loading…
Reference in a new issue