CMDB资产管理系统开发day26:批准资产入库

Posted python运维自动化之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CMDB资产管理系统开发day26:批准资产入库相关的知识,希望对你有一定的参考价值。

刚才都是一条像内存,硬盘,网卡、多条的话如何操作

只有一条数据

下面的是有多条数据的

硬盘必须字段的验证

    def __create_disk_component(self):
        disk_info = self.clean_data.get(\'physical_disk_driver\')
        if disk_info:
            for disk_item in disk_info:
                try:
                    self.__verify_field(disk_item, \'slot\', str)
					\'\'\'
					这里确实是验证了一个slot,首先获取数据,0是不成立的,插槽是0
					这一段根本就不走,客户端操作,导致这一块就报错
					\'\'\'
                    self.__verify_field(disk_item, \'capacity\', float)
                    self.__verify_field(disk_item, \'iface_type\', str)
                    self.__verify_field(disk_item, \'model\', str)
                    if not len(self.response[\'error\']):  # no processing when there\'s no error happend
                        data_set = {
                            \'asset_id\': self.asset_obj.id,
                            \'sn\': disk_item.get(\'sn\'),
                            \'slot\': disk_item.get(\'slot\'),
                            \'capacity\': disk_item.get(\'capacity\'),
                            \'model\': disk_item.get(\'model\'),
                            \'iface_type\': disk_item.get(\'iface_type\'),
                            \'manufactory\': disk_item.get(\'manufactory\'),
                        }

                        obj = models.Disk(**data_set)
                        obj.save()

                except Exception as e:
                    self.response_msg(\'error\', \'ObjectCreationException\', \'Object [disk] %s\' % str(e))
        else:
            self.response_msg(\'error\', \'LackOfData\', \'Disk info is not provied in your reporting data\')

硬盘类型不对报错

 解决办法:

 except Exception as e:
                    self.response_msg(\'error\', \'ObjectCreationException\', \'Object [disk] %s\' % str(e))			
	\'\'\'编写except ValueError Exception as e不再抓异常\'\'\'   

硬盘验证字段bug导致获取不到资产组件信息

    

客户端命令行拍错截图:

解决办法:

验证字段的时候:if not none 就可以了

资产表创建逻辑解析

	def _create_server(self):
			self.__create_server_info()
			self.__create_or_update_manufactory()

			self.__create_cpu_component()
			self.__create_disk_component()
			self.__create_nic_component()
			self.__create_ram_component()
	
			log_msg = "Asset [<a href=\'/admin/assets/asset/%s/\' target=\'_blank\'>%s</a>] has been created!" % (
				self.asset_obj.id, self.asset_obj)
			self.response_msg(\'info\', \'NewAssetOnline\', log_msg)
			
			\'\'\'
                        如果上面的都获取成功,我就返回一条消息,创建资产成功,
			你可以\'/admin/assets/asset/%s/\'到这个页面,打开这个资产
			然后记录一条日志
			\'\'\'                            

创建资产成功创建记录截图

后台资产表截图如下:

   

内存capacity字段类型验证

    def __create_ram_component(self):
        ram_info = self.clean_data.get(\'ram\')
        if ram_info:
            for ram_item in ram_info:
                try:
                    self.__verify_field(ram_item, \'capacity\', int)
					\'\'\'验证capacity必须是int,如果不是int就走不下去了,内存就不创建了\'\'\'
                    if not len(self.response[\'error\']):  # no processing when there\'s no error happend
                        data_set = {
                            \'asset_id\': self.asset_obj.id,
                            \'slot\': ram_item.get("slot"),
                            \'sn\': ram_item.get(\'sn\'),
                            \'capacity\': ram_item.get(\'capacity\'),
                            \'model\': ram_item.get(\'model\'),
                        }

                        obj = models.RAM(**data_set)
                        obj.save()

                except Exception as e:
                    self.response_msg(\'error\', \'ObjectCreationException\', \'Object [ram] %s\' % str(e))
        else:
            self.response_msg(\'error\', \'LackOfData\', \'RAM info is not provied in your reporting data\')

验证必须字段,没有必须字段就不不创建硬盘

网卡验证字段

    def __create_nic_component(self):
        nic_info = self.clean_data.get(\'nic\')
        if nic_info:
            for nic_item in nic_info:
                try:
                    self.__verify_field(nic_item, \'macaddress\', str)
					\'\'\'IP地址可以没有,验证macaddress地址必须存在\'\'\'
                    if not len(self.response[\'error\']):  # no processing when there\'s no error happend
                        data_set = {
                            \'asset_id\': self.asset_obj.id,
                            \'name\': nic_item.get(\'name\'),
                            \'sn\': nic_item.get(\'sn\'),
                            \'macaddress\': nic_item.get(\'macaddress\'),
                            \'ipaddress\': nic_item.get(\'ipaddress\'),
                            \'bonding\': nic_item.get(\'bonding\'),
		\'\'\'这个网卡是绑定的,是客户端检测的,我服务器端只是检测这个字段\'\'\'
                            \'model\': nic_item.get(\'model\'),
                            \'netmask\': nic_item.get(\'netmask\'),
                        }

                        obj = models.NIC(**data_set)
                        obj.save()

                except Exception as e:
                    self.response_msg(\'error\', \'ObjectCreationException\', \'Object [nic] %s\' % str(e))
        else:
            self.response_msg(\'error\', \'LackOfData\', \'NIC info is not provied in your reporting data\')

  

以上是关于CMDB资产管理系统开发day26:批准资产入库的主要内容,如果未能解决你的问题,请参考以下文章

CMDB项目

Day73-CMDB(资产管理采集)的三种实现方式

资产管理文档

CMDB资产采集

CMDB

硬件资产管理系统总结