def refreshClusterStatus(line): if line: arrayLines = line.split(" ") appClusterStatus = arrayLines[len(arrayLines) - 2] appOrClusterName = arrayLines[len(arrayLines) - 1] #appClusterStatus 为1 为集群 appClusterId为集群id #appClusterStatus 为0 为集群 appClusterId为应用id if "Falied" in line: if appClusterStatus == 1: cmdbAppCluster = CmdbAppCluster.objects.get( name=appOrClusterName) cmdbAppCluster.cluster_status = "3" cmdbAppCluster.save() # 否则是0,为应用 else: cmdbApp = CmdbApp.objects.get(app_name=appOrClusterName) cmdbApp.app_status = "3" cmdbApp.save() else: if appClusterStatus == 1: cmdbAppCluster = CmdbAppCluster.objects.get( name=appOrClusterName) cmdbAppCluster.cluster_status = get_tuple_key( CLUSTER_STATUS, arrayLines[3]) cmdbAppCluster.save() # 否则是0,为应用 else: cmdbApp = CmdbApp.objects.get(app_name=appOrClusterName) cmdbApp.app_status = get_tuple_key(APP_STATUS, arrayLines[2]) cmdbApp.save() else: print("此行数据为空!")
def excel_opr_cluster(table,nrows): for i in range(1, nrows): # i/o row_value = table.row_values(i) # 一行的数据 # 添加多对多字段 # 主要这里不能使用get,否则报错 bgTaskManagement_obj = BgTaskManagement.objects.get(bg_module=row_value[1], bg_domain=str(row_value[2])) # 生成主机对象,添加 cmdbApp_obj = CmdbAppCluster.objects.create( name=row_value[0], bgTaskManagement=bgTaskManagement_obj, cluster_status=get_tuple_key(CLUSTER_STATUS, row_value[3]) )
def excel_opr_app(table,nrows): for i in range(1, nrows): # i/o row_value = table.row_values(i) # 一行的数据 # 添加多对多字段 # 主要这里不能使用get,否则报错 cmdb_host_obj=CmdbHost.objects.get(cmdb_host_name=row_value[1]) appNetmode_obj = CmdbAppNetmode.objects.get(net_mode=row_value[2]) cmdbAppCluster_obj=CmdbAppCluster.objects.get(name=row_value[3]) # 生成主机对象,添加 cmdbHost_obj = CmdbApp.objects.create( app_name=row_value[0], cmdb_host=cmdb_host_obj, appNetmode=appNetmode_obj, cmdbAppCluster=cmdbAppCluster_obj, app_insert_time=datetime.datetime.now(), app_status=get_tuple_key(APP_STATUS, row_value[4]) )
def excel_import_host(file_obj): type_excel = file_obj.name.split('.')[1] if type_excel in ['xlsx', 'xls']: # 开始解析上传的excel表格 wb = xlrd.open_workbook(filename=None, file_contents=file_obj.read()) table = wb.sheets()[0] nrows = table.nrows # 行数 # ncole = table.ncols # 列数 try: # 正常的数据库操作应该是原子性操作 with transaction.atomic(): for i in range(1, nrows): # i/o row_value = table.row_values(i) # 一行的数据 # 添加多对多字段 # 主要这里不能使用get,否则报错 bg_module = row_value[5] bg_domain = str(row_value[6]) bgTaskManagement = BgTaskManagement.objects.filter(bg_module=bg_module,bg_domain=bg_domain) bgTaskManagement_obj = bgTaskManagement[0] # 生成主机对象,添加 cmdbHost_obj = CmdbHost.objects.create( cmdb_host_name=row_value[0], cmdb_host_type=row_value[1], cmdb_host_pod=row_value[2], cmdb_host_busip=row_value[3], cmdb_host_manip=row_value[4], bg=bgTaskManagement_obj, cmdb_host_system=row_value[7], cmdb_host_cpu=row_value[8], cmdb_host_RAM=row_value[9], cmdb_host_local_disc = row_value[10], cmdb_host_outlay_disc = row_value[11], cmdb_host_insert_time=datetime.datetime.now(), cmdb_host_status = get_tuple_key(HOST_STATUS,row_value[13]) ) except Exception as e: return '出现错误...%s' % e print(e) return "上传成功" else: return '上传文件格式不是xlsx或xls'