Django的cmdb探索与开发

Posted feigerlan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django的cmdb探索与开发相关的知识,希望对你有一定的参考价值。

经过上次对itchat调用zabbixAPI的研究之后对python的兴趣一发不可收拾,近期上面要求研究一下cmdb,于是自己做了一个简单的cmdb系统 。对其实踩坑的地方做一些记录,由于之前对于前端的知识也是一窍不通,所以很多问题可能也比较初级。

环境:python 3.6.5 ,django 1.8.16。前端模板是网上找到的,希望通过这次研究在前端js/ajax/css上也能有所提升。

1、项目目录,页面存在templates

技术分享图片

2、settings.py设置,还是注意下Setting的配置。当时STATICFILES_DIRS字段少写了一个“s”加载不了静态,排错排了一天。

"""
Django settings for CMDB project.

Generated by ‘django-admin startproject‘ using Django 1.8.16.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = z0z7(!&_&mxf0rafg^[email protected]#n2%g)=0o8p*[email protected]=7p

# SECURITY WARNING: don‘t run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    django.contrib.admin,
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
    "users"
)

MIDDLEWARE_CLASSES = (
    django.contrib.sessions.middleware.SessionMiddleware,
    django.middleware.common.CommonMiddleware,
    # ‘django.middleware.csrf.CsrfViewMiddleware‘,
    django.contrib.auth.middleware.AuthenticationMiddleware,
    django.contrib.auth.middleware.SessionAuthenticationMiddleware,
    django.contrib.messages.middleware.MessageMiddleware,
    django.middleware.clickjacking.XFrameOptionsMiddleware,
    django.middleware.security.SecurityMiddleware,
)

ROOT_URLCONF = CMDB.urls

TEMPLATES = [
    {
        BACKEND: django.template.backends.django.DjangoTemplates,
        DIRS: [
            os.path.join(BASE_DIR,"templates").replace("\\\\","/")
        ],
        APP_DIRS: True,
        OPTIONS: {
            context_processors: [
                django.template.context_processors.debug,
                django.template.context_processors.request,
                django.contrib.auth.context_processors.auth,
                django.contrib.messages.context_processors.messages,
            ],
        },
    },
]

WSGI_APPLICATION = CMDB.wsgi.application


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    default: {
        ENGINE : django.db.backends.mysql,
        NAME : cmdb,
        PASSWORD : cmdb,
        USER : cmdb,
        HOST:192.168.58.130,
        PORT:3306
        # ‘ENGINE‘: ‘django.db.backends.sqlite3‘,
        # ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = zh-hans

TIME_ZONE = UTC

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = /static/
STATICFILES_DIRS = (

    os.path.join(BASE_DIR,"static").replace("\\\\","/"),

    )

MEDIA_ROOT = BASE_DIR + /excel/ # BASE_DIR 是项目所在的目录,这样文件就会存储在当前的开发目录下的一个`excel`的文件夹下
MEDIA_URL = /excel/


2、关于,model,这里就只贴devices和select下拉框的代码了。


class BaseModel(models.Model):     
  delete_flag = models.CharField(max_length=4, verbose_name="删除标志")

class
Devices(BaseModel): sn = models.CharField(max_length=32, verbose_name="序列号") Psn = models.CharField(max_length=32,verbose_name="资产编号") DataRoomID = models.IntegerField(verbose_name="机房ID") CabinetID = models.IntegerField(verbose_name="机柜ID") deviceType = models.IntegerField(verbose_name="设备类型")#1、网络设备 2、安全设备 3、服务器 4、虚拟机 deviceMap = models.CharField(max_length=32, verbose_name="设备位置") company = models.CharField(max_length=32, verbose_name="设备品牌") model = models.CharField(max_length=32, verbose_name="设备型号") adminIP = models.CharField(max_length=32, verbose_name="管理IP") produceIP = models.CharField(max_length=32, verbose_name="业务IP") system = models.CharField(max_length=32, verbose_name="操作系统") uplinkdev = models.CharField(max_length=32, verbose_name="上联设备SN") downlinkdev = models.CharField(max_length=32, verbose_name="下联设备SN") lastLogin = models.DateField(verbose_name="更新时间") deviceUser = models.CharField(max_length=32,verbose_name="设备使用人") lastLoginUser = models.IntegerField(verbose_name="操作员ID") deviceDes = models.CharField(max_length=64, verbose_name="设备描述") DataRoom = models.CharField(max_length=32, verbose_name="机房位置") Uplinkport = models.CharField(max_length=32,verbose_name="上联口") Downlinkport = models.CharField(max_length=32, verbose_name="下联口") Memory = models.CharField(max_length=32, verbose_name="内存占用率") Cpu = models.CharField(max_length=32, verbose_name="cpu占用率") Disk = models.CharField(max_length=32, verbose_name="磁盘占用率") isVirtual = models.CharField(max_length=32, verbose_name="是否虚拟化") isActive = models.CharField(max_length=32,verbose_name="是否上线") isNomal = models.CharField(max_length=32,verbose_name="状态是否正常")
class ImportForm(forms.Form):
    Device_type_list = (
        (1, "服务器"),
        (2, "网络设备"),
        (3, "安全设备"),
        (4, "虚拟机"),
        (5, "交换机")
    )
    #固定下拉选项,Device_type为post值,widget为下拉内容,下同。
    device_type = forms.IntegerField(
        widget = forms.Select(choices=Device_type_list)
    )
    #动态不定下拉选项
    Dataroom_list = forms.IntegerField(
    widget=forms.Select()
    )
    def __init__(self,*args,**kwargs):
        super(ImportForm,self).__init__(*args,**kwargs)
        #查询数据库获取下拉内容,根据主键basemodel_ptr_id查询获取name字段。并赋值给选项
        self.fields[Dataroom_list].widget.choices = Dataroom.objects.all().values_list(basemodel_ptr_id,name)

3、关于views
  这部分涉及到功能。下一部分详细记录。

 





以上是关于Django的cmdb探索与开发的主要内容,如果未能解决你的问题,请参考以下文章

Django之入门 CMDB系统 登录注销

Django之入门 CMDB系统 基础环境

Django之入门 CMDB系统 增删改查

Django之入门 CMDB系统 前后端分离之后端

python运维开发(二十五)---cmdb开发

CMDB项目开发