您现在的位置是:首页 > 文章详情

Django 搭建CMDB系统完整[11](服务器)

日期:2018-09-06点击:292

templates/add_server.html edit_server.html search_server.html

<!DOCTYPE html>
<html>
<head>
<title>CMDB</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="/static/scripts/jquery/jquery-1.7.1.js"></script>
<link href="/static/style/authority/basic_layout.css" rel="stylesheet" type="text/css">
<link href="/static/style/authority/common_style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/scripts/authority/commonAll.js"></script>
<script type="text/javascript" src="/static/scripts/jquery/jquery-1.4.4.min.js"></script>
<script src="/static/scripts/My97DatePicker/WdatePicker.js" type="text/javascript" defer="defer"></script>
<script type="text/javascript" src="/static/scripts/artDialog/artDialog.js?skin=default"></script>
</head>
<body>
<form id="addserverform" name="addserverform" action="add_server.html" method="post">
<div id="container">
<div id="nav_links">
当前位置:服务器资产管理 > <span style="color: #1A5CC6;">新增服务器</span>
<div id="page_close">
<a href="javascript:parent.$.fancybox.close();">
<img src="/static/images/common/page_close.png" width="20" height="20" style="vertical-align: text-top;"/>
</a>
</div>
</div>
<div class="ui_content">
<table cellspacing="0" cellpadding="0" width="100%" align="left" border="0">
<tr>
<td class="ui_text_rt">主机名字</td>
<td class="ui_text_lt">
<input type="text" id="serverhostname" name="serverhostname" value="" class="ui_input_txt02" />
</td>
</tr>
<tr>
<td class="ui_text_rt">制造商/型号</td>
<td class="ui_text_lt">
<input type="text" id="servermodel" name="servermodel" value="" class="ui_input_txt02" />
</td>
</tr>
<tr>
<td class="ui_text_rt">IP</td>
<td class="ui_text_lt">
<input type="text" id="serverip" name="serverip" value="" class="ui_input_txt02" />
</td>
</tr>
<tr>
<td class="ui_text_rt">操作系统</td>
<td class="ui_text_lt">
<input type="text" id="serveros" name="serveros" value="" class="ui_input_txt02" />
</td>
</tr>

 <tr> <td class="ui_text_rt">CPU</td> <td class="ui_text_lt"> <input type="text" id="servercpu" name="servercpu" value="" class="ui_input_txt02" /> </td> </tr> <tr> <td class="ui_text_rt">内存</td> <td class="ui_text_lt"> <input type="text" id="servermemory" name="servermemory" value="" class="ui_input_txt02" /> </td> </tr> <tr> <td class="ui_text_rt">硬盘</td> <td class="ui_text_lt"> <input type="text" id="serverdisk" name="serverdisk" value="" class="ui_input_txt02" /> </td> </tr> <tr> 

</tr>

 <tr> <td class="ui_text_rt">状态</td> <td class="ui_text_lt"> <select name="serverstatus" id="serverstatus" class="ui_select01" > <option value=0>在线</option> <option value=1>已下线</option> <option value=2>未知</option> <option value=3>故障</option> <option value=4>备用</option> </select> </td> </tr> <tr> <td class="ui_text_rt">所属机房</td> <td class="ui_text_lt"> <select name="servermachinaroom" id="servermachinaroom" class="ui_select01"> {% for mr in machinaroomlist %} <option value={{ mr.id }}>{{ mr.name }}</option> {% endfor %} </select> </td> </tr> <tr> <td>&nbsp;</td> <td class="ui_text_lt"> &nbsp;<input id="submitbutton" type="submit" value="提交" class="ui_input_btn01"/> &nbsp;<input id="cancelbutton" type="cancel" value="取消" class="ui_input_btn01"/> </td> </tr> </table> </div> </div> 

</form>

</body>
</html>

