source: genesis/nodes/make-map.py@ 8300

Last change on this file since 8300 was 8296, checked in by rick, 15 years ago
  • Allow generation of all config files for testing purposes
  • Property svn:executable set to *
File size: 6.6 KB
RevLine 
[8280]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>
[8285]6
7import cgi
8import gformat
[8280]9import re
10import sys
11import urllib
12import yaml
[8293]13import math
[8280]14
15def get_yaml(gfile):
16 """ Get configuration yaml for 'item'"""
17 f = open(gfile, 'r')
18 datadump = yaml.load(f)
19 return datadump
20
21def write_yaml(gfile, datadump):
22 """ Write configuration yaml for 'item'"""
23 f = open(gfile, 'w')
24 f.write(yaml.dump(datadump, default_flow_style=False))
25 f.close()
26
27CACHE_FILE = '/tmp/rd2etrs.yaml'
28coordinates = None
29
[8293]30def etrs2rd(lam, phi):
31 """ Convert rd to etrs """
32
33 item = dict()
34 (remainder, item['lam_deg']) = math.modf(lam)
35 remainder *= 60
36 (remainder, item['lam_min']) = math.modf(remainder)
37 item['lam_sec'] = remainder * 60
38
39 (remainder, item['phi_deg']) = math.modf(phi)
40 remainder *= 60
41 (remainder, item['phi_min']) = math.modf(remainder)
42 item['phi_sec'] = remainder * 60
43
44 item['func'] = 'etrs2rd'
45
46 args = "&".join(["%s=%s" % (k,v) for k,v in item.iteritems()])
47 url = 'http://www.rdnap.nl/cgi-bin/rdetrs.pl?%s' % args
48 print "### Fetching coordinate %s, %s using: %s" % (phi, lam, url)
49 f = urllib.urlopen(url)
50 raw = f.read()
51
52 r = re.compile('name="([a-z_]+)" value="([0-9\.]+)"')
53 for i in r.finditer(raw):
54 name, value = i.group(1,2)
55 value = float(value)
56 item[name] = value
57 return (item['xrd'], item['yrd'])
58
59
[8280]60def rd2etrs(xrd, yrd, hnap=0.0):
61 """ Convert rd to etrs """
62 global coordinates
[8293]63 xrd = float(str(xrd))
64 yrd = float(str(yrd))
[8280]65 if coordinates == None:
66 try:
67 coordinates = get_yaml(CACHE_FILE)
68 if coordinates.has_key((xrd, yrd)):
69 return coordinates[(xrd, yrd)]
70 except (IOError,AttributeError):
71 coordinates = dict()
72 pass
73
74 item = dict()
75 item['xrd'] = xrd
76 item['yrd'] = yrd
77 item['hnap'] = hnap
[8293]78 item['func'] = 'rd2etrs'
79 args = "&".join(["%s=%s" % (k,v) for k,v in item.iteritems()])
80 url = 'http://www.rdnap.nl/cgi-bin/rdetrs.pl?%s' % args
81 print "### Fetching coordinate %s, %s from %s" % (xrd, yrd, url)
82 f = urllib.urlopen(url)
[8280]83 raw = f.read()
84
85 r = re.compile('name="([a-z_]+)" value="([0-9\.]+)"')
86 for i in r.finditer(raw):
87 name, value = i.group(1,2)
88 value = float(value)
89 item[name] = value
90
91 lam = item['lam_deg'] + (item['lam_min'] + (item['lam_sec'] / 60)) / 60
92 phi = item['phi_deg'] + (item['phi_min'] + (item['phi_sec'] / 60)) / 60
[8293]93 coordinates[(xrd, yrd)] = (lam, phi)
[8280]94 write_yaml(CACHE_FILE, coordinates)
95 return (lam, phi)
96
97def make_graph():
[8282]98 f = open('kmlfile.kml', 'w')
[8285]99 f.write("""<?xml version="1.0" encoding="UTF-8"?>
[8282]100<kml xmlns="http://earth.google.com/kml/2.0">
101 <Document>
[8285]102 <name>WirelessLeiden Nodemap</name>
[8282]103 <open>1</open>
[8285]104 <description>Generated realtime status of all Wireless Leiden AccessPoints</description>
105 <Style id="node_status_up">
[8282]106 <IconStyle>
[8285]107 <scale>0.5</scale>
108 <Icon><href>http://www.google.com/mapfiles/kml/paddle/grn-stars-lv.png</href></Icon>
[8282]109 </IconStyle>
110 </Style>
[8285]111 <Style id="node_status_down">
[8282]112 <IconStyle>
[8285]113 <scale>0.5</scale>
114 <Icon><href>http://www.google.com/mapfiles/kml/paddle/red-stars-lv.png</href></Icon>
[8282]115 </IconStyle>
116 </Style>
[8285]117 <Style id="node_status_planned">
118 <IconStyle>
119 <scale>0.5</scale>
120 <Icon><href>http://www.google.com/mapfiles/kml/paddle/wht-stars-lv.png</href></Icon>
121 </IconStyle>
[8282]122 </Style>
123 <Folder>
[8285]124 <name>Nodes</name>
[8282]125 <visibility>0</visibility>
[8285]126 <description>All active nodes and links</description>
[8282]127 """)
128
[8280]129 poel = {}
130 link_type = {}
131 node = {}
132
133 nodes = []
134 links = []
135 try:
[8296]136 for host in gformat.get_hostlist():
[8280]137 print "## Processing host", host
138 datadump = gformat.get_yaml(host)
139 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
140 for iface_key in iface_keys:
141 l = datadump[iface_key]['ip']
142 addr, mask = l.split('/')
143
144 addr = gformat.parseaddr(addr)
145 mask = int(mask)
146 addr = addr & ~((1 << (32 - mask)) - 1)
147 if poel.has_key(addr):
148 poel[addr] += [host]
149 else:
150 poel[addr] = [host]
151 # Assume all eth2wifibridge to be 11a for a moment
[8285]152 iface_parent = '_'.join(iface_key.split('_')[0:2])
153 if datadump[iface_parent].has_key('extra_type') and datadump[iface_parent]['extra_type'] == 'eth2wifibridge':
[8280]154 link_type[addr] = '11a'
155 else:
[8285]156 link_type[addr] = datadump[iface_parent]['type']
[8280]157 print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
158 lam, phi = rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
159 node[host] = (lam, phi)
[8282]160 f.write("""
[8285]161 <description>All active nodes</description>
162 <Placemark>
163 <name>Node %(name)s</name>
164 <description>%(desc)s></description>
165 <styleUrl>#node_status_up</styleUrl>
166 <Point><coordinates>%(lam)s,%(phi)s,0</coordinates></Point>
167 </Placemark>
168 """ % {'name' : host, 'desc' : cgi.escape(datadump['location']), 'lam' : lam, 'phi' : phi})
[8280]169 nodes += [("POINT(%s, %s)" % (lam, phi))]
170 except (KeyError, ValueError), e:
171 print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
[8285]172 raise
[8280]173 sys.exit(1)
174
[8285]175 f.write("""
176 </Folder>
177 <Folder>
178 <name>Links</name>
179 <visibility>0</visibility>
180 <description>All links</description>
181 """)
[8280]182 for addr,leden in poel.iteritems():
183 if link_type[addr] == '11a':
[8285]184 color = '#ff0000ff'
185 weight = 2
186 elif link_type[addr] == 'eth':
187 color = '#ffff0000'
[8280]188 weight = 4
189 else:
[8285]190 color = '#ff000000'
[8280]191 weight = 1
[8285]192
[8280]193 leden = sorted(set(leden))
194 for index,lid in enumerate(leden[:-1]):
195 for buur in leden[index + 1:]:
[8282]196 f.write("""
197 <Placemark>
[8285]198 <name>%(name)s</name>
[8282]199 <visibility>0</visibility>
[8285]200 <description>%(desc)s</description>
[8282]201 <LineString>
202 <tessellate>0</tessellate>
[8285]203 <coordinates> %(lam1)s, %(phi1)s, 0
204 %(lam2)s , %(phi2)s, 0 </coordinates>
[8282]205 </LineString>
[8285]206 <Style>%(style)s</Style>
[8282]207 </Placemark>
[8285]208 """ % { 'lam1' : node[lid][0],
209 'phi1' : node[lid][1],
210 'lam2' : node[buur][0],
211 'phi2' : node[buur][1],
212 'name' : "Interlink: %s --- %s" % (lid, buur),
213 'desc' : "%s [%s]" % (gformat.showaddr(addr), link_type[addr]),
214 'style' : "<LineStyle><color>%s</color><width>%s</width></LineStyle>" % (color, weight),
215 })
[8282]216 f.write("""
217 </Folder>
218 </Document>
219</kml>
220 """)
[8280]221 f.close()
222
223
224if __name__ == "__main__":
225 make_graph()
226
Note: See TracBrowser for help on using the repository browser.