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 |
|
---|
7 | import cgi
|
---|
8 | import gformat
|
---|
9 | import re
|
---|
10 | import sys
|
---|
11 | import urllib
|
---|
12 | import yaml
|
---|
13 | import math
|
---|
14 |
|
---|
15 | def get_yaml(gfile):
|
---|
16 | """ Get configuration yaml for 'item'"""
|
---|
17 | f = open(gfile, 'r')
|
---|
18 | datadump = yaml.load(f)
|
---|
19 | return datadump
|
---|
20 |
|
---|
21 | def 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 |
|
---|
27 | CACHE_FILE = '/tmp/rd2etrs.yaml'
|
---|
28 | coordinates = None
|
---|
29 |
|
---|
30 | def etrs2rd(lam, phi):
|
---|
31 | """ Convert etrs to rd """
|
---|
32 |
|
---|
33 | item['lam'] = lam
|
---|
34 | item['phi'] = phi
|
---|
35 | item['func'] = 'etrs2rd'
|
---|
36 | url = 'http://vanderzwet.net/rdnap/%(func)s/%(xrd)s/%(yrd)s/0/' % item
|
---|
37 |
|
---|
38 | f = urllib.urlopen(url)
|
---|
39 | raw = f.read()
|
---|
40 |
|
---|
41 | rdnap_x,rdnap_y,rdnap_h = raw.split('/')
|
---|
42 | return (rdnap_x, rdnap_y)
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | def rd2etrs(xrd, yrd, hnap=0.0):
|
---|
47 | """
|
---|
48 | Convert rd to etrs
|
---|
49 | JavaScript Version: https://rdinfo.kadaster.nl/rd/transformator.html
|
---|
50 | """
|
---|
51 | # Get cache is exists
|
---|
52 | global coordinates
|
---|
53 | if coordinates == None:
|
---|
54 | try:
|
---|
55 | coordinates = get_yaml(CACHE_FILE)
|
---|
56 | except (IOError,AttributeError):
|
---|
57 | coordinates = dict()
|
---|
58 | pass
|
---|
59 |
|
---|
60 | # Check if item in cache
|
---|
61 | xrd = float(str(xrd))
|
---|
62 | yrd = float(str(yrd))
|
---|
63 | if coordinates.has_key((xrd, yrd)):
|
---|
64 | return coordinates[(xrd, yrd)]
|
---|
65 |
|
---|
66 | # Get new coordinate
|
---|
67 | item = dict()
|
---|
68 | item['xrd'] = xrd
|
---|
69 | item['yrd'] = yrd
|
---|
70 | item['hnap'] = hnap
|
---|
71 | item['func'] = 'rd2etrs'
|
---|
72 | url = 'http://vanderzwet.net/rdnap/%(func)s/%(xrd)s/%(yrd)s/%(hnap)s/' % item
|
---|
73 | print "### Not in Cache, Fetching coordinate %s, %s from %s" % (xrd, yrd, url)
|
---|
74 | f = urllib.urlopen(url)
|
---|
75 | raw = f.read()
|
---|
76 |
|
---|
77 | lam,phi,h = raw.split('/')
|
---|
78 | coordinates[(xrd, yrd)] = (lam, phi)
|
---|
79 | write_yaml(CACHE_FILE, coordinates)
|
---|
80 | return (lam, phi)
|
---|
81 |
|
---|
82 | def make_graph():
|
---|
83 | status = None
|
---|
84 | try:
|
---|
85 | stream = file('/tmp/nodemap_status.yaml','r')
|
---|
86 | status = yaml.load(stream)
|
---|
87 | except IOError,e:
|
---|
88 | print "# Error loading status '%s'" % e
|
---|
89 |
|
---|
90 | f = open('kmlfile.kml', 'w')
|
---|
91 | f.write("""<?xml version="1.0" encoding="UTF-8"?>
|
---|
92 | <kml xmlns="http://earth.google.com/kml/2.0">
|
---|
93 | <Document>
|
---|
94 | <name>WirelessLeiden Nodemap</name>
|
---|
95 | <open>1</open>
|
---|
96 | <description>Generated realtime status of all Wireless Leiden AccessPoints</description>
|
---|
97 | <Style id="node_status_up">
|
---|
98 | <IconStyle>
|
---|
99 | <scale>0.5</scale>
|
---|
100 | <Icon><href>http://www.google.com/mapfiles/kml/paddle/grn-stars-lv.png</href></Icon>
|
---|
101 | </IconStyle>
|
---|
102 | </Style>
|
---|
103 | <Style id="node_status_down">
|
---|
104 | <IconStyle>
|
---|
105 | <scale>0.5</scale>
|
---|
106 | <Icon><href>http://www.google.com/mapfiles/kml/paddle/red-stars-lv.png</href></Icon>
|
---|
107 | </IconStyle>
|
---|
108 | </Style>
|
---|
109 | <Style id="node_status_planned">
|
---|
110 | <IconStyle>
|
---|
111 | <scale>0.5</scale>
|
---|
112 | <Icon><href>http://www.google.com/mapfiles/kml/paddle/wht-stars-lv.png</href></Icon>
|
---|
113 | </IconStyle>
|
---|
114 | </Style>
|
---|
115 | <Style id="node_status_unknown">
|
---|
116 | <IconStyle>
|
---|
117 | <scale>0.5</scale>
|
---|
118 | <Icon><href>http://www.google.com/mapfiles/kml/paddle/wht-stars-lv.png</href></Icon>
|
---|
119 | </IconStyle>
|
---|
120 | </Style>
|
---|
121 | <Folder>
|
---|
122 | <name>Nodes</name>
|
---|
123 | <visibility>0</visibility>
|
---|
124 | <description>All active nodes and links</description>
|
---|
125 | """)
|
---|
126 |
|
---|
127 | poel = {}
|
---|
128 | link_type = {}
|
---|
129 | node = {}
|
---|
130 |
|
---|
131 | nodes = []
|
---|
132 | links = []
|
---|
133 | try:
|
---|
134 | for host in gformat.get_hostlist():
|
---|
135 | fqdn = host + ".wLeiden.NET"
|
---|
136 | print "## Processing host", host
|
---|
137 | datadump = gformat.get_yaml(host)
|
---|
138 | iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
139 | for iface_key in iface_keys:
|
---|
140 | l = datadump[iface_key]['ip']
|
---|
141 | addr, mask = l.split('/')
|
---|
142 |
|
---|
143 | addr = gformat.parseaddr(addr)
|
---|
144 | mask = int(mask)
|
---|
145 | addr = addr & ~((1 << (32 - mask)) - 1)
|
---|
146 | if poel.has_key(addr):
|
---|
147 | poel[addr] += [host]
|
---|
148 | else:
|
---|
149 | poel[addr] = [host]
|
---|
150 | # Assume all eth2wifibridge to be 11a for a moment
|
---|
151 | iface_parent = '_'.join(iface_key.split('_')[0:2])
|
---|
152 | if datadump[iface_parent].has_key('extra_type') and datadump[iface_parent]['extra_type'] == 'eth2wifibridge':
|
---|
153 | link_type[addr] = '11a'
|
---|
154 | else:
|
---|
155 | link_type[addr] = datadump[iface_parent]['type']
|
---|
156 | print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
|
---|
157 | lam, phi = rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
|
---|
158 | node[host] = (lam, phi)
|
---|
159 | if not status:
|
---|
160 | # By default assume up
|
---|
161 | node_status = "up"
|
---|
162 | elif status['node'][fqdn] == gformat.OK:
|
---|
163 | node_status = "up"
|
---|
164 | elif status['node'][fqdn] == gformat.DOWN:
|
---|
165 | node_status = "down"
|
---|
166 | elif status['node'][fqdn] == gformat.UNKNOWN:
|
---|
167 | node_status = "unknown"
|
---|
168 | else:
|
---|
169 | assert False, "Status cannot be generated"
|
---|
170 |
|
---|
171 | print "### Node status is '%s'" % node_status
|
---|
172 | f.write("""
|
---|
173 | <description>All active nodes</description>
|
---|
174 | <Placemark>
|
---|
175 | <name>%(nodetype)s %(nodename)s</name>
|
---|
176 | <description>%(desc)s</description>
|
---|
177 | <styleUrl>%(style)s</styleUrl>
|
---|
178 | <Point><coordinates>%(lam)s,%(phi)s,0</coordinates></Point>
|
---|
179 | </Placemark>
|
---|
180 | """ % {'nodetype' : datadump['nodetype'], 'nodename' : datadump['nodename'], 'desc' : cgi.escape(datadump['location']), 'style' : '#node_status_' + node_status, 'lam' : lam, 'phi' : phi})
|
---|
181 | nodes += [("POINT(%s, %s)" % (lam, phi))]
|
---|
182 | except (KeyError, ValueError), e:
|
---|
183 | print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
|
---|
184 | raise
|
---|
185 | sys.exit(1)
|
---|
186 |
|
---|
187 | f.write("""
|
---|
188 | </Folder>
|
---|
189 | <Folder>
|
---|
190 | <name>Links</name>
|
---|
191 | <visibility>0</visibility>
|
---|
192 | <description>All links</description>
|
---|
193 | """)
|
---|
194 | for addr,leden in poel.iteritems():
|
---|
195 | if not gformat.valid_addr(gformat.showaddr(addr)):
|
---|
196 | print "## Adres '%s' met leden '%s' ignored'" % (gformat.showaddr(addr), ','.join(leden))
|
---|
197 | continue
|
---|
198 | if link_type[addr] == '11a':
|
---|
199 | weight = 2
|
---|
200 | elif link_type[addr] == 'eth':
|
---|
201 | weight = 4
|
---|
202 | else:
|
---|
203 | weight = 1
|
---|
204 |
|
---|
205 | leden = sorted(set(leden))
|
---|
206 | for index,lid in enumerate(leden[:-1]):
|
---|
207 | for buur in leden[index + 1:]:
|
---|
208 | key = (lid + ".wLeiden.NET", buur + ".wLeiden.NET")
|
---|
209 | rev_key = (key[1],key[0])
|
---|
210 | link_status = None
|
---|
211 | if not status:
|
---|
212 | # Default assume OK
|
---|
213 | link_status = gformat.OK
|
---|
214 | elif status['link'].has_key(key):
|
---|
215 | link_status = status['link'][key]
|
---|
216 | elif status['link'].has_key(rev_key):
|
---|
217 | link_status = status['link'][rev_key]
|
---|
218 | else:
|
---|
219 | # If link is not known assume nothing
|
---|
220 | link_status = gformat.UNKNOWN
|
---|
221 |
|
---|
222 |
|
---|
223 | if link_status == gformat.OK:
|
---|
224 | color = '#ffff0000' # green
|
---|
225 | elif link_status == gformat.DOWN:
|
---|
226 | color = '#ff0000ff' # red
|
---|
227 | elif link_status == gformat.UNKNOWN:
|
---|
228 | color = '#55000000' # black
|
---|
229 | else:
|
---|
230 | assert False, "Link status not mapped properly"
|
---|
231 |
|
---|
232 | f.write("""
|
---|
233 | <Placemark>
|
---|
234 | <name>%(name)s</name>
|
---|
235 | <visibility>0</visibility>
|
---|
236 | <description>%(desc)s</description>
|
---|
237 | <LineString>
|
---|
238 | <tessellate>0</tessellate>
|
---|
239 | <coordinates> %(lam1)s, %(phi1)s, 0
|
---|
240 | %(lam2)s , %(phi2)s, 0 </coordinates>
|
---|
241 | </LineString>
|
---|
242 | <Style>%(style)s</Style>
|
---|
243 | </Placemark>
|
---|
244 | """ % { 'lam1' : node[lid][0],
|
---|
245 | 'phi1' : node[lid][1],
|
---|
246 | 'lam2' : node[buur][0],
|
---|
247 | 'phi2' : node[buur][1],
|
---|
248 | 'name' : "Interlink: %s --- %s" % (lid, buur),
|
---|
249 | 'desc' : "%s [%s]" % (gformat.showaddr(addr), link_type[addr]),
|
---|
250 | 'style' : "<LineStyle><color>%s</color><width>%s</width></LineStyle>" % (color, weight),
|
---|
251 | })
|
---|
252 | f.write("""
|
---|
253 | </Folder>
|
---|
254 | </Document>
|
---|
255 | </kml>
|
---|
256 | """)
|
---|
257 | f.close()
|
---|
258 |
|
---|
259 |
|
---|
260 | if __name__ == "__main__":
|
---|
261 | make_graph()
|
---|
262 |
|
---|