收集主机信息到 cmdb

安在信息安全新媒体 2017-12-14

1、定义 URL 访问路径

[root@localhost simplecmdb]# vim simplecmdb/urls.py
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'simplecmdb.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^hostinfo/collect', 'hostinfo.views.collect'),    # 添加该URL
)

2、定义视图

[root@localhost simplecmdb]vim hostinfo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from hostinfo.models import Host

# Create your views here.

def collect(req):
    if req.POST:
        hostname = req.POST.get('hostname')
        ip = req.POST.get('ip')
        osver = req.POST.get('osver')
        vendor = req.POST.get('vendor')
        product = req.POST.get('product')
        cpu_model = req.POST.get('cpu_model')
        cpu_num = req.POST.get('cpu_num')
        memory = req.POST.get('memory')
        sn = req.POST.get('sn')

        host = Host()
        host.hostname= hostname
        host.ip = ip
        host.osver = osver
        host.vendor = vendor
        host.product = product
        host.cpu_model = cpu_model
        host.cpu_num = cpu_num
        host.memory = memory
        host.sn = sn
        host.save()
        return HttpResponse('OK')

    else:
        return HttpResponse('No Data')

3、通过 POST 方法传递数据

[root@localhost ~]# curl -d hostname='node02' -d ip='192.168.1.2' -d osver='CentOS 6.5' -d vendor='hp' -d product='BL 380' -d sn='1234xxx' -d cpu_model='Intel' -d cpu_num= -d memory='32G' http://192.168.5.131:8000/hostinfo/collect

查看是否插入了数据:

收集主机信息到 cmdb

相关推荐