Merge pull request #45 from mweinelt/pull2

Update argument parser
influxdb
Nils Schneider 2015-03-26 14:14:34 +01:00
commit c93d6c0192
1 changed files with 58 additions and 33 deletions

View File

@ -6,6 +6,7 @@ https://github.com/ffnord/ffmap-backend
import argparse import argparse
import json import json
import os import os
import sys
from datetime import datetime from datetime import datetime
import networkx as nx import networkx as nx
@ -23,9 +24,37 @@ def main(params):
now = datetime.utcnow().replace(microsecond=0) now = datetime.utcnow().replace(microsecond=0)
# parse mesh param and instantiate Alfred/Batman instances
alfred_instances = []
batman_instances = []
for value in params['mesh']:
# (1) only batman-adv if, no alfred sock
if ':' not in value:
if len(params['mesh']) > 1:
raise ValueError(
'Multiple mesh interfaces require the use of '
'alfred socket paths.')
alfred_instances.append(Alfred(unix_sockpath=None))
batman_instances.append(Batman(mesh_interface=value))
else:
# (2) batman-adv if + alfred socket
try:
batif, alfredsock = value.split(':')
alfred_instances.append(Alfred(unix_sockpath=alfredsock))
batman_instances.append(Batman(mesh_interface=batif,
alfred_sockpath=alfredsock))
except ValueError:
raise ValueError(
'Unparseable value "{0}" in --mesh parameter.'.
format(value))
# read nodedb state from node.json # read nodedb state from node.json
try:
with open(nodes_fn, 'r') as nodedb_handle: with open(nodes_fn, 'r') as nodedb_handle:
nodedb = json.load(nodedb_handle) nodedb = json.load(nodedb_handle)
except FileNotFoundError:
nodedb = {'nodes': dict()}
# flush nodedb if it uses the old format # flush nodedb if it uses the old format
if 'links' in nodedb: if 'links' in nodedb:
nodedb = {'nodes': dict()} nodedb = {'nodes': dict()}
@ -36,7 +65,7 @@ def main(params):
node['flags']['online'] = False node['flags']['online'] = False
# integrate alfred nodeinfo # integrate alfred nodeinfo
alfred = Alfred(unix_sockpath=params['alfred_sock']) for alfred in alfred_instances:
nodes.import_nodeinfo(nodedb['nodes'], alfred.nodeinfo(), nodes.import_nodeinfo(nodedb['nodes'], alfred.nodeinfo(),
now, assume_online=True) now, assume_online=True)
@ -47,22 +76,19 @@ def main(params):
now, assume_online=False) now, assume_online=False)
nodes.reset_statistics(nodedb['nodes']) nodes.reset_statistics(nodedb['nodes'])
for alfred in alfred_instances:
nodes.import_statistics(nodedb['nodes'], alfred.statistics()) nodes.import_statistics(nodedb['nodes'], alfred.statistics())
# initialize batman bindings for each mesh interface # acquire gwl and visdata for each batman instance
# and acquire gwl and visdata mesh_info = []
mesh_interfaces = frozenset(params['mesh']) for batman in batman_instances:
mesh_info = {} vd = batman.vis_data(True)
for interface in mesh_interfaces: gwl = batman.gateway_list()
bm = Batman(mesh_interface=interface,
alfred_sockpath=params['alfred_sock'])
vd = bm.vis_data(True)
gwl = bm.gateway_list()
mesh_info[interface] = (vd, gwl) mesh_info.append((vd, gwl))
# update nodedb from batman-adv data # update nodedb from batman-adv data
for vd, gwl in mesh_info.values(): for vd, gwl in mesh_info:
nodes.import_mesh_ifs_vis_data(nodedb['nodes'], vd) nodes.import_mesh_ifs_vis_data(nodedb['nodes'], vd)
nodes.import_vis_clientcount(nodedb['nodes'], vd) nodes.import_vis_clientcount(nodedb['nodes'], vd)
nodes.mark_vis_data_online(nodedb['nodes'], vd, now) nodes.mark_vis_data_online(nodedb['nodes'], vd, now)
@ -70,11 +96,11 @@ def main(params):
# clear the nodedb from nodes that have not been online in $prune days # clear the nodedb from nodes that have not been online in $prune days
if params['prune']: if params['prune']:
nodes.prune_nodes(nodedb['nodes'], now, int(params['prune'])) nodes.prune_nodes(nodedb['nodes'], now, params['prune'])
# build nxnetworks graph from nodedb and visdata # build nxnetworks graph from nodedb and visdata
batadv_graph = nx.DiGraph() batadv_graph = nx.DiGraph()
for vd, gwl in mesh_info.values(): for vd, gwl in mesh_info:
graph.import_vis_data(batadv_graph, nodedb['nodes'], vd) graph.import_vis_data(batadv_graph, nodedb['nodes'], vd)
# force mac addresses to be vpn-link only (like gateways for example) # force mac addresses to be vpn-link only (like gateways for example)
@ -104,26 +130,25 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-a', '--aliases', parser.add_argument('-a', '--aliases',
help='read aliases from FILE', help='Read aliases from FILE',
default=[], action='append', default=[], action='append',
metavar='FILE') metavar='FILE')
parser.add_argument('-m', '--mesh', action='append', parser.add_argument('-m', '--mesh',
default=['bat0'], default=['bat0'], nargs='+',
help='batman mesh interface (defaults to bat0)') help='Use given batman-adv mesh interface(s) (defaults'
parser.add_argument('-s', '--alfred-sock', 'to bat0); specify alfred unix socket like '
default=None, 'bat0:/run/alfred0.sock.')
help='alfred unix socket path')
parser.add_argument('-d', '--dest-dir', action='store', parser.add_argument('-d', '--dest-dir', action='store',
help='destination directory for generated files', help='Write output to destination directory',
required=True) required=True)
parser.add_argument('--vpn', action='append', metavar='MAC', parser.add_argument('-V', '--vpn', nargs='+', metavar='MAC',
help='assume MAC to be part of the VPN') help='Assume MAC addresses are part of vpn')
parser.add_argument('--prune', metavar='DAYS', parser.add_argument('-p', '--prune', metavar='DAYS', type=int,
help='forget nodes offline for at least DAYS') help='forget nodes offline for at least DAYS')
parser.add_argument('--rrd', dest='rrd', action='store_true', parser.add_argument('--with-rrd', dest='rrd', action='store_true',
default=False, default=False,
help='create RRD graphs') help='enable the rendering of RRD graphs (cpu '
'intensive)')
options = vars(parser.parse_args()) options = vars(parser.parse_args())
main(options) main(options)