Skip to content

Commit

Permalink
cmdb完成,web界面模板化
Browse files Browse the repository at this point in the history
  • Loading branch information
guohongze committed Jan 30, 2017
1 parent e66bcb8 commit bf4b452
Show file tree
Hide file tree
Showing 1,228 changed files with 252,257 additions and 445 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

626 changes: 301 additions & 325 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions .idea/woss2.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmdb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def collect(request):
if req.POST:
vendor = req.POST.get('vendor')
group = req.POST.get('group')
disk = req.POST.get('disk')
cpu_model = req.POST.get('cpu_model')
cpu_num = req.POST.get('cpu_num')
memory = req.POST.get('memory')
Expand All @@ -38,6 +39,7 @@ def collect(request):
host.cpu_model = cpu_model
host.memory = int(memory)
host.sn = sn
host.disk = disk
host.os = osver
host.vendor = vendor
host.ip = ip
Expand Down
Binary file modified cmdb/api.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions cmdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __unicode__(self):

class Idc(models.Model):
name = models.CharField(max_length=30,null=True)
address = models.CharField(max_length=30,null=True)
address = models.CharField(max_length=100,null=True)
tel = models.CharField(max_length=30,null=True)
contact = models.CharField(max_length=30,null=True)

Expand All @@ -27,7 +27,7 @@ class Host(models.Model):
os = models.CharField(max_length=50,null=True)
vendor = models.CharField(max_length=30,null=True)
group = models.CharField(max_length=30,null=True)
cpu_model = models.CharField(max_length=30,null=True)
cpu_model = models.CharField(max_length=100,null=True)
cpu_num = models.IntegerField(null=True)
memory = models.IntegerField(null=True)
disk = models.CharField(max_length=255,null=True)
Expand Down
Binary file modified cmdb/models.pyc
Binary file not shown.
5 changes: 2 additions & 3 deletions cmdb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
app_name = 'cmdb'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^index1', views.index1, name='index1'),
url(r'^index2', views.index2, name='index2'),
url(r'^cmdb', views.cmdb, name='cmdb'),
url(r'^index3', views.index3, name='index3'),
url(r'^execl', views.execl, name='execl'),
url(r'^excel', views.excel, name='excel'),
url(r'^login', views.login, name='login'),
url(r'^collect', api.collect, name='update api'),
#url(r'^api/gethostsjson', api.gethostsjson, name='gethostsjson'),
Expand Down
Binary file modified cmdb/urls.pyc
Binary file not shown.
30 changes: 18 additions & 12 deletions cmdb/views.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from django.views import generic
from .models import Host
from django.http import HttpResponse
from django.shortcuts import render_to_response,redirect
import csv
import models
import sys
reload(sys)
sys.setdefaultencoding('utf8')


def index1(request):
return HttpResponse("Hello, world. You're at the polls index.")


def index2(request):
def cmdb(request):
hosts = Host.objects.all()
return render_to_response('cmdb/index.html', {'host_list':hosts})
return render_to_response('cmdb.html', {'host_list':hosts})


def index3(request):
host_list = Host.objects.all()
return render_to_response('cmdb/index.html',locals())
return render_to_response('index.html',locals())


class IndexView(generic.ListView):
template_name = 'cmdb/index.html'
template_name = 'index.html'
context_object_name = 'host_list'

def get_queryset(self):
return Host.objects.order_by('hostname')


def execl(request):
def excel(request):
host = Host.objects.all()
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
response['Content-Disposition'] = 'attachment; filename="cmdb.csv"'
writer = csv.writer(response)
writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])
writer.writerow(['HostName', 'IP ADDRESS', 'Group', 'Memory', 'Disk', 'CPU', 'Cpu Cores', 'OS', 'IDC'])
for h in host:
writer.writerow([h.hostname, h.ip, h.group, h.memory, h.disk, h.cpu_model, h.cpu_num, h.os, str(h.idc).encode('gb2312')])
return response

def __str__(self):
return self.name


def login(request):
ret = {}
Expand Down
Binary file modified cmdb/views.pyc
Binary file not shown.
Binary file modified db.sqlite3
Binary file not shown.
5 changes: 4 additions & 1 deletion plugins/get_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def getIpaddr():


