def virtual_can_not_virtual(): response = BaseResponse() try: ret = {} can_virtual_online = collect_handle.get_can_virtual_online() # 可虚拟化 在线 can_virtual_offline = collect_handle.get_can_virtual_offline( ) # 可虚拟化 下线 can_virtual_scrap = collect_handle.get_can_virtual_scrap() # 可虚拟化 报废 not_virtual_online = collect_handle.get_not_virtual_online( ) # 不可虚拟化 在线 not_virtual_offline = collect_handle.get_not_virtual_offline( ) # 不可虚拟化 在线 not_virtual_scrap = collect_handle.get_not_virtual_scrap() # 不可虚拟化 报废 all_can_virtual = can_virtual_online + can_virtual_offline + can_virtual_scrap all_not_virtual = not_virtual_online + not_virtual_offline + not_virtual_scrap ret['can_virtual_online'] = can_virtual_online ret['can_virtual_offline'] = can_virtual_offline ret['can_virtual_scrap'] = can_virtual_scrap ret['not_virtual_online'] = not_virtual_online ret['not_virtual_offline'] = not_virtual_offline ret['not_virtual_scrap'] = not_virtual_scrap ret['all_can_virtual'] = all_can_virtual ret['all_not_virtual'] = all_not_virtual response.data = ret response.status = True except Exception, e: response.message = e.message
def sql_data(): response = BaseResponse() try: ret = {} all_app_type_count = collect_handle.get_all_apply_type() # 所有应用类型的个数 all_sql_count = collect_handle.get_all_sql() # 所有数据库个数 all_app_virtual = collect_handle.get_all_app_virtual() # 应用宿主机 all_app_no_virtual = collect_handle.get_all_app_no_virtual() # 应用非宿主机 all_device_can_not_use = collect_handle.get_no_user_device_status( ) # 获取下线报废的个数 all_count = all_sql_count + all_app_virtual + all_app_no_virtual + all_device_can_not_use sql_percent = data_percent(all_sql_count, all_app_type_count - all_device_can_not_use) app_percent = data_percent(all_app_virtual, all_count - all_device_can_not_use) # ret['all_app_type_count'] = all_app_type_count # ret['all_app_count'] = all_app_count ret['all_sql_count'] = all_sql_count ret['all_app_virtual'] = all_app_virtual ret['all_app_no_virtual'] = all_app_no_virtual ret['all_device_can_not_use'] = all_device_can_not_use ret['all_count'] = all_count ret['sql_percent'] = sql_percent ret['app_percent'] = app_percent response.data = ret response.status = True except Exception, e: response.message = e.message
def check_valid(**kwargs): response = BaseResponse() try: result = user_models.AdminInfo.objects.get(**kwargs) response.status = True response.data = result except Exception, e: response.message = str(e)
def get_all_rule_list(): response = BaseResponse() try: ret = iptables_handle.get_all_rules() response.status = 1 response.data = list(ret) except Exception, e: response.message = str(e)
def delete_date(data): response = BaseResponse() try: id_list = json.loads(data) for i in id_list: i = int(i) search_handle.delete_data_by_id(i) response.status = True response.data = u"删除成功" except Exception, e: response.message = str(e)
def get_pass_log_count(): ''' 获取iptables日志总条数 ''' response = BaseResponse() try: result = log_handle.get_pass_log_count() response.data = result response.status = True except Exception, e: response.message = str(e)
def get_all_pro_type(): response = BaseResponse() try: values = [ 'id', 'name', ] result = search_handle.get_pro_type_list(values) # 返回的是一个类似列表 result = list(result) # 转换为list类型 response.data = result # 封装到对象中 response.status = True except Exception, e: response.message = str(e)
def pass_log_data_list(page_start, page_stop): ''' 获取指定条数的日志记录 ''' response = BaseResponse() try: values = ['id', 'import_type', 'user_name', 'ip_list', 're_state', 'add_time'] # 在这里定义好查询表时 需要筛选的字段 result = log_handle.get_pass_log_list(page_start, page_stop, values) # 搜索工单信息 result = list(result) response.data = result # 封装到对象中 response.status = True except Exception, e: response.message = str(e)
def get_log_count(): ''' 获取iptables日志总条数 :return: ''' response = BaseResponse() try: result = iptables_handle.get_log_lists_count( ) # j将搜索条件Q 传入其中 取出搜索到的数据的条数 response.data = result response.status = True except Exception, e: response.message = str(e)
def get_host_rules(data): ''' 获取主机下的所有iptables规则 :param data: :return: ''' response = BaseResponse() ip_list = data.get('ip_list', None) # 输入的IP地址 envir = data.get('envir', None) # 环境 value_list = [ip_list, envir] for i in value_list: if i is None: response.message = u'请检查提交的数据' return response ipv4_re = re.compile( r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$' ) error_ip = [] host_ip = ip_list.strip().split(",") for ip in host_ip: if not ipv4_re.match(ip): error_ip.append(ip) if len(error_ip) > 0: response.host_ip = error_ip response.ip_status = False response.message = u"IP地址出现错误!!!" return response set_host = list(set(host_ip)) # 将列表去重 pwd_check = pwd_query.main(set_host, [], envir) # 调用密码API 获取密码的列表 ***** if len(pwd_check) == 0: response.message = u"密码检测失败!!请检查所选环境和密码是否存在~" response.pwd_status = False return response re_list = [] # 存放每台IP的执行结果 for host_info in pwd_check: ip = host_info['ip'] pwd = host_info['pwd'] iptables_obj = execute_iptables.RunIptables([], ip, pwd) # 连接服务器对象 exe_re = iptables_obj.get_all_rules() # 获取规则的方法 re_list.append(exe_re) response.data = re_list response.status = True return response
def delete_upload_file(filename, file_dir): ''' 删除用户所选的文件 ''' response = BaseResponse() try: file_path = file_dir + filename if os.path.isfile(file_path): os.remove(file_path) response.status = True return response else: response.message = u'删除失败' return response except Exception, e: response.message = str(e) return response
def make_delete_rule(host_ips, rule_ips): ''' 制作简单规则 ''' cmd_list = [] easy_rule_cmd = "iptables -D INPUT -s %s -j ACCEPT" save_cmd = "service iptables save" response = BaseResponse() try: for rule_ip in rule_ips: cmd_list.append(easy_rule_cmd % rule_ip) cmd_list.append(save_cmd) response.status = True response.data = cmd_list except Exception, e: response.message = str(e)
def delete_data_excel(data): ''' 删除excel中所选的行数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: delete_list = json.loads(data.get('delete_list')) current_file = data.get("current_file") s = read_excel_data.delete_excel_data(BUDGET_FILE_DIR + current_file, delete_list) response.data = "OKOKOKOKKK" response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def fix_rule_type(rule_data_list, rule_type, user): ''' 处理iptables字典数据 生成规则 ''' response = BaseResponse() try: if rule_type == 'easy': # 如果规则是简单快速规则 easy_result = easy_rules(rule_data_list, user) if not easy_result.status: raise Exception(easy_result.message) response.other = easy_result.other response.data = easy_result.data elif rule_type.strip() == 'normal': normal_result = NormalRule.process(rule_data_list, user) if not normal_result.status: raise Exception(normal_result.message) response.data = normal_result.data else: raise Exception("rule type is Error.") response.status = True except Exception, e: response.message = e.message
def single_ping(ip_data): response = BaseResponse() try: ip_list = json.loads(ip_data) # 先将输入的空ip去除 for i, ip in enumerate(ip_list): if len(ip) == 0: ip_list.pop(i) # 检测ip输入不匹配 error_ip = [] for ip in ip_list: if not ipv4_re.match(ip): error_ip.append(ip) if len(error_ip) > 0: # 说明IP地址有输入错误的 返回错误 response.status = False response.message = u"IP地址输入错误 %s" % ",".join(error_ip) return response # ip地址输入正确,开始处理ping re_list = ping_host_ip.run_ping_by_ip(ip_list) response.data = re_list response.status = True return response except Exception, e: response.message = str(e) return response
def brand_data(): response = BaseResponse() try: ret = [] all_deivce_list = [u"浪潮", u"华为", u"DELL", u"HP", u"曙光", u"IBM"] for name in all_deivce_list: brand_dict = {} online, offline, scrap = collect_handle.get_brand_status(name) brand_dict['name'] = name brand_dict['online'] = online brand_dict['offline'] = offline brand_dict['scrap'] = scrap brand_dict['all_count'] = online + offline + scrap ret.append(brand_dict) response.data = ret response.status = True except Exception, e: response.message = e.message
def idc_info_data(): response = BaseResponse() try: ret = [] all_idc_list = [u"德信", u"M6", u"鹏博士", u"M5", u"大簇"] for name in all_idc_list: idc_dict = {} online, offline, scrap = collect_handle.get_idc_device_status(name) idc_dict['name'] = name idc_dict['online'] = online idc_dict['offline'] = offline idc_dict['scrap'] = scrap idc_dict['all_count'] = online + offline + scrap ret.append(idc_dict) response.data = ret response.status = True except Exception, e: response.message = e.message
def get_log_lists(page_start, page_stop): ''' 获取指定条数的日志记录 ''' response = BaseResponse() try: values = [ 'id', 'log_type__type_name', 'login_user', 'user_num', 'msg', 'add_time' ] # 在这里定义好查询表时 需要筛选的字段 result = iptables_handle.get_log_list(page_start, page_stop, values) # 搜索工单信息 result = list(result) response.data = result # 封装到对象中 response.status = True except Exception, e: response.message = str(e)
def delete_ping_file(file_dir): ''' 删除所有的上传ping文件 ''' response = BaseResponse() try: all_file_list = os.listdir(file_dir) print ">>>>>>>>>>>>>>>>>>>>", all_file_list for file_name in all_file_list: print "<<<<<<<<<<<<<<<<<<<<<", file_dir+file_name os.remove(file_dir+file_name) # all_file_list2 = os.listdir(file_dir) response.status = True return response except Exception, e: response.message = str(e) return response
def check_user_info(post_data): ''' 检查用户的信息 ''' response = BaseResponse() try: user = post_data['user'] ip = post_data['ip'] user_key = post_data['p_key'] user_dict = {'user': user, 'ip': ip, 'p_key': user_key} ret = user_models.IptablesAuth.objects.filter(**user_dict).first() if not ret: raise Exception('Not found this user info, Authentication failed.') response.status = True except Exception, e: response.message = e.message
def save_change_data(data): ''' 保存表格变更的数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: current_file = data.get('current_file') change_dict = json.loads(data.get('update_data')) print "current_file", current_file s = read_excel_data.save_change_excel(BUDGET_FILE_DIR + current_file, change_dict) response.data = "修改成功!!!" response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def add_new_excel_row(data): ''' 新增一行表格数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: add_raw = data.get("action") current_file = data.get("current_file") print "dddddddddddddddddd2222222222222", add_raw, current_file s = read_excel_data.add_new_raw(BUDGET_FILE_DIR + current_file) response.data = "增加行成功!" response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def read_data_from_excel(file_name): ''' 从excel表格中读取数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: all_file_list = os.listdir(BUDGET_FILE_DIR) for i in all_file_list: print "--------------", i print "==============", file_name data_list = read_excel_data.read_excel_data(BUDGET_FILE_DIR + file_name) response.data = data_list response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def make_iptables_script(cmd_list, user): ''' 生成执行iptables命令的脚本 ''' response = BaseResponse() script_dir = settings.IPTABLES_SCRIPTS try: now_time = str(datetime.datetime.now()) w = now_time.split(".")[0].replace(":", "_").split(" ") w.append(user+".sh") script_path = script_dir + "_".join(w) with open(script_path, 'w') as file_obj: for i in cmd_list: file_obj.write(i+"\n") response.status = True response.data = script_path except Exception, e: response.message = e.message
def add_new_excel_col(data): ''' 新增一列表格数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: add_col_list = json.loads(data.get("new_vol")) current_file = data.get("current_file") if add_col_list: read_excel_data.add_new_col(BUDGET_FILE_DIR + current_file, add_col_list) else: print "no________add_____col____data" read_excel_data.add_new_col_end(BUDGET_FILE_DIR + current_file) response.data = "增加列成功!" response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def get_ping_file(file_dir): ''' 获取所有的上传ping文件 ''' response = BaseResponse() try: file_list = [] all_file_list = os.listdir(file_dir) for file_name in all_file_list: if file_name.endswith('xlsx'): file_list.append(file_name) # 将正确格式的文件筛选出 print "file_______________list", file_list response.data = file_list response.status = True return response except Exception, e: response.message = str(e) return response
def update_date(data): response = BaseResponse() rows = json.loads(data) error_count = 0 error_message = [] for item in rows: try: search_handle.update_date_by_id(item) except Exception, e: error_count += 1 error_message.append({'message': str(e)})
def upload_budget_file(file_obj, file_dir): ''' 上传表格文件 ''' response = BaseResponse() try: # 保存上传的脚本 filename = file_obj.name file_path = file_dir + filename print "upload_file_path", file_path f = open(file_path, 'wb') for line in file_obj.chunks(): # 保存文件 f.write(line) f.close() response.status = True response.message = u'上传成功' return response except AttributeError: response.message = u'选择上传文件' return response except Exception, e: print "up____error", str(e) response.message = str(e) return response
def get_upload_files(file_dir): ''' 获取ansible_tmp下所有的文件 ''' Kilobyte = 1000 Megabyte = float(1000 * Kilobyte) Gigabyte = float(1000 * Megabyte) response = BaseResponse() try: file_list = [] all_file_list = os.listdir(file_dir) for file_name in all_file_list: t_dict = {} # dt = datetime.datetime.strptime(pageTime, "%Y-%m-%d %H:%M:%S") file_path = os.path.join(file_dir, file_name.decode('utf8')) file_obj = os.stat(file_path) str_t = file_obj.st_ctime f_time = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(str_t)) # 文件修改时间 change_time = datetime.datetime.strptime(f_time, "%Y-%m-%d %H:%M:%S") d = change_time + datetime.timedelta(hours=8) nTime = d.strftime("%Y-%m-%d %H:%M:%S") f_size = float(file_obj.st_size) if f_size < 1000000: f_size = str(f_size / Kilobyte) + 'KB' else: f_size = str(f_size / Megabyte) + 'MB' t_dict['filename'] = file_name.decode('utf8') t_dict['filesize'] = f_size t_dict['filetime'] = nTime file_list.append(t_dict) response.data = file_list response.status = True return response except Exception, e: response.message = str(e) return response
def run_iptables_cmd(host_ip, rule_cmd_list): ''' 查询主机列表的用户名和密码 调用ansible API执行iptables的命令 ''' response = BaseResponse() try: pwd_info_list = pwd_query_no_envir.main(host_ip, []) # 查询host 密码 用户名 对结果要进行对比分析 no_found_ip_list = check_query_reuslt(host_ip, pwd_info_list) host_file_status = create_iptables_host_file(pwd_info_list) # 创建ansible host文件 if not host_file_status: raise Exception("ansible iptables host file create failed") if isinstance(rule_cmd_list, list): rule_cmd = make_iptables_cmd(rule_cmd_list) else: rule_cmd = rule_cmd_list ansible_obj = Ansible_API(host_file_status.data) ansible_obj.host = 'iptables' run_cmd_result = ansible_obj.shell_run(rule_cmd) for k, e in run_cmd_result.items(): print k, "--------------------", e if len(no_found_ip_list) != 0: response.other = "host %s is not found password" % " ".join(no_found_ip_list) response.status = True response.data = run_cmd_result except Exception, e: print "run_iptables_cmd Error: ", e response.message = str(e)