[10037] | 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 gformat
|
---|
| 7 | import glob
|
---|
| 8 | import os
|
---|
| 9 | import re
|
---|
| 10 | import subprocess
|
---|
| 11 | import sys
|
---|
| 12 | import tempfile
|
---|
| 13 | import yaml
|
---|
| 14 |
|
---|
| 15 |
|
---|
[10375] | 16 | try:
|
---|
| 17 | store = yaml.load(open('store.yaml','r'))
|
---|
| 18 | except IOError:
|
---|
| 19 | store = None
|
---|
[10037] | 20 |
|
---|
| 21 |
|
---|
| 22 | HEADER = '''<?xml version="1.0" encoding="UTF-8"?>
|
---|
| 23 | <kml xmlns="http://www.opengis.net/kml/2.2">
|
---|
| 24 | <Document>
|
---|
| 25 | '''
|
---|
| 26 |
|
---|
| 27 | POINT = '''
|
---|
| 28 | <Placemark>
|
---|
| 29 | <name>%(nodename)s</name>
|
---|
| 30 | <description>%(nodename)s</description>
|
---|
| 31 | <Point>
|
---|
[10375] | 32 | <coordinates>%(longitude)s,%(latitude)s,0</coordinates>
|
---|
[10037] | 33 | </Point>
|
---|
| 34 | </Placemark>
|
---|
| 35 | '''
|
---|
| 36 |
|
---|
| 37 | LINE = '''
|
---|
| 38 | <Placemark>
|
---|
| 39 | <LineString>
|
---|
| 40 | <coordinates>
|
---|
[10375] | 41 | %(longitudeA)s,%(latitudeA)s,0
|
---|
| 42 | %(longitudeB)s,%(latitudeB)s,0
|
---|
[10037] | 43 | </coordinates>
|
---|
| 44 | </LineString>
|
---|
| 45 | </Placemark>
|
---|
| 46 | '''
|
---|
| 47 |
|
---|
| 48 | FOOTER = '''
|
---|
| 49 | </Document>
|
---|
| 50 | </kml>
|
---|
| 51 | '''
|
---|
[10377] | 52 | def make_graph(debug=False):
|
---|
[10037] | 53 | poel = {}
|
---|
| 54 | link_type = {}
|
---|
| 55 | link_data = {}
|
---|
| 56 | hosts = {}
|
---|
[10377] | 57 |
|
---|
[10037] | 58 | try:
|
---|
| 59 | for host in gformat.get_hostlist():
|
---|
[10377] | 60 | if debug: print "## Processing host", host
|
---|
[10037] | 61 | datadump = gformat.get_yaml(host)
|
---|
| 62 | nodename = datadump['nodename']
|
---|
| 63 | hosts[nodename] = datadump
|
---|
| 64 |
|
---|
| 65 | iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
| 66 | for iface_key in iface_keys:
|
---|
| 67 | l = datadump[iface_key]['ip']
|
---|
| 68 | addr, mask = l.split('/')
|
---|
| 69 |
|
---|
| 70 | # Not parsing of these folks please
|
---|
| 71 | if not gformat.valid_addr(addr):
|
---|
| 72 | continue
|
---|
| 73 |
|
---|
| 74 | addr = gformat.parseaddr(addr)
|
---|
| 75 | mask = int(mask)
|
---|
| 76 | addr = addr & ~((1 << (32 - mask)) - 1)
|
---|
| 77 | if poel.has_key(addr):
|
---|
| 78 | poel[addr] += [nodename]
|
---|
| 79 | else:
|
---|
| 80 | poel[addr] = [nodename]
|
---|
| 81 | # Assume all ns_ip to be 11a for a moment
|
---|
| 82 | if datadump[iface_key].has_key('ns_ip'):
|
---|
| 83 | link_type[addr] = '11a'
|
---|
| 84 | else:
|
---|
| 85 | link_type[addr] = datadump[iface_key]['type']
|
---|
| 86 |
|
---|
| 87 | link_data[addr] = 1
|
---|
| 88 | iface = datadump[iface_key]['interface']
|
---|
| 89 | nodename = datadump['nodename']
|
---|
| 90 | INTERVAL = 60 * 10
|
---|
[10375] | 91 | if store and store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
|
---|
| 92 | if store and store['traffic'][nodename].has_key(iface):
|
---|
[10037] | 93 | (b_in, b_out) = store['traffic'][nodename][iface]
|
---|
| 94 | uptime = store['uptime'][nodename]
|
---|
| 95 | t_kb = float(b_in + b_out) / 1024
|
---|
[10377] | 96 | if debug: print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
|
---|
[10037] | 97 | retval = ((t_kb) / uptime) * INTERVAL
|
---|
| 98 | link_data[addr] = retval
|
---|
| 99 |
|
---|
[10377] | 100 | if debug: print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
|
---|
[10037] | 101 | except (KeyError, ValueError), e:
|
---|
[10377] | 102 | if debug: print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
|
---|
| 103 | if debug: print e
|
---|
[10037] | 104 | sys.exit(1)
|
---|
| 105 |
|
---|
[10377] | 106 | output = ''
|
---|
| 107 | if debug: print "# Building KML file"
|
---|
| 108 | output += HEADER
|
---|
[10037] | 109 | for nodename, datadump in hosts.iteritems():
|
---|
[10377] | 110 | output += POINT % datadump
|
---|
[10037] | 111 |
|
---|
| 112 | for addr,leden in poel.iteritems():
|
---|
| 113 | if link_type[addr] == '11a':
|
---|
| 114 | color = 'green'
|
---|
| 115 | weight = 4
|
---|
| 116 | elif link_type[addr] == 'eth':
|
---|
| 117 | color = 'blue'
|
---|
| 118 | weight = 8
|
---|
| 119 | else:
|
---|
| 120 | color = 'black'
|
---|
| 121 | weight = 1
|
---|
| 122 | width = max(1,link_data[addr])
|
---|
| 123 | leden = sorted(set(leden))
|
---|
| 124 | for index,lid in enumerate(leden[:-1]):
|
---|
| 125 | for buur in leden[index + 1:]:
|
---|
| 126 | keys = {}
|
---|
| 127 | for key, value in hosts[buur].iteritems():
|
---|
| 128 | keys[key + 'A'] = value
|
---|
| 129 | for key, value in hosts[lid].iteritems():
|
---|
| 130 | keys[key + 'B'] = value
|
---|
[10377] | 131 | output += LINE % keys
|
---|
| 132 | output += FOOTER
|
---|
| 133 | return output
|
---|
[10037] | 134 |
|
---|
| 135 | if __name__ == "__main__":
|
---|
[10377] | 136 | OUTFILE = os.path.join(os.getcwd(),'network.kml')
|
---|
| 137 | kml_data = make_graph(debug=True)
|
---|
| 138 | f = open(OUTFILE,'w')
|
---|
| 139 | f.write(kml_data)
|
---|
| 140 | f.close()
|
---|
| 141 | print "# COMPLETED find your output in %s\n" % OUTFILE
|
---|
[10037] | 142 |
|
---|