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