2014-09-23 11:57:45 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-03-24 16:49:37 +01:00
|
|
|
"""
|
|
|
|
backend.py - ffmap-backend runner
|
|
|
|
https://github.com/ffnord/ffmap-backend
|
|
|
|
"""
|
2014-09-23 11:57:45 +02:00
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import networkx as nx
|
|
|
|
from datetime import datetime
|
|
|
|
from networkx.readwrite import json_graph
|
|
|
|
|
|
|
|
import alfred
|
|
|
|
import nodes
|
|
|
|
import graph
|
2015-03-24 16:49:37 +01:00
|
|
|
from batman import Batman
|
|
|
|
from rrddb import RRD
|
2014-09-23 11:57:45 +02:00
|
|
|
|
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
def main(params):
|
|
|
|
if not params['mesh']:
|
|
|
|
params['mesh'] = ['bat0']
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
nodes_fn = os.path.join(params['destination_directory'], 'nodes.json')
|
|
|
|
graph_fn = os.path.join(params['destination_directory'], 'graph.json')
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
now = datetime.utcnow().replace(microsecond=0)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
with open(nodes_fn, 'r') as nodedb_handle:
|
|
|
|
nodedb = json.load(nodedb_handle)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
# flush nodedb if it uses the old format
|
|
|
|
if 'links' in nodedb:
|
|
|
|
nodedb = {'nodes': dict()}
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
nodedb['timestamp'] = now.isoformat()
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
for node_id, node in nodedb['nodes'].items():
|
|
|
|
node['flags']['online'] = False
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
nodes.import_nodeinfo(nodedb['nodes'], alfred.nodeinfo(), now, assume_online=True)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
for aliases in params['aliases']:
|
|
|
|
with open(aliases, 'r') as f:
|
|
|
|
nodes.import_nodeinfo(nodedb['nodes'], json.load(f), now, assume_online=False)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
nodes.reset_statistics(nodedb['nodes'])
|
|
|
|
nodes.import_statistics(nodedb['nodes'], alfred.statistics())
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
bm = list(map(lambda d: (d.vis_data(True), d.gateway_list()), map(Batman, params['mesh'])))
|
|
|
|
for vis_data, gateway_list in bm:
|
|
|
|
nodes.import_mesh_ifs_vis_data(nodedb['nodes'], vis_data)
|
|
|
|
nodes.import_vis_clientcount(nodedb['nodes'], vis_data)
|
|
|
|
nodes.mark_vis_data_online(nodedb['nodes'], vis_data, now)
|
|
|
|
nodes.mark_gateways(nodedb['nodes'], gateway_list)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
if params['prune']:
|
|
|
|
nodes.prune_nodes(nodedb['nodes'], now, int(params['prune']))
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
batadv_graph = nx.DiGraph()
|
|
|
|
for vis_data, gateway_list in bm:
|
|
|
|
graph.import_vis_data(batadv_graph, nodedb['nodes'], vis_data)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
if params['vpn']:
|
|
|
|
graph.mark_vpn(batadv_graph, frozenset(params['vpn']))
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
batadv_graph = graph.merge_nodes(batadv_graph)
|
|
|
|
batadv_graph = graph.to_undirected(batadv_graph)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
with open(nodes_fn, 'w') as f:
|
|
|
|
json.dump(nodedb, f)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
with open(graph_fn, 'w') as f:
|
|
|
|
json.dump({'batadv': json_graph.node_link_data(batadv_graph)}, f)
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
scriptdir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
rrd = RRD(scriptdir + '/nodedb/', params['destination_directory'] + '/nodes')
|
|
|
|
rrd.update_database(nodedb['nodes'])
|
|
|
|
rrd.update_images()
|
2014-09-23 11:57:45 +02:00
|
|
|
|
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
parser.add_argument('-a', '--aliases',
|
|
|
|
help='read aliases from FILE',
|
|
|
|
default=[], action='append',
|
|
|
|
metavar='FILE')
|
|
|
|
parser.add_argument('-m', '--mesh', action='append',
|
|
|
|
help='batman mesh interface')
|
|
|
|
parser.add_argument('-d', '--destination-directory', action='store',
|
|
|
|
help='destination directory for generated files',
|
|
|
|
required=True)
|
|
|
|
parser.add_argument('--vpn', action='append', metavar='MAC',
|
|
|
|
help='assume MAC to be part of the VPN')
|
|
|
|
parser.add_argument('--prune', metavar='DAYS',
|
|
|
|
help='forget nodes offline for at least DAYS')
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
options = vars(parser.parse_args())
|
2014-09-23 11:57:45 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
main(options)
|