<!DOCTYPE html>
<html>
<head>
<title>CMDB</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="/static/scripts/jquery/jquery-1.7.1.js"></script>
<link href="/static/style/authority/basic_layout.css" rel="stylesheet" type="text/css">
<link href="/static/style/authority/common_style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/scripts/authority/commonAll.js"></script>
<script type="text/javascript" src="/static/scripts/jquery/jquery-1.4.4.min.js"></script>
<script src="/static/scripts/My97DatePicker/WdatePicker.js" type="text/javascript" defer="defer"></script>
<script type="text/javascript" src="/static/scripts/artDialog/artDialog.js?skin=default"></script>
</head>
<body>
<form id="editserverform" name="editserverform" action="edit_server.html" method="post">
<div id="container">
<div id="nav_links">
当前位置:服务器资产管理 > <span style="color: #1A5CC6;">编辑服务器</span>
<div id="page_close">
<a href="javascript:parent.$.fancybox.close();">
<img src="/static/images/common/page_close.png" width="20" height="20" style="vertical-align: text-top;"/>
</a>
</div>
</div>
<div class="ui_content"><input style="visibility:hidden" value={{ server.id }} type="text" name="id" id="id" />
<table cellspacing="0" cellpadding="0" width="100%" align="left" border="0">
<tr>
<td class="ui_text_rt">主机名字</td>
<td class="ui_text_lt">
<input type="text" id="serverhostname" name="serverhostname" value={{ server.hostname }} class="ui_input_txt02" />
</td>
</tr>
<tr>
<td class="ui_text_rt">制造商/型号</td>
<td class="ui_text_lt">
<input type="text" id="servermodel" name="servermodel" value={{ server.model }} class="ui_input_txt02" />
</td>
</tr>
<tr>
<td class="ui_text_rt">IP</td>
<td class="ui_text_lt">
<input type="text" id="serverip" name="serverip" value={{ server.ip }} class="ui_input_txt02" />
</td>
</tr>
<tr>
<td class="ui_text_rt">操作系统</td>
<td class="ui_text_lt">
<input type="text" id="serveros" name="serveros" value={{ server.os }} class="ui_input_txt02" />
</td>
</tr>

 <tr> <td class="ui_text_rt">CPU</td> <td class="ui_text_lt"> <input type="text" id="servercpu" name="servercpu" value={{ server.cpu }} class="ui_input_txt02" /> </td> </tr> <tr> <td class="ui_text_rt">内存</td> <td class="ui_text_lt"> <input type="text" id="servermemory" name="servermemory" value={{ server.memory }} class="ui_input_txt02" /> </td> </tr> <tr> <td class="ui_text_rt">硬盘</td> <td class="ui_text_lt"> <input type="text" id="serverdisk" name="serverdisk" value={{ server.disk }} class="ui_input_txt02" /> </td> </tr> <tr> 

</tr>

 <tr> <td class="ui_text_rt">状态</td> <td class="ui_text_lt"> <select name="serverstatus" id="serverstatus" class="ui_select01" > <option value=0>在线</option> <option value=1>已下线</option> <option value=2>未知</option> <option value=3>故障</option> <option value=4>备用</option> </select> </td> </tr> <tr> <td class="ui_text_rt">所属机房</td> <td class="ui_text_lt"> <select name="servermachinaroom" id="servermachinaroom" class="ui_select01"> {% for mr in machinaroomlist %} <option value={{ mr.id }}>{{ mr.name }}</option> {% endfor %} </select> </td> </tr> <tr> <td>&nbsp;</td> <td class="ui_text_lt"> &nbsp;<input id="submitbutton" type="submit" value="提交" class="ui_input_btn01"/> &nbsp;<input id="cancelbutton" type="cancel" value="取消" class="ui_input_btn01"/> </td> </tr> </table> </div> </div> 

</form>

</body>
</html>

{% extends 'base.html' %}

{% block title %}
<script type="text/javascript" src="/static/scripts/jquery/jquery-1.7.1.js"></script>
<link href="/static/style/authority/basic_layout.css" rel="stylesheet" type="text/css">
<link href="/static/style/authority/common_style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/scripts/authority/commonAll.js"></script>
<script type="text/javascript" src="/static/scripts/fancybox/jquery.fancybox-1.3.4.js"></script>
<script type="text/javascript" src="/static/scripts/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" type="text/css" href="/static/style/authority/jquery.fancybox-1.3.4.css" media="screen"></link>
<script type="text/javascript" src="/static/scripts/artDialog/artDialog.js?skin=default"></script>

