|
@@ -1,5 +1,6 @@
|
|
|
#!/usr/bin/python
|
|
|
from __future__ import print_function
|
|
|
+import argparse
|
|
|
from copy import deepcopy
|
|
|
import daemon
|
|
|
import logging
|
|
@@ -8,33 +9,45 @@ import time
|
|
|
|
|
|
from ffstatus import *
|
|
|
|
|
|
-INTERVAL = 15
|
|
|
-LOGFILE = '/var/log/batcave.log'
|
|
|
+DEFAULT_INTERVAL = 15
|
|
|
|
|
|
-DUMMY_MODE = 1
|
|
|
+parser = argparse.ArgumentParser(description='Batman/Alfred Transmission Collection, Aggregation & Value Engine')
|
|
|
+parser.add_argument('--logfile', help='path for log file')
|
|
|
+parser.add_argument('--interval', type=int, default=DEFAULT_INTERVAL, help='data poll interval')
|
|
|
+parser.add_argument('-n', '--no-send', action='store_true', help='Fetch data but don\'t send it')
|
|
|
+parser.add_argument('-A', '--alfred-json', help='executable path for alfred-json')
|
|
|
+parser.add_argument('-B', '--batadv-vis', help='executable path for batadv-vis')
|
|
|
+parser.add_argument('-G', '--graphite-host', help='Graphite host')
|
|
|
+parser.add_argument('--graphite-port', type=int, default=2003, help='Graphite port')
|
|
|
+parser.add_argument('--dashing-url', help='Dashing URL')
|
|
|
+parser.add_argument('--dashing-token', help='Dashing\'s secret update token')
|
|
|
+args = parser.parse_args()
|
|
|
+
|
|
|
+if args.interval < 5:
|
|
|
+ print('A poll interval lower than 5s is not supported.')
|
|
|
+ sys.exit(1)
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
-fh = logging.FileHandler(LOGFILE)
|
|
|
-fh.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S'))
|
|
|
-logger.addHandler(fh)
|
|
|
+
|
|
|
+if not args.logfile is None:
|
|
|
+ fh = logging.FileHandler(args.logfile)
|
|
|
+ fh.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S'))
|
|
|
+ logger.addHandler(fh)
|
|
|
|
|
|
logger.info('Starting up')
|
|
|
|
|
|
a = AlfredParser()
|
|
|
b = BatmanParser()
|
|
|
-d = DashingClient('dashing.krombel.de', 'TODO')
|
|
|
-g = GraphitePush('fdca:ffee:ff12:a254::da7a', 2003)
|
|
|
+d = DashingClient(args.dashing_url, args.dashing_token) if not args.dashing_url is None else None
|
|
|
+g = GraphitePush(args.graphite_host, args.graphite_port) if not args.graphite_host is None else None
|
|
|
data = { }
|
|
|
|
|
|
-if DUMMY_MODE:
|
|
|
- import os
|
|
|
- mydir = os.path.realpath(os.path.dirname(__file__))
|
|
|
- a.alfred_json = os.path.join(mydir, 'alfred-json')
|
|
|
- print('DUMMY.a =', a.alfred_json)
|
|
|
- b.batadv_vis = os.path.join(mydir, 'batadv_vis')
|
|
|
- print('DUMMY.b =', b.batadv_vis)
|
|
|
- g.dont_send = True
|
|
|
+if args.no_send:
|
|
|
+ if not g is None: g.dont_send = True
|
|
|
+
|
|
|
+if not args.alfred_json is None: a.alfred_json = args.alfred_json
|
|
|
+if not args.batadv_vis is None: b.batadv_vis = args.batadv_vis
|
|
|
|
|
|
for i in [ ('AlfredParser', a), ('BatmanParser', b) ]:
|
|
|
try:
|
|
@@ -58,10 +71,12 @@ with daemon_context:
|
|
|
logger.info('Fetched data: {0} ALFRED with {1} BATMAN makes {2} total'.format(len(alfreddata), len(batmandata), len(newdata)))
|
|
|
|
|
|
logger.debug('Step 2/3: Pushing update data ...')
|
|
|
- graphitedata = g.push(newdata, ts=ts)
|
|
|
- d.push(newdata)
|
|
|
+ if not g is None:
|
|
|
+ graphitedata = g.push(newdata, ts=ts)
|
|
|
+ logger.info('Sent ' + str(graphitedata.count('\n')+1) + ' lines to Graphite.')
|
|
|
+ if not d is None:
|
|
|
+ d.push(newdata)
|
|
|
|
|
|
- logger.info('Sent ' + str(graphitedata.count('\n')+1) + ' lines to Graphite.')
|
|
|
|
|
|
logger.debug('Step 3/3: Merging current data ...')
|
|
|
data = dict_merge(data, newdata)
|
|
@@ -69,7 +84,8 @@ with daemon_context:
|
|
|
except Exception as err:
|
|
|
logger.error(str(err))
|
|
|
|
|
|
- time.sleep(INTERVAL)
|
|
|
+ logger.debug('Sleeping for {0} seconds'.format(args.interval))
|
|
|
+ time.sleep(args.interval)
|
|
|
|
|
|
logger.info('Shutting down')
|
|
|
|