From ffbb2ae31da37049f04d50997ca71f5906bb59e7 Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Sat, 5 Aug 2017 09:08:03 +0200 Subject: [PATCH] =?UTF-8?q?fastd2redis.py=20hinzugef=C3=BCgt:=20Tunnelinfo?= =?UTF-8?q?rmationen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastd2redis.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 fastd2redis.py diff --git a/fastd2redis.py b/fastd2redis.py new file mode 100755 index 0000000..a97e65b --- /dev/null +++ b/fastd2redis.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Dieses Programm sollte auf einem Gateway laufen +# Es liefert Informationen über die Tunnel nach Redis + +import os +import sys +import socket +import json +import subprocess +import datetime + +from rediscluster import StrictRedisCluster + +def get_gate_nodeid(): + # Die ID kann statisch angegeben werden, falls das nicht + # der Fall ist, wird der Hostname angenommen + try: + with open('/etc/alfred/statics.json', 'r') as fh: + statics = json.load(fh) + except (IOError, ValueError): + return socket.gethostname() + try: + nodeid = statics['node']['node_id'] + except: + nodeid = socket.gethostname() + return nodeid + + +def main(rc): + + # fastd-Socket auslesen, liefert ein JSON-Objekt + client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + try: + client.connect("/var/run/fastd/ffpi.sock") + except socket.error, msg: + print >>sys.stderr, msg + sys.exit(1) + data = json.loads(client.makefile('r').read()) + client.close() + # Ermittelte Daten aufbereiten und nach Redis schreiben + gate_id = get_gate_nodeid() + now = datetime.datetime.now().replace(microsecond=0).isoformat() + peers = {} + for key, peer in data['peers'].iteritems(): + if peer['connection']: + try: + tunnel_id = peer['connection']['mac_addresses'][0].replace(':', '') + except IndexError: + continue + rckey = "fastd:tunnel:%s" % tunnel_id + rc.hset(rckey, 'key', key) + rc.hset(rckey, 'last_seen', now) + rc.hset(rckey, 'last_gate', gate_id) + +if __name__ == '__main__': + startup_nodes = [{"host": "127.0.0.1", "port": "7000"}] + try: + rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True) + except ConnectionError: + # Kann z.B. bei lokalen Netzwerkproblemen auftreten + sys.exit(1) + main(rc)