<div id="container">
<div class="ui_content">
<div class="ui_text_indent">
<div id="box_border">
<div id="box_top">搜索</div>
  机房名字  <input type="text" id="mname" name="mname" value="{{ mname }}" class="ui_input_txt03" />
  主机名字  <input type="text" id="sname" name="sname" value="{{ sname }}" class="ui_input_txt03" />
  ip地址  <input type="text" id="ip" name="ip" value="{{ ip }}" class="ui_input_txt03" />
  操作系统  <input type="text" id="os" name="os" value="{{ os }}" class="ui_input_txt03" />
  状态  
<select id="status" name="status" class="ui_select02">
<option value=100 selected = "selected">所有</option>
<option value=0 {% if status == 0 %} selected = "selected" {% endif %}>在线</option>
<option value=1 {% if status == 1 %} selected = "selected" {% endif %}>已下线</option>
<option value=2 {% if status == 2 %} selected = "selected" {% endif %}>未知</option>
<option value=3 {% if status == 3 %} selected = "selected" {% endif %}>故障</option>
<option value=4 {% if status == 4 %} selected = "selected" {% endif %}>备用</option>
</select>
</div>
<div id="box_bottom">
<div class="pagination">
<span class="current">
{% if serverlist.has_previous %}
<a href="javascript:void(0)" onclick="search_server({{ serverlist.previous_page_number }});">上一页</a>
{% endif %}

 <span class="current"> Page {{ serverlist.number }} of {{ serverlist.paginator.num_pages }}. </span> {% if serverlist.has_next %} <a href="javascript:void(0)" onclick="search_server({{ serverlist.next_page_number }});">下一页</a> {% endif %} </span> 

</div>
<input type="button" value="查询" class="ui_input_btn01" onclick="search_server(1);"/>
<input type="button" value="新增" class="ui_input_btn01" onclick="add_server();" />
<input type="button" value="删除" class="ui_input_btn01" onclick="batdel_server();" />
<input type="button" value="导出EXCEL" class="ui_input_btn01" onclick="excel_server();" />
</div>
</form>
</div>
</div>
{% endblock %}

{% block content %}

<div class="ui_content">
<div class="ui_tb">
<table class="table" cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tr>
<th width="30"><input type="checkbox" id="id" name="id" />
</th>
<th>主机名字</th>
<th>制造商/型号</th>
<th>IP</th>
<th>操作系统</th>
<th>CPU</th>
<th>内存</th>
<th>硬盘</th>
<th>状态</th>
<th>机房</th>
<th>操作</th>
</tr>
{% for s in serverlist.object_list %}
<tr>
<td><input type="checkbox" name="idcheck" value={{ s.id }} class="acb" /></td>
<td>{{ s.hostname }}</td>
<td>{{ s.model }}</td>
<td>{{ s.ip }}</td>
<td>{{ s.os }}</td>
<td>{{ s.cpu }}</td>
<td>{{ s.memory }}</td>
<td>{{ s.disk }}</td>
<td>{% if s.status == 0 %}
在线
{% elif s.status == 1 %}
已下线
{% elif s.status == 2 %}
未知
{% elif s.status == 3 %}
故障
{% elif s.status == 4 %}
备用
{% endif %}
</td>
<td>{{ s.machinaroom.name }}</td>
<td>
<a href="javascript:void(0)" onclick="edit_server('{{ s.id }}')" class="edit">编辑</a>
<a href="javascript:void(0)" onclick="del_server({{ s.id }},{{ serverlist.number }});">删除</a>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
<script type="text/javascript">
function search_server(page){
var mname = document.getElementById("mname").value;
var sname = document.getElementById("sname").value;
var ip= document.getElementById("ip").value;
var os= document.getElementById("os").value;
var status = document.getElementById("status").value;
var myurl="search_server.html"+"?"+"mname="+mname+"&&sname="+sname+"&&ip="+ip+"&&os="+os+"&&status="+status+"&&page="+page;
window.location.assign(encodeURI(myurl))
}
function add_server(){
var width = 400;
var height = 500;
var left = parseInt((screen.availWidth/2) - (width/2));//屏幕居中
var top = parseInt((screen.availHeight/2) - (height/2));
var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
newWindow = window.open("add_server.html", "subWind", windowFeatures);
}
function edit_server(id){
var width = 400;
var height = 500;
var left = parseInt((screen.availWidth/2) - (width/2));//屏幕居中
var top = parseInt((screen.availHeight/2) - (height/2));
var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
newWindow = window.open("edit_server.html?id="+id, "subWind", windowFeatures);
}
function del_server(id,page){
if(window.confirm('确定要删除该记录吗?')){
var text1= document.getElementById("mname").value;
var sname=document.getElementById("sname").value;
var ip=document.getElementById("ip").value;
var os=document.getElementById("os").value;
var status=document.getElementById("status").value;
var myurl="del_server.html"+"?"+"id="+id+"&&mname="+text1+"&&page="+page+"&&ip="+ip+"&&os="+os+"&&status="+status+"&&sname="+sname;
window.location.assign(encodeURI(myurl))
return true;
}else{
//alert("取消");
return false;
}
}
function batdel_server(){
if(window.confirm('确定要删除记录吗?')){
obj = document.getElementsByName("idcheck");
var text1= document.getElementById("mname").value;
var sname=document.getElementById("sname").value;
var ip=document.getElementById("ip").value;
var os=document.getElementById("os").value;
var status=document.getElementById("status").value;
check_val = [];
for(k in obj){
if(obj[k].checked)
check_val.push(obj[k].value);
}
var myurl="batdel_server.html"+"?"+"ids="+check_val+"&&mname="+text1+"&&ip="+ip+"&&os="+os+"&&status="+status+"&&sname="+sname;
window.location.assign(encodeURI(myurl))
return true;
}else{
return false;
}
}
function excel_server(){
var text1 = document.getElementById("mname").value;
var sname=document.getElementById("sname").value;
var ip=document.getElementById("ip").value;
var os=document.getElementById("os").value;
var status=document.getElementById("status").value;
var myurl="excel_server.html"+"?mname="+text1+"&&ip="+ip+"&&os="+os+"&&status="+status+"&&sname="+sname;
window.location.assign(encodeURI(myurl))
}
</script>
{% endblock %}

