Skip to content

Commit

Permalink
新增GetSysData类
Browse files Browse the repository at this point in the history
CPU MEM数据获取函数变更
新增按时间维度查询监控数据
将获取监控数据拆分为独立文件
  • Loading branch information
guohongze committed Jun 20, 2017
1 parent 0aa527b commit 2d85f43
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 80 deletions.
5 changes: 3 additions & 2 deletions monitor/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

urlpatterns = [
url(r'^system/$', views.index, name='monitor'),
url(r'^system/(?P<hostname>.+)/$', views.host_info, name='host_info'),
url(r'^get/data/(?P<hostname>.+)/$', views.get_sys_data, name='get_sys_data'),
url(r'^system/(?P<hostname>.+)/(?P<timing>\d+)/$', views.host_info, name='host_info'),
url(r'^get/cpu/(?P<hostname>.+)/(?P<timing>\d+)/$', views.get_cpu, name='get_cpu'),
url(r'^get/mem/(?P<hostname>.+)/(?P<timing>\d+)/$', views.get_mem, name='get_mem'),
url(r'^received/sys/info/$', api.received_sys_info, name='received_sys_info'),
]
99 changes: 69 additions & 30 deletions monitor/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from django.shortcuts import render

# Create your views here.
Expand All @@ -10,6 +13,70 @@
import time
from config.views import get_dir

TIME_SECTOR = (
(str(1), 3600),
(str(2), 3600*3),
(str(3), 3600*5),
(str(4), 86400),
(str(5), 86400*3),
(str(6), 86400*7),
)


class GetSysData(object):

def __init__(self, hostname, monitor_item, timing):
self.hostname = hostname
self.monitor_item = monitor_item
self.timing = timing

def get_data(self):
mongodb_ip = get_dir("mongodb_ip")
mongodb_port = get_dir("mongodb_port")
client = MongoClient(mongodb_ip, int(mongodb_port))
db = client.sys_info
collection = db[self.hostname]
now_time = int(time.time())
find_time = now_time-self.timing
cursor = collection.find({'timestamp': {'$gte': find_time}}, {self.monitor_item: 1, "timestamp": 1})
return cursor


@login_required()
@permission_verify()
def get_cpu(request, hostname, timing):
data_time = []
cpu_percent = []
range_time = TIME_SECTOR[int(timing)]
cpu_data = GetSysData(hostname, "cpu", range_time[1])
for doc in cpu_data.get_data():
unix_time = doc['timestamp']
times = time.localtime(unix_time)
dt = time.strftime("%m%d-%H:%M", times)
data_time.append(dt)
c_percent = doc['cpu']['percent']
cpu_percent.append(c_percent)
data = {"data_time": data_time, "cpu_percent": cpu_percent}
return HttpResponse(json.dumps(data))


@login_required()
@permission_verify()
def get_mem(request, hostname, timing):
data_time = []
mem_percent = []
range_time = TIME_SECTOR[int(timing)]
mem_data = GetSysData(hostname, "mem", range_time[1])
for doc in mem_data.get_data():
unix_time = doc['timestamp']
times = time.localtime(unix_time)
dt = time.strftime("%m%d-%H:%M", times)
data_time.append(dt)
m_percent = doc['mem']['percent']
mem_percent.append(m_percent)
data = {"data_time": data_time, "mem_percent": mem_percent}
return HttpResponse(json.dumps(data))


@login_required()
@permission_verify()
Expand All @@ -21,35 +88,7 @@ def index(request):

@login_required()
@permission_verify()
def host_info(request, hostname):
def host_info(request, hostname, timing):
temp_name = "monitor/monitor-header.html"
return render_to_response("monitor/host_info.html", locals(), RequestContext(request))
return render_to_response("monitor/host_info_{}.html".format(timing), locals(), RequestContext(request))


@login_required()
@permission_verify()
def get_sys_data(request, hostname):
mongodb_ip = get_dir("mongodb_ip")
mongodb_port = get_dir("mongodb_port")
data_time = []
cpu_percent = []
memory_percent = []
client = MongoClient(mongodb_ip, int(mongodb_port))
db = client.sys_info
collection = db[hostname]
cursor = collection.find()
now = int(time.time())
for doc in cursor:
unix_time = doc['timestamp']
if unix_time >= now-3600:
times = time.localtime(unix_time)
dt = time.strftime("%m%d-%H:%M", times)
# get cpu data
c_percent = doc['cpu']['percent']
cpu_percent.append(c_percent)
data_time.append(dt)
# get mem data
m_percent = doc['mem']['percent']
memory_percent.append(m_percent)
data = {"data_time": data_time, "cpu_percent": cpu_percent, "memory_percent": memory_percent}
return HttpResponse(json.dumps(data))
Original file line number Diff line number Diff line change
@@ -1,46 +1,8 @@
{% extends 'base.html' %}
{% block self_head_css_js %}
<script src="/static/js/echarts.js"></script>
{% endblock %}
{% block content %}
<script>
window.onload=init;
function init() {
showhide(1);
}
function showhide(n) {
var box = document.getElementById("monitor");
box.className="active";
}
</script>
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
</section>
<!-- Main content -->
<section class="content">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">{{ hostname }} 监控信息</h3>
</div>
<div class="box-body">
<div id="cpu" style="width: 900px;height:300px;"></div>
<div id="memory" style="width: 900px;height:300px;"></div>

