1 | #!/usr/bin/env python
|
---|
2 | # vim:ts=2:et:sw=2:ai
|
---|
3 | #
|
---|
4 | # Build topological network graph
|
---|
5 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
6 | import re
|
---|
7 | import sys
|
---|
8 | import glob
|
---|
9 | import tempfile
|
---|
10 | import subprocess
|
---|
11 | import gformat
|
---|
12 |
|
---|
13 | OUTFILE = 'network.png'
|
---|
14 |
|
---|
15 | def make_graph():
|
---|
16 | poel = {}
|
---|
17 | link_type = {}
|
---|
18 | try:
|
---|
19 | for host in gformat.get_proxylist() + gformat.get_nodelist():
|
---|
20 | print "## Processing host", host
|
---|
21 | datadump = gformat.get_yaml(host)
|
---|
22 | iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
23 | for iface_key in iface_keys:
|
---|
24 | l = datadump[iface_key]['ip']
|
---|
25 | addr, mask = l.split('/')
|
---|
26 |
|
---|
27 | addr = gformat.parseaddr(addr)
|
---|
28 | mask = int(mask)
|
---|
29 | addr = addr & ~((1 << (32 - mask)) - 1)
|
---|
30 | if poel.has_key(addr):
|
---|
31 | poel[addr] += [host]
|
---|
32 | else:
|
---|
33 | poel[addr] = [host]
|
---|
34 | # Assume all eth2wifibridge to be 11a for a moment
|
---|
35 | if datadump[iface_key].has_key('eth2wifibridge'):
|
---|
36 | link_type[addr] = '11a'
|
---|
37 | else:
|
---|
38 | link_type[addr] = datadump[iface_key]['type']
|
---|
39 | print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
|
---|
40 | except (KeyError, ValueError), e:
|
---|
41 | print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
|
---|
42 | print e
|
---|
43 | sys.exit(1)
|
---|
44 |
|
---|
45 | f = tempfile.NamedTemporaryFile(bufsize=0)
|
---|
46 | sys.stderr.write("# Building temponary graph file\n")
|
---|
47 | print >> f, "Graph WirelessLeidenNetwork {"
|
---|
48 | print >> f ,"""
|
---|
49 | graph [ fontsize = 36,
|
---|
50 | overlap = scalexy,
|
---|
51 | splines = true,
|
---|
52 | ]
|
---|
53 | """
|
---|
54 | for addr,leden in poel.iteritems():
|
---|
55 | if link_type[addr] == '11a':
|
---|
56 | color = 'red'
|
---|
57 | weight = 4
|
---|
58 | elif link_type[addr] == 'eth':
|
---|
59 | color = 'blue'
|
---|
60 | weight = 8
|
---|
61 | else:
|
---|
62 | color = 'black'
|
---|
63 | weight = 1
|
---|
64 | leden = sorted(set(leden))
|
---|
65 | for index,lid in enumerate(leden[:-1]):
|
---|
66 | for buur in leden[index + 1:]:
|
---|
67 | print >> f,' %s -- %s [label="%s", color="%s", weight="%s"]' % (lid, buur, gformat.showaddr(addr), color, weight)
|
---|
68 | print >> f, "}"
|
---|
69 | sys.stderr.write("# Plotting temponary graph file using graphviz\n")
|
---|
70 | retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
|
---|
71 | if retval != 0:
|
---|
72 | sys.stderr.write("# FAILED [%i]\n" % retval)
|
---|
73 | subprocess.call(["cat",f.name])
|
---|
74 | exit(1)
|
---|
75 | sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
|
---|
76 |
|
---|
77 |
|
---|
78 | if __name__ == "__main__":
|
---|
79 | make_graph()
|
---|
80 |
|
---|