python_way ,day25 CMDB_models (数据库设计)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python_way ,day25 CMDB_models (数据库设计)相关的知识,希望对你有一定的参考价值。

from django.db import models
from django.contrib.auth.models import User


# Create your models here.


class UserProfile(User):
    name = models.CharField("姓名", max_length=32)

    def __str__(self):
        return self.name

    class Meta:  #在admin中可以显示中文
        super(User.Meta)  #因为继承了父类的UserMeta 所以下面定义的verbose_name 就会把父类的同样的参数所覆盖。
        verbose_name = ‘用户‘
        verbose_name_plural = "用户"


class Asset(models.Model):
    ‘‘‘资产信息表‘‘‘
    name = models.CharField(max_length=64, unique=True)  # 资产名不要重复
    asset_type_choices = (
        (‘server‘, ‘服务器‘),
        (‘networkdevice‘, ‘网络设备‘),
        (‘storagedevice‘, ‘存储设备‘),
        (‘securitydevice‘, ‘安全设备‘),
        (‘securitydevice‘, ‘机房设备‘),
        # (‘switch‘, u‘交换机‘),
        # (‘router‘, u‘路由器‘),
        # (‘firewall‘, u‘防火墙‘),
        # (‘storage‘, u‘存储设备‘),
        # (‘NLB‘, u‘NetScaler‘),
        # (‘wireless‘, u‘无线AP‘),
        (‘software‘, ‘软件资产‘),
        # (‘others‘, u‘其它类‘),
    )
    asset_type = models.CharField(choices=asset_type_choices, max_length=64, default=‘server‘)
    business_unit = models.ForeignKey("BusinessUnit", blank=True, null=True)
    sn = models.CharField(u‘资产SN号‘, max_length=128, unique=True)
    manufactory = models.ForeignKey(‘Manufactory‘, verbose_name=‘制造商‘, null=True, blank=True)
    management_ip = models.GenericIPAddressField(‘管理IP‘, blank=True, null=True)
    contract = models.ForeignKey(‘Contract‘, verbose_name=‘合同‘, null=True, blank=True)
    trade_date = models.DateField(‘购买时间‘, null=True, blank=True)
    expire_date = models.DateField(‘过保修期‘, null=True, blank=True)
    price = models.FloatField(‘价格‘, null=True, blank=True)
    idc = models.ForeignKey(‘IDC‘, verbose_name=‘IDC机房‘, null=True, blank=True)
    admin = models.ForeignKey(‘UserProfile‘, verbose_name=‘资产管理员‘, null=True, blank=True)
    memo = models.TextField(u‘备注‘, null=True, blank=True)
    create_date = models.DateTimeField(blank=True, auto_now_add=True)
    update_date = models.DateTimeField(blank=True, auto_now=True)

    class Meta:
        verbose_name = ‘资产总表‘
        verbose_name_plural = "资产总表"

    def __str__(self):
        return ‘<id:%s , name:%s>‘ % (self.id, self.name)

class Server(models.Model):
    ‘‘‘服务器信息‘‘‘
    asset = models.OneToOneField(Asset)
    sub_assset_type_choices = (
        (0, ‘PC服务器‘),
        (1, ‘刀片机‘),
        (2, ‘小型机‘),
        (3, ‘阿里云‘),
    )
    sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="服务器类型", default=0)
    created_by_choices = (
        (‘auto‘, ‘Auto‘),
        (‘manual‘, ‘Manual‘),
    )
    created_by = models.CharField(choices=created_by_choices, max_length=32,
                                  default=‘auto‘)  # 手动添加还是自动添加auto: auto created,   manual:created manually
    hosted_on = models.ForeignKey(‘self‘, related_name=‘hosted_on_server‘, blank=True, null=True)  # for vitural server
    model = models.CharField(verbose_name=‘型号‘, max_length=128, null=True, blank=True)  # model 所有的设备都有型号,为什么不存放在公共字段
    raid_type = models.CharField(‘raid类型‘, max_length=512, blank=True, null=True)
    os_type = models.CharField(‘操作系统类型‘, max_length=64, blank=True, null=True)
    os_distribution = models.CharField(‘发型版本‘, max_length=64, blank=True, null=True)
    os_release = models.CharField(‘操作系统版本‘, max_length=64, blank=True, null=True)

    class Meta:
        verbose_name = ‘服务器‘
        verbose_name_plural = "服务器"
        # together = ["sn", "asset"]

    def __str__(self):
        return ‘%s sn:%s‘ % (self.asset.name, self.asset.sn)


