[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
|
---|
[9989] | 13 | import yaml
|
---|
[8240] | 14 |
|
---|
[9809] | 15 | OUTFILE = os.path.join(os.getcwd(),'network.png')
|
---|
[8240] | 16 |
|
---|
[9989] | 17 | store = yaml.load(open('store.yaml','r'))
|
---|
| 18 |
|
---|
[8240] | 19 | def make_graph():
|
---|
[8268] | 20 | poel = {}
|
---|
| 21 | link_type = {}
|
---|
[9989] | 22 | link_data = {}
|
---|
[8268] | 23 | try:
|
---|
[8296] | 24 | for host in gformat.get_hostlist():
|
---|
[8268] | 25 | print "## Processing host", host
|
---|
| 26 | datadump = gformat.get_yaml(host)
|
---|
| 27 | iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
| 28 | for iface_key in iface_keys:
|
---|
| 29 | l = datadump[iface_key]['ip']
|
---|
| 30 | addr, mask = l.split('/')
|
---|
[8240] | 31 |
|
---|
[8281] | 32 | # Not parsing of these folks please
|
---|
[8321] | 33 | if not gformat.valid_addr(addr):
|
---|
[8281] | 34 | continue
|
---|
| 35 |
|
---|
[8268] | 36 | addr = gformat.parseaddr(addr)
|
---|
| 37 | mask = int(mask)
|
---|
| 38 | addr = addr & ~((1 << (32 - mask)) - 1)
|
---|
| 39 | if poel.has_key(addr):
|
---|
| 40 | poel[addr] += [host]
|
---|
| 41 | else:
|
---|
| 42 | poel[addr] = [host]
|
---|
[9989] | 43 | # Assume all ns_ip to be 11a for a moment
|
---|
| 44 | if datadump[iface_key].has_key('ns_ip'):
|
---|
[8268] | 45 | link_type[addr] = '11a'
|
---|
| 46 | else:
|
---|
| 47 | link_type[addr] = datadump[iface_key]['type']
|
---|
[9989] | 48 |
|
---|
| 49 | link_data[addr] = 1
|
---|
| 50 | iface = datadump[iface_key]['interface']
|
---|
| 51 | nodename = datadump['nodename']
|
---|
| 52 | print nodename, iface
|
---|
| 53 | INTERVAL = 60 * 10
|
---|
| 54 | if store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
|
---|
| 55 | if store['traffic'][nodename].has_key(iface):
|
---|
| 56 | (b_in, b_out) = store['traffic'][nodename][iface]
|
---|
| 57 | uptime = store['uptime'][nodename]
|
---|
| 58 | t_kb = float(b_in + b_out) / 1024
|
---|
| 59 | print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
|
---|
| 60 | retval = ((t_kb) / uptime) * INTERVAL
|
---|
| 61 | link_data[addr] = retval
|
---|
| 62 |
|
---|
[8268] | 63 | print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
|
---|
| 64 | except (KeyError, ValueError), e:
|
---|
| 65 | print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
|
---|
| 66 | print e
|
---|
| 67 | sys.exit(1)
|
---|
| 68 |
|
---|
[9989] | 69 | #f = tempfile.NamedTemporaryFile(bufsize=0)
|
---|
| 70 | f = open('/tmp/test.dot','w')
|
---|
[8268] | 71 | sys.stderr.write("# Building temponary graph file\n")
|
---|
| 72 | print >> f, "Graph WirelessLeidenNetwork {"
|
---|
| 73 | print >> f ,"""
|
---|
[8240] | 74 | graph [ fontsize = 36,
|
---|
| 75 | overlap = scalexy,
|
---|
| 76 | splines = true,
|
---|
| 77 | ]
|
---|
| 78 | """
|
---|
[8268] | 79 | for addr,leden in poel.iteritems():
|
---|
| 80 | if link_type[addr] == '11a':
|
---|
[9989] | 81 | color = 'green'
|
---|
[8268] | 82 | weight = 4
|
---|
| 83 | elif link_type[addr] == 'eth':
|
---|
| 84 | color = 'blue'
|
---|
| 85 | weight = 8
|
---|
| 86 | else:
|
---|
| 87 | color = 'black'
|
---|
| 88 | weight = 1
|
---|
[9989] | 89 | width = max(1,link_data[addr])
|
---|
[8268] | 90 | leden = sorted(set(leden))
|
---|
| 91 | for index,lid in enumerate(leden[:-1]):
|
---|
| 92 | for buur in leden[index + 1:]:
|
---|
[9989] | 93 | print >> f,' %s -- %s [label="%s", color="%s", weight="%s", style="setlinewidth(%s)"]' % (lid, buur, gformat.showaddr(addr), color, weight, width)
|
---|
[8268] | 94 | print >> f, "}"
|
---|
[9989] | 95 | f.flush()
|
---|
[8268] | 96 | sys.stderr.write("# Plotting temponary graph file using graphviz\n")
|
---|
| 97 | retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
|
---|
| 98 | if retval != 0:
|
---|
| 99 | sys.stderr.write("# FAILED [%i]\n" % retval)
|
---|
| 100 | subprocess.call(["cat",f.name])
|
---|
| 101 | exit(1)
|
---|
| 102 | sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
|
---|
[8240] | 103 |
|
---|
| 104 |
|
---|
| 105 | if __name__ == "__main__":
|
---|
[8268] | 106 | make_graph()
|
---|
[8240] | 107 |
|
---|