Merge pull request #14 from sargon/master

alfred: Retrieve aliases from alfred and integrate batadv-vis.
This commit is contained in:
Nils Schneider 2014-01-30 13:44:15 -08:00
commit 634b37fee2
5 changed files with 68 additions and 8 deletions

28
alfred.py Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env python3
import subprocess
import json
class alfred:
def __init__(self,request_data_type = 158):
self.request_data_type = request_data_type
def aliases(self):
output = subprocess.check_output(["alfred-json","-r",str(self.request_data_type),"-f","json"])
alfred_data = json.loads(output.decode("utf-8"))
alias = {}
for mac,node in alfred_data.items():
node_alias = {}
if 'location' in node:
node_alias['gps'] = str(node['location']['latitude']) + ' ' + str(node['location']['longitude'])
if 'hostname' in node:
node_alias['name'] = node['hostname']
elif 'name' in node:
node_alias['name'] = node['name']
if len(node_alias):
alias[mac] = node_alias
return alias
if __name__ == "__main__":
ad = alfred()
al = ad.alias()
print(al)

View file

@ -6,6 +6,7 @@ import argparse
import os import os
from batman import batman from batman import batman
from alfred import alfred
from rrd import rrd from rrd import rrd
from nodedb import NodeDB from nodedb import NodeDB
from d3mapbuilder import D3MapBuilder from d3mapbuilder import D3MapBuilder
@ -32,6 +33,9 @@ parser.add_argument('-m', '--mesh', action='append',
parser.add_argument('-o', '--obscure', action='store_true', parser.add_argument('-o', '--obscure', action='store_true',
help='obscure client macs') help='obscure client macs')
parser.add_argument('-A', '--alfred', action='store_true',
help='retrieve aliases from alfred')
parser.add_argument('-d', '--destination-directory', action='store', parser.add_argument('-d', '--destination-directory', action='store',
help='destination directory for generated files',required=True) help='destination directory for generated files',required=True)
@ -43,12 +47,12 @@ db = NodeDB()
if options['mesh']: if options['mesh']:
for mesh_interface in options['mesh']: for mesh_interface in options['mesh']:
bm = batman(mesh_interface) bm = batman(mesh_interface)
db.parse_vis_data(bm.vis_data()) db.parse_vis_data(bm.vis_data(options['alfred']))
for gw in bm.gateway_list(): for gw in bm.gateway_list():
db.mark_gateways(gw.mac) db.mark_gateways(gw.mac)
else: else:
bm = batman() bm = batman()
db.parse_vis_data(bm.vis_data()) db.parse_vis_data(bm.vis_data(options['alfred']))
for gw in bm.gateway_list(): for gw in bm.gateway_list():
db.mark_gateways([gw['mac']]) db.mark_gateways([gw['mac']])
@ -56,6 +60,10 @@ if options['aliases']:
for aliases in options['aliases']: for aliases in options['aliases']:
db.import_aliases(json.load(open(aliases))) db.import_aliases(json.load(open(aliases)))
if options['alfred']:
af = alfred()
db.import_aliases(af.aliases())
if options['obscure']: if options['obscure']:
db.obscure_clients() db.obscure_clients()

View file

@ -9,11 +9,13 @@ class batman:
def __init__(self, mesh_interface = "bat0"): def __init__(self, mesh_interface = "bat0"):
self.mesh_interface = mesh_interface self.mesh_interface = mesh_interface
def vis_data(self): def vis_data(self,batadv_vis=False):
""" Parse "batctl -m <mesh_interface> vd json -n" into an array of dictionaries. vds = self.vis_data_batctl_legacy()
""" if batadv_vis:
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"vd","json","-n"]) vds += self.vis_data_batadv_vis()
lines = output.splitlines() return vds
def vis_data_helper(self,lines):
vd = [] vd = []
for line in lines: for line in lines:
try: try:
@ -23,6 +25,23 @@ class batman:
pass pass
return vd return vd
def vis_data_batctl_legacy(self):
""" Parse "batctl -m <mesh_interface> vd json -n" into an array of dictionaries.
"""
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"vd","json","-n"])
lines = output.splitlines()
vds = self.vis_data_helper(lines)
for vd in vds:
vd['legacy'] = True
return vds
def vis_data_batadv_vis(self):
""" Parse "batadv-vis -i <mesh_interface> -f json" into an array of dictionaries.
"""
output = subprocess.check_output(["batadv-vis","-i",self.mesh_interface,"-f","json"])
lines = output.splitlines()
return self.vis_data_helper(lines)
def gateway_list(self): def gateway_list(self):
""" Parse "batctl -m <mesh_interface> gwl -n" into an array of dictionaries. """ Parse "batctl -m <mesh_interface> gwl -n" into an array of dictionaries.
""" """

View file

@ -52,6 +52,8 @@ class NodeDB:
except: except:
node = Node() node = Node()
node.flags['online'] = True node.flags['online'] = True
if 'legacy' in x:
node.flags['legacy'] = True
self._nodes.append(node) self._nodes.append(node)
node.add_mac(x['of']) node.add_mac(x['of'])
@ -65,6 +67,8 @@ class NodeDB:
except: except:
node = Node() node = Node()
node.flags['online'] = True node.flags['online'] = True
if 'legacy' in x:
node.flags['legacy'] = True
node.add_mac(x['router']) node.add_mac(x['router'])
self._nodes.append(node) self._nodes.append(node)

3
rrd.py
View file

@ -118,7 +118,8 @@ class rrd:
if not node.flags['client']: if not node.flags['client']:
nodes[node.id] = node nodes[node.id] = node
node.clients = 0; node.clients = 0;
clientCount -= 1 # XXX: might not be needed with gluon/alfred if 'legacy' in node.flags and node.flags['legacy']:
clientCount -= 1
else: else:
clientCount += 1 clientCount += 1
for link in db.get_links(): for link in db.get_links():