Utoljára aktív 1 month ago

Linux Network Traffic Measure/Calculator Python Script

Lucas Teske gist felülvizsgálása 11 years ago. Revízióhoz ugrás

2 files changed, 138 insertions

measure.py(fájl létrehozva)

@@ -0,0 +1,83 @@
1 + #!/usr/bin/env python
2 +
3 + '''
4 + _______ ______
5 + |_ _\ \ / / ___|
6 + | | \ \ / /\___ \
7 + | | \ V / ___) |
8 + |_| \_/ |____/
9 + Teske Virtual System
10 +
11 + Made by Lucas Teske
12 +
13 + This script reads /proc/net/dev using tools.py to get the data.
14 + The average TX/RX Rates are calculated using an Low Pass Complementary Filter with k
15 + configured as AVG_LOW_PASS. Default to 0.2
16 +
17 + This should output something like this on your console:
18 + eth0: in B/S
19 + RX - MAX: 0 AVG: 0 CUR: 0
20 + TX - MAX: 0 AVG: 0 CUR: 0
21 +
22 + lo: in B/S
23 + RX - MAX: 1972 AVG: 0 CUR: 0
24 + TX - MAX: 1972 AVG: 0 CUR: 0
25 +
26 + wlan0: in B/S
27 + RX - MAX: 167658 AVG: 490 CUR: 188
28 + TX - MAX: 3648202 AVG: 386 CUR: 424
29 +
30 + vmnet1: in B/S
31 + RX - MAX: 0 AVG: 0 CUR: 0
32 + TX - MAX: 0 AVG: 0 CUR: 0
33 +
34 + '''
35 +
36 + import time
37 + import math
38 + from tools import *
39 +
40 + INTERVAL = 1 # 1 second
41 + AVG_LOW_PASS = 0.2 # Simple Complemetary Filter
42 +
43 + ifaces = {}
44 +
45 + print "Loading Network Interfaces"
46 + idata = GetNetworkInterfaces()
47 + print "Filling tables"
48 + for eth in idata:
49 + ifaces[eth["interface"]] = {
50 + "rxrate" : 0,
51 + "txrate" : 0,
52 + "avgrx" : 0,
53 + "avgtx" : 0,
54 + "toptx" : 0,
55 + "toprx" : 0,
56 + "sendbytes" : eth["tx"]["bytes"],
57 + "recvbytes" : eth["rx"]["bytes"]
58 + }
59 +
60 + while True:
61 + idata = GetNetworkInterfaces()
62 + for eth in idata:
63 + # Calculate the Rate
64 + ifaces[eth["interface"]]["rxrate"] = (eth["rx"]["bytes"] - ifaces[eth["interface"]]["recvbytes"]) / INTERVAL
65 + ifaces[eth["interface"]]["txrate"] = (eth["tx"]["bytes"] - ifaces[eth["interface"]]["sendbytes"]) / INTERVAL
66 +
67 + # Set the rx/tx bytes
68 + ifaces[eth["interface"]]["recvbytes"] = eth["rx"]["bytes"]
69 + ifaces[eth["interface"]]["sendbytes"] = eth["tx"]["bytes"]
70 +
71 + # Calculate the Average Rate
72 + ifaces[eth["interface"]]["avgrx"] = int(ifaces[eth["interface"]]["rxrate"] * AVG_LOW_PASS + ifaces[eth["interface"]]["avgrx"] * (1.0-AVG_LOW_PASS))
73 + ifaces[eth["interface"]]["avgtx"] = int(ifaces[eth["interface"]]["txrate"] * AVG_LOW_PASS + ifaces[eth["interface"]]["avgtx"] * (1.0-AVG_LOW_PASS))
74 +
75 + # Set the Max Rates
76 + ifaces[eth["interface"]]["toprx"] = ifaces[eth["interface"]]["rxrate"] if ifaces[eth["interface"]]["rxrate"] > ifaces[eth["interface"]]["toprx"] else ifaces[eth["interface"]]["toprx"]
77 + ifaces[eth["interface"]]["toptx"] = ifaces[eth["interface"]]["txrate"] if ifaces[eth["interface"]]["txrate"] > ifaces[eth["interface"]]["toptx"] else ifaces[eth["interface"]]["toptx"]
78 +
79 + print "%s: in B/S" %(eth["interface"])
80 + print "\tRX - MAX: %s AVG: %s CUR: %s" %(ifaces[eth["interface"]]["toprx"],ifaces[eth["interface"]]["avgrx"],ifaces[eth["interface"]]["rxrate"])
81 + print "\tTX - MAX: %s AVG: %s CUR: %s" %(ifaces[eth["interface"]]["toptx"],ifaces[eth["interface"]]["avgtx"],ifaces[eth["interface"]]["txrate"])
82 + print ""
83 + time.sleep(INTERVAL)

tools.py(fájl létrehozva)

@@ -0,0 +1,55 @@
1 + #!/usr/bin/env python
2 +
3 + '''
4 + _______ ______
5 + |_ _\ \ / / ___|
6 + | | \ \ / /\___ \
7 + | | \ V / ___) |
8 + |_| \_/ |____/
9 + Teske Virtual System
10 +
11 + Made by Lucas Teske
12 +
13 + This is an tool script that I did for getting network interface data
14 + from /proc/net/dev. I got all data you could from /proc/net/dev
15 +
16 + Let me know if any linux kernel outputs different things on /proc/net/dev
17 +
18 + '''
19 +
20 + def GetNetworkInterfaces():
21 + ifaces = []
22 + f = open("/proc/net/dev")
23 + data = f.read()
24 + f.close()
25 + data = data.split("\n")[2:]
26 + for i in data:
27 + if len(i.strip()) > 0:
28 + x = i.split()
29 + # Interface | Receive | Transmit
30 + # iface | bytes packets errs drop fifo frame compressed multicast | bytes packets errs drop fifo frame compressed multicast
31 + k = {
32 + "interface" : x[0][:len( x[0])-1],
33 + "tx" : {
34 + "bytes" : int(x[1]),
35 + "packets" : int(x[2]),
36 + "errs" : int(x[3]),
37 + "drop" : int(x[4]),
38 + "fifo" : int(x[5]),
39 + "frame" : int(x[6]),
40 + "compressed" : int(x[7]),
41 + "multicast" : int(x[8])
42 + },
43 + "rx" : {
44 + "bytes" : int(x[9]),
45 + "packets" : int(x[10]),
46 + "errs" : int(x[11]),
47 + "drop" : int(x[12]),
48 + "fifo" : int(x[13]),
49 + "frame" : int(x[14]),
50 + "compressed" : int(x[15]),
51 + "multicast" : int(x[16])
52 + }
53 + }
54 + ifaces.append(k)
55 + return ifaces
Újabb Régebbi