-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmongo_kml.py
More file actions
executable file
·531 lines (469 loc) · 14.4 KB
/
Copy pathmongo_kml.py
File metadata and controls
executable file
·531 lines (469 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/env python
#
# Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
# Copyright (C) 2018 Darryl Quinn
# Additional contributors: Conrad Lara
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Additional Terms:
#
# Additional use restrictions exist on the AREDN(TM) trademark and logo.
# See AREDNLicense.txt for more info.
#
# Attributions to the AREDN Project must be retained in the source code.
# If importing this code into a new or existing project attribution
# to the AREDN project must be added to the source code.
#
# You must not misrepresent the origin of the material contained within.
#
# Modified versions must be modified to attribute to the original source
# and be marked in reasonable ways as differentiate it from the original
# version.
#
'''Generate a KML document of a tour based on rotating around locations.
'''
from pykml.factory import nsmap
from pykml.factory import KML_ElementMaker as KML
from pykml.factory import GX_ElementMaker as GX
from pykml.parser import Schema
from lxml import etree
from bson.json_util import dumps
import json, requests, requests.exceptions
import sys, socket
from pymongo import MongoClient
from pymongo.errors import WriteError, DuplicateKeyError
import ConfigParser
# Get config
config=ConfigParser.ConfigParser({'baseuri' : 'http://usercontent.aredn.org/K/5/K5DLQ/', 'dbhost' : 'mongodb', 'dbport' : '27017'})
configdata=config.read("sysinfodb.ini")
if not config.has_section('server'):
config.add_section('server')
server_baseuri=config.get("server","baseuri")
database_host=config.get("server","dbhost")
database_port=config.getint("server","dbport")
FIRMWARE_CURRENT_VERSION="3.16.1.1"
#----------------
def board_icon(x):
return {
'2': 'wifi2',
'3' : 'wifi3',
'5' : 'wifi5',
'9' : 'wifi9',
}.get(x, 'wifi2') # default value
#---------------- NO LONGER NEEDED as of develop-160+
def board_model(x):
return {
'0xe012': 'NanoStation M2', # nsm2
'TP-Link CPE210 v1.0' : 'TP-Link CPE210 v1.0',
'TP-Link CPE510 v1.0' : 'TP-Link CPE510 v1.0',
'TP-Link CPE210 v2.0' : 'TP-Link CPE210 v2.0',
'TP-Link CPE510 v2.0' : 'TP-Link CPE510 v2.0',
'TP-Link CPE210 v3.0' : 'TP-Link CPE210 v3.0',
'TP-Link CPE510 v3.0' : 'TP-Link CPE510 v3.0',
'Mikrotik RouterBOARD 912UAG-5HPnD' : 'Mikrotik RouterBOARD 912UAG-5HPnD',
'Mikrotik RouterBOARD 912UAG-2HPnD' : 'Mikrotik RouterBOARD 912UAG-2HPnD',
'0xe005' : 'NanoStation M5',
'0xe009' : 'NanoStation Loco M9',
'0xe012' : 'NanoStation M2',
'0xe035' : 'NanoStation M3',
'0xe0a2' : 'NanoStation Loco M2',
'0xe0a5' : 'NanoStation Loco M5',
'0xe105' : 'Rocket M5',
'0xe1a5' : 'PowerBridge M5',
'0xe1b2' : 'Rocket M2',
'0xe1b5' : 'Rocket M5',
'0xe1b9' : 'Rocket M9',
'0xe1c3' : 'Rocket M3',
'0xe202' : 'Bullet M2 HP',
'0xe205' : 'Bullet M5',
'0xe212' : 'airGrid M2',
'0xe215' : 'airGrid M5',
'0xe232' : 'NanoBridge M2',
'0xe239' : 'NanoBridge M9',
'0xe242' : 'airGrid M2 HP',
'0xe243' : 'NanoBridge M3',
'0xe245' : 'airGrid M5 HP',
'0xe252' : 'airGrid M2 HP',
'0xe255' : 'airGrid M5 HP',
'0xe2b5' : 'NanoBridge M5',
'0xe2c2' : 'NanoBeam M2 International',
'0xe2d2' : 'Bullet M2 Titanium HP',
'0xe2d5' : 'Bullet M5 Titanium',
'0xe302' : 'PicoStation M2',
'0xe3e5' : 'PowerBeam M5 300',
'0xe4a2' : 'AirRouter',
'0xe4b2' : 'AirRouter HP',
'0xe4e5' : 'PowerBeam M5 400',
'0xe805' : 'NanoStation M5',
'0xe815' : 'NanoBeam M5 16',
'0xe825' : 'NanoBeam M5 19',
'0xe835' : 'AirGrid M5 XW',
'0xe855' : 'NanoStation M5 XW',
'0xe866' : 'NanoStation M2 XW',
'0xe867' : 'NanoStation Loco M2 XW',
'Ubiquiti Nanostation M XW' : 'NanoStation M5 XW',
'Ubiquiti Rocket M XW' : 'Rocket M XW',
'0xe6b5' : 'Rocket M5 XW', #Rocket M5 XW
'0xe8a5' : 'NanoStation Loco M5',
'Ubiquiti Loco M XW' : 'NanoStation Loco M5 XW',
'unknown': 'unknown',
}.get(x, 'unknown') # default value
#----------------
def styleIdExists(d, v):
rc = False
for s in d.Document.getchildren():
if s.tag == '{' + s.nsmap.get(None) + '}Style':
if s.attrib['id'] == v:
rc = True
return rc
#----------------
def createStyle(nstyle,iconurl):
return KML.Style(
KML.LabelStyle(
KML.scale(0),
),
KML.IconStyle(
KML.scale(1.0),
KML.Icon(
KML.href(iconurl)
),
),
id=nstyle,
)
def createLineStyle(nstyle):
if nstyle.endswith('9'):
color='FFFF78F0'
elif nstyle.endswith('2'):
color='FF78323C'
elif nstyle.endswith('3'):
color='FFF00014'
elif nstyle.endswith('5'):
color='FF0078F0'
elif nstyle.endswith('t'):
color='FF7800F0'
return KML.Style(
KML.LabelStyle(
KML.scale(1),
),
KML.LineStyle(
KML.color(color),
KML.width('3'),
),
id=str(nstyle),
)
def createKML(docid,iconurl):
return KML.kml(
KML.Document(
KML.Style(
KML.IconStyle(
KML.scale(1.0),
KML.Icon(
KML.href(iconurl)
),
),
id=docid,
)
)
)
def createFolder(fname, fid):
return KML.Folder(
KML.name(fname),
id=fid,
)
def createPlacemark(node,nstyle,warn):
firmware_version=""
if warn==True:
firmware_version="<font style='color:rgb(255,0,0)'>" + node['firmware_version'] + "</font>"
else:
firmware_version=node['firmware_version']
desc="<a href=\"http://{name}.local.mesh:8080\">Node Console</a><br />Desc: {desc}<br/>FW ver: {firmware_ver}<br />Ch: {channel}<br />SSID: {ssid}<br />Tunnel installed: {hastunnel}<br />MAC: {mac}<br />Last updated: {lastmod}".format(
name=node['name'],
desc=node['desc'],
firmware_ver=firmware_version,
channel=node['channel'],
ssid=node['ssid'],
hastunnel=node['tunnel_installed'],
mac=node['_id'],
lastmod=node['audit']['last_updated'],
)
pm=KML.Placemark(
KML.name(node['name']),
KML.description(desc),
KML.styleUrl('#' + nstyle),
KML.Point(
KML.coordinates("{lon},{lat},{alt}".format(
lon=node['lon'],
lat=node['lat'],
alt=0,
)
)
),
id=node['name'].replace(' ','_')
)
return pm
def createLine(lineid, node1,node2,nstyle):
return KML.Placemark(
KML.name(lineid),
KML.styleUrl('#' + str(nstyle)),
KML.LineString(
KML.tessellate('1'),
KML.altitudeMode('clampToGround'),
KML.coordinates("{lon},{lat},100,{lon2},{lat2},100".format(
lon=node1['lon'],
lat=node1['lat'],
lon2=node2['lon'],
lat2=node2['lat'],
)
)
),
id=lineid
)
def getBandCh(ch):
band=2 # default
ich=int(ch)
if ich >= -2 and ich <= 6:
band=2
elif ich >= 3380 and ich <= 3495:
band=3
elif ich >= 133 and ich <= 184:
band=5
return band
def getBand(bid):
#print "BOARD_ID=" + bid
band=2 # default
if bid=='0xe035':
band=3
elif bid=='0xe866': # NSLM2XW
band=2
elif bid=='0xe867': # NSM2XW
band=2
elif bid=='Ubiquiti Nanostation M XW':
band=5
elif bid=='Ubiquiti Rocket M XW':
band=5
elif bid=='Ubiquiti Loco M XW':
band=5
elif bid=='TP-Link CPE210 v1.0':
band=2
elif bid=='TP-Link CPE210 v2.0':
band=2
elif bid=='TP-Link CPE210 v3.0':
band=2
elif bid=='TP-Link CPE510 v1.0':
band=5
elif bid=='TP-Link CPE510 v2.0':
band=5
elif bid=='TP-Link CPE510 v3.0':
band=5
elif bid=='Mikrotik RouterBOARD 912UAG-5HPnD':
band=5
elif bid=='Mikrotik RouterBOARD 912UAG-2HPnD':
band=2
elif bid=='unknown':
band=2
else:
#try:
band=int(bid[len(bid)-1])
#except ValueError, e:
# band=2
# pass
return band
def getNodeIPs(node):
ips=[]
try:
for i in node['interfaces']:
if i['name'] not in ['eth0.1','eth1','wlan0-1']:
ips.append(i['ip'])
except Exception, e:
pass
return json.dumps(ips) # because we don't want unicode
def getLinks(node):
rc=[]
nodeIPs=getNodeIPs(node)
#if "172." in nodeIPs:
#print "My IP's: " + str(nodeIPs)
try:
for l in node['olsr']['topology']:
if l['destinationIP'] in nodeIPs:
rc.append(str(l['lastHopIP']))
#if l['lastHopIP'] in nodeIPs:
# rc.append(l['destinationIP'])
except KeyError, e:
pass
return rc
def getNodeByIp(ip):
node=[]
try:
#node=collection.find({'interfaces.ip': { '$eq': ip}},{'node':'true','lat':'true','lon':'true'})
node=collection.find({ '$and': [{'interfaces.ip': { '$eq': ip}},{'lat': {'$ne': ''}},{'lon': {'$ne': ''}}]},{'node':'true','lat':'true','lon':'true'})
except KeyError, e:
pass
except Exception, e:
print "(getNodeByIp: " + e + ")"
if node.count()>0:
return json.loads(dumps(node[0]))
else:
return {}
def getNode(nodename):
node=[]
try:
node=collection.find_one({ "node": { "$eq": nodename}})
except KeyError, e:
pass
except Exception, e:
print "(getNode: " + e + ")"
return node
def writeFile(doc,suff,folder):
doc.Document.append(folder)
ofile = file(__file__.rstrip('.py') + suff +'.kml','w')
ofile.write(etree.tostring(doc, pretty_print=True))
try:
assert(Schema("kml22gx.xsd").validate(doc))
except AssertionError, e:
print "AssertionError on " + suff + " file"
pass
return True
def checkForWarnings(node):
warn=False
# check for non-current firmware versions
if node['firmware_version']!=FIRMWARE_CURRENT_VERSION:
warn=True
return warn
#----------------
#icon_src='http://www.aredn.org/sites/default/files/ubnt_icons/'
icon_src=server_baseuri
# define a variable for the Google Extensions namespace URL string
gxns = '{' + nsmap['gx'] + '}'
warnbgcolor="3214E7FF"
node_list=[]
d={}
links=[]
warn=False
# start with a base KML doc
k_doc = createKML('default',icon_src + 'wifi.png')
k_doc9 = createKML('default',icon_src + 'wifi.png')
k_doc2 = createKML('default',icon_src + 'wifi.png')
k_doc3 = createKML('default',icon_src + 'wifi.png')
k_doc5 = createKML('default',icon_src + 'wifi.png')
k_doclinks = createKML('default',icon_src + 'wifi.png')
links_folder=createFolder('Links', 'links')
links_band9_folder=createFolder('900Mhz', 'lf900Mhz')
links_band2_folder=createFolder('2Ghz', 'lf2Ghz')
links_band3_folder=createFolder('3Ghz', 'lf3Ghz')
links_band5_folder=createFolder('5Ghz', 'lf5Ghz')
bands_folder=createFolder('Bands', 'bands')
band9_folder=createFolder('900Mhz', 'f900Mhz')
band2_folder=createFolder('2Ghz', 'f2Ghz')
band3_folder=createFolder('3Ghz', 'f3Ghz')
band5_folder=createFolder('5Ghz', 'f5Ghz')
# Get a list of all hosts
# MONGO QUERY HERE
connection = MongoClient(database_host, database_port)
db = connection.aredn
collection = db.sysinfo
j=collection.find( { "$and": [ { "lat": { "$ne": "" } }, { "lon": { "$ne": "" } } ] } )
# --- Iterate over the results to build the placemarks ---
for node in j:
try:
#print "Processing " + node['node']
node['name']=str(node["node"])
if "develop-16" in str(node['firmware_version']):
node['desc']=str(node["model"]) # from develop-160+, the model will be accurate that is passed to mongo
else:
node['desc']=board_model(str(node["board_id"]))
b=getBand(node['board_id'])
# get the style
nstyle=board_icon(str(b))
kml_style=createStyle(nstyle, icon_src + nstyle + '.png')
if not styleIdExists(k_doc, nstyle):
k_doc.Document.insert(0,kml_style);
if not styleIdExists(k_doc9, nstyle):
k_doc9.Document.insert(0,kml_style);
if not styleIdExists(k_doc2, nstyle):
k_doc2.Document.insert(0,kml_style);
if not styleIdExists(k_doc3, nstyle):
k_doc3.Document.insert(0,kml_style);
if not styleIdExists(k_doc5, nstyle):
k_doc5.Document.insert(0,kml_style);
if not styleIdExists(k_doclinks, nstyle):
k_doclinks.Document.insert(0,kml_style);
# ie. if old firmware, set WARN flag
warn = checkForWarnings(node)
# add a placemark for the node
pm=createPlacemark(node, nstyle, warn)
# print str(node['board_id'])
if b==9:
band9_folder.append(pm)
elif b==2:
band2_folder.append(pm)
elif b==3:
band3_folder.append(pm)
elif b==5:
band5_folder.append(pm)
# get neighbor links for this node
#
ls=getLinks(node) # returns list of IP's of neighbors
for l in ls:
stylename="line" + str(b)
n2=getNodeByIp(l)
if bool(n2): # we have a valid dictionary object
#if "172.31." in l: # it's a tunnel
# stylename=stylename + "t"
# DISABLED for soft launch
#if not styleIdExists(k_doc, stylename):
# k_doc.Document.insert(0,createLineStyle(stylename))
if not styleIdExists(k_doclinks, stylename):
k_doclinks.Document.insert(0,createLineStyle(stylename))
lineid=str(node['node']) + "-to-" + str(n2['node'])
line=createLine(lineid, node, n2, stylename)
if b==9:
links_band9_folder.append(line)
elif b==2:
links_band2_folder.append(line)
elif b==3:
links_band3_folder.append(line)
elif b==5:
links_band5_folder.append(line)
except KeyError, e:
print "KeyError: " + str(e)
pass
except KeyboardInterrupt:
break
except Exception, e:
print "(" + str(e) + ")"
print 'Error on line ' + str(sys.exc_info()[-1].tb_lineno)
# add bandX folders to bands folder
bands_folder.append(band9_folder)
bands_folder.append(band2_folder)
bands_folder.append(band3_folder)
bands_folder.append(band5_folder)
# add links band folders to links folder
links_folder.append(links_band9_folder)
links_folder.append(links_band2_folder)
links_folder.append(links_band3_folder)
links_folder.append(links_band5_folder)
# add links to the main bands file -- DISABLED TO DO A SOFT-RELEASE. Must download the links.kml file
# bands_folder.append(links_folder)
# All nodes file
writeFile(k_doc,"",bands_folder)
# 900 file
writeFile(k_doc9,"9",band9_folder)
# 2G file
writeFile(k_doc2,"2",band2_folder)
# 3G file
writeFile(k_doc3,"3",band3_folder)
# 5G file
writeFile(k_doc5,"5",band5_folder)
# Links file
writeFile(k_doclinks,"links",links_folder)