forked from vitawasalreadytaken/tcviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcviz.py
executable file
·72 lines (49 loc) · 1.65 KB
/
tcviz.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
#!/usr/bin/env python
# tcviz 1.2
#
# Licensed under the terms of the MIT/X11 license.
# Copyright (c) 2009-2013 Vita Smid <http://ze.phyr.us>
import subprocess, sys
from Node import Node
from Filter import Filter
TCPATH = '/sbin/tc'
def main():
if len(sys.argv) == 2:
(q, c, f) = [ readTc([type, 'show', 'dev', sys.argv[1]]) for type in ('qdisc', 'class', 'filter') ]
elif len(sys.argv) == 4:
(q, c, f) = [ readFile(p) for p in sys.argv[1:4] ]
else:
usage()
return 1
nodes = parse(q, Node) + parse(c, Node)
filters = parse(f, Filter)
gv = 'digraph tc { %s \n %s \n %s \n %s }' % (genSetup(), genNodes(nodes), genEdges(nodes), genEdges(filters))
print gv
return 0
def usage():
print >>sys.stderr, 'Usage: %s <interface>' % sys.argv[0]
print >>sys.stderr, '\nOR'
print >>sys.stderr, 'If you want to feed tcviz with offline data:'
print >>sys.stderr, '%s <qdiscs file> <classes file> <filters file>' % sys.argv[0]
def readFile(path):
return open(path).read()
def readTc(args):
return subprocess.Popen([TCPATH] + args, stdout = subprocess.PIPE).communicate()[0]
def parse(string, constructor):
specs = []
for line in string.split('\n'):
if not line:
continue
elif line[:2] == ' ': # continuation of the previous line
specs[-1] += ' ' + line.strip()
else:
specs.append(line.strip())
return [ constructor(spec) for spec in specs ]
def genSetup():
return 'node [ fontname = "DejaVu Sans" ]; edge [ fontname = "DejaVu Sans" ];'
def genNodes(objects):
return '\n'.join([ o.getNodeSpec() for o in objects ])
def genEdges(objects):
return '\n'.join([ o.getEdgeSpec() for o in objects ])
if __name__ == '__main__':
sys.exit(main())