def __create_server_info(self, ignore_errs=False): try: self.__verify_field(self.clean_data, 'model', str) # no processing when there's no error happend if not len(self.response['error']) or ignore_errs == True: data_set = { 'asset_id': self.asset_obj.id, 'hostname': self.clean_data.get('hostname'), 'model': self.clean_data.get('model'), 'Memory': self.clean_data.get('Memory'), 'DeviceSize': self.clean_data.get('DeviceSize'), 'CpuUsage': self.clean_data.get('CpuUsage'), 'MemUsage': self.clean_data.get('MemUsage'), 'DiskUsage': self.clean_data.get('DiskUsage'), 'LoadInfo': self.clean_data.get('LoadInfo'), 'configuration': self.clean_data.get('configuration'), 'os_type': self.clean_data.get('os_type'), 'os_release': self.clean_data.get('os_release'), 'ipaddress': self.clean_data.get('ipaddress'), } obj = CMDB_MODELS.Server(**data_set) obj.save() self.obj = obj return obj except Exception as e: print('error', 'ObjectCreationException', 'Object [server] %s' % str(e)) self.response_msg('error', 'ObjectCreationException', 'Object [server] %s' % str(e))
def asset_data_create(request): response = BaseResponse() try: asset_data = QueryDict(request.body, encoding='utf-8') new_asset_num = asset_num.asset_num_builder() asset_sn = asset_data.get('sn') try: Memory = int(asset_data.get('Memory')) except: Memory = None try: DeviceSize = int(asset_data.get('DeviceSize')) except: DeviceSize = None try: cpu_count = int(asset_data.get('cpu_count')) except: cpu_count = None if not asset_sn: asset_sn = new_asset_num # 创建asset obj asset_obj = models.Asset( device_type_id=asset_data.get('device_type_id'), asset_num=new_asset_num, sn=asset_sn, idc_id=asset_data.get('idc_id'), business_unit_id=asset_data.get('business_unit_id'), ) asset_obj.save() # 创建server obj server_obj = models.Server( asset_id=asset_obj.id, hostname=asset_data.get('hostname'), ipaddress=asset_data.get('ipaddress'), manage_ip=asset_data.get('manage_ip'), Memory=Memory, DeviceSize=DeviceSize, cpu_count=cpu_count, ) server_obj.save() response.message = '获取成功' except Exception as e: print(Exception, e) response.status = False response.message = str(e) return response
def _create_server_info(self, ignore_errs=False): try: if not len(self.response['error']) or ignore_errs: data_set = { 'asset_id': self.asset_obj.id, 'raid_type': self.clean_data.get('raid_type'), # 'model':self.clean_data.get('model'), 'os_type': self.clean_data.get('os_type'), 'os_distribution': self.clean_data.get('os_distribution'), 'os_release': self.clean_data.get('os_release'), } obj = models.Server(**data_set) obj.save() return obj except Exception as e: self.response_msg('error', 'ObjectCreateException', str(e))
def asset_data_create(request): response = BaseResponse() try: asset_data = QueryDict(request.body, encoding='utf-8') new_asset_num = asset_num.asset_num_builder() asset_sn = asset_data.get('sn') Memory = int(asset_data.get('Memory')) DeviceSize = int(asset_data.get('DeviceSize')) cpu_count = int(asset_data.get('cpu_count')) if not asset_sn: asset_sn = new_asset_num if not models.Server.objects.filter( ipaddress=asset_data.get('ipaddress')): # 创建asset obj asset_obj = models.Asset( device_type_id=asset_data.get('device_type_id'), asset_num=new_asset_num, sn=asset_sn, idc_id=asset_data.get('idc_id'), business_unit_id=asset_data.get('business_unit_id'), manage_ip=asset_data.get('manage_ip'), creator_id=request.user.id, memo=asset_data.get('memo')) asset_obj.save() asset_obj.tag.add(asset_data.get('tag_id')) # 创建server obj server_obj = models.Server( asset_id=asset_obj.id, hostname=asset_data.get('hostname'), ipaddress=asset_data.get('ipaddress'), Memory=Memory, DeviceSize=DeviceSize, cpu_count=cpu_count, configuration='( %sC /%sG /%sG )' % (cpu_count, Memory, DeviceSize)) server_obj.save() else: response.status = False response.message = 'Ipaddress is already in system, Please check.' except Exception as e: print(Exception, e) response.status = False response.message = str(e) return response
def _create_server_info(self): server_obj = models.Server( asset=self.asset_obj, created_by=self.created_by, kernel_release=self.asset_data.get("essential_information").get( "kernel_release"), os_type=self.asset_data.get("essential_information").get( "os_type"), os_distribution=self.asset_data.get("essential_information").get( "os_distribution"), model=self.asset_data.get("essential_information").get("model"), os_release=self.asset_data.get("essential_information").get( "os_release"), ) logmsg = "创建服务器%s" % self.asset_obj.name self.log_handler(0, detail=logmsg) server_obj.save()
def __create_server_info(self, ignore_errs=False): try: self.__verify_field(self.data, 'model', str) if not len(self.response["error"]) or ignore_errs == True: data_set = { 'asset_id': self.asset_obj.id, 'os_type': self.data.get('os_type'), 'os_distribution': self.data.get('os_distribution'), 'os_release': self.data.get('os_release'), } obj = models.Server(**data_set) obj.asset.model = self.data.get('model') obj.save() return obj except Exception as e: self.response_msg("error", "ObjectCreationException", "Object [server] %s" % str(e))
def __create_server_info(self,ignore_errs=False): try: self.__verify_field(self.clean_data,'model',str) if not len(self.response['error']) or ignore_errs == True: #no processing when there's no error happend data_set = { 'asset_id' : self.asset_obj.id, 'raid_type': self.clean_data.get('raid_type'), 'model':self.clean_data.get('model'), 'os_type':self.clean_data.get('os_type'), 'os_distribution':self.clean_data.get('os_distribution'), 'os_release':self.clean_data.get('os_release'), } obj = models.Server(**data_set) obj.save() return obj except Exception as e: self.response_msg('error','ObjectCreationException','Object [server] %s' % str(e) )
def __handle_update(self): file_data = xlrd.open_workbook('%s/%s' % (FILE_PATH, self.file_name)) table = file_data.sheets()[0] table_dic = {} # 临时IP列表,用于存放更新失败的IP地址 false_ip_list = [] count = 0 for row in range(0, table.nrows): for col in range(0, table.ncols): if row == 0: continue else: # 更新数据 table_dic[self.col_name[col]] = table.row_values(row)[col] if table_dic: try: get_update_object = cmdb_models.Asset.objects.get( asset_num=table_dic['资产编号']) except: get_update_object = None if get_update_object: get_update_object.idc = cmdb_models.IDC.objects.get( name=table_dic['机房']) get_update_object.nic_set.all().update( ipaddress=table_dic['IP']) get_update_object.name = table_dic['主机名称'] get_update_object.server.sn = table_dic['SN'] get_update_object.asset_type = table_dic['资产类型'] if table_dic['厂商']: get_update_object.manufactory = cmdb_models.Manufactory.objects.get( manufactory=table_dic['厂商']) get_update_object.server.model = table_dic['型号'] get_update_object.server.configuration = table_dic['配置信息'] get_update_object.admin = table_dic['运维人'] get_update_object.user = table_dic['使用人'] get_update_object.dept = table_dic['业务部门'] get_update_object.memo = table_dic['备注'] get_update_object.server.save() get_update_object.save() for i in get_update_object.nic_set.all(): print(i.ipaddress) else: get_asset_num = asset_num.asset_num_builder() try: new_asset_data = cmdb_models.Asset( name=table_dic['主机名称'], asset_num=get_asset_num, idc=cmdb_models.IDC.objects.get( name=table_dic['机房']), admin=table_dic['运维人'], user=table_dic['使用人'], dept=table_dic['业务部门'], memo=table_dic['备注'], ) if table_dic['厂商']: new_asset_data.manufactory = cmdb_models.Manufactory.objects.get( manufactory=table_dic['厂商']) new_asset_data.save() new_server_data = cmdb_models.Server( asset=new_asset_data, created_by='excel', sn=table_dic['SN'], ) new_server_data.save() new_nic_data = cmdb_models.NIC( asset=new_asset_data, ipaddress=table_dic['IP'], ) new_nic_data.save() except Exception as e: print(Exception, e) pass return 1
def update_apply_order(request, order_id): response = BaseResponse() update_data = QueryDict(request.body, encoding='utf-8') order_id = update_data.get('order_id') try: apply_order_data = models.ServerApplyOrder.objects.filter( id=order_id) if apply_order_data: apply_order_obj = apply_order_data[0] for asset_obj in apply_order_obj.project.all(): try: cmdb_asset_obj = models.Asset( device_type_id=3, asset_num=asset_num.asset_num_builder(), sn=asset_num.asset_num_builder(), idc=models.IDC.objects.get(name=asset_obj.idc), business_unit=models.BusinessUnit.objects.get( id=apply_order_obj.business), memo=asset_obj.memo, creator=user_center_models.UserProfile.objects.get( username=request.user.username)) cmdb_asset_obj.save() cmdb_asset_obj.tag.add( models.Tag.objects.get(name=asset_obj.function)) # 创建资产日志 log_handler(asset_id=cmdb_asset_obj.id, event_type=1, detail='Server Created, apply user is %s' % apply_order_obj.user_apply, user=request.user) except Exception as e: print(e) cmdb_asset_obj.delete() asset_obj.approved = 4 asset_obj.save() continue try: cmdb_server_obj = models.Server( asset=cmdb_asset_obj, ipaddress=asset_obj.ipaddress, configuration='( %sC /%sG /%sG )' % (asset_obj.cpu, asset_obj.mem, asset_obj.disk), cpu_count=asset_obj.cpu, Memory=asset_obj.mem, DeviceSize=asset_obj.disk, os_type=asset_obj.sys_type, ) cmdb_server_obj.save() except Exception as e: print(e) cmdb_asset_obj.delete() asset_obj.approved = 4 asset_obj.save() continue asset_obj.approved = 3 asset_obj.save() # 更新任务单状态 这个地方注意一下更新的顺序,放在这里有点问题,上面失败,下面也会被更新 apply_order_obj.approved = True apply_order_obj.user_approve = request.user.username apply_order_obj.save() asset_detail_json = list(apply_order_obj.project.values()) # 任务创建完成,通知组内成员 send_to = '' business_obj = models.BusinessUnit.objects.filter( id=apply_order_obj.business) for user_groups in business_obj[0].manager.all(): for user in user_groups.users.all(): send_to += user.username + '@m1om.me,' template_var = { 'title': apply_order_obj.name, 'json_data': asset_detail_json, 'business_unit': business_node_top(business_obj[0]), 'user_apply': apply_order_obj.user_apply, 'user_approve': apply_order_obj.user_approve, 'server_count': len(asset_detail_json) } email_smtp.mail_send(send_to, 'asset_apply_create_inform_group', template_var) except Exception as e: print(Exception, e) response.status = False response.message = str(e) return response