[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 |
|
---|
[10038] | 17 | try:
|
---|
| 18 | store = yaml.load(open('store.yaml','r'))
|
---|
| 19 | except IOError:
|
---|
| 20 | store = {'uptime' : {}, 'snmp' : {}, 'traffic' : {}}
|
---|
| 21 | pass
|
---|
[9989] | 22 |
|
---|
[11753] | 23 | def pass_filter(host):
|
---|
| 24 | for t in sys.argv:
|
---|
| 25 | if t in host:
|
---|
| 26 | return True
|
---|
| 27 | return False
|
---|
| 28 |
|
---|
| 29 | nodes = {}
|
---|
| 30 |
|
---|
[8240] | 31 | def make_graph():
|
---|
[8268] | 32 | poel = {}
|
---|
| 33 | link_type = {}
|
---|
[9989] | 34 | link_data = {}
|
---|
[8268] | 35 | try:
|
---|
[8296] | 36 | for host in gformat.get_hostlist():
|
---|
[11753] | 37 | if not pass_filter(host):
|
---|
| 38 | continue
|
---|
[8268] | 39 | print "## Processing host", host
|
---|
| 40 | datadump = gformat.get_yaml(host)
|
---|
[11753] | 41 | nodename = datadump['nodename']
|
---|
[8268] | 42 | iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
[11753] | 43 | nodes[nodename] = datadump
|
---|
[8268] | 44 | for iface_key in iface_keys:
|
---|
| 45 | l = datadump[iface_key]['ip']
|
---|
| 46 | addr, mask = l.split('/')
|
---|
[8240] | 47 |
|
---|
[8281] | 48 | # Not parsing of these folks please
|
---|
[8321] | 49 | if not gformat.valid_addr(addr):
|
---|
[8281] | 50 | continue
|
---|
| 51 |
|
---|
[8268] | 52 | addr = gformat.parseaddr(addr)
|
---|
| 53 | mask = int(mask)
|
---|
| 54 | addr = addr & ~((1 << (32 - mask)) - 1)
|
---|
[11753] | 55 | nk = nodename + ':' + datadump[iface_key]['autogen_ifname']
|
---|
[8268] | 56 | if poel.has_key(addr):
|
---|
[11753] | 57 | poel[addr] += [nk]
|
---|
[8268] | 58 | else:
|
---|
[11753] | 59 | poel[addr] = [nk]
|
---|
[9989] | 60 | # Assume all ns_ip to be 11a for a moment
|
---|
| 61 | if datadump[iface_key].has_key('ns_ip'):
|
---|
[8268] | 62 | link_type[addr] = '11a'
|
---|
| 63 | else:
|
---|
| 64 | link_type[addr] = datadump[iface_key]['type']
|
---|
[9989] | 65 |
|
---|
| 66 | link_data[addr] = 1
|
---|
[10890] | 67 | iface = datadump[iface_key]['autogen_ifname']
|
---|
[9989] | 68 | print nodename, iface
|
---|
| 69 | INTERVAL = 60 * 10
|
---|
| 70 | if store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
|
---|
| 71 | if store['traffic'][nodename].has_key(iface):
|
---|
| 72 | (b_in, b_out) = store['traffic'][nodename][iface]
|
---|
| 73 | uptime = store['uptime'][nodename]
|
---|
| 74 | t_kb = float(b_in + b_out) / 1024
|
---|
| 75 | print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
|
---|
| 76 | retval = ((t_kb) / uptime) * INTERVAL
|
---|
| 77 | link_data[addr] = retval
|
---|
| 78 |
|
---|
[8268] | 79 | print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
|
---|
| 80 | except (KeyError, ValueError), e:
|
---|
| 81 | print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
|
---|
| 82 | print e
|
---|
| 83 | sys.exit(1)
|
---|
| 84 |
|
---|
[9989] | 85 | f = open('/tmp/test.dot','w')
|
---|
[8268] | 86 | sys.stderr.write("# Building temponary graph file\n")
|
---|
[11753] | 87 | print >> f, "digraph WirelessLeidenNetwork {"
|
---|
[8268] | 88 | print >> f ,"""
|
---|
[11753] | 89 | graph [ fontsize = 10,
|
---|
[8240] | 90 | overlap = scalexy,
|
---|
| 91 | splines = true,
|
---|
[11753] | 92 | rankdir = LR,
|
---|
[8240] | 93 | ]
|
---|
[11753] | 94 | node [ fontsize = 10, ]
|
---|
| 95 | edge [ fontsize = 10, ]
|
---|
[8240] | 96 | """
|
---|
[11753] | 97 | for node, datadump in nodes.iteritems():
|
---|
| 98 | lines = [node]
|
---|
| 99 | for ik in datadump['autogen_iface_keys']:
|
---|
| 100 | if 'alias' in ik or 'wlan' in datadump[ik]['autogen_ifname']:
|
---|
| 101 | continue
|
---|
| 102 | if datadump[ik].has_key('ns_mac'):
|
---|
| 103 | lines.append('<%(autogen_ifname)s> %(bridge_type)s at %(autogen_ifname)s - %(ns_mac)s\\n%(ssid)s\\n%(ns_ip)s' % datadump[ik])
|
---|
| 104 | else:
|
---|
| 105 | lines.append('<%(autogen_ifname)s> %(autogen_ifname)s' % datadump[ik])
|
---|
| 106 | print >> f,"""
|
---|
| 107 | "%s" [
|
---|
| 108 | shape = "record"
|
---|
| 109 | label = "%s"
|
---|
| 110 | ];
|
---|
| 111 | """ % (node, "|".join(lines))
|
---|
| 112 |
|
---|
| 113 |
|
---|
[8268] | 114 | for addr,leden in poel.iteritems():
|
---|
| 115 | if link_type[addr] == '11a':
|
---|
[9989] | 116 | color = 'green'
|
---|
[8268] | 117 | weight = 4
|
---|
| 118 | elif link_type[addr] == 'eth':
|
---|
| 119 | color = 'blue'
|
---|
| 120 | weight = 8
|
---|
| 121 | else:
|
---|
| 122 | color = 'black'
|
---|
| 123 | weight = 1
|
---|
[9989] | 124 | width = max(1,link_data[addr])
|
---|
[8268] | 125 | leden = sorted(set(leden))
|
---|
| 126 | for index,lid in enumerate(leden[:-1]):
|
---|
| 127 | for buur in leden[index + 1:]:
|
---|
[11753] | 128 | print >> f,' %s -> %s [color="%s", weight="%s", style="setlinewidth(%s)"]' % (lid, buur, color, weight, width)
|
---|
[8268] | 129 | print >> f, "}"
|
---|
[9989] | 130 | f.flush()
|
---|
[8268] | 131 | sys.stderr.write("# Plotting temponary graph file using graphviz\n")
|
---|
| 132 | retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
|
---|
| 133 | if retval != 0:
|
---|
| 134 | sys.stderr.write("# FAILED [%i]\n" % retval)
|
---|
| 135 | subprocess.call(["cat",f.name])
|
---|
| 136 | exit(1)
|
---|
| 137 | sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
|
---|
[8240] | 138 |
|
---|
| 139 |
|
---|
| 140 | if __name__ == "__main__":
|
---|
[8268] | 141 | make_graph()
|
---|
[8240] | 142 |
|
---|