def parserIpaddr(ipdata):
device = re.compile(r'^(eno\d{0,9})')
try:
device = re.compile(r'^(eth\d{0,9})')
except ImportError,e:
device = re.compile(r'^(eno\d{0,9})')
mac = re.compile(r'(ether\s[0-9A-Fa-f:]{17})')
ip = re.compile(r'inet ([\d.]{7,15})')
for lines in ipdata.split('\n\n'):
Expand Down
55 changes: 14 additions & 41 deletions post_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,12 @@
import urllib
import urllib2
import platform
import socket

def getIpaddr():
p = Popen(['ifconfig'],shell=False,stdout=PIPE)
stdout, stderr = p.communicate()
return stdout.strip()


def parserIpaddr(ipdata):
device = re.compile(r'^(eno\d{0,9})')
mac = re.compile(r'(ether\s[0-9A-Fa-f:]{17})')
ip = re.compile(r'inet ([\d.]{7,15})')
for lines in ipdata.split('\n\n'):
pd = {}
eth_device = re.search(device,lines)
hw = re.search(mac,lines)
ips = re.search(ip,lines)
if eth_device:
if eth_device:
Device = eth_device.groups()[0]
if hw:
Mac = hw.groups()[0].split()[1]
if ips:
Ip = ips.groups()[0]
pd['Device'] = Device
pd['Mac'] = Mac
pd['Ip'] = Ip
yield pd

def get_ip():
hostname = socket.getfqdn(socket.gethostname())
ipaddr = socket.gethostbyname(hostname)
return ipaddr

def getDMI():
p = Popen('dmidecode', stdout=PIPE, shell=True)
Expand All @@ -60,14 +38,15 @@ def getMemTotal():
p = Popen(cmd, stdout = PIPE, shell = True)
data = p.communicate()[0]
mem_total = data.split()[1]
return mem_total
memtotal = int(round(int(mem_total)/1024.0/1024.0, 0))
return memtotal

def getCpu():
cmd = "cat /proc/cpuinfo"
p = Popen(cmd, stdout = PIPE, stderr = PIPE, shell = True)
stdout, stderr = p.communicate()
return stdout

def parserCpu(stdout):
groups = [i for i in stdout.split('\n\n')]
group = groups[-2]
Expand All @@ -78,7 +57,6 @@ def parserCpu(stdout):
cpu_info[k] = v
return cpu_info


def getDiskInfo():
ret = {}
disk_dev = re.compile(r'Disk\s/dev/sd[a-z]{1}')
Expand Down Expand Up @@ -113,23 +91,18 @@ def parserDiskInfo(diskdata):

def postData(data):
postdata = urllib.urlencode(data)
req = urllib2.urlopen('http://192.168.47.141:8000/cmdb/collect',postdata)
req = urllib2.urlopen('http://192.168.47.1:8000/cmdb/collect',postdata)
req.read()
return True

def main():
data_info = {}
"""
data_info = {'ipaddrs':'192.168.3.123','memory':16,'cpu_model':'Intel','cpu_num':4,'sn':'R9NBEZA','vendor':'LENOVO','product':'ThinkPad X220','osver':'Fedora 16 x86_64','hostname':'Sibiao Luo'}
"""
memtotal = int(round(int(getMemTotal())/1024.0/1024.0, 0))
data_info['memory'] = memtotal
data_info['disk'] = parserDiskInfo()
data_info['memory'] = getMemTotal()
data_info['disk'] = parserDiskInfo(getDiskInfo())
cpuinfo = parserCpu(getCpu())
data_info['cpu_num'] = int(cpuinfo['processor']) + 1
data_info['cpu_model'] = cpuinfo['vendor_id']
for i in parserIpaddr(getIpaddr()):
data_info['ip'] = i['Ip']
data_info['cpu_num'] = cpuinfo['cpu cores']
data_info['cpu_model'] = cpuinfo['model name']
data_info['ip'] = get_ip()
data_info['sn'] = parserDMI(getDMI())['Serial Number']
data_info['vendor'] = parserDMI(getDMI())['Manufacturer']
data_info['product'] = parserDMI(getDMI())['Version']
Expand Down
Empty file removed python
Empty file.
10 changes: 0 additions & 10 deletions t.py

This file was deleted.

1 change: 1 addition & 0 deletions templates/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
3 changes: 3 additions & 0 deletions templates/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.6"
21 changes: 21 additions & 0 deletions templates/MIT-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 - Vincent Gabriel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit bf4b452

Please sign in to comment.