Skip to content

Instantly share code, notes, and snippets.

@thaihust
Forked from racerxdl/measure.py
Created December 12, 2020 06:56
Show Gist options
  • Save thaihust/5fe4a7000f918c918498ee1f02b1427f to your computer and use it in GitHub Desktop.
Save thaihust/5fe4a7000f918c918498ee1f02b1427f to your computer and use it in GitHub Desktop.
Linux Network Traffic Measure/Calculator Python Script
#!/usr/bin/env python
'''
_______ ______
|_ _\ \ / / ___|
| | \ \ / /\___ \
| | \ V / ___) |
|_| \_/ |____/
Teske Virtual System
Made by Lucas Teske
This script reads /proc/net/dev using tools.py to get the data.
The average TX/RX Rates are calculated using an Low Pass Complementary Filter with k
configured as AVG_LOW_PASS. Default to 0.2
This should output something like this on your console:
eth0: in B/S
RX - MAX: 0 AVG: 0 CUR: 0
TX - MAX: 0 AVG: 0 CUR: 0
lo: in B/S
RX - MAX: 1972 AVG: 0 CUR: 0
TX - MAX: 1972 AVG: 0 CUR: 0
wlan0: in B/S
RX - MAX: 167658 AVG: 490 CUR: 188
TX - MAX: 3648202 AVG: 386 CUR: 424
vmnet1: in B/S
RX - MAX: 0 AVG: 0 CUR: 0
TX - MAX: 0 AVG: 0 CUR: 0
'''
import time
import math
from tools import *
INTERVAL = 1 # 1 second
AVG_LOW_PASS = 0.2 # Simple Complemetary Filter
ifaces = {}
print "Loading Network Interfaces"
idata = GetNetworkInterfaces()
print "Filling tables"
for eth in idata:
ifaces[eth["interface"]] = {
"rxrate" : 0,
"txrate" : 0,
"avgrx" : 0,
"avgtx" : 0,
"toptx" : 0,
"toprx" : 0,
"sendbytes" : eth["tx"]["bytes"],
"recvbytes" : eth["rx"]["bytes"]
}
while True:
idata = GetNetworkInterfaces()
for eth in idata:
# Calculate the Rate
ifaces[eth["interface"]]["rxrate"] = (eth["rx"]["bytes"] - ifaces[eth["interface"]]["recvbytes"]) / INTERVAL
ifaces[eth["interface"]]["txrate"] = (eth["tx"]["bytes"] - ifaces[eth["interface"]]["sendbytes"]) / INTERVAL
# Set the rx/tx bytes
ifaces[eth["interface"]]["recvbytes"] = eth["rx"]["bytes"]
ifaces[eth["interface"]]["sendbytes"] = eth["tx"]["bytes"]
# Calculate the Average Rate
ifaces[eth["interface"]]["avgrx"] = int(ifaces[eth["interface"]]["rxrate"] * AVG_LOW_PASS + ifaces[eth["interface"]]["avgrx"] * (1.0-AVG_LOW_PASS))
ifaces[eth["interface"]]["avgtx"] = int(ifaces[eth["interface"]]["txrate"] * AVG_LOW_PASS + ifaces[eth["interface"]]["avgtx"] * (1.0-AVG_LOW_PASS))
# Set the Max Rates
ifaces[eth["interface"]]["toprx"] = ifaces[eth["interface"]]["rxrate"] if ifaces[eth["interface"]]["rxrate"] > ifaces[eth["interface"]]["toprx"] else ifaces[eth["interface"]]["toprx"]
ifaces[eth["interface"]]["toptx"] = ifaces[eth["interface"]]["txrate"] if ifaces[eth["interface"]]["txrate"] > ifaces[eth["interface"]]["toptx"] else ifaces[eth["interface"]]["toptx"]
print "%s: in B/S" %(eth["interface"])
print "\tRX - MAX: %s AVG: %s CUR: %s" %(ifaces[eth["interface"]]["toprx"],ifaces[eth["interface"]]["avgrx"],ifaces[eth["interface"]]["rxrate"])
print "\tTX - MAX: %s AVG: %s CUR: %s" %(ifaces[eth["interface"]]["toptx"],ifaces[eth["interface"]]["avgtx"],ifaces[eth["interface"]]["txrate"])
print ""
time.sleep(INTERVAL)
#!/usr/bin/env python
'''
_______ ______
|_ _\ \ / / ___|
| | \ \ / /\___ \
| | \ V / ___) |
|_| \_/ |____/
Teske Virtual System
Made by Lucas Teske
This is an tool script that I did for getting network interface data
from /proc/net/dev. I got all data you could from /proc/net/dev
Let me know if any linux kernel outputs different things on /proc/net/dev
'''
def GetNetworkInterfaces():
ifaces = []
f = open("/proc/net/dev")
data = f.read()
f.close()
data = data.split("\n")[2:]
for i in data:
if len(i.strip()) > 0:
x = i.split()
# Interface | Receive | Transmit
# iface | bytes packets errs drop fifo frame compressed multicast | bytes packets errs drop fifo frame compressed multicast
k = {
"interface" : x[0][:len( x[0])-1],
"tx" : {
"bytes" : int(x[1]),
"packets" : int(x[2]),
"errs" : int(x[3]),
"drop" : int(x[4]),
"fifo" : int(x[5]),
"frame" : int(x[6]),
"compressed" : int(x[7]),
"multicast" : int(x[8])
},
"rx" : {
"bytes" : int(x[9]),
"packets" : int(x[10]),
"errs" : int(x[11]),
"drop" : int(x[12]),
"fifo" : int(x[13]),
"frame" : int(x[14]),
"compressed" : int(x[15]),
"multicast" : int(x[16])
}
}
ifaces.append(k)
return ifaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment