data-collector.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. h="""
  2. This is a data-collector client, that is intended to perform data-collection operations and submit them to an endpoint
  3. @required:
  4. - key application/service key
  5. - id node identifier
  6. usage :
  7. python data-collector.py --path config.json
  8. The configuration file is structured as JSON object as follows :
  9. {
  10. id: node identifier
  11. key: customer's identification key
  12. apps:"app_1,app_2,...",
  13. folders:"path_1,path_2, ..."
  14. }
  15. """
  16. from utils.params import PARAMS as SYS_ARGS, Logger
  17. import requests
  18. import pickle
  19. import json
  20. from threading import Thread, RLock
  21. ENDPOINT="https://the-phi.com/monitor"
  22. class Collector(Thread) :
  23. def __init__(self):
  24. Thread.__init__(self)
  25. """
  26. This function initializes the data collector with critical information
  27. The process will include validating the user's account and plan
  28. @param key customer's key
  29. @param id node identifier
  30. """
  31. for id in ['apps','folders']:
  32. if id in SYS_ARGS :
  33. SYS_ARGS[id] = SYS_ARGS[id].split(',')
  34. headers = {"key":SYS_ARGS["key"],"id":SYS_ARGS["id"]} #,"scope":json.dumps(scope)}
  35. headers['content-type'] = 'application/json'
  36. try:
  37. Logger.log(subject='Collector',object='api',action='request',value=ENDPOINT)
  38. url = "/".join([ENDPOINT,"init/collector"])
  39. data = {}
  40. for id in SYS_ARGS :
  41. if id not in ['id','key'] :
  42. data[id] = SYS_ARGS[id]
  43. r = requests.post(url,headers=headers,data=json.dumps(data))
  44. r = json.loads(r.text)
  45. self.monitor = pickle.loads(r[0])
  46. self.monitor.lock = RLock()
  47. #:w
  48. #self.monitor.set('lock',RLock())
  49. Logger.log(subject='Collector',object='api',action='load',value='')
  50. except Exception,e:
  51. Logger.log(subject='Collector',object='api',action='error',value=str(e))
  52. self.monitor = None
  53. def run(self):
  54. """
  55. This funtion runs the authorized features and
  56. """
  57. #self.monitor.start()
  58. Logger.log(subject='Collector',object='monitor',action='start',value='')
  59. thread = Thread(target=self.monitor.run)
  60. thread.start()
  61. # print self.monitor.config['store']
  62. # for agent in self.pool :
  63. # try:
  64. # agent = pickle.loads(agent)
  65. # agent.start()
  66. # #p = pickle.loads(self.pool[key])
  67. # #p.init(SYS_ARGS[key].split(','))
  68. # #p.start()
  69. # except Exception,e:
  70. # print e
  71. if __name__ == '__main__' and 'path' in SYS_ARGS:
  72. #
  73. #
  74. path = SYS_ARGS['path']
  75. f = open(path)
  76. p = json.loads(f.read())
  77. f.close()
  78. Logger.init('data-collector')
  79. SYS_ARGS = dict(SYS_ARGS,** p)
  80. thread = Collector()
  81. thread.start()
  82. else:
  83. print (h)