-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmonitor.py
72 lines (63 loc) · 2.31 KB
/
monitor.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
from time import sleep, time
from subprocess import *
import re
default_dir = '.'
def monitor_qlen(iface, interval_sec = 0.01, fname='%s/qlen.txt' % default_dir, host=None):
pat_queued = re.compile(r'backlog\s[^\s]+\s([\d]+)p')
cmd = "tc -s qdisc show dev %s" % (iface)
# monitoring is run on host if provided
runner = Popen if host is None else host.popen
ret = []
open(fname, 'w').write('')
while 1:
p = runner(cmd, shell=True, stdout=PIPE)
output = p.stdout.read()
# Not quite right, but will do for now
matches = pat_queued.findall(output)
if matches and len(matches) > 1:
ret.append(matches[1])
t = "%f" % time()
open(fname, 'a').write(t + ',' + matches[1] + '\n')
sleep(interval_sec)
return
def monitor_devs_ng(fname="%s/txrate.txt" % default_dir, interval_sec=0.01):
"""Uses bwm-ng tool to collect iface tx rate stats. Very reliable."""
cmd = ("sleep 1; bwm-ng -t %s -o csv "
"-u bits -T rate -C ',' > %s" %
(interval_sec * 1000, fname))
Popen(cmd, shell=True).wait()
def capture_packets(options="", fname='%s/capture.dmp' % default_dir, runner=None):
cmd = "tcpdump -w {} {}".format(fname, options)
print cmd
runner = Popen if runner is None else runner
return runner(cmd, shell=True).wait()
def monitor_bbr(dst, interval_sec = 0.01, fname='%s/bbr.txt' % default_dir, runner=None):
cmd = "ss -iet dst %s" % (dst)
runner = Popen if runner is None else runner
print dst
ret = []
open(fname, 'w').write('')
while 1:
p = runner(cmd, shell=True, stdout=PIPE)
output = p.stdout.read()
try:
start = output.find("bbr:(")
end = output.find(")", start)
if start == -1 or end == -1:
continue
data_elems = output[start+5:end].split(",")
data = {}
for d in data_elems:
k, v = d.split(":")
data[k] = v
csvformat = "%s, %s, %s, %s" % (
data["bw"],
data["mrtt"],
data["pacing_gain"],
data["cwnd_gain"]
)
open(fname, 'a').write(csvformat + "\n")
except Exception:
pass
sleep(interval_sec)
return