2
0

status-daemon.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3. from copy import deepcopy
  4. import daemon
  5. import logging
  6. import sys
  7. import time
  8. from ffstatus import *
  9. INTERVAL = 15
  10. LOGFILE = '/var/log/ffstatus.log'
  11. DUMMY_MODE = 1
  12. logger = logging.getLogger()
  13. logger.setLevel(logging.DEBUG)
  14. fh = logging.FileHandler(LOGFILE)
  15. fh.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S'))
  16. logger.addHandler(fh)
  17. logger.info('Starting up')
  18. a = AlfredParser()
  19. b = BatmanParser()
  20. d = DashingClient('dashing.krombel.de', 'TODO')
  21. g = GraphitePush('fdca:ffee:ff12:a254::da7a', 2003)
  22. data = { }
  23. if DUMMY_MODE:
  24. import os
  25. mydir = os.path.realpath(os.path.dirname(__file__))
  26. a.alfred_json = os.path.join(mydir, 'alfred-json')
  27. print('DUMMY.a =', a.alfred_json)
  28. b.batadv_vis = os.path.join(mydir, 'batadv_vis')
  29. print('DUMMY.b =', b.batadv_vis)
  30. g.dont_send = True
  31. for i in [ ('AlfredParser', a), ('BatmanParser', b) ]:
  32. try:
  33. i[1].sanitycheck()
  34. except Exception as err:
  35. logger.critical(i[0] + '.sanitycheck() failed: ' + str(err))
  36. print('FAILED SANITY CHECK: ' + str(err))
  37. sys.exit(1)
  38. daemon_context = daemon.DaemonContext(
  39. files_preserve = [ fh.stream ],
  40. )
  41. with daemon_context:
  42. while True:
  43. try:
  44. ts = int(time.time())
  45. logger.debug('Step 1/3: Fetching data ...')
  46. alfreddata = a.fetch()
  47. batmandata = b.fetch()
  48. newdata = dict_merge(alfreddata, batmandata)
  49. logger.info('Fetched data: {0} ALFRED with {1} BATMAN makes {2} total'.format(len(alfreddata), len(batmandata), len(newdata)))
  50. logger.debug('Step 2/3: Pushing update data ...')
  51. graphitedata = g.push(newdata, ts=ts)
  52. d.push(newdata)
  53. logger.info('Sent ' + str(graphitedata.count('\n')+1) + ' lines to Graphite.')
  54. logger.debug('Step 3/3: Merging current data ...')
  55. data = dict_merge(data, newdata)
  56. logger.info('I have data for ' + str(len(data)) + ' nodes.')
  57. except Exception as err:
  58. logger.error(str(err))
  59. time.sleep(INTERVAL)
  60. logger.info('Shutting down')