def data_save(self,data): """ 讲收集到的系统信息数据处理后写入数据库 """ data_dict = {} for i in data['grains']: if not isinstance(data['grains'][i], dict): continue data_dict[i] = {} #print data['grains'][i]['host'] data_dict[i]['hostname'] = data['grains'][i]['host'] data_dict[i]['sn'] = data['grains'][i]['serialnumber'] data_dict[i]['manufacturer'] = data['grains'][i]['manufacturer'] data_dict[i]['model'] = data['grains'][i]['productname'] data_dict[i]['work_ip'] = data['grains'][i]['ipv4'] data_dict[i]['os_platform'] = data['grains'][i]['os'] data_dict[i]['os_version'] = data['grains'][i]['osrelease_info'] data_dict[i]['cpu_count'] = data['grains'][i]['num_cpus'] data_dict[i]['cpu_model'] = data['grains'][i]['cpu_model'] data_dict[i]['mem_capacity'] = data['grains'][i]['mem_total'] for i in data['disk']: if not isinstance(data['disk'][i], dict): continue disk_count = 0 for disk in data['disk'][i]: if not data['disk'][i][disk]['1K-blocks']: continue disk_count += int(data['disk'][i][disk]['1K-blocks']) data_dict[i]['disk_capacity'] = disk_count / 1024 / 1024 for i in data_dict: obj_update=models.Server.objects.filter(hostname=data_dict[i]['hostname']) if obj_update: obj_update.update(sn=data_dict[i]['sn']) obj_update.update(manufacturer=data_dict[i]['manufacturer']) obj_update.update(model=data_dict[i]['model']) obj_update.update(work_ip=data_dict[i]['work_ip']) obj_update.update(os_platform=data_dict[i]['os_platform']) obj_update.update(os_version=data_dict[i]['os_version']) obj_update.update(cpu_count=data_dict[i]['cpu_count']) obj_update.update(cpu_model=data_dict[i]['cpu_model']) obj_update.update(mem_capacity=data_dict[i]['mem_capacity']) obj_update.update(disk_capacity=data_dict[i]['disk_capacity']) else: obj=models.Server() obj.hostname=data_dict[i]['hostname'] obj.sn=data_dict[i]['sn'] obj.manufacturer=data_dict[i]['manufacturer'] obj.model=data_dict[i]['model'] obj.work_ip=data_dict[i]['work_ip'] obj.os_platform=data_dict[i]['os_platform'] obj.os_version=data_dict[i]['os_version'] obj.cpu_count=data_dict[i]['cpu_count'] obj.cpu_model=data_dict[i]['cpu_model'] obj.mem_capacity=data_dict[i]['mem_capacity'] obj.disk_capacity=data_dict[i]['disk_capacity'] obj_asset=models.Asset.objects.create(device_type_id='server') obj.asset=obj_asset obj.save()
def __create_server_info(self, ignore_errs=False): # 创建server表的相关信息,第一步进行小字典的合法验证 try: self.__verify_field(self.clean_data, 'model', str) if not len(self.response['error']) or ignore_errs is True: data_set = { 'asset_id': self.asset_obj.id, 'raid_type': self.clean_data.get('raid_type'), '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.asset.model = self.clean_data.get('model') obj.save() return obj except Exception as e: self.response_msg('error', 'ObjectCreationException', 'Object [server] %s' % str(e))
def __create_server_into(self): try: self._type_validation(self.clean_data, 'model', str) if not len(self.response['error']): 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', 'CreationException', '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, '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: pass
def __create_server_info(self,ignore_errs=False): """创建server表里的相关信息""" 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.asset.model = self.clean_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): # 1. 验证字段合法性 __verify_field,有问题保存在 self.response['error']中 # 2. ignore_errs=True,开关选项,可忽略错误 # 3. 汇总新增记录所需数据 data_set # 4. 新增记录 obj = models.Server(**data_set);obj.save() try: self.__verify_field(self.clean_data, 'model', str) if not len(self.response['error']) or ignore_errs == True: 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 __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'), } #根据获取信息,创建与此资产id对应的server字段 obj = models.Server(**data_set) obj.asset.model = self.clean_data.get('model') obj.save() return obj except Exception as e: #否则创建对象存在异常 self.response_msg('error', 'ObjectCreationException', 'Object [server] %s' % str(e))