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