码迷,mamicode.com
首页 > 数据库 > 详细

Django SimpleCMDB API

时间:2019-02-04 09:03:57      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:linux   pen   span   []   members   tin   .sh   res   django   

编写一个API,当我们访问 http://192.168.216.128:8000/hostinfo/getjson 时,返回 json 格式的主机组和组成员信息:

[root@localhost SimpleCMDB]$ cat SimpleCMDB/urls.py
....
urlpatterns = patterns(‘‘,
    ....
    url(r^hostinfo/getjson/$, hostinfo.views.getjson),
)
[root@localhost SimpleCMDB]$ cat hostinfo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from hostinfo.models import Host, HostGroup
import json

....

def getjson(request): data = [] host_group = HostGroup.objects.all() for group in host_group: result = {groupname: group.group_name, members: []} for hosts in group.group_members.all(): hostinfo = {hostname: hosts.hostname, ip: hosts.ip, vendor: hosts.vendor, product: hosts.product, sn: hosts.sn, cpu_model: hosts.cpu_model, cpu_num: hosts.cpu_num, memory: hosts.memory, osver: hosts.osver} result[members].append(hostinfo) data.append(result) return HttpResponse(json.dumps(data))

技术图片

 

编写一个API,当我们访问 http://192.168.216.128:8000/hostinfo/getshell 时,返回 shell 格式的主机组和组成员信息(返回 shell 格式的信息,主要是为了我们能在 Linux 下使用 grep 、awk 等对返回的数据做进一步处理):

[root@localhost SimpleCMDB]$ cat SimpleCMDB/urls.py
....
urlpatterns = patterns(‘‘,
    ....
    url(r^hostinfo/getshell/$, hostinfo.views.getshell),
)
[root@localhost SimpleCMDB]$ cat hostinfo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from hostinfo.models import Host, HostGroup
import json

....def getshell(request):
    data = ‘‘
    host_group = HostGroup.objects.all()
    for group in host_group:
        groupname = group.group_name
        for hosts in group.group_members.all():
            hostname = hosts.hostname
            ip = hosts.ip
            vendor = hosts.vendor
            product = hosts.product
            sn = hosts.sn
            cpu_model = hosts.cpu_model
            cpu_num = hosts.cpu_num
            memory = hosts.memory
            osver = hosts.osver
            data += groupname +   + hostname +   + ip +   + osver +   + memory + \n

    return HttpResponse(data)

技术图片

 

 

 

 

 

 

 

 

 

    

Django SimpleCMDB API

标签:linux   pen   span   []   members   tin   .sh   res   django   

原文地址:https://www.cnblogs.com/pzk7788/p/10351404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!