</div>
<!-- /.row -->
<!-- /.box-body -->
<div class="box-footer">
Footer
</div>
<!-- /.box-footer-->
</div>
<script type="text/javascript">
// 基于准备好的dom初始化echarts实例
var mycpu = echarts.init(document.getElementById('cpu'));
mycpu.showLoading();
setInterval(function () {
$.get("/monitor/get/data/{{ hostname }}").done(function (data) {
$.get("/monitor/get/cpu/{{ hostname }}/"+timing).done(function (data) {
var data = JSON.parse(data);
mycpu.hideLoading();// 加载动画
mycpu.setOption({
Expand Down Expand Up @@ -92,11 +54,10 @@ <h3 class="box-title">{{ hostname }} 监控信息</h3>
}, 5000);
</script>
<script type="text/javascript">
// 基于准备好的dom初始化echarts实例
var mymemory = echarts.init(document.getElementById('memory'));
mymemory.showLoading();
setInterval(function () {
$.get("/monitor/get/data/{{ hostname }}").done(function (data) {
$.get("/monitor/get/mem/{{ hostname }}/"+timing).done(function (data) {
var data = JSON.parse(data);
mymemory.hideLoading();// 加载动画
mymemory.setOption({
Expand Down Expand Up @@ -139,14 +100,10 @@ <h3 class="box-title">{{ hostname }} 监控信息</h3>
color: '#00a65a'
}
},
data: data.memory_percent
data: data.mem_percent
}
]
});
});
}, 5000);
</script>
</section>
<!-- /.content -->
</div>
{% endblock %}
</script>
62 changes: 62 additions & 0 deletions templates/monitor/host_info_0.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends 'base.html' %}
{% block self_head_css_js %}
<script src="/static/js/echarts.js"></script>
{% endblock %}
{% block content %}
<script>
window.onload=init;
function init() {
showhide(1);
}
function showhide(n) {
var box = document.getElementById("monitor");
box.className="active";
}
</script>
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
</section>
<!-- Main content -->
<section class="content">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">{{ hostname }} 监控信息</h3>
</div>
<div class="box-body">
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 0 %}"><li class="btn btn-default .btn-flat active">一小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 1 %}"><li class="btn btn-default .btn-flat">三小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 2 %}"><li class="btn btn-default .btn-flat">五小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 3 %}"><li class="btn btn-default .btn-flat">一天</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 4 %}"><li class="btn btn-default .btn-flat">三天</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 5 %}"><li class="btn btn-default .btn-flat">一周</li></a>
</div>
<div id="cpu" style="width: 800px;height:300px;"></div>
<div id="memory" style="width: 800px;height:300px;"></div>

</div>
<!-- /.row -->
<!-- /.box-body -->
<div class="box-footer">
</div>
<!-- /.box-footer-->
</div>
<script type="text/javascript">
var timing=0; //通过URL传递元组数值调用7天数据
</script>
{% include 'monitor/get_data.html' %}
</section>
<!-- /.content -->
</div>
{% endblock %}
62 changes: 62 additions & 0 deletions templates/monitor/host_info_1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends 'base.html' %}
{% block self_head_css_js %}
<script src="/static/js/echarts.js"></script>
{% endblock %}
{% block content %}
<script>
window.onload=init;
function init() {
showhide(1);
}
function showhide(n) {
var box = document.getElementById("monitor");
box.className="active";
}
</script>
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
</section>
<!-- Main content -->
<section class="content">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">{{ hostname }} 监控信息</h3>
</div>
<div class="box-body">
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 0 %}"><li class="btn btn-default .btn-flat">一小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 1 %}"><li class="btn btn-default .btn-flat active">三小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 2 %}"><li class="btn btn-default .btn-flat">五小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 3 %}"><li class="btn btn-default .btn-flat">一天</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 4 %}"><li class="btn btn-default .btn-flat">三天</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 5 %}"><li class="btn btn-default .btn-flat">一周</li></a>
</div>
<div id="cpu" style="width: 800px;height:300px;"></div>
<div id="memory" style="width: 800px;height:300px;"></div>

</div>
<!-- /.row -->
<!-- /.box-body -->
<div class="box-footer">
</div>
<!-- /.box-footer-->
</div>
<script type="text/javascript">
var timing=1; //通过URL传递元组数值调用7天数据
</script>
{% include 'monitor/get_data.html' %}
</section>
<!-- /.content -->
</div>
{% endblock %}
62 changes: 62 additions & 0 deletions templates/monitor/host_info_2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends 'base.html' %}
{% block self_head_css_js %}
<script src="/static/js/echarts.js"></script>
{% endblock %}
{% block content %}
<script>
window.onload=init;
function init() {
showhide(1);
}
function showhide(n) {
var box = document.getElementById("monitor");
box.className="active";
}
</script>
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
</section>
<!-- Main content -->
<section class="content">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">{{ hostname }} 监控信息</h3>
</div>
<div class="box-body">
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 0 %}"><li class="btn btn-default .btn-flat">一小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 1 %}"><li class="btn btn-default .btn-flat">三小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 2 %}"><li class="btn btn-default .btn-flat active">五小时</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 3 %}"><li class="btn btn-default .btn-flat">一天</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 4 %}"><li class="btn btn-default .btn-flat">三天</li></a>
</div>
<div class="btn-group btn-group-lg">
<a href="{% url 'host_info' hostname 5 %}"><li class="btn btn-default .btn-flat">一周</li></a>
</div>
<div id="cpu" style="width: 800px;height:300px;"></div>
<div id="memory" style="width: 800px;height:300px;"></div>

</div>
<!-- /.row -->
<!-- /.box-body -->
<div class="box-footer">
</div>
<!-- /.box-footer-->
</div>
<script type="text/javascript">
var timing=2; //通过URL传递元组数值调用7天数据
</script>
{% include 'monitor/get_data.html' %}
</section>
<!-- /.content -->
</div>
{% endblock %}
Loading

0 comments on commit 2d85f43

Please sign in to comment.