This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic.py
81 lines (59 loc) · 2.22 KB
/
traffic.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
import usnmp
import time
import socket
import sys
import gc
class Traffic:
agent_ip = "10.23.42.254"
agent_port = 161
agent_community = "public"
# oid of incoming bytes on wan-port
oid_if_inoct = "1.3.6.1.2.1.2.2.1.10.9"
# oid of uptime
oid_uptime = "1.3.6.1.2.1.1.3.0"
# oid of ports speed
oid_speed = "1.3.6.1.2.1.2.2.1.5.9"
sock = None
greq = None
gresp = None
last_ut = None
last_in8 = None
speed = None
def __init__(self):
gc.collect()
self.sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self.sock.settimeout(1)
self.greq = usnmp.SnmpPacket(type=usnmp.SNMP_GETREQUEST, community=self.agent_community, id=time.ticks_us())
for i in (self.oid_speed, self.oid_uptime, self.oid_if_inoct):
self.greq.varbinds[i] = None
self.sock.sendto(self.greq.tobytes(), (self.agent_ip, self.agent_port))
d = self.sock.recvfrom(1024)
self.gresp = usnmp.SnmpPacket(d[0])
self.last_ut = self.gresp.varbinds[self.oid_uptime][1]
self.last_in8 = self.gresp.varbinds[self.oid_if_inoct][1]
def get_traffic(self):
gc.collect()
speed = self.gresp.varbinds[self.oid_speed][1]
time.sleep(1)
self.greq.id = time.ticks_us()
self.sock.sendto(self.greq.tobytes(), (self.agent_ip, self.agent_port))
try:
d = self.sock.recvfrom(1024)
self.gresp = usnmp.SnmpPacket(d[0])
if self.greq.id == self.gresp.id:
ut = self.gresp.varbinds[self.oid_uptime][1]
in8 = self.gresp.varbinds[self.oid_if_inoct][1]
if in8 != self.last_in8:
print("New traffic value!")
timediff = ut - self.last_ut
traffic = in8 - self.last_in8
mbps = (traffic * 8 * 100) / (timediff/100 * speed)
print(" --> " + str(timediff/100) + " seconds")
print(" --> " + str(mbps) + " Mb/s")
self.last_in8 = in8
self.last_ut = ut
return mbps
except Exception as e:
print("error")
print(e.args[0])
return 0