cmdb Asset Management 2

 

Add assets

Now api service has been able to get to the end we have to do the operation. Additional procedures should be followed after obtaining the corresponding write operation

We need to do is to save the post request is sent over the data to the database. We create a repository name app, and design models create a table to store data. Behind can get information from the database and display it

from django.db Import Models 


class BusinessUnit (models.Model):
     "" "
     Business Line
     " ""
     name = models.CharField ( ' line of business ' , = MAX_LENGTH 64 , UNIQUE = True) 

    class Meta -: 
        verbose_name_plural = " line of business TABLE " 

    DEF __str __ (Self): 
        return the self.name 


class the IDC (models.Model):
     " ""
     room information
     "" "
     name = models.CharField ( ' engine room ' ,max_length=32)
    floor Models.IntegerField = ( ' floor ' , default = . 1 ) 

    class Meta -: 
        verbose_name_plural = " room table " 

    DEF __str __ (Self): 
        return the self.name 


class Server (models.Model):
     "" "
     Server Information Host
     " ""
 
    device_status_choices = ( 
        ( 1 , ' shelves ' ), 
        ( 2 , ' online ' ), 
        ( 3 , 'Offline ' ), 
        ( 4 , ' off the shelf ' ), 
    ) 
    device_status_id = models.IntegerField ( " device state " , choices = device_status_choices, default = . 1 ) 

    IDC = models.ForeignKey ( ' the IDC ' , the verbose_name = ' the IDC room ' , null = True, blank = True, on_delete = models.CASCADE) 
    cabinet_num = models.CharField ( ' cabinet number ' , = MAX_LENGTH 30 , nullTrue =, blank = True) 
    cabinet_order = models.CharField ( ' cabinet number ' , = MAX_LENGTH 30 , null = True, blank = True) 

    business_unit = models.ForeignKey ( ' BusinessUnit ' , the verbose_name = ' service lines belong ' , null = True, blank = True, 
                                      on_delete = models.CASCADE) 

    # basic information + motherboard information + the CPU information 
    hostname = models.CharField ( ' host name ' , MAX_LENGTH = 128, unique=True)
    os_platform = models.CharField('系统', max_length=16, null=True, blank=True)
    os_version = models.CharField('系统版本', max_length=16, null=True, blank=True)

    sn = models.CharField('SN号', max_length=64, db_index=True)
    manufacturer = models.CharField(verbose_name='制造商', max_length=64, null=True, blank=True)
    model = models.CharField('型号', max_length=64, null=True, blank=True)

    cpu_count = models.IntegerField('CPU个数', null=True, blank=True)
    cpu_physical_count = models.IntegerField('CPU物理个数', null=True, blank=True)
    cpu_model = models.CharField('CPU型号', max_length=128, null=True, blank=True) 

    LATEST_DATE = models.DateField ( ' last update time ' , null = true) 
    create_at = models.DateTimeField (auto_now_add = True, blank = True) 

    class Meta -: 
        verbose_name_plural = " server list " 

    DEF __str __ (Self): 
        return Self. hostname 


class Disk (models.Model):
     "" "
     Disk information
     " ""
     slot models.CharField = ( ' slot position ' , = MAX_LENGTH . 8 ) 
    Model = models.CharField ( 'Disk model ' , MAX_LENGTH = 108 ) 
    Capacity = models.FloatField ( ' disk capacity GB ' ) 
    pd_type = models.CharField ( ' disk type ' , MAX_LENGTH = 32 ) 

    Server = models.ForeignKey (the verbose_name = ' server ' , to = ' Server ' , the related_name = ' disk_list ' , on_delete = models.CASCADE) 

    class Meta -: 
        verbose_name_plural = " hard sheet " 

    DEF __str __ (Self):
        return self.slot 


