source: genesis/tools/make-network-kml.py@ 10113

Last change on this file since 10113 was 10037, checked in by rick, 13 years ago

I have done this somehow/somewhere but forgot where. Anyways...

KML file of WL Nodes to be used for planning and display in Google Earth.
Please do mind this does not work at Google Maps very well due to the limit of
items allowed to display.

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