class BusinessUnit(models.Model):
    ‘‘‘业务线‘‘‘
    parent_unit = models.ForeignKey(‘self‘, null=True, blank=True)
    name = models.CharField(max_length=64, unique=True)

    def __str__(self):
        return self.name


class SecurityDevice(models.Model):
    """安全设备"""
    asset = models.OneToOneField(‘Asset‘)
    sub_assset_type_choices = (
        (0, ‘防火墙‘),
        (1, ‘入侵检测设备‘),
        (2, ‘互联网网关‘),
        (4, ‘运维审计系统‘),
    )
    sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="服务器类型", default=0)

    # 可以自己查找设备的一些参数
    def __str__(self):
        return self.asset.id


class NetworkDevice(models.Model):
    """网络设备"""
    asset = models.OneToOneField(‘Asset‘)
    sub_assset_type_choices = (
        (0, ‘路由器‘),
        (1, ‘交换机‘),
        (2, ‘负载均衡‘),
        (4, ‘VPN设备‘),
    )
    sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="网络设备类型", default=0)

    vlan_ip = models.GenericIPAddressField(‘VlanIP‘, blank=True, null=True)
    intranet_ip = models.GenericIPAddressField(‘内网IP‘, blank=True, null=True)
    # sn = models.CharField(u‘SN号‘,max_length=128,unique=True)
    # manufactory = models.CharField(verbose_name=u‘制造商‘,max_length=128,null=True, blank=True)
    model = models.CharField(‘型号‘, max_length=128, null=True, blank=True)
    firmware = models.CharField("固件", max_length=64, blank=True, null=True)
    port_num = models.SmallIntegerField(‘端口个数‘, null=True, blank=True)
    device_detail = models.TextField(u‘设置详细配置‘, null=True, blank=True)

    class Meta:
        verbose_name = ‘网络设备‘
        verbose_name_plural = "网络设备"


class Software(models.Model):
    ‘‘‘
    only save software which company purchased
    ‘‘‘
    os_types_choice = (
        (0, ‘OS‘),
        (1, ‘办公\开发软件‘),
        (2, ‘业务软件‘),

    )
    license_num = models.IntegerField(verbose_name="授权数")
    # os_distribution_choices = ((‘windows‘,‘Windows‘),
    #                            (‘centos‘,‘CentOS‘),
    #                            (‘ubuntu‘, ‘Ubuntu‘))
    # type = models.CharField(u‘系统类型‘, choices=os_types_choice, max_length=64,help_text=u‘eg. GNU/Linux‘,default=1)
    # distribution = models.CharField(u‘发型版本‘, choices=os_distribution_choices,max_length=32,default=‘windows‘)
    version = models.CharField(‘软件/系统版本‘, max_length=64, help_text=‘eg. CentOS release 6.5 (Final)‘, unique=True)

    # language_choices = ((‘cn‘,u‘中文‘),
    #                     (‘en‘,u‘英文‘))
    # language = models.CharField(u‘系统语言‘,choices = language_choices, default=‘cn‘,max_length=32)
    # #version = models.CharField(u‘版本号‘, max_length=64,help_text=u‘2.6.32-431.3.1.el6.x86_64‘ )

    def __str__(self):
        return self.version

    class Meta:
        verbose_name = ‘软件/系统‘
        verbose_name_plural = "软件/系统"


class CPU(models.Model):
    asset = models.OneToOneField(‘Asset‘)  # onetoone 只是django在应用级别上的一对一关联,只是限定在一张表中同一个cpu不用同时关联多个assect。
    cpu_model = models.CharField(‘CPU型号‘, max_length=128, blank=True)
    cpu_count = models.SmallIntegerField(‘物理cpu个数‘)
    cpu_core_count = models.SmallIntegerField(u‘cpu核数‘)
    memo = models.TextField(‘备注‘, null=True, blank=True)
    create_date = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(blank=True, null=True)

    class Meta:
        verbose_name = ‘CPU部件‘
        verbose_name_plural = "CPU部件"

    def __str__(self):
        return self.cpu_model


