创建两个views,一个显示CPU和内存信息;另一个现实进程信息
# Author: Allan # Datetime: 2019-05-21 import time import json from django.shortcuts import render from web import models # Create your views here. def show_procstat(request): servers = ['rotestZone01', 'rotestZone02'] procstat_ret = {} for server in servers: procstat_info = models.ProcstatInfo.objects.filter(host=server) procstat_exe = {} for row in procstat_info: temp_time = int(time.mktime(row.time.timetuple())) * 1000 key = str(row.exe) + '_' + str(int(row.pid)) if key not in procstat_exe: procstat_exe[key] = {} procstat_exe[key]['cpu_usage'] = [] procstat_exe[key]['memory_rss'] = [] procstat_exe[key]['cpu_usage'].append([temp_time, row.cpu_usage]) procstat_exe[key]['memory_rss'].append([temp_time, row.memory_rss]) procstat_ret[server] = procstat_exe return render(request, 'show_procstat.html', {"procstat_ret": json.dumps(procstat_ret)}) def show_servers(request): servers = ['rotestZone01', 'rotestZone02'] server_res = {} cpu_res = {} mem_res = {} for server in servers: cpu_info = models.CpuInfo.objects.filter(host=server) cpu_ret = {} usage_system = [] usage_user = [] usage_softirq = [] usage_iowait = [] for row in cpu_info: temp_time = int(time.mktime(row.time.timetuple())) * 1000 usage_system.append([temp_time, row.usage_system]) usage_user.append([temp_time, row.usage_user]) usage_softirq.append([temp_time, row.usage_softirq]) usage_iowait.append([temp_time, row.usage_iowait]) cpu_ret['usage_system'] = usage_system cpu_ret['usage_user'] = usage_user cpu_ret['usage_softirq'] = usage_softirq cpu_ret['usage_iowait'] = usage_iowait cpu_res[server] = cpu_ret mem_info = models.MemoryInfo.objects.filter(host=server) mem_ret = [] for row in mem_info: temp_time = int(time.mktime(row.time.timetuple()))*1000 mem_ret.append([temp_time, row.used_percent]) mem_res[server] = mem_ret server_res['cpu_res'] = cpu_res server_res['mem_res'] = mem_res return render(request, 'show_servers.html', {"server_res": json.dumps(server_res)})创建两个html模版,一个显示CPU和内存信息;另一个现实进程信息
<html xmlns="http://www.w3.org/1999/html"> <head> <meta charset="UTF-8" /> <title>服务器监控信息</title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/export-data.js"></script> <script language="JavaScript"> $(document).ready(function() { $("#servers").change(function (){ var selected = $(this).children('option:selected').val(); var server_res = jQuery.parseJSON('{{ server_res|safe }}'); var cpu_res = server_res['cpu_res'] var cpu_info = cpu_res[selected] Highcharts.setOptions({ global:{ useUTC: false } }); Highcharts.chart('cpu', { chart: { zoomType: 'x' }, title: { text: selected + ': CPU 信息' }, subtitle: { text: document.ontouchstart === undefined ? 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: '使用率 (%)' } }, legend: { enabled: false }, plotOptions: { area: { fillColor: { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [ [0, Highcharts.getOptions().colors[0]], [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')] ] }, marker: { radius: 2 }, lineWidth: 1, states: { hover: { lineWidth: 1 } }, threshold: null } }, series: [ { type: 'area', name: 'usage_system', data: cpu_info['usage_system'] }, { type: 'area', name: 'usage_user', data: cpu_info['usage_user'] }, { type: 'area', name: 'usage_softirq', data: cpu_info['usage_softirq'] }, { type: 'area', name: 'usage_iowait', data: cpu_info['usage_iowait'] }] }); var mem_res = server_res['mem_res'] var mem_info = mem_res[selected] Highcharts.chart('mem', { chart: { zoomType: 'x' }, title: { text: selected + ': 内存信息' }, subtitle: { text: document.ontouchstart === undefined ? 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: '使用率 (%)' } }, legend: { enabled: false }, plotOptions: { area: { fillColor: { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [ [0, Highcharts.getOptions().colors[0]], [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')] ] }, marker: { radius: 2 }, lineWidth: 1, states: { hover: { lineWidth: 1 } }, threshold: null } }, series: [{ type: 'area', name: 'used_percent', data: mem_info }] }); }); }); </script> </head> <body> <div><H2>服务器监控信息</H2></div> <select id="servers"> <option value="servers">请选择要监控的服务器</option> <option value="rotestZone01">rotestZone01</option> <option value="rotestZone02">rotestZone02</option> </select> <div id="cpu" style="width: 1560px; height: 400px; margin: 0 auto"></div> <div id="mem" style="width: 1560px; height: 400px; margin: 0 auto"></div> </body> </html> <html xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html"> <head> <meta charset="UTF-8" /> <title>服务器监控信息</title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/export-data.js"></script> <script language="JavaScript"> $(document).ready(function() { $("#servers").change(function (){ var selected = $(this).children('option:selected').val(); var server_res = jQuery.parseJSON('{{ procstat_ret|safe }}'); var xServers = server_res[selected] var obj=document.getElementById('processes'); if (obj.options.length != 1){ obj.options.length=1; } for(var key in xServers) { obj.options.add(new Option(key,key)); } $("#processes").change(function (){ var procSelected = $(this).children('option:selected').val(); var dataMem = new Array(); var dataCpu = new Array(); for(var key in xServers) { if (key == procSelected){ dataCpu = xServers[key]['cpu_usage'] dataMem = xServers[key]['memory_rss']; break; } } Highcharts.chart('cpu', { chart: { zoomType: 'x' }, title: { text: selected + ': CPU 信息' }, subtitle: { text: document.ontouchstart === undefined ? 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: '使用率 (%)' } }, legend: { enabled: false }, plotOptions: { area: { fillColor: { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [ [0, Highcharts.getOptions().colors[0]], [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')] ] }, marker: { radius: 2 }, lineWidth: 1, states: { hover: { lineWidth: 1 } }, threshold: null } }, series: [ { type: 'area', name: 'cpu_usage', data: dataCpu }] }); Highcharts.chart('mem', { chart: { zoomType: 'x' }, title: { text: selected + ': 内存信息' }, subtitle: { text: document.ontouchstart === undefined ? 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: '使用率 (%)' } }, legend: { enabled: false }, plotOptions: { area: { fillColor: { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [ [0, Highcharts.getOptions().colors[0]], [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')] ] }, marker: { radius: 2 }, lineWidth: 1, states: { hover: { lineWidth: 1 } }, threshold: null } }, series: [{ type: 'area', name: 'memory_rss', data: dataMem }] }); }); }); }); </script> </head> <body> <div><H2>服务器监控信息</H2></div> <div id="monitor"> <select id="servers"> <option value="server">请选择要监控的服务器</option> <option value="rotestZone01">rotestZone01</option> <option value="rotestZone02">rotestZone02</option> </select> <select id="processes"> <option value="process">请选择要监控的进程</option> </select> </div> <div id="cpu" style="width: 1560px; height: 400px; margin: 0 auto"></div> <div id="mem" style="width: 1560px; height: 400px; margin: 0 auto"></div> </body> </html>CPU和内存信息
进程信息