From 6fba8ad21b50cacbb77e27ad6e981135db9e576d Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Tue, 24 Mar 2015 22:48:00 +0100 Subject: [PATCH] add alfred socket support (--alfred-sock) --- backend.py | 8 +++++++- lib/alfred.py | 37 ++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/backend.py b/backend.py index 5b60d3e..3eaeaf1 100755 --- a/backend.py +++ b/backend.py @@ -11,7 +11,8 @@ from datetime import datetime import networkx as nx from networkx.readwrite import json_graph -from lib import alfred, graph, nodes +from lib import graph, nodes +from lib.alfred import Alfred from lib.batman import Batman from lib.rrddb import RRD @@ -34,6 +35,8 @@ def main(params): for node_id, node in nodedb['nodes'].items(): node['flags']['online'] = False + alfred = Alfred(unix_sockpath=params['alfred_sock']) + nodes.import_nodeinfo(nodedb['nodes'], alfred.nodeinfo(), now, assume_online=True) @@ -91,6 +94,9 @@ if __name__ == '__main__': parser.add_argument('-m', '--mesh', action='append', default=['bat0'], help='batman mesh interface (defaults to bat0)') + parser.add_argument('-s', '--alfred-sock', + default=None, + help='alfred unix socket path') parser.add_argument('-d', '--dest-dir', action='store', help='destination directory for generated files', required=True) diff --git a/lib/alfred.py b/lib/alfred.py index 0467316..1b9b220 100644 --- a/lib/alfred.py +++ b/lib/alfred.py @@ -1,20 +1,35 @@ import subprocess import json +import os -def _fetch(data_type): - output = subprocess.check_output( - ["alfred-json", "-z", "-f", "json", "-r", str(data_type)]) - return json.loads(output.decode("utf-8")).values() +class Alfred(object): + """ + Bindings for the alfred-json utility + """ + def __init__(self, unix_sockpath=None): + if unix_sockpath: + if os.path.exists(unix_sockpath): + self.unix_sock = unix_sockpath + else: + raise RuntimeError('alfred: invalid unix socket path given') + def _fetch(self, data_type): + cmd = ['alfred-json', + '-z', + '-f', 'json', + '-r', data_type] + if self.unix_sock: + cmd.extend(['-s', self.unix_sock]) -def nodeinfo(): - return _fetch(158) + output = subprocess.check_output(cmd) + return json.loads(output.decode("utf-8")).values() + def nodeinfo(self): + return self._fetch(158) -def statistics(): - return _fetch(159) + def statistics(self): + return self._fetch(159) - -def vis(): - return _fetch(160) + def vis(self): + return self._fetch(160)