class RAM(models.Model):
    asset = models.ForeignKey(‘Asset‘)
    sn = models.CharField(‘SN号‘, max_length=128, blank=True, null=True)
    model = models.CharField(u‘内存型号‘, max_length=128)
    slot = models.CharField(‘插槽‘, max_length=64)
    capacity = models.IntegerField(‘内存大小(MB)‘)
    memo = models.CharField(‘备注‘, max_length=128, blank=True, null=True)
    create_date = models.DateTimeField(blank=True, auto_now_add=True)
    update_date = models.DateTimeField(blank=True, null=True)

    def __str__(self):
        return ‘%s:%s:%s‘ % (self.asset_id, self.slot, self.capacity)

    class Meta:
        verbose_name = ‘RAM‘
        verbose_name_plural = "RAM"
        unique_together = ("asset", "slot")

    auto_create_fields = [‘sn‘, ‘slot‘, ‘model‘, ‘capacity‘]


class Disk(models.Model):
    asset = models.ForeignKey(‘Asset‘)
    sn = models.CharField(‘SN号‘, max_length=128, blank=True, null=True)
    slot = models.CharField(‘插槽位‘, max_length=64)
    # manufactory = models.CharField(u‘制造商‘, max_length=64,blank=True,null=True)
    model = models.CharField(‘磁盘型号‘, max_length=128, blank=True, null=True)
    capacity = models.FloatField(‘磁盘容量GB‘)
    disk_iface_choice = (
        (‘SATA‘, ‘SATA‘),
        (‘SAS‘, ‘SAS‘),
        (‘SCSI‘, ‘SCSI‘),
        (‘SSD‘, ‘SSD‘),
    )

    iface_type = models.CharField(‘接口类型‘, max_length=64, choices=disk_iface_choice, default=‘SAS‘)
    memo = models.TextField(u‘备注‘, blank=True, null=True)
    create_date = models.DateTimeField(blank=True, auto_now_add=True)  # 单独更新
    update_date = models.DateTimeField(blank=True, null=True)

    auto_create_fields = [‘sn‘, ‘slot‘, ‘manufactory‘, ‘model‘, ‘capacity‘, ‘iface_type‘]

    class Meta:
        unique_together = ("asset", "slot")  # 资产和插槽联合为一
        verbose_name = ‘硬盘‘
        verbose_name_plural = "硬盘"

    def __str__(self):
        return ‘%s:slot:%s capacity:%s‘ % (self.asset_id, self.slot, self.capacity)


class NIC(models.Model):
    asset = models.ForeignKey(‘Asset‘)
    # server = models.ForeignKey(‘Server‘)
    name = models.CharField(‘网卡名‘, max_length=64, blank=True, null=True)
    sn = models.CharField(‘SN号‘, max_length=128, blank=True, null=True)
    model = models.CharField(‘网卡型号‘, max_length=128, blank=True, null=True)
    macaddress = models.CharField(‘MAC‘, max_length=64, unique=True)
    ipaddress = models.GenericIPAddressField(‘IP‘, blank=True, null=True)
    netmask = models.CharField(max_length=64, blank=True, null=True)
    bonding = models.CharField(max_length=64, blank=True, null=True)  # vip
    memo = models.CharField(‘备注‘, max_length=128, blank=True, null=True)
    create_date = models.DateTimeField(blank=True, auto_now_add=True)
    update_date = models.DateTimeField(blank=True, null=True)

    def __str__(self):
        return ‘%s:%s‘ % (self.asset_id, self.macaddress)

    class Meta:
        verbose_name = u‘网卡‘
        verbose_name_plural = u"网卡"
        # unique_together = ("asset_id", "slot")
        # unique_together = ("asset", "macaddress")

    auto_create_fields = [‘name‘, ‘sn‘, ‘model‘, ‘macaddress‘, ‘ipaddress‘, ‘netmask‘, ‘bonding‘]


