-
Notifications
You must be signed in to change notification settings - Fork 7
/
net.py
166 lines (131 loc) · 3.93 KB
/
net.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import subprocess
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Image
import ImageDraw
import ImageFont
# Raspberry Pi pin configuration:
RST = 24
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Load default font.
font = ImageFont.load_default()
#Display Image
disp.image(image)
disp.display()
#print 'Press Ctl-C to exit'
#functions
def getSnmpData (oid):
try:
data = subprocess.check_output("snmpget -v 1 -c public 192.168.0.1 " + oid, shell = True)
except subprocess.CalledProcessError as e:
data = e
else:
data = "unhandled error"
return data;
def getSnmpInt (oid):
try:
data = subprocess.check_output("snmpget -v 1 -c public 192.168.0.1 " + oid, shell = True)
data = data.split()
data = data.pop()
except:
data = "0"
return int(data)
def drawBar (x, barHeight):
# parameters are x, y, end x, end y
draw.rectangle ((x, height - barHeight, x + 10, height -1), outline=255, fill=255)
def textRate (rate):
rate = rate * 8 / 1000
if rate < 1000:
result = str(round(rate,1)) + ' kbps'
else:
result = str(round(rate/1000,1)) + ' mbps'
return result
#defines
oidInWan = '1.3.6.1.2.1.2.2.1.10.1'
oidOutWan = '1.3.6.1.2.1.2.2.1.16.1'
lastInBytes = getSnmpInt (oidInWan);
lastOutBytes = getSnmpInt (oidOutWan);
lastTime = time.time()
maxRateIn = 565000
maxRateOut = 120000
#timed array vars
timerTime = time.time()
highestSpeedIn = 0
highestSpeedOut = 0
speedArrayIn = []
speedArrayOut = []
inMax = 0
outMax = 0
while (1):
time.sleep(0.5)
draw.rectangle((0, 0, width, height), outline=0, fill=0)
now = time.time()
elapsed = now - lastTime
lastTime = now
#calculate rates in and out
inBytes = getSnmpInt (oidInWan)
currInBytes = (inBytes - lastInBytes) / elapsed
lastInBytes = inBytes
outBytes = getSnmpInt (oidOutWan)
currOutBytes = (outBytes - lastOutBytes) / elapsed
lastOutBytes = outBytes
#max rate last 24 hours calculations
if currInBytes > highestSpeedIn:
highestSpeedIn = currInBytes
if currOutBytes > highestSpeedOut:
highestSpeedOut = currOutBytes
if now > timerTime + 3600:
print '----------------------------------------------------------------- time expired'
timerTime = now
speedArrayIn.append (highestSpeedIn)
if len (speedArrayIn) > 23:
del speedArrayIn[0]
inMax = max(speedArrayIn)
speedArrayOut.append (highestSpeedOut)
if len (speedArrayOut) > 23:
del speedArrayOut[0]
outMax = max(speedArrayOut)
highestSpeedIn = 0
highestSpeedOut = 0
#test output
for inSpd in speedArrayIn:
print '--- ' + str(inSpd)
print 'inMax ' + str(inMax)
#adjust these in each loop in case we find a faster speed
inMax = max(inMax, highestSpeedIn)
outMax = max(outMax, highestSpeedOut)
#print str(now) + ' ' + str(timerTime)
print 'In: ' + textRate(currInBytes) + '\tOut: ' + textRate(currOutBytes) + '\tElapsed ' + str(round(elapsed,2)) + '\t\tInMax ' + textRate(inMax) + '\tOutMax ' + textRate(outMax)
#print 'Hi In ' + str(highestSpeedIn) + ' Hi Out ' + str(highestSpeedOut)
#draw graph
inHeight = 0.0
outHeight = 0.0
if currInBytes > 0:
inHeight = float(currInBytes * height / inMax) #was maxRateIn
if currOutBytes > 0:
outHeight = float(currOutBytes * height / outMax) #was maxRateOut
drawBar (0, inHeight)
drawBar (117, outHeight)
#write rates
draw.text((20,44), textRate(currInBytes), font=font, fill=255)
draw.text((20,54), textRate(currOutBytes), font=font, fill=255)
#max rates
draw.text((20,15), textRate(inMax), font=font, fill=255)
draw.text((20,25), textRate(outMax), font=font, fill=255)
#display
disp.image(image)
disp.display()