class the NIC (models.Model):
     "" "
     card information
     " ""
     name = models.CharField ( ' card name ' , MAX_LENGTH = 128 ) 
    HWaddr = models.CharField ( ' card mac address ' , MAX_LENGTH = 64 ) 
    Netmask = models.CharField (MAX_LENGTH = 64 ) 
    ipaddrs = models.CharField ( ' IP address ' , MAX_LENGTH = 256 ) 
    up = models.BooleanField ( default = False) 
    ServerModels.ForeignKey = ( ' Server ' , the related_name = ' nic_list ' , on_delete = models.CASCADE) 

    class Meta -: 
        verbose_name_plural = " card table " 

    DEF __str __ (Self): 
        return the self.name 


class Memory (models.Model):
     "" "
     memory information
     " ""
     slot models.CharField = ( ' slot position ' , MAX_LENGTH = 32 ) 
    manufacturer = models.CharField ( ' manufacturer ' ,max_length=32, null=True, blank=True)
    model = models.CharField('型号', max_length=64)
    capacity = models.FloatField('容量', null=True, blank=True)
    sn = models.CharField('内存SN号', max_length=64, null=True, blank=True)
    speed = models.CharField('速度', max_length=16, null=True, blank=True)

    server = models.ForeignKey('Server', related_name='memory_list', on_delete=models.CASCADE)

    class Meta:
        verbose_name_plural = "内存表"

    def __str__(self):
        return self.slot


class AssetRecord(models.Model):
    """
    资产变更记录
    """
    server = models.ForeignKey('Server', related_name='servers', on_delete=models.CASCADE)
    content = models.TextField(null= True) 
    create_at = models.DateTimeField (auto_now_add = True) 

    class Meta -: 
        verbose_name_plural = " asset record table " 


class the ErrorLog (models.Model):
     "" "
     error log, such as: agent collection operation error or data error
     " ""
     Server models.ForeignKey = ( ' Server ' , null = True, blank = True, on_delete = models.CASCADE) 
    title = models.CharField (MAX_LENGTH = 16 ) 
    Content = models.TextField () 
    create_at = models.DateTimeField (= auto_now_addTrue) 

    class Meta: 
        verbose_name_plural = " Error log table " 

    DEF __str __ (Self): 
        return self.title
models.py

We can set this line of business first. Run-table then

We first cert deleted, and the file will be written to comment out here, so that each judge is a new host

Receives service data below, it will be stored in the database

We want to add these asset information stored in the database

Look at the new server, there is a default at 1, at 2 can be empty, we first matter

We look at three hardware information

These three hardware and consistent information, and we pass over the data field name, too

We will take out the data dictionary, and then update to the same dictionary, a synthesis.

Obtained as follows dictionary

{
    'os_platform': 'linux',
    'os_version': '6.5',
    'hostname': 'c1.com',
    'manufacturer': 'Parallels Software International Inc.',
    'model': 'Parallels Virtual Platform',
    'sn': 'Parallels-1A 1B CB 3B 64 66 4B 13 86 B0 86 FF 7E 2B 20 30',
    'cpu_count': 24,
    'cpu_physical_count': 2,
    'cpu_model': ' Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz'
}

 然后执行创建orm对象的命令,将这个字典打散存入数据库

再看看新增硬盘,硬盘是一个槽位是一个硬盘,前面新增一台server,已经有了这台server对象了,所以硬盘所属外键server=server。for循环外层字典的值,然后将每个内层字典打散插入到Disk数据表中。

这样就保存了6个槽位的硬盘

这里for循环插入数据,插入一次连接一次数据库,我们可以进行优化一下

 for循环创建Disk对象并追加到列表。bulk_create方法根据对象列表一次批量创建数据表记录

新增内存和新增硬盘一样

出问题了,因为数据结构内存和网卡没有修改,没有保存进去

修改后点击执行客户端,客户端采集信息发送给api然后保存到数据库

而新增网卡有点区别,它以name为键但是数据里没有name,

所以创建要将name加进去,循环并取出它的键加到数据库中

 

Guess you like

Origin www.cnblogs.com/machangwei-8/p/11355680.html