cmdbapp/serverviews.py

-- coding: utf-8 --

from future import unicode_literals

from django.shortcuts import render,render_to_response
from django.core.paginator import Paginator,InvalidPage,EmptyPage
from cmdbapp.models import *
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from xlwt import *
import StringIO

def search_server(request):
error = False
each_page = 8
mname=request.GET.get('mname','').strip()
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=int(request.GET.get('status',100))
if mname=='' and sname=='' and ip=='' and os=='' and status==100:
record_list = Server.objects.all()
paginator = Paginator(record_list,each_page)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
return render_to_response('search_server.html',{'serverlist':contacts,"mname":mname,"sname":sname,"ip":ip,"os":os,"status":status})
else:
q={}
if mname!='' and sname=='' and ip=='' and os=='' and status==100:
a=Machinaroom.objects.filter(name__icontains=mname).values("id")
q['machinaroom__in']=a
if sname!='':
q['hostname__icontains']=sname
if ip!='':
q['ip__icontains']=ip
if os!='':
q['os__icontains']=os
if status!=100:
q['status']=status
record_list = Server.objects.filter(**q)
paginator = Paginator(record_list,each_page)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
return render_to_response('search_server.html',{'serverlist':contacts,"mname":mname,"sname":sname,"ip":ip,"os":os,"status":status})

