batcave.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3. import argparse
  4. from copy import deepcopy
  5. import daemon
  6. import logging
  7. import sys
  8. import time
  9. from ffstatus import *
  10. DEFAULT_INTERVAL = 15
  11. parser = argparse.ArgumentParser(description='Batman/Alfred Transmission Collection, Aggregation & Value Engine')
  12. parser.add_argument('--logfile', help='path for log file')
  13. parser.add_argument('--interval', type=int, default=DEFAULT_INTERVAL, help='data poll interval')
  14. parser.add_argument('-d', '--no-detach', action='store_true', help='Don\'t detach (daemonize) ourself')
  15. parser.add_argument('-n', '--no-send', action='store_true', help='Fetch data but don\'t send it')
  16. parser.add_argument('-A', '--alfred-json', help='executable path for alfred-json')
  17. parser.add_argument('-B', '--batadv-vis', help='executable path for batadv-vis')
  18. parser.add_argument('-G', '--graphite-host', help='Graphite host')
  19. parser.add_argument('--graphite-port', type=int, default=2003, help='Graphite port')
  20. parser.add_argument('--dashing-url', help='Dashing URL')
  21. parser.add_argument('--dashing-token', help='Dashing\'s secret update token')
  22. args = parser.parse_args()
  23. if args.interval < 5:
  24. print('A poll interval lower than 5s is not supported.')
  25. sys.exit(1)
  26. shall_daemonize = not args.no_detach
  27. logger = logging.getLogger()
  28. logger.setLevel(logging.DEBUG)
  29. if not args.logfile is None:
  30. fh = logging.FileHandler(args.logfile)
  31. fh.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S'))
  32. logger.addHandler(fh)
  33. if args.no_detach:
  34. ch = logging.StreamHandler(sys.stdout)
  35. ch.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S'))
  36. logger.addHandler(ch)
  37. logger.info('Starting up')
  38. storage = Storage()
  39. a = AlfredParser()
  40. b = BatmanParser()
  41. d = DashingClient(args.dashing_url, args.dashing_token) if not args.dashing_url is None else None
  42. g = GraphitePush(args.graphite_host, args.graphite_port) if not args.graphite_host is None else None
  43. if args.no_send:
  44. if not g is None: g.dont_send = True
  45. if not args.alfred_json is None: a.alfred_json = args.alfred_json
  46. if not args.batadv_vis is None: b.batadv_vis = args.batadv_vis
  47. logger.debug('Configured A.L.F.R.E.D. source: ' + str(a))
  48. logger.debug('Configured B.A.T.M.A.N. source: ' + str(b))
  49. logger.debug('Configured Dashing: ' + str(d))
  50. logger.debug('Configured Graphite: ' + str(g))
  51. for i in [ ('AlfredParser', a), ('BatmanParser', b) ]:
  52. try:
  53. i[1].sanitycheck()
  54. except Exception as err:
  55. logger.critical(i[0] + '.sanitycheck() failed: ' + str(err))
  56. print('FAILED SANITY CHECK: ' + str(err))
  57. sys.exit(1)
  58. if shall_daemonize:
  59. daemon_context = daemon.DaemonContext(
  60. files_preserve = [ fh.stream ],
  61. )
  62. daemon_context.open()
  63. while True:
  64. try:
  65. ts = int(time.time())
  66. logger.debug('Step 1/3: Fetching data ...')
  67. alfreddata = a.fetch()
  68. batmandata = b.fetch()
  69. newdata = merge_alfred_batman(alfreddata, batmandata)
  70. logger.info('Fetched data: {0} ALFRED with {1} BATMAN makes {2} total'.format(len(alfreddata), len(batmandata), len(newdata)))
  71. logger.debug('Step 2/3: Pushing update data ...')
  72. if not g is None:
  73. graphitedata = g.push(newdata, ts=ts)
  74. logger.info('Sent ' + str(graphitedata.count('\n')+1) + ' lines to Graphite.')
  75. if not d is None:
  76. d.push(newdata)
  77. logger.debug('Step 3/3: Merging current data ...')
  78. storage.data = dict_merge(storage.data, newdata)
  79. logger.info('I have data for ' + str(len(storage.data)) + ' nodes.')
  80. except Exception as err:
  81. logger.error(str(err))
  82. logger.debug('Sleeping for {0} seconds'.format(args.interval))
  83. time.sleep(args.interval)
  84. logger.info('Shut down.')