-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.py
55 lines (45 loc) · 1.54 KB
/
importer.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
import sys
import threading
import time
from random import randint
from managed import Net
class Importer:
def __init__(self, interfaces, reverse, code=None):
if code:
self.code = code
else:
self.code = str(randint(0, 2**32))
self.ipset = Net(self.code, interfaces, reverse)
self.data = [self.ipset.get_all_flows()]
def run(x):
while True:
time.sleep(1)
flows = x.ipset.get_all_flows()
x.data = x.data[-60:]+[flows]
t = threading.Thread(target=run, args=(self,))
t.start()
def get_data(self):
if len(self.data) > 15:
self.time = [-len(self.data), -16, -2]
elif len(self.data) > 1:
self.time = [-len(self.data)]*2+[-2]
else:
self.time = [-1]*3
now = self.data[-1]
measurements = [self.data[t] for t in self.time]
flows_usage = {}
for flow in now:
flow_now = now[flow]
flow_id = (flow_now.source, flow_now.port, flow_now.destination)
down_now = flow_now.down
up_now = flow_now.up
def delta(user):
if user is None:
return (down_now, up_now)
else:
return (down_now - user.down, up_now - user.up)
down, up = zip(*[delta(m.get(flow)) for m in measurements])
flows_usage[flow_id] = down, up
return flows_usage
def get_raw_data(self):
return self.data[-1]