在执行filter_type(self)方法时,报如下错误

来源:5-18 (资产探测、扫描发现进阶)章节知识回顾

慕桂英8092138

2018-06-29

在执行filter_type(self)方法时,报外键错误,具体报错信息如下:

django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`mydevops`.`virtualserverinfo`, CONSTRAINT `virtualserverinfo_vir_phy_id_45b1088b_fk_physicalserverinfo_id` FOREIGN KEY (`vir_phy_id`) REFERENCES `physicalserverinfo` (`id`))')

有一个地方想不清楚,在运行这个方法时,VirtualServerInfo表是空的,physicalserverinfo表也是空的,这样通过VirtualServerInfo表中的vir_phy_id字段,去关联physicalserverinfo表中的id,这肯定是有问题吧,因为后面那张表是空的。但参数老师的代码就是这样执行的

写回答

3回答

Jeson

2018-07-02

elif re.search("VMware",item_val[-1]):

                mac_addr = self.return_key_mac(item_val[-3])

                VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[sn_key]))


扫描到Vmware的时候,执行的是这一段。vir_phy_id=1,所以 PhysicalServerInfo是空的是允许的。

0
0

Jeson

2018-06-30

 def filter_type(self):
        VirtualServerInfo.objects.all().delete()
        NetWorkInfo.objects.all().delete()
        PhysicalServerInfo.objects.all().delete()
        for item,item_val in self.key_dic_map.items():
            mac_addr = self.return_key_mac(item_val[-3])
            sn_key = self.return_true_string(item_val[-2])
            if re.search("KVM",item_val[-1]) or re.search("QEMU",item_val[-1]):
                VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[mac_addr]))
            elif re.search("VMware",item_val[-1]):
                mac_addr = self.return_key_mac(item_val[-3])
                VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[sn_key]))
            elif re.search("Network",item_val[-1]):
                NetWorkInfo.objects.create(host_ip=item_val[0],host_name=item_val[7],net_cab_id=1,sn=item_val[-2])
                # print "Network...........",item_val[-1]
            else:
                self.key_ip_relate[item] = item_val[0].split(',')[0]
                if sn_key and sn_key != 'NotSpecified':
                    PhysicalServerInfo.objects.create(server_ip=item_val[0],machine_brand=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],ser_cabin_id=1,conn_phy_id=int(self.key_sn_relate[sn_key]))
                else:
                    PhysicalServerInfo.objects.create(server_ip=item_val[0],machine_brand=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],ser_cabin_id=1,conn_phy_id=int(self.key_sn_relate[mac_addr]))
                # print "Machine............",item_val[-1]
        return True

当然执行了,你看PhysicalServerInfo入库这段的逻辑,是非"KVM"、"WMware"、"Network"类型的设备才会执行入库PhysicalServerInfo。key_dic_map.items(),你先把这段内容打印下。调试下原因,分析下

0
2
慕桂英8092138
老师你好,可能你还没明白我的意思,上面那段代码在执行 匹配到‘VMware’关键词时,这个时候会往VirtualServerInfo这张表写数据,这张表的vir_phy_id字段是PhysicalServerInfo.id的外键,此时PhysicalServerInfo完全就是一张空白啊。
2018-07-01
共2条回复

Jeson

2018-06-29

是的,所以先需要入物理设备这张表数据physicalserverinfo,通常是这张表对应扫描到的物理机数据没有入进去,导致外键关联才出现的问题。

建议你debug下,这个执行过程,打印一些信息出来追下为什么physicalserverinfo表的数据没有入进去

0
2
慕桂英8092138
老师,在执行这段代码前,先把 physicalserverinfo表清空,那怎么还能通过外键去关联呢?我真的有点怀疑老师你这个项目并没有执行成功啊 def filter_type(self): VirtualServerInfo.objects.all().delete() NetWorkInfo.objects.all().delete() PhysicalServerInfo.objects.all().delete() for item,item_val in self.key_dic_map.items(): mac_addr = self.return_key_mac(item_val[-3]) sn_key = self.return_true_string(item_val[-2]) if re.search("KVM",item_val[-1]) or re.search("QEMU",item_val[-1]): VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[mac_addr])) elif re.search("VMware",item_val[-1]): mac_addr = self.return_key_mac(item_val[-3]) VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[sn_key]))
2018-06-30
共2条回复

Python+Django+Ansible Playbook自动化运维项目实战

【课程升级3.6版本】大牛全面践行DevOps,运维与开发间高效协作

861 学习 · 326 问题

查看课程