def add_server(request):
if request.method=='GET':
objs=Machinaroom.objects.all()
return render_to_response("add_server.html",{"machinaroomlist":objs})
else:
hostname=request.POST.get("serverhostname","")
model=request.POST.get("servermodel","")
ip=request.POST.get("serverip","")
os=request.POST.get("serveros","")
cpu=request.POST.get("servercpu","")
memory=request.POST.get("servermemory","")
disk=request.POST.get("serverdisk","")
status=request.POST.get("serverstatus",0)
machinaroomid=request.POST.get("servermachinaroom")
machinaroom=Machinaroom.objects.get(id=int(machinaroomid))
uu=Server(hostname=hostname,model=model,ip=ip,os=os,cpu=cpu,memory=memory,disk=disk,status=status,machinaroom=machinaroom)
uu.save()
return HttpResponseRedirect("add_server.html")
def edit_server(request):
if request.method=='GET':
obj=Server.objects.get(id=int(request.GET.get('id')))
objs=Machinaroom.objects.all()
return render_to_response('edit_server.html',{'server':obj,'machinaroomlist':objs})
else:
id=request.POST.get("id")
hostname=request.POST.get("serverhostname","")
model=request.POST.get("servermodel","")
ip=request.POST.get("serverip","")
os=request.POST.get("serveros","")
cpu=request.POST.get("servercpu","")
memory=request.POST.get("servermemory","")
disk=request.POST.get("serverdisk","")
status=request.POST.get("serverstatus",0)
machinaroomid=request.POST.get("servermachinaroom")
machinaroom=Machinaroom.objects.get(id=int(machinaroomid))
uu=Server.objects.filter(id=id).update(hostname=hostname,model=model,ip=ip,os=os,cpu=cpu,memory=memory,disk=disk,status=status,machinaroom=machinaroom)
return HttpResponseRedirect("edit_server.html?id="+id)
def del_server(request):
id=request.GET.get('id')
iid=int(id)
Server.objects.filter(id=iid).delete()
mname=request.GET.get('mname','').strip()
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=request.GET.get('status',100)
page=request.GET.get('page','1')
return HttpResponseRedirect('search_server.html?mname='+mname+"&&page="+page+"&&sname="+sname+"&&ip="+ip+"&&os="+os+"&&status="+status)
def batdel_server(request):
ids=request.GET.get('ids')
b=ids.split(',')
arr = map(int,b)
for aaa in arr:
Server.objects.filter(id=aaa).delete()
mname=request.GET.get('mname')
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=request.GET.get('status',100)
return HttpResponseRedirect('search_server.html?mname='+mname+"&&sname="+sname+"&&ip="+ip+"&&os="+os+"&&status="+status)
def excel_server(request):
mname=request.GET.get('mname','').strip()
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=int(request.GET.get('status',100))
if mname=='' and sname=='' and ip=='' and os=='' and status==100:
list_obj = Server.objects.all()
else:
q={}
if mname!='' and sname=='' and ip=='' and os=='' and status==100:
a=Machinaroom.objects.filter(name__icontains=mname).values("id")
q['machinaroom__in']=a
if sname!='':
q['hostname__icontains']=sname
if ip!='':
q['ip__icontains']=ip
if os!='':
q['os__icontains']=os
if status!=100:
q['status']=status
list_obj = Server.objects.filter(**q)
if list_obj:
# 创建工作薄
ws = Workbook(encoding='utf-8')
w = ws.add_sheet(u"服务器清单")
w.write(0, 0, "id")
w.write(0, 1, u"主机名字")
w.write(0, 2, u"制造商/型号")
w.write(0, 3, u"IP")
w.write(0, 4, u"操作系统")
w.write(0,5,u"CPU")
w.write(0,6,u"内存")
w.write(0,7,u"硬盘")
w.write(0,8,u"状态")
w.write(0,9,u"机房")
# 写入数据
excel_row = 1
for obj in list_obj:
data_id = obj.id
data_hostname = obj.hostname
data_model = obj.model
data_ip = obj.ip
data_os = obj.os
data_cpu=obj.cpu
data_memory=obj.memory
data_disk=obj.disk
if obj.status == 0:
data_status='在线'
elif obj.status == 1:
data_status='已下线'
elif obj.status == 2:
data_status='未知'
elif obj.status == 3:
data_status='故障'
else:
data_status='备用'
data_machinaroom=obj.machinaroom.name
w.write(excel_row, 0, data_id)
w.write(excel_row, 1, data_hostname)
w.write(excel_row, 2, data_model)
w.write(excel_row, 3, data_ip)
w.write(excel_row, 4, data_os)
w.write(excel_row, 5, data_cpu)
w.write(excel_row, 6, data_memory)
w.write(excel_row, 7, data_disk)
w.write(excel_row, 8, data_status)
w.write(excel_row, 9, data_machinaroom)
excel_row += 1
sio = StringIO.StringIO()
ws.save(sio)
sio.seek(0)
response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=服务器清单-'+mname+sname+ip+os+'.xls'
response.write(sio.getvalue())
return response

原文链接:https://yq.aliyun.com/articles/660784
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章