added memory json

This commit is contained in:
Ziver Koc 2015-04-16 20:42:31 +00:00
parent 0ddc98e730
commit 0a112d0068
38 changed files with 1582 additions and 175 deletions

View file

@ -24,6 +24,8 @@ package wa.server.plugin;
import zutil.parser.DataNode;
import java.util.Map;
/**
* Created by Ziver on 2015-04-06.
*/
@ -33,5 +35,5 @@ public interface WAStatus {
public String html();
public void jsonUpdate(DataNode root);
public void jsonUpdate(Map<String, String> request, DataNode root);
}

View file

@ -1,72 +0,0 @@
/*
* Copyright (c) 2015 Ziver
*
* 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.
*/
package wa.server.plugin.hwstatus;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.cmd.Shell;
import wa.server.plugin.WAStatus;
import zutil.io.file.FileUtil;
import zutil.parser.DataNode;
import java.io.IOException;
import java.text.DecimalFormat;
/**
* Created by Ziver on 2015-04-07.
*/
public class CpuStatus implements WAStatus {
@Override
public String getName() {
return "Cpu Load";
}
@Override
public String html() {
try {
return FileUtil.getContent(FileUtil.findURL("wa/server/plugin/hwstatus/CpuStatus.tmpl"));
} catch (IOException e) {
return e.getMessage();
}
}
@Override
public void jsonUpdate(DataNode root) {
DataNode cpuNode = new DataNode(DataNode.DataType.List);
try{
Sigar sigar = new Shell().getSigar();
CpuInfo cpu_info = sigar.getCpuInfoList()[0];
for(CpuPerc cpu : sigar.getCpuPercList()){
cpuNode.add(Math.round(cpu.getCombined() * 100.0) / 100.0 );
}
} catch (SigarException e) {
e.printStackTrace();
}
root.set("cpu", cpuNode);
}
}

View file

@ -1,98 +0,0 @@
<div class="col-md-12"><div class="panel panel-default">
<div class="panel-heading">Cpu Status</div>
<div class="panel-body">
<div><canvas id="cpu-chart" height="300"></canvas></div>
</div>
</div></div>
<div class="col-md-4"><div class="panel panel-default">
<div class="panel-heading">Memory</div>
<div class="panel-body">
<center><canvas id="mem-chart" height="300"></canvas></center>
</div>
</div></div>
<div class="col-md-8"><div class="panel panel-default">
<div class="panel-heading">Process List</div>
<div class="panel-body">
<div id="proc-list">
<table class="table"></table>
</div>
</div>
</div></div>
<script language="javascript" type="text/javascript">
$(function() {
updateCpuChart();
updateMemChart();
});
var count = 0;
var cpu_chart = null;
var cpu_data = {labels : [], datasets: []};
function updateCpuChart(){
$.getJSON("?i=0&json&type=cpu", function( data ) {
// Setup graph
if(cpu_chart == null){
// Fill in cpus
for(i=0; i<data['cpu'].length; ++i)
cpu_data['datasets'].push({
strokeColor: "rgba(220,220,220,1)",
//strokeColor: "rgba(151,187,205,1)",
data: []
});
// Graph
var ctx = $("#cpu-chart").get(0).getContext("2d");
cpu_chart = new Chart(ctx).Line(cpu_data, {
responsive: true,
maintainAspectRatio: false, // Fixes stuped behaviour with size
datasetFill: false, pointDot: false, showTooltips: false,
scaleOverride: true, scaleStartValue: 0, scaleStepWidth: 0.1, scaleSteps: 10, scaleIntegersOnly: false,
animation : false,
});
}
// Update graph
cpu_chart.addData(data['cpu'], "");
if(count > 30)
cpu_chart.removeData();
++count;
});
setTimeout(updateCpuChart, 1000);
}
var mem_chart = null;
var mem_data = [
{
value: 300,
color:"#46BFBD",
highlight: "#5AD3D1",
label: "Used"
},{
value: 50,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Cache"
},{
value: 100,
color: "#FFFFFF",
highlight: "#EEEEEE",
label: "Free"
}
]
function updateMemChart(){
$.getJSON("?i=0&json&type=mem", function( data ) {
if(mem_chart == null){
var ctx = $("#mem-chart").get(0).getContext("2d");
mem_chart = new Chart(ctx).Doughnut(mem_data, {
animateScale: true
});
}
});
setTimeout(updateMemChart, 5000);
}
</script>

View file

@ -0,0 +1,113 @@
/*
* Copyright (c) 2015 Ziver
*
* 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.
*/
package wa.server.plugin.hwstatus;
import org.hyperic.sigar.*;
import org.hyperic.sigar.cmd.Shell;
import wa.server.plugin.WAStatus;
import zutil.io.file.FileUtil;
import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType;
import java.io.IOException;
import java.util.Map;
/**
* Created by Ziver on 2015-04-07.
*/
public class HwStatus implements WAStatus {
@Override
public String getName() {
return "Cpu Load";
}
@Override
public String html() {
try {
return FileUtil.getContent(FileUtil.findURL("wa/server/plugin/hwstatus/HwStatus.tmpl"));
} catch (IOException e) {
return e.getMessage();
}
}
@Override
public void jsonUpdate(Map<String, String> request, DataNode root) {
try{
Sigar sigar = new Shell().getSigar();
if(request.containsKey("cpu")) {
DataNode cpuNode = new DataNode(DataType.List);
CpuInfo cpu_info = sigar.getCpuInfoList()[0];
for (CpuPerc cpu : sigar.getCpuPercList()) {
cpuNode.add(Math.round(cpu.getCombined() * 100.0) / 100.0);
}
root.set("cpu", cpuNode);
}
if(request.containsKey("memory")) {
DataNode memNode = new DataNode(DataType.Map);
Mem mem = sigar.getMem();
memNode.set("free", sizeByteToMB(mem.getFree()));
memNode.set("used", sizeByteToMB(mem.getUsed()));
root.set("memory", memNode);
DataNode swapNode = new DataNode(DataType.Map);
Swap swap = sigar.getSwap();
swapNode.set("free", sizeByteToMB(swap.getFree()));
swapNode.set("used", sizeByteToMB(swap.getUsed()));
root.set("swap", swapNode);
}
if(request.containsKey("proc")) {
DataNode procListNode = new DataNode(DataType.List);
long[] pids = sigar.getProcList();
//long[] pids = Shell.getPids(sigar, new String[0]);
for( long pid : pids ) {
DataNode procNode = new DataNode(DataType.Map);
procNode.set("pid", pid);
procNode.set("mem", sizeByteToMB(sigar.getProcMem(pid).getSize()));
try {
procNode.set("user", sigar.getProcCredName(pid).getUser());
procNode.set("cputime", sigar.getProcTime(pid).getTotal() / 1000);
ProcCpu cpu = sigar.getProcCpu(pid);
procNode.set("cpu", CpuPerc.format(cpu.getPercent()));
}catch(Exception e){}
procNode.set("cmd", ProcUtil.getDescription(sigar, pid) );
procListNode.add(procNode);
}
root.set("proc", procListNode);
}
} catch (SigarException e) {
e.printStackTrace();
}
}
/**
* Takes in a size of B and returns MB
*/
private static long sizeByteToMB(long size){
return size/(1024*1024);
}
}

View file

@ -0,0 +1,125 @@
<div class="col-md-12"><div class="panel panel-default">
<div class="panel-heading">Cpu Status</div>
<div class="panel-body">
<div><canvas id="cpu-chart" height="300"></canvas></div>
</div>
</div></div>
<div class="col-md-4"><div class="panel panel-default">
<div class="panel-heading">Memory</div>
<div class="panel-body">
<center><canvas id="mem-chart" height="300"></canvas></center>
<br>
<table class="table">
<tr><th></th><th>Used</th><th>Free</th></tr>
<tr><th><b>Memory</b></th><td>55</td><td>55</td></tr>
<tr><th><b>Swap</b></th><td>55</td><td>55</td></tr>
</table>
</div>
</div></div>
<div class="col-md-8"><div class="panel panel-default">
<div class="panel-heading">Process List</div>
<div class="panel-body">
<table id="proc-list" class="table table-hover small" data-sort-name="cpu" data-sort-order="desc">
<thead>
<tr>
<th data-field="pid" data-sortable="true"><b>PID</b></th>
<th data-field="user" data-sortable="true">User</th>
<th data-field="cpu" data-sortable="true">CPU</th>
<th data-field="cputime" data-sortable="true">CpuTime</th>
<th data-field="mem" data-sortable="true">Memory(MB)</th>
<th data-field="cmd" data-sortable="true">Proc</th>
</tr>
</thead>
</table>
</div>
</div></div>
<script language="javascript" type="text/javascript">
$(function() {
//updateCpuChart();
//updateMemChart();
updateProcTable();
});
var cpu_chart = null;
var cpu_data = {
labels : ["","","","","","","","","","",
"","","","","","","","","","",
"","","","","","","","","","",],
datasets: []
};
function updateCpuChart(){
$.getJSON("?i=0&json&cpu", function( data ) {
// Setup graph
if(cpu_chart == null){
// Fill in cpus
for(i=0; i<data['cpu'].length; ++i)
cpu_data['datasets'].push({
strokeColor: "rgba(151,187,205,1)",
fillColor: "rgba(151,187,205,0.2)",
data: [0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0]
});
// Graph
var ctx = $("#cpu-chart").get(0).getContext("2d");
cpu_chart = new Chart(ctx).Line(cpu_data, {
responsive: true,
maintainAspectRatio: false, // Fixes stuped behaviour with size
datasetFill: true, pointDot: false, showTooltips: false,
scaleOverride: true, scaleStartValue: 0, scaleStepWidth: 0.1, scaleSteps: 10, scaleIntegersOnly: false,
animation : false,
});
}
// Update graph
cpu_chart.addData(data['cpu'], "");
cpu_chart.removeData();
});
setTimeout(updateCpuChart, 1000);
}
var mem_chart = null;
var mem_data = [
{
value: 0,
color:"#46BFBD",
highlight: "#5AD3D1",
label: "Used"
},{
value: 1,
color: "#EEEEEE",
highlight: "#DDDDDD",
label: "Free"
}
];
function updateMemChart(){
$.getJSON("?i=0&json&memory", function( data ) {
if(mem_chart == null){
var ctx = $("#mem-chart").get(0).getContext("2d");
mem_chart = new Chart(ctx).Doughnut(mem_data, {
animateScale: true
});
}
mem_chart.segments[0].value = data.memory.used;
mem_chart.segments[1].value = data.memory.free;
mem_chart.update();
});
setTimeout(updateMemChart, 5000);
}
function updateProcTable(){
$.getJSON("?i=0&json&proc", function( data ) {
$('#proc-list').bootstrapTable({
data: data['proc']
});
});
setTimeout(updateProcTable, 10000);
}
</script>

View file

@ -2,8 +2,7 @@
"version": "1.0",
"name": "HW Status",
"interfaces": [
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.CpuStatus"},
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.MemStatus"},
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.HwStatus"},
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.HDDStatus"},
{"wa.server.plugin.WAStatus": "wa.server.plugin.hwstatus.NetStatus"}
]