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