data-collector.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  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. """
  7. from utils.params import PARAMS as SYS_ARGS
  8. import requests
  9. import pickle
  10. import json
  11. from threading import Thread, RLock
  12. ENDPOINT="https://the-phi.com/monitor"
  13. class Collector(Thread) :
  14. def __init__(self):
  15. Thread.__init__(self)
  16. """
  17. This function initializes the data collector with critical information
  18. The process will include validating the user's account and plan
  19. @param key customer's key
  20. @param id node identifier
  21. """
  22. for id in ['apps','folders']:
  23. if id in SYS_ARGS :
  24. SYS_ARGS[id] = SYS_ARGS[id].split(',')
  25. headers = {"key":SYS_ARGS["key"],"id":SYS_ARGS["id"]} #,"scope":json.dumps(scope)}
  26. headers['content-type'] = 'application/json'
  27. try:
  28. url = "/".join([ENDPOINT,"init/collector"])
  29. r = requests.post(url,headers=headers,data=json.dumps(SYS_ARGS))
  30. r = json.loads(r.text)
  31. self.monitor = pickle.loads(r[0])
  32. self.monitor.lock = RLock()
  33. #:w
  34. #self.monitor.set('lock',RLock())
  35. except Exception,e:
  36. print e.message
  37. self.monitor = None
  38. def run(self):
  39. """
  40. This funtion runs the authorized features and
  41. """
  42. #self.monitor.start()
  43. thread = Thread(target=self.monitor.run)
  44. thread.start()
  45. # print self.monitor.config['store']
  46. # for agent in self.pool :
  47. # try:
  48. # agent = pickle.loads(agent)
  49. # agent.start()
  50. # #p = pickle.loads(self.pool[key])
  51. # #p.init(SYS_ARGS[key].split(','))
  52. # #p.start()
  53. # except Exception,e:
  54. # print e
  55. if __name__ == '__main__' :
  56. thread = Collector()
  57. thread.start()