1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- h="""
- This is a data-collector client, that is intended to perform data-collection operations and submit them to an endpoint
- @required:
- - key application/service key
- - id node identifier
- usage :
- python data-collector.py --path config.json
- The configuration file is structured as JSON object as follows :
- {
- id: node identifier
- key: customer's identification key
- apps:"app_1,app_2,...",
- folders:"path_1,path_2, ..."
- }
- """
- from utils.params import PARAMS as SYS_ARGS, Logger
- import requests
- import pickle
- import json
- from threading import Thread, RLock
- ENDPOINT="https://the-phi.com/monitor"
- class Collector(Thread) :
- def __init__(self):
- Thread.__init__(self)
- """
- This function initializes the data collector with critical information
- The process will include validating the user's account and plan
- @param key customer's key
- @param id node identifier
-
- """
- for id in ['apps','folders']:
- if id in SYS_ARGS :
- SYS_ARGS[id] = SYS_ARGS[id].split(',')
-
-
- headers = {"key":SYS_ARGS["key"],"id":SYS_ARGS["id"]} #,"scope":json.dumps(scope)}
- headers['content-type'] = 'application/json'
- try:
- Logger.log(subject='Collector',object='api',action='request',value=ENDPOINT)
- url = "/".join([ENDPOINT,"init/collector"])
- data = {}
- for id in SYS_ARGS :
- if id not in ['id','key'] :
- data[id] = SYS_ARGS[id]
-
- r = requests.post(url,headers=headers,data=json.dumps(data))
- r = json.loads(r.text)
-
- self.monitor = pickle.loads(r[0])
- self.monitor.lock = RLock()
- #:w
- #self.monitor.set('lock',RLock())
- Logger.log(subject='Collector',object='api',action='load',value='')
- except Exception,e:
-
- Logger.log(subject='Collector',object='api',action='error',value=str(e))
- self.monitor = None
- def run(self):
- """
- This funtion runs the authorized features and
- """
- #self.monitor.start()
-
- Logger.log(subject='Collector',object='monitor',action='start',value='')
- thread = Thread(target=self.monitor.run)
- thread.start()
- # print self.monitor.config['store']
- # for agent in self.pool :
- # try:
- # agent = pickle.loads(agent)
- # agent.start()
- # #p = pickle.loads(self.pool[key])
- # #p.init(SYS_ARGS[key].split(','))
- # #p.start()
- # except Exception,e:
- # print e
- if __name__ == '__main__' and 'path' in SYS_ARGS:
- #
- #
-
- path = SYS_ARGS['path']
- f = open(path)
- p = json.loads(f.read())
- f.close()
- Logger.init('data-collector')
- SYS_ARGS = dict(SYS_ARGS,** p)
- thread = Collector()
- thread.start()
- else:
- print (h)
|