-
Notifications
You must be signed in to change notification settings - Fork 7
/
net.bak
121 lines (93 loc) · 2.64 KB
/
net.bak
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
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
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
#print 'In: ' + str(round(currInBytes,1)) + 'bytes/sec'+ '\tOut: ' + str(round(currOutBytes,1)) + 'bytes/sec' + '\tElapsed ' + str(round(elapsed,2))
#draw graph
inHeight = 0.0
outHeight = 0.0
if currInBytes > 0:
inHeight = float(currInBytes * height / maxRateIn)
if currOutBytes > 0:
outHeight = float(currOutBytes * height / 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)
#display
disp.image(image)
disp.display()