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

Last change on this file since 8293 was 8293, checked in by rick, 15 years ago

Fixed CNodeRick coordinates using make-map new function

  • Property svn:executable set to *
File size: 6.7 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>
6
7import cgi
8import gformat
9import re
10import sys
11import urllib
12import yaml
13import math
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
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
60def rd2etrs(xrd, yrd, hnap=0.0):
61 """ Convert rd to etrs """
62 global coordinates
63 xrd = float(str(xrd))
64 yrd = float(str(yrd))
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
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)
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
93 coordinates[(xrd, yrd)] = (lam, phi)
94 write_yaml(CACHE_FILE, coordinates)
95 return (lam, phi)
96
97OUTFILE = 'network.png'
98
99def make_graph():
100 f = open('kmlfile.kml', 'w')
101 f.write("""<?xml version="1.0" encoding="UTF-8"?>
102<kml xmlns="http://earth.google.com/kml/2.0">
103 <Document>
104 <name>WirelessLeiden Nodemap</name>
105 <open>1</open>
106 <description>Generated realtime status of all Wireless Leiden AccessPoints</description>
107 <Style id="node_status_up">
108 <IconStyle>
109 <scale>0.5</scale>
110 <Icon><href>http://www.google.com/mapfiles/kml/paddle/grn-stars-lv.png</href></Icon>
111 </IconStyle>
112 </Style>
113 <Style id="node_status_down">
114 <IconStyle>
115 <scale>0.5</scale>
116 <Icon><href>http://www.google.com/mapfiles/kml/paddle/red-stars-lv.png</href></Icon>
117 </IconStyle>
118 </Style>
119 <Style id="node_status_planned">
120 <IconStyle>
121 <scale>0.5</scale>
122 <Icon><href>http://www.google.com/mapfiles/kml/paddle/wht-stars-lv.png</href></Icon>
123 </IconStyle>
124 </Style>
125 <Folder>
126 <name>Nodes</name>
127 <visibility>0</visibility>
128 <description>All active nodes and links</description>
129 """)
130
131 poel = {}
132 link_type = {}
133 node = {}
134
135 nodes = []
136 links = []
137 try:
138 for host in gformat.get_proxylist() + gformat.get_nodelist():
139 print "## Processing host", host
140 datadump = gformat.get_yaml(host)
141 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
142 for iface_key in iface_keys:
143 l = datadump[iface_key]['ip']
144 addr, mask = l.split('/')
145
146 addr = gformat.parseaddr(addr)
147 mask = int(mask)
148 addr = addr & ~((1 << (32 - mask)) - 1)
149 if poel.has_key(addr):
150 poel[addr] += [host]
151 else:
152 poel[addr] = [host]
153 # Assume all eth2wifibridge to be 11a for a moment
154 iface_parent = '_'.join(iface_key.split('_')[0:2])
155 if datadump[iface_parent].has_key('extra_type') and datadump[iface_parent]['extra_type'] == 'eth2wifibridge':
156 link_type[addr] = '11a'
157 else:
158 link_type[addr] = datadump[iface_parent]['type']
159 print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
160 lam, phi = rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
161 node[host] = (lam, phi)
162 f.write("""
163 <description>All active nodes</description>
164 <Placemark>
165 <name>Node %(name)s</name>
166 <description>%(desc)s></description>
167 <styleUrl>#node_status_up</styleUrl>
168 <Point><coordinates>%(lam)s,%(phi)s,0</coordinates></Point>
169 </Placemark>
170 """ % {'name' : host, 'desc' : cgi.escape(datadump['location']), 'lam' : lam, 'phi' : phi})
171 nodes += [("POINT(%s, %s)" % (lam, phi))]
172 except (KeyError, ValueError), e:
173 print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
174 raise
175 sys.exit(1)
176
177 f.write("""
178 </Folder>
179 <Folder>
180 <name>Links</name>
181 <visibility>0</visibility>
182 <description>All links</description>
183 """)
184 for addr,leden in poel.iteritems():
185 if link_type[addr] == '11a':
186 color = '#ff0000ff'
187 weight = 2
188 elif link_type[addr] == 'eth':
189 color = '#ffff0000'
190 weight = 4
191 else:
192 color = '#ff000000'
193 weight = 1
194
195 leden = sorted(set(leden))
196 for index,lid in enumerate(leden[:-1]):
197 for buur in leden[index + 1:]:
198 f.write("""
199 <Placemark>
200 <name>%(name)s</name>
201 <visibility>0</visibility>
202 <description>%(desc)s</description>
203 <LineString>
204 <tessellate>0</tessellate>
205 <coordinates> %(lam1)s, %(phi1)s, 0
206 %(lam2)s , %(phi2)s, 0 </coordinates>
207 </LineString>
208 <Style>%(style)s</Style>
209 </Placemark>
210 """ % { 'lam1' : node[lid][0],
211 'phi1' : node[lid][1],
212 'lam2' : node[buur][0],
213 'phi2' : node[buur][1],
214 'name' : "Interlink: %s --- %s" % (lid, buur),
215 'desc' : "%s [%s]" % (gformat.showaddr(addr), link_type[addr]),
216 'style' : "<LineStyle><color>%s</color><width>%s</width></LineStyle>" % (color, weight),
217 })
218 f.write("""
219 </Folder>
220 </Document>
221</kml>
222 """)
223 f.close()
224
225
226if __name__ == "__main__":
227 make_graph()
228
Note: See TracBrowser for help on using the repository browser.