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_hostlist():
|
---|
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 | # Not parsing of these folks please
|
---|
28 | if "192.168" in addr:
|
---|
29 | continue
|
---|
30 | if "0.0.0.0" in addr:
|
---|
31 | continue
|
---|
32 |
|
---|
33 | addr = gformat.parseaddr(addr)
|
---|
34 | mask = int(mask)
|
---|
35 | addr = addr & ~((1 << (32 - mask)) - 1)
|
---|
36 | if poel.has_key(addr):
|
---|
37 | poel[addr] += [host]
|
---|
38 | else:
|
---|
39 | poel[addr] = [host]
|
---|
40 | # Assume all eth2wifibridge to be 11a for a moment
|
---|
41 | if datadump[iface_key].has_key('eth2wifibridge'):
|
---|
42 | link_type[addr] = '11a'
|
---|
43 | else:
|
---|
44 | link_type[addr] = datadump[iface_key]['type']
|
---|
45 | print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
|
---|
46 | except (KeyError, ValueError), e:
|
---|
47 | print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
|
---|
48 | print e
|
---|
49 | sys.exit(1)
|
---|
50 |
|
---|
51 | f = tempfile.NamedTemporaryFile(bufsize=0)
|
---|
52 | sys.stderr.write("# Building temponary graph file\n")
|
---|
53 | print >> f, "Graph WirelessLeidenNetwork {"
|
---|
54 | print >> f ,"""
|
---|
55 | graph [ fontsize = 36,
|
---|
56 | overlap = scalexy,
|
---|
57 | splines = true,
|
---|
58 | ]
|
---|
59 | """
|
---|
60 | for addr,leden in poel.iteritems():
|
---|
61 | if link_type[addr] == '11a':
|
---|
62 | color = 'red'
|
---|
63 | weight = 4
|
---|
64 | elif link_type[addr] == 'eth':
|
---|
65 | color = 'blue'
|
---|
66 | weight = 8
|
---|
67 | else:
|
---|
68 | color = 'black'
|
---|
69 | weight = 1
|
---|
70 | leden = sorted(set(leden))
|
---|
71 | for index,lid in enumerate(leden[:-1]):
|
---|
72 | for buur in leden[index + 1:]:
|
---|
73 | print >> f,' %s -- %s [label="%s", color="%s", weight="%s"]' % (lid, buur, gformat.showaddr(addr), color, weight)
|
---|
74 | print >> f, "}"
|
---|
75 | sys.stderr.write("# Plotting temponary graph file using graphviz\n")
|
---|
76 | retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
|
---|
77 | if retval != 0:
|
---|
78 | sys.stderr.write("# FAILED [%i]\n" % retval)
|
---|
79 | subprocess.call(["cat",f.name])
|
---|
80 | exit(1)
|
---|
81 | sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
|
---|
82 |
|
---|
83 |
|
---|
84 | if __name__ == "__main__":
|
---|
85 | make_graph()
|
---|
86 |
|
---|