From 2c79b1d91cb896b65f3cbd670ab8d5fab43261c2 Mon Sep 17 00:00:00 2001 From: Daniel Ehlers Date: Fri, 24 Jan 2014 02:53:17 +0100 Subject: [PATCH 1/4] alfred: Retrieve aliases from alfred and integrate batadv-vis. Extending the visualisation data sources by the new batadv-vis, which itself base ontop of alfred. Also extending the aliases import by parsing informations from alfred. The last part is possible through the preprocessing of data with alfred-json, so we don't need to process the 'broken' format alfred itself supports. This patch needs batadv-vis and tcatm/alfred-json in the PATH. --- alfred.py | 28 ++++++++++++++++++++++++++++ bat2nodes.py | 8 ++++++++ batman.py | 21 +++++++++++++++++---- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100755 alfred.py diff --git a/alfred.py b/alfred.py new file mode 100755 index 0000000..c0f9d13 --- /dev/null +++ b/alfred.py @@ -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) diff --git a/bat2nodes.py b/bat2nodes.py index af1f10f..783e53f 100755 --- a/bat2nodes.py +++ b/bat2nodes.py @@ -6,6 +6,7 @@ import argparse import os from batman import batman +from alfred import alfred from rrd import rrd from nodedb import NodeDB from d3mapbuilder import D3MapBuilder @@ -32,6 +33,9 @@ parser.add_argument('-m', '--mesh', action='append', parser.add_argument('-o', '--obscure', action='store_true', 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', help='destination directory for generated files',required=True) @@ -56,6 +60,10 @@ if options['aliases']: for aliases in options['aliases']: db.import_aliases(json.load(open(aliases))) +if options['alfred']: + af = alfred() + db.import_aliases(af.aliases()) + if options['obscure']: db.obscure_clients() diff --git a/batman.py b/batman.py index 5a19016..1260ad1 100755 --- a/batman.py +++ b/batman.py @@ -10,10 +10,9 @@ class batman: self.mesh_interface = mesh_interface def vis_data(self): - """ Parse "batctl -m vd json -n" into an array of dictionaries. - """ - output = subprocess.check_output(["batctl","-m",self.mesh_interface,"vd","json","-n"]) - lines = output.splitlines() + return self.vis_data_batadv_vis() + self.vis_data_batctl_legacy() + + def vis_data_helper(self,lines): vd = [] for line in lines: try: @@ -23,6 +22,20 @@ class batman: pass return vd + def vis_data_batctl_legacy(self): + """ Parse "batctl -m vd json -n" into an array of dictionaries. + """ + output = subprocess.check_output(["batctl","-m",self.mesh_interface,"vd","json","-n"]) + lines = output.splitlines() + return self.vis_data_helper(lines) + + def vis_data_batadv_vis(self): + """ Parse "batadv-vis -i -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): """ Parse "batctl -m gwl -n" into an array of dictionaries. """ From d9451e2ea7ee2f3ee0552e9f9add9cf6dce23937 Mon Sep 17 00:00:00 2001 From: Daniel Ehlers Date: Thu, 30 Jan 2014 19:10:48 +0100 Subject: [PATCH 2/4] alfred: Mark all nodes from legacy support. --- batman.py | 5 ++++- nodedb.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/batman.py b/batman.py index 1260ad1..c33f3e0 100755 --- a/batman.py +++ b/batman.py @@ -27,7 +27,10 @@ class batman: """ output = subprocess.check_output(["batctl","-m",self.mesh_interface,"vd","json","-n"]) lines = output.splitlines() - return self.vis_data_helper(lines) + 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 -f json" into an array of dictionaries. diff --git a/nodedb.py b/nodedb.py index 8685c81..9dd9849 100644 --- a/nodedb.py +++ b/nodedb.py @@ -52,6 +52,8 @@ class NodeDB: except: node = Node() node.flags['online'] = True + if 'legacy' in x: + node.flags['legacy'] = True self._nodes.append(node) node.add_mac(x['of']) @@ -65,6 +67,8 @@ class NodeDB: except: node = Node() node.flags['online'] = True + if 'legacy' in x: + node.flags['legacy'] = True node.add_mac(x['router']) self._nodes.append(node) From 4c4e8d846bbf341c61b822c716fa89ce6d5dd2b8 Mon Sep 17 00:00:00 2001 From: Daniel Ehlers Date: Thu, 30 Jan 2014 22:08:40 +0100 Subject: [PATCH 3/4] rrd: Only decrease client count for legacy nodes. --- rrd.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rrd.py b/rrd.py index 26ff956..aaf64d7 100755 --- a/rrd.py +++ b/rrd.py @@ -118,7 +118,8 @@ class rrd: if not node.flags['client']: nodes[node.id] = node 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: clientCount += 1 for link in db.get_links(): From 319b071fd8fe9bbd2e8398358722fe5f3862ec2b Mon Sep 17 00:00:00 2001 From: Daniel Ehlers Date: Thu, 30 Jan 2014 22:21:15 +0100 Subject: [PATCH 4/4] alfred: Make the alfred based batadv-vis data source optional. --- bat2nodes.py | 4 ++-- batman.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bat2nodes.py b/bat2nodes.py index 783e53f..15e0a39 100755 --- a/bat2nodes.py +++ b/bat2nodes.py @@ -47,12 +47,12 @@ db = NodeDB() if options['mesh']: for mesh_interface in options['mesh']: 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(): db.mark_gateways(gw.mac) else: bm = batman() - db.parse_vis_data(bm.vis_data()) + db.parse_vis_data(bm.vis_data(options['alfred'])) for gw in bm.gateway_list(): db.mark_gateways([gw['mac']]) diff --git a/batman.py b/batman.py index c33f3e0..fac2a9e 100755 --- a/batman.py +++ b/batman.py @@ -9,8 +9,11 @@ class batman: def __init__(self, mesh_interface = "bat0"): self.mesh_interface = mesh_interface - def vis_data(self): - return self.vis_data_batadv_vis() + self.vis_data_batctl_legacy() + def vis_data(self,batadv_vis=False): + vds = self.vis_data_batctl_legacy() + if batadv_vis: + vds += self.vis_data_batadv_vis() + return vds def vis_data_helper(self,lines): vd = []