django.db import models
class BusinessUnit(models.Model):
"""
业务线
"""
name = models.CharField(
‘业务线‘, max_length=
64, unique=
True)
class Meta:
verbose_name_plural =
"业务线表"
def __str__(self):
return self.name
class IDC(models.Model):
"""
机房信息
"""
name = models.CharField(
‘机房‘, max_length=
32)
floor = models.IntegerField(
‘楼层‘,
default=
1)
class Meta:
verbose_name_plural =
"机房表"
def __str__(self):
return self.name
class Server(models.Model):
"""
服务器信息 主机
"""
device_status_choices =
(
(1,
‘上架‘),
(2,
‘在线‘),
(3,
‘离线‘),
(4,
‘下架‘),
)
device_status_id = models.IntegerField(
‘设备状态‘, choices=device_status_choices,
default=
1)
idc = models.ForeignKey(
‘IDC‘, verbose_name=
‘IDC机房‘,
null=True, blank=True, on_delete=
models.CASCADE)
cabinet_num = models.CharField(
‘机柜号‘, max_length=
30,
null=True, blank=
True)
cabinet_order = models.CharField(
‘机柜中序号‘, max_length=
30,
null=True, blank=
True)
business_unit = models.ForeignKey(
‘BusinessUnit‘, verbose_name=
‘属于的业务线‘,
null=True, blank=
True,
on_delete=
models.CASCADE)
# 基本信息 + 主板信息 +
CPU信息
hostname = models.CharField(
‘主机名‘, 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(
‘最后更新时间‘,
null=
True)
create_at = models.DateTimeField(auto_now_add=True, blank=
True)
class Meta:
verbose_name_plural =
"服务器表"
def __str__(self):
return self.hostname
class Disk(models.Model):
"""
硬盘信息
"""
slot = models.CharField(
‘插槽位‘, max_length=
8)
model = models.CharField(
‘磁盘型号‘, max_length=
108)
capacity = models.FloatField(
‘磁盘容量GB‘)
pd_type = models.CharField(
‘磁盘类型‘, max_length=
32)
server = models.ForeignKey(verbose_name=
‘服务器‘, to=
‘Server‘, related_name=
‘disk_list‘, on_delete=
models.CASCADE)
class Meta:
verbose_name_plural =
"硬盘表"
def __str__(self):
return self.slot
class NIC(models.Model):
"""
网卡信息
"""
name = models.CharField(
‘网卡名称‘, max_length=
128)
hwaddr = models.CharField(
‘网卡mac地址‘, max_length=
64)
netmask = models.CharField(max_length=
64)
ipaddrs = models.CharField(
‘ip地址‘, max_length=
256)
up = models.BooleanField(
default=
False)
server = models.ForeignKey(
‘Server‘, related_name=
‘nic_list‘, on_delete=
models.CASCADE)
class Meta:
verbose_name_plural =
"网卡表"
def __str__(self):
return self.name
class Memory(models.Model):
"""
内存信息
"""
slot = models.CharField(
‘插槽位‘, max_length=
32)
manufacturer = models.CharField(
‘制造商‘, 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 =
"资产记录表"
class ErrorLog(models.Model):
"""
错误日志,如:agent采集数据错误 或 运行错误
"""
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_add=
True)
class Meta:
verbose_name_plural =
"错误日志表"
def __str__(self):
return self.title
models.py
我们可以将业务线先设置成这个。然后执行命令生成表
我们先将cert删除,并且将这里写入文件的注释掉,这样每次判断都是新增主机了
服务端接收的数据如下图,将它存入数据库
我们要将新增的这些资产信息存入数据库
先看新增server,1处有默认,2处可以为空,我们先不管
我们先看这三种硬件信息
而这三种硬件信息和我们传过来的数据一致,字段名字也一样
我们将数据字典取出来,然后update到同一个字典中,合成一个。
获得如下的字典
{
‘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加进去,循环并取出它的键加到数据库中
cmdb资产管理2
标签:default ip地址 img ret mem 建表 补充 count als