def result(channel): #[{ # "send_time":"2020-07-03 12:53:10", # "report_time":"2020-07-03 12:53:16", # "success":true, # "err_msg":"用户接收成功", # "err_code":"DELIVERED", # "phone_number":"13031046676", # "sms_size":"1", # "biz_id":"706715193751989145^0" # }] if channel == Ali.channel_name: data = json.loads(request.data.decode()) for i in data: myredis.mpush( json.dumps({ 'to': i['phone_number'], 'status': 'SUCCESS' if i['err_code'] == Ali.success_code else i['err_code'], 'message': i['err_msg'], 'message_id': i['biz_id'], 'channel_type': Ali.channel_name }), RedisSql.result_queue_name) resp = json.dumps({"code": 0, "msg": "成功"}) else: resp = '<title>404 Not Found</title><h1>Not Found</h1>' return resp
def send_forever(): while True: if myredis.mlen(RedisSql.sending): data = get_one() try: print(f'发送短信 ## [{data["yzm"]}] ==> [{data["to"]}]', 0, '发送短信') status, message, message_id, channel_type = dispatcher(**data) # status, message, message_id, channel_type = True, 'aaa', 'aaa', 'wy' print(f'短信商返回值 ## 信息:[{message}] | 状态:[{status}]', 0, '发送短信返回值') myredis.mpush( json.dumps({ 'to': data['to'], 'yzm': data['yzm'], 'status': status, 'channel_type': channel_type, 'message': message, 'message_id': message_id }), RedisSql.pending) except Exception as e: myredis.mpush( json.dumps({ 'to': data['to'], 'yzm': data['yzm'], 'status': False, 'channel_type': 'al', 'message': f'错误{e}', 'message_id': -11 }), RedisSql.pending) print(f'发送短信失败, 失败原因[{e}]', 2, '发送短信错误')
def queue_result_forever(): while True: num = myredis.mlen(RedisSql.result_queue_name) if num > 0: for n in range(num): try: data = json.loads(myredis.mpop(RedisSql.result_queue_name)) print(f'处理短信数据:{data}', 0, '处理短信结果') channel_type = data.pop('channel_type') with channel.ChannelDB() as db: channel_id, need_report = db.get_channel_info( channel_type=channel_type) if data['status']: resp = handle_success(data['to'], data['message_id']) else: resp = handle_failed(data['to'], data['yzm'], channel_id, data['message'], data['message_id'], is_update=True) if not resp: if data.get('count', 0) < 3: data['count'] = data.get('count', 0) + 1 myredis.mpush(json.dumps(data), RedisSql.result_queue_name) else: print(f'更新短信结果失败, 当前数据[{data}]', 2, '更新短信结果失败') except Exception as e: print(f'处理短信结果失败, 失败原因[{e}]', 2, '处理短信结果错误') time.sleep(60)
def send(): data = request.data.decode() if data: data = json.loads(data) to, yzm, signature = data['to'], data['yzm'], data['signature'] if api.verify_(signature, f'to{to}yzm{yzm}key{Sign.key}'): myredis.mpush(json.dumps({'to': to, 'yzm': yzm}), RedisSql.sending) return api.handle_httpresponse('ok', 0) return api.handle_httpresponse('验签失败!') return api.handle_httpresponse('参数错误!')
def connection_machine(): # 连接服务器 while True: try: if mlen(RedisSql.cm_queue_name) > 0: machine_id = mpop(RedisSql.cm_queue_name) with Machine() as db: data = db.get_machine_by_id(machine_id) for i in range(3): try: ssh = SSHConnection(data['ip'], data['usr'], data['pwd'], data['ssh_port']) ssh.connect() result1 = ssh.cmd(Remote.remote_configuration_comm) print( f'远程服务器[{data["ip"]}执行初始化命令成功, 执行结果[{result1}]', 0, '启动远程服务') ssh.upload(Remote.local_py_path, Remote.remote_py_path) ssh.upload(Remote.local_service_path, Remote.remote_service_path) print(f'拷贝文件到远程服务器[{data["ip"]}]成功', 0, '启动远程服务') result2 = ssh.cmd( Remote.remote_change_port_comm.format( port=data['http_port'])) result3 = ssh.cmd(Remote.remote_reload_comm) result4 = ssh.cmd(Remote.remote_start_comm) result5 = ssh.cmd(Remote.remote_enable_comm) print( f'远程服务器[{data["ip"]}执行启动命令成功, 执行结果[{result5}]', 0, '启动远程服务') ssh.close() db.change_status_by_id(machine_id, '连接正常', 1) with UnhandledAlert() as db1: alert_id = db1.is_have_alert( machine_id, Alert.alert_type[0]) if alert_id != -1: db.update_alert(alert_id, '已处理') break except Exception as e: if i == 2: mpush(machine_id, RedisSql.cm_queue_name) db.change_status_by_id(machine_id, '连接失败', 0) with UnhandledAlert() as db1: db1.create_alert(Alert.alert_type[0], '连接失败, 请检查远程主机的网络连通性!', machine_id) print(f'第{i+1}次连接远程服务器错误, 错误原因[{e}]', 2, '连接主机错误') time.sleep(60) time.sleep(60 * 5) else: time.sleep(60 * 10) except Exception as e: # traceback.print_exc() print(f'连接服务器进程错误, 错误原因[{e}]', 2, '连接主机错误')
def send_and_recv(ip, http_port, _type): try: data = my_requests(Remote.remote_http.format(ip=ip, port=http_port), 'post', params={'type': _type}, need_json_params=True) if data is not None and data['status']: mpush( json.dumps({ 'data': data["data"], 'http_port': http_port, 'ip': ip, 'type': _type, 'status': True, 'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }), RedisSql.machine_info_queue_name) else: mpush( json.dumps({ 'ip': ip, 'type': _type, 'http_port': http_port, 'status': False, 'data': f'获取远程主机数据失败', 'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }), RedisSql.machine_info_queue_name) print(f'获取[{ip}:{_type}]信息失败', 2, f'{_type}错误') except Exception as e: mpush( json.dumps({ 'ip': ip, 'type': _type, 'http_port': http_port, 'status': False, 'data': f'获取远程主机[{ip}]的{_type}信息失败[{e}]', 'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }), RedisSql.machine_info_queue_name) print(f'获取[{ip}:{_type}]信息失败, 失败原因[{e}]', 2, f'{_type}错误')
def machine_action(): action_type = request.form['action_type'] with Machine() as db: if action_type == 'get_machines': start, end = request.form.get('start', None), request.form.get('end', None) page, machine = request.form.get('page', 0), request.form.get( 'machine', None) page = int(page) - 1 if page not in (None, 'none', '', 'None', 'null', 0, '0') else 0 page_list, total = db.get_page(start, end, machine, Page.machines_limit) ua = db.get_machines(page, machine, start, end) data, code = { 'value_list': ua, 'page_list': page_list, 'total': total }, 0 elif action_type == 'get_simple_machines': with IntervalDB() as db1: data = db1.get_interval() data['machine_list'], code = db.get_machines(simple=True), 0 elif action_type == 'get_alert_type': data, code = [Alert.alert_type[i] for i in Alert.alert_type], 0 elif action_type == 'get_net': machine_id = request.form['machine_id'] data, code = db.get_net_by_id(machine_id), 0 elif action_type == 'change_machine': machine_id, http_port = request.form['machine_id'], request.form[ 'http_port'] mem_alarm, cpu_alarm = request.form['mem_alarm'], request.form[ 'cpu_alarm'] name, description, is_active = request.form['name'], request.form[ 'description'], request.form['is_active'] data = db.change_machine_by_id(api.get_uid(), machine_id, name, description, mem_alarm, cpu_alarm, http_port, is_active) code = 0 if data == '修改成功!' else -1 elif action_type == 'add_machine': name, description = request.form.get('name', ''), request.form.get( 'description', '') mem_alarm, cpu_alarm = request.form['mem_alarm'], request.form[ 'cpu_alarm'] need_auto_connection, ip = request.form[ 'need_auto_connection'], request.form['ip'] http_port = request.form['http_port'] if need_auto_connection == '1': # 需要自动发现服务器 user, pwd, ssh_port = request.form[ 'machine_user'], request.form['machine_pwd'], request.form[ 'ssh_port'] else: # 不需要自动发现 user, pwd, ssh_port = '', '', 22 data, machine_id = db.create_machine(api.get_uid(), name, ip, description, mem_alarm, cpu_alarm, user, pwd, ssh_port, http_port, need_auto_connection) if need_auto_connection == '1' and machine_id != 0: mpush(machine_id, RedisSql.cm_queue_name) code = 0 if data == '创建成功!' else -1 elif action_type == 'change_status': machine_id, is_active = request.form['machine_id'], request.form[ 'is_active'] data = db.change_status(api.get_uid(), machine_id, is_active) code = 0 if data == '修改成功!' else -1 elif action_type == 'change_interval': interval, interval_type = request.form['time'], request.form[ 'type'] with IntervalDB() as db1: data = db1.change_interval(interval_type, interval) code = 0 if data == '修改成功!' else -1 else: data, code = 'ERROR', -1 return api.handle_httpresponse(data, code)