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 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 |
|
---|
60 | def 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 |
|
---|
97 | def make_graph():
|
---|
98 | f = open('kmlfile.kml', 'w')
|
---|
99 | f.write("""<?xml version="1.0" encoding="UTF-8"?>
|
---|
100 | <kml xmlns="http://earth.google.com/kml/2.0">
|
---|
101 | <Document>
|
---|
102 | <name>WirelessLeiden Nodemap</name>
|
---|
103 | <open>1</open>
|
---|
104 | <description>Generated realtime status of all Wireless Leiden AccessPoints</description>
|
---|
105 | <Style id="node_status_up">
|
---|
106 | <IconStyle>
|
---|
107 | <scale>0.5</scale>
|
---|
108 | <Icon><href>http://www.google.com/mapfiles/kml/paddle/grn-stars-lv.png</href></Icon>
|
---|
109 | </IconStyle>
|
---|
110 | </Style>
|
---|
111 | <Style id="node_status_down">
|
---|
112 | <IconStyle>
|
---|
113 | <scale>0.5</scale>
|
---|
114 | <Icon><href>http://www.google.com/mapfiles/kml/paddle/red-stars-lv.png</href></Icon>
|
---|
115 | </IconStyle>
|
---|
116 | </Style>
|
---|
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>
|
---|
122 | </Style>
|
---|
123 | <Folder>
|
---|
124 | <name>Nodes</name>
|
---|
125 | <visibility>0</visibility>
|
---|
126 | <description>All active nodes and links</description>
|
---|
127 | """)
|
---|
128 |
|
---|
129 | poel = {}
|
---|
130 | link_type = {}
|
---|
131 | node = {}
|
---|
132 |
|
---|
133 | nodes = []
|
---|
134 | links = []
|
---|
135 | try:
|
---|
136 | for host in gformat.get_hostlist():
|
---|
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
|
---|
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':
|
---|
154 | link_type[addr] = '11a'
|
---|
155 | else:
|
---|
156 | link_type[addr] = datadump[iface_parent]['type']
|
---|
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)
|
---|
160 | f.write("""
|
---|
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})
|
---|
169 | nodes += [("POINT(%s, %s)" % (lam, phi))]
|
---|
170 | except (KeyError, ValueError), e:
|
---|
171 | print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
|
---|
172 | raise
|
---|
173 | sys.exit(1)
|
---|
174 |
|
---|
175 | f.write("""
|
---|
176 | </Folder>
|
---|
177 | <Folder>
|
---|
178 | <name>Links</name>
|
---|
179 | <visibility>0</visibility>
|
---|
180 | <description>All links</description>
|
---|
181 | """)
|
---|
182 | for addr,leden in poel.iteritems():
|
---|
183 | if link_type[addr] == '11a':
|
---|
184 | color = '#ff0000ff'
|
---|
185 | weight = 2
|
---|
186 | elif link_type[addr] == 'eth':
|
---|
187 | color = '#ffff0000'
|
---|
188 | weight = 4
|
---|
189 | else:
|
---|
190 | color = '#ff000000'
|
---|
191 | weight = 1
|
---|
192 |
|
---|
193 | leden = sorted(set(leden))
|
---|
194 | for index,lid in enumerate(leden[:-1]):
|
---|
195 | for buur in leden[index + 1:]:
|
---|
196 | f.write("""
|
---|
197 | <Placemark>
|
---|
198 | <name>%(name)s</name>
|
---|
199 | <visibility>0</visibility>
|
---|
200 | <description>%(desc)s</description>
|
---|
201 | <LineString>
|
---|
202 | <tessellate>0</tessellate>
|
---|
203 | <coordinates> %(lam1)s, %(phi1)s, 0
|
---|
204 | %(lam2)s , %(phi2)s, 0 </coordinates>
|
---|
205 | </LineString>
|
---|
206 | <Style>%(style)s</Style>
|
---|
207 | </Placemark>
|
---|
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 | })
|
---|
216 | f.write("""
|
---|
217 | </Folder>
|
---|
218 | </Document>
|
---|
219 | </kml>
|
---|
220 | """)
|
---|
221 | f.close()
|
---|
222 |
|
---|
223 |
|
---|
224 | if __name__ == "__main__":
|
---|
225 | make_graph()
|
---|
226 |
|
---|