def run(self): url = settings.HTTP_URL + "/report" now = int(time.time()) nonce = ''.join( random.sample('abcdefghijklmnopqrstuvwxyz0123456789', 32)) headers = { r'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', r'Accept': 'application/json, text/javascript, */*; q=0.01', r'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } data = { 'appid': settings.HTTP_SECRET_ID, 'nonce': nonce, 'timestamp': str(now), 'hostname': settings.CACHE_HOST_NAME, 'taskid': self.taskid, 'status': str(self.status), 'msg': self.msg } sign = osutil.createSign(settings.HTTP_SECRET_KEY, data) data['sign'] = sign data = parse.urlencode(data).encode('utf-8') try: req = request.Request(url, data, headers, None, None, 'POST') request.urlopen(req, None, settings.HTTP_TIMEOUT).read() except Exception as e: log.error(str(e) + ':::' + url)
def run(self): url = settings.HTTP_URL + "/task" now = int(time.time()) nonce = ''.join( random.sample('abcdefghijklmnopqrstuvwxyz0123456789', 32)) headers = { r'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', r'Accept': 'application/json, text/javascript, */*; q=0.01', r'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } mem = psutil.virtual_memory() cpu_percent = psutil.cpu_percent(0.5) net = psutil.net_io_counters(pernic=False, nowrap=True) hdd = psutil.disk_usage('/') data = { 'appid': settings.HTTP_SECRET_ID, 'nonce': nonce, 'timestamp': str(now), 'apps': ','.join(settings.APP_LIST), 'hostname': settings.CACHE_HOST_NAME, 'mem_total': str(mem.total), 'mem_used': str(mem.used), 'mem_free': str(mem.free), 'mem_percent': str(mem.percent), 'cpu_percent': str(cpu_percent), 'net_sent': str(net.bytes_sent), 'net_recv': str(net.bytes_recv), 'net_tcp': str(osutil.netstat()), 'hdd_total': str(hdd.total), 'hdd_used': str(hdd.used), 'hdd_free': str(hdd.free), 'hdd_percent': str(hdd.percent) } sign = osutil.createSign(settings.HTTP_SECRET_KEY, data) data['sign'] = sign data = parse.urlencode(data).encode('utf-8') try: req = request.Request(url, data, headers, None, None, 'POST') page = request.urlopen(req, None, settings.HTTP_TIMEOUT).read() result = page.decode('utf-8') log.debug(result) res_data = json.loads(result) if res_data['code'] == 0: task_list = res_data['data'] for task in task_list: # 将任务ID放到缓存里,防止重复执行同一个任务 if task['id'] in settings.CACHE_TASK_IDS: log.debug('任务已存在,任务ID:::' + task['id']) else: settings.CACHE_TASK_IDS.append(task['id']) t = execTaskThread(task['id'], task) t.start() log.debug('任务已添加,任务ID:::' + task['id']) except Exception as e: log.error(str(e) + ':::' + url)
def dowload(type, name, version): if type == 'conf': url = settings.HTTP_URL + "/conf/download" else: url = settings.HTTP_URL + "/jar/download" now = int(time.time()) nonce = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz0123456789', 32)) headers = { r'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', r'Accept': 'application/json, text/javascript, */*; q=0.01', r'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } data = { 'appid': settings.HTTP_SECRET_ID, 'nonce': nonce, 'timestamp': str(now), 'hosts': ','.join(settings.APP_LIST), 'hostname': settings.CACHE_HOST_NAME, 'name': name, 'version': version } sign = osutil.createSign(settings.HTTP_SECRET_KEY, data) data['sign'] = sign data = parse.urlencode(data).encode('utf-8') try: req = request.Request(url, data, headers, None, None, 'POST') data = request.urlopen(req, None, settings.HTTP_TIMEOUT).read() if type == 'conf': filepath = settings.APP_ROOT + name + '/conf/' + version + "/" + name + '.properties' else: filepath = settings.APP_ROOT + name + '/app/' + version + "/" + name + '.jar' with open(filepath, "wb+") as file: file.write(data) return True except Exception as e: log.error(str(e) + ':::' + url) return False