def get_monitor_status(request): req = json.loads(request.body) monitor_id = req['monitor_id'] cookie = req['cookie'] result = monitor.getMonitorById(monitor_id=monitor_id) url = result['monitor_url'] print(result) sys_code = result['sysCode'] data = { "sessionID": "01D9A743AF3DDF5D7CBD7B36CC2405E8", "cameraID": "", "sysCode": sys_code } # 请求头 header = { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", } rm = RunMethod() cm = CommonUtil() print(url, data, header, cookie) req = rm.post_main(url=url, data=data, header=header, cookies=cookie) status = '' if cm.is_contain('<IsOnline>1</IsOnline>', req.text): status = "在线" else: status = "不在线" response = {'code': 200, 'message': '已获得监控状态', 'data': status} return JsonResponse(response)
def CombineVersion(request): req = json.loads(request.body) sd = SystemData() version_list = req['versionList'] version_list = str(version_list) version_list = version_list.split(',') ip = sd.getRemoteIp() # ip username = sd.getRemoteUsername() # 用户名 password = sd.getRemotePassword() # 密码 rm = OperationWINRM(ip, username, password) project_id = req['project_id'] environment_id = req['environment_id'] svn_info = getSVNInfo(project_id=project_id, environment_id=environment_id) cmd = getRightCommend(version_list, svn_info) # list_command = "" # for i in version_list: # list_command = list_command + " -c " + i if environment_id != '3': print("svn merge " + svn_info.svn_position + " " + cmd + " " + "\"" + svn_info.project_position + "\"" + " --username " + svn_info.svn_username + " --password " + svn_info.svn_password + " --no-auth-cache") data = rm.do_cmd("svn merge " + svn_info.svn_position + " " + cmd + " " + "\"" + svn_info.project_position + "\"" + " --username " + svn_info.svn_username + " --password " + svn_info.svn_password + " --no-auth-cache --non-interactive") else: print("svn merge " + svn_info.svn_position + " " + cmd + " " + "\"" + svn_info.project_position + "\"" + " --username " + svn_info.svn_username + " --password " + svn_info.svn_password + " --no-auth-cache") data = rm.do_cmd("svn merge " + svn_info.svn_position + " " + cmd + " " + "\"" + svn_info.project_position + "\"" + " --username " + svn_info.svn_username + " --password " + svn_info.svn_password + " --no-auth-cache --non-interactive") cm = CommonUtil() if cm.is_contain("Summary of conflicts", data): conflicts = re.findall("C .*?\n", data) conflicts_str = '' for conflict in conflicts: conflicts_str = conflicts_str + str(conflict) data = str(data) + '\n 冲突如下:\n' + conflicts_str response = {'code': 200, 'message': "conflicts", 'data': data} else: response = {'code': 200, 'message': "success", 'data': data} return JsonResponse(response)
def __init__(self, cookie=None, data_file_name=None, json_file_name=None): self.run_method = RunMethod() self.com_util = CommonUtil() self.send_mail = SendEmail() self.cookie = cookie self.pass_count = [] self.fail_count = [] # json文件 self.json_file_name = 'all_test/json_file/' + json_file_name self.json = OperationJson(self.json_file_name) # case文件 self.data_file_name = 'all_test/test_file/' + data_file_name # result文件 self.result_file_name = 'all_test/test_file/' + data_file_name.split( '.')[0] + '_out.xlsx' # 获得需要提取数据的excel self.data = GetData(file_name=self.data_file_name, sheet_id=0) # 获取需要写入的excel self.result_data = GetData(file_name=self.result_file_name, sheet_id=0)
def set_data(self, main_id, id_input, values): ids = id_input.split(self.data_split) print(ids) common_util = CommonUtil() count = 0 for id in ids: # 假如id是数组类型 如test[].xx if common_util.is_contain("[]", id): # 暂时只支持数组里面只有一位 temp = id.split('[') id = temp[0] + "[" + str(0) + temp[1] else: id = id print(id) print(values) if values is None: self.data[main_id][id] = "" else: self.data[main_id][id] = values[count] count = count + 1 with open(self.file_name, 'w', encoding='utf-8') as fp: fp.write(json.dumps(self.data, ensure_ascii=False, indent=4))
def UpdateVersion(request): req = json.loads(request.body) project_id = req['project_id'] environment_id = req['environment_id'] # 获得远程服务器信息 sd = SystemData() ip = sd.getRemoteIp() # ip username = sd.getRemoteUsername() # 用户名 password = sd.getRemotePassword() # 密码 # 连接远程cmd rm = OperationWINRM(ip, username, password) # 获得svn相关信息 svn_info = getSVNInfo(project_id=project_id, environment_id=environment_id) # 执行更新命令 data = rm.do_cmd("svn update --ignore-externals " + "\"" + svn_info.project_position + "\"" + " --username " + svn_info.svn_username + " --password " + svn_info.svn_password + " --no-auth-cache") cm = CommonUtil() if cm.is_contain("At revision", data): response = {'code': 200, 'message': "success", 'data': data} else: response = {'code': 200, 'message': "fail", 'data': data} return JsonResponse(response)
class RunTest: def __init__(self, cookie=None, data_file_name=None, json_file_name=None): self.run_method = RunMethod() self.com_util = CommonUtil() self.send_mail = SendEmail() self.cookie = cookie self.pass_count = [] self.fail_count = [] # json文件 self.json_file_name = 'all_test/json_file/' + json_file_name self.json = OperationJson(self.json_file_name) # case文件 self.data_file_name = 'all_test/test_file/' + data_file_name # result文件 self.result_file_name = 'all_test/test_file/' + data_file_name.split( '.')[0] + '_out.xlsx' # 获得需要提取数据的excel self.data = GetData(file_name=self.data_file_name, sheet_id=0) # 获取需要写入的excel self.result_data = GetData(file_name=self.result_file_name, sheet_id=0) # 根据行号运行 def run_it_by_row_num(self, row): # 返回的结果 result = {'cookie': ''} case_id = self.data.get_case_id(row) url = self.data.get_request_url(row) method = self.data.get_request_method(row) header = self.data.is_header(row) is_cookie = self.data.is_cookie(row) expect = self.data.get_expect_data(row) # case是否有依赖case dependent_case = self.data.get_depend_case(row) # case是否有依赖数据 dependent_data = self.data.get_depend_key(row) # case得到依赖数据应该填在哪 target_data = self.data.get_depend_field(row) # case得到依赖数据之后应该填在json的哪个大类中 request_data = self.data.get_request_data(row) # case得到的数据是什么类型的 if dependent_case is not None: # 有case依赖,首先运行case依赖的case # 先通过依赖case_id获得相应行号 row_num = self.result_data.opera_excel.get_row_num(dependent_case) # 取得对应行号的是否成功与返回数据 # 判断时,应取最新的excel数据 new_data = GetData(self.result_file_name, sheet_id=0) is_pass = new_data.get_isPass(row_num) response_data = new_data.get_result(row_num) # 获取返回结果的类型 response_type = new_data.get_response_type(row_num) print("pass: "******"HTML": op_html = OperationHtml(case_id + ".html", res.text, is_write=True) self.result_data.write_result(row, op_html.get_root_path()) result['result'] = op_html.get_root_path() else: self.result_data.write_result(row, res.text) result['result'] = res.text if self.com_util.is_contain(expect, res.text): self.result_data.write_isPass(row, "pass") self.pass_count.append(row) result['is_pass'] = '******' else: self.result_data.write_isPass(row, "fail") self.fail_count.append(row) result['is_pass'] = '******' return result # 程序执行的主入口 def go_on_run(self, data_file_name, json_file_name): self.data_file_name = data_file_name self.json_file_name = json_file_name rows_count = self.data.get_case_lines() print(rows_count) res = None print("-------start-------") for i in range(2, rows_count + 1): # case是否要运行 is_run = self.data.get_is_run(i) if is_run: print("----------" + str(i - 1) + "start-----------") self.run_it_by_row_num(i) print("----------" + str(i - 1) + "end-----------") # self.send_mail.send_main(pass_count, fail_count) return res
def monitor(): # 第一步,登录wms rm = RunMethod() cm = CommonUtil() # 登录wms参数 wms_login = { "userName": "******", "password": "******", "pushClientId": "null", "deviceId": "", "deviceType": "app", "phoneType": "EVR-AL00", "loginFrom": "Android", "appVersion": "1.1.6" } # 登录wms url wms_login_url = "http://wms.zmd.com.cn//api/login" # 登录监控平台参数 monitor_login = { "username": "******", "password": "******", "mac": "02:00:00:00:00:00", "loginAddr": "115.238.88.202", "passwordLevel": "2", "captchaKey": "", "captcha": "" } # 登录监控平台url monitor_login_url = "https://115.238.88.202:1443/msp/mobile/login" # 请求头 header = { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", } rm.post_main(url=wms_login_url, data=wms_login, header=header) wms_cookie = rm.getCookies(url=wms_login_url, data=wms_login, header=header) rm.post_main(url=monitor_login_url, data=monitor_login, header=header, cookies=wms_cookie) cookie = rm.getCookies(url=monitor_login_url, data=monitor_login, header=header) # 第二步,取得所有仓库的信息 url = "http://wms.zmd.com.cn//api/camera/warehousePartition/list" data = { } # 请求头 header = { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", } rm = RunMethod() print(url, data, header, wms_cookie) req = rm.post_main(url=url, data=data, header=header, cookies=wms_cookie) warehouses = req.json() warehouse_infos = [] cur = 0 # 第三步,检查仓库下面的监控列表 warehouse_monitor.initStatus() for warehouse in warehouses: # cur = cur + 1 # if cur > 4: # break online_list = [] offline_list = [] warehouse_url = "http://wms.zmd.com.cn//api/camera/userCamera/list" warehouse_data = { 'warehouseId': warehouse['id'], 'partitionId': '' } # 请求头 warehouse_header = { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", } rm = RunMethod() req = rm.post_main(url=warehouse_url, data=warehouse_data, header=warehouse_header, cookies=wms_cookie) # 监控列表 monitors = req.json() for item in monitors: if type(item) == str: print(item) print(item['cameraName'] + ' ' + warehouse['warehouseName'] + ' 异常') offline_list.append(item['cameraName']) break url_8700 = "https://115.238.88.202:1443/msp/mobile/getCameraInfo" data_8700 = { "sessionID": "01D9A743AF3DDF5D7CBD7B36CC2405E8", "cameraID": "", "sysCode": item['cameraUuid'] } # 请求头 header_8700 = { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", } req = rm.post_main(url=url_8700, data=data_8700, header=header_8700, cookies=cookie) if cm.is_contain('<IsOnline>1</IsOnline>', req.text): online_list.append(item['cameraName']) else: offline_list.append(item['cameraName']) is_normal = True if len(offline_list) != 0: is_normal = False warehouse_info = { 'warehouseName': warehouse['warehouseName'], 'online': online_list, 'offline': offline_list, 'is_normal': is_normal, 'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # time.strftime("%H:%M:%S", time.localtime() } warehouse_infos.append(warehouse_info) # 第四步,邮件通知 content = "此次一共检查仓库个数为%s个," % len(warehouse_infos) wrong_count = 0 wrong_content = "" for item in warehouse_infos: # 判断之前是否有过这个仓库 flag = warehouse_monitor.getMessage(item['warehouseName']) is_normal = item['is_normal'] # 取得所有数据 online_count = len(item['online']) offline_count = len(item['offline']) offline_list_str = '' for offline in item['offline']: offline_list_str = offline_list_str + offline + ',' # 如果监控正常 if is_normal: # 且没有这个仓库,则在表中新增 if flag is None: warehouse_monitor.addMessage(warehouse_name=item['warehouseName'], online=len(item['online']), offline=len(item['offline']), offline_list=offline_list_str, check_time=item['time']) # 且有这个仓库,则比较与上次的差别,并且更新表 else: last = warehouse_monitor.getMessage(warehouse_name=item['warehouseName']) # 如果比历史最大在线数小 if last.max_online>online_count: # 记录 wrong_content = wrong_content + "【" + item['warehouseName'] + "】" + "【检测时间: " + item['time'] + "】" \ '当前监控在线数(%s)小于历史最大在线数(%s), 请核查!\r\n'\ % (online_count, last.max_online) wrong_count = wrong_count + 1 warehouse_monitor.addMessage(warehouse_name=item['warehouseName'], online=online_count, offline=offline_count, offline_list=offline_list_str, check_time=item['time']) # 监控不正常 else: wrong_content = wrong_content + "【" + item['warehouseName'] + "】" + "【检测时间: " + item['time'] + "】" \ "有%s个异常监控:\r\n" % len(item['offline']) for wrong in item['offline']: wrong_content = wrong_content + str(wrong) + '\r\n' wrong_count = wrong_count + 1 warehouse_monitor.addMessage(warehouse_name=item['warehouseName'], online=online_count, offline=offline_count, offline_list=offline_list_str, check_time=item['time']) if wrong_count > 0: content = content + "其中有%s个仓库存在异常监控,如下:\r\n" % wrong_count content = content + wrong_content user_list = ['*****@*****.**'] sub = "%s个仓库在线" % len(warehouse_infos) + ", %s个仓库异常" % wrong_count else: sub = "%s个仓库在线" % len(warehouse_infos) user_list = ['*****@*****.**'] content = "监控正常 \r\n" not_check = warehouse_monitor.getNotCheckWarehouseName() not_check_str = '' for item in not_check: not_check_str = not_check_str + '【' + item + '】' warehouse_monitor.updateCheckTime(item) if len(not_check) != 0: sub = sub + ", %s个仓库掉线" % len(not_check) content = content + not_check_str + '未被检查,可能掉线,请核实!' em = SendEmail() em.send_mail(user_list, sub, content)