class RaidAdaptor(models.Model):
    asset = models.ForeignKey(‘Asset‘)   #如果明确了一台服务器可以有多快raid卡就用foreignkey
    # asset = models.OneToOneField(‘Asset‘)   #如果一台机器只能有一块raid卡,那就用ontoon
    sn = models.CharField(u‘SN号‘, max_length=128, blank=True, null=True)
    slot = models.CharField(u‘插口‘, max_length=64)
    model = models.CharField(u‘型号‘, max_length=64, blank=True, null=True)
    memo = models.TextField(u‘备注‘, blank=True, null=True)
    create_date = models.DateTimeField(blank=True, auto_now_add=True)
    update_date = models.DateTimeField(blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        unique_together = ("asset", "slot")


class Manufactory(models.Model):  #厂商是asset中的外面
    manufactory = models.CharField(u‘厂商名称‘, max_length=64, unique=True)
    support_num = models.CharField(u‘支持电话‘, max_length=30, blank=True)
    memo = models.CharField(u‘备注‘, max_length=128, blank=True)

    def __str__(self):
        return self.manufactory

    class Meta:
        verbose_name = ‘厂商‘
        verbose_name_plural = "厂商"


class Contract(models.Model):  #合同
    sn = models.CharField(‘合同号‘, max_length=128, unique=True)
    name = models.CharField(‘合同名称‘, max_length=64)
    memo = models.TextField(‘备注‘, blank=True, null=True)
    price = models.IntegerField(‘合同金额‘)
    detail = models.TextField(‘合同详细‘, blank=True, null=True)
    start_date = models.DateField(blank=True)
    end_date = models.DateField(blank=True)
    license_num = models.IntegerField(‘license数量‘, blank=True)
    create_date = models.DateField(auto_now_add=True)
    update_date = models.DateField(auto_now=True)

    class Meta:
        verbose_name = ‘合同‘
        verbose_name_plural = "合同"

    def __str__(self):
        return self.name


class IDC(models.Model):#机房,可以添加楼层,机柜 都是手动添加,不是动态的
    name = models.CharField(u‘机房名称‘, max_length=64, unique=True)
    memo = models.CharField(u‘备注‘, max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = ‘机房‘
        verbose_name_plural = "机房"


class Tag(models.Model):  #打的标签
    name = models.CharField(‘Tag name‘, max_length=32, unique=True)
    creater = models.ForeignKey(‘UserProfile‘)
    create_date = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.name


class EventLog(models.Model):  #日志表
    name = models.CharField(‘事件名称‘, max_length=100)
    event_type_choices = (
        (1, ‘硬件变更‘),
        (2, ‘新增配件‘),
        (3, ‘设备下线‘),
        (4, ‘设备上线‘),
        (5, ‘定期维护‘),
        (6, ‘业务上线\更新\变更‘),
        (7, ‘其它‘),
    )
    event_type = models.SmallIntegerField(‘事件类型‘, choices=event_type_choices)
    asset = models.ForeignKey(‘Asset‘)  #一个资产可以有多个事件
    component = models.CharField(‘事件子项‘, max_length=255, blank=True, null=True) #存储某一个位置发生的变更
    detail = models.TextField(‘事件详情‘)
    date = models.DateTimeField(‘事件时间‘, auto_now_add=True)
    user = models.ForeignKey(‘UserProfile‘, verbose_name=‘事件源‘)    #是谁产生的
    memo = models.TextField(‘备注‘, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = ‘事件纪录‘
        verbose_name_plural = "事件纪录"

    def colored_event_type(self):
        if self.event_type == 1:
            cell_html = ‘<span style="background: orange;">%s</span>‘
        elif self.event_type == 2:
            cell_html = ‘<span style="background: yellowgreen;">%s</span>‘
        else:
            cell_html = ‘<span >%s</span>‘
        return cell_html % self.get_event_type_display()

    colored_event_type.allow_tags = True
    colored_event_type.short_description = u‘事件类型‘

  

创建数据库:

python3 manage.py makemigretions
python3 manage.py migrate

创建用户

python3 manage.py creatiesuperuser

在django  admin中创建一个user,和一个资产asset

然后再使用命令查看一下这个资产的内容

python3 manage.py shell
from assets import models
models.Asset.objects.last().name()
>>> models.Asset.objects.last().name
‘dell0811‘
这样就可以查看数据库中的内容了

  

数据库完毕,然后就开始考虑数据怎么获取了

  

 

以上是关于python_way ,day25 CMDB_models (数据库设计)的主要内容,如果未能解决你的问题,请参考以下文章

python_way,day3

python_way,day4

python_way ,day22 tonardo

python_way ,day2 字符串,列表,字典,时间模块

python_way day18 html-day4, 模板

python_way day16 JQuary