一.主机资产管理

一需求分析:
excel做公司内部资产记录如下:--但这样不方便
  资产编号, ip, 主机名, 使用者, os, mem, cpu, 上架时间, 过保时间 所以开发web版,可以主机进行增,删,改,查 1.添加:方法如下 (1)人手工录入: ssh ip hostname cat
/proc/cpuinfo | grep processor | wc -l 查到它是几核 cat /proc/meminfo | grep MemTotal 去内存文件看内存 (2)主机内部安装了程序 agent => server 定时上报数据(ip, hostname, os, mem, cpu) (3)写一程序扫描ip范围: ip, hostname, os
(4)手工变自动化---自动远程获取: 上述四种方式就是通过excel导入(把excel文件上传服务器进行解析入库)text
/csv/excel/xm => 导入 ip, hostname, os, mem, cpu (5)其他第三方系统(如阿里云机器) => 调用它们的API => 导入 2.删除: 人工删除 自动删除(与第三方系统结合) 3.改: 人工改 自动改 4.查: 自己查 提供数据 => API(别人把你当第三方系统,你给别人提供数据,别人调你的api) 删除, 查询 => 人工 添加, 修改 => 自动 修改 => 人工 定义model => table 资产编号, ip, 主机名, 使用者, os, arch, mem, cpu, disk, 上架时间, 过保时间, 在线状态

机制推: 每周三所有人把作业发给我 agent采集 => server 

拉: 每周三我找所有人要
  扫描(ansible)

 

 

 

 

 

 

 

 

 

 

 

 

 二.创建资产管理app

1. 创建app
(python36env) [[email protected] cmdb]$ python manage.py startapp asset
2. 启用app    
    settings INSTALLED_APPS asset.apps.AssetConfig
3. asset/urls.py
from django.urls import path
from . import views
app_name = 'asset'
urlpatterns = [
    path('', views.index, name='index'),
    path('list/ajax/', views.list_ajax, name='list_ajax'),
]
4. cmdb/urls.py根路由配置相关的url(/app名称/)分发给app
    path('asset/', include('asset.urls'))  把app的url映射包进来
5. asset/models.py
6. 迁移数据库
    python manage.py makemigrations
    python manage.py migrate

7.asset/views.py:

#encoding: utf-8
from django.http import JsonResponse
from django.shortcuts import render, redirect

from .models import Host

def index(request):
    if not request.session.get('user'):
        return redirect('user:login')
    return render(request, 'asset/index.html')


def list_ajax(request):
    if not request.session.get('user'):
        return JsonResponse({'code' : 403, 'result' : []})
    result= [host.as_dict() for host in Host.objects.all()]
    return JsonResponse({'code' : 200, 'result' : result})

8.asset/urls.py:

#encoding: utf-8
from django.urls import path
from . import views

app_name = 'asset'
urlpatterns = [
    #这里我是想访问根路径(http://127.0.0.1:9000/asset/)时直接请求此app的index,所以为空''
    path('', views.index, name='index'),
    path('list/ajax/', views.list_ajax, name='list_ajax'),
]

9.asset/models.py:

#encoding: utf-8

from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone
import datetime

#要继承自models.Model
class Host(models.Model):
    #这一块是我收集的:
    name = models.CharField(max_length=128, null=False, default='')
    ip = models.GenericIPAddressField(null=False, default='0.0.0.0')
    mac = models.CharField(max_length=32, null=False, default='')
    os = models.CharField(max_length=64, null=False, default='')
    arch = models.CharField(max_length=16, null=False, default='')
    mem = models.BigIntegerField(null=False, default=0)
    cpu = models.IntegerField(null=False, default=0)
    disk = models.CharField(max_length=512, null=False, default='{}')

    #这一块是要进行输入的:
    sn = models.CharField(max_length=128, null=False, default='')
    user = models.CharField(max_length=128, null=False, default='')
    remark = models.TextField(null=False, default='')
    purchase_time = models.DateTimeField(null=False)
    over_insurance_time = models.DateTimeField(null=False)

    #这一块是我应用程序用的:
    created_time = models.DateTimeField(null=False, auto_now_add=True)
    last_time = models.DateTimeField(null=False)

    @classmethod
    def create_or_replace(cls, ip, name, mac, os, arch, mem, cpu, disk):
        obj = None
        try:
            obj = cls.objects.get(ip=ip)
        except ObjectDoesNotExist as e:
            obj = cls()
            obj.ip = ip
            obj.purchase_time = timezone.now()
            obj.over_insurance_time = timezone.now()

        obj.name = name
        obj.mac = mac
        obj.os = os
        obj.arch = arch
        obj.mem = mem
        obj.cpu = cpu
        obj.disk = disk

        obj.last_time = timezone.now()
        obj.save()
        return obj

    def as_dict(self):
        rt = {}
        for k, v in self.__dict__.items():
            if isinstance(v, (int, float, bool, str, datetime.datetime)):
                rt[k] = v
        return rt

如下图迁移后就有了

 

猜你喜欢

转载自www.cnblogs.com/dbslinux/p/13401290.html