Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #17

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions application/admin/controller/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ public function index($uuid){
}
throw new HttpException(405, 'Method Not Allowed');
}
public function clear(){
//TODO RESTful API
if (!session('?is_login'))
throw new HttpException(403, 'Forbidden');

$count = SystemMonitor::clearInfo();
return json(['status' => 200, 'message' => "$count cleared"]);
}
public function purge(){
//TODO RESTful API
if (!session('?is_login'))
throw new HttpException(403, 'Forbidden');
SystemMonitor::deleteInfo();
return json(['status' => 200, 'message' => "ok"]);
}
}
37 changes: 36 additions & 1 deletion application/admin/view/index/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
<div class="mdui-toolbar mdui-color-theme">
<a href="{:url('/')}" class="mdui-typo-headline">{:lang("Server Monitor")}</a>
<div class="mdui-toolbar-spacer"></div>
<a href="{:url('admin/index/logout')}" class="mdui-btn mdui-btn-icon"><i class="mdui-icon material-icons">&#xe879;</i></a>
<span id="purge" class="mdui-btn mdui-btn-icon" title="Remove All">
<i class="mdui-icon material-icons">&#xe92b;</i>
</span>
<span id="clear" class="mdui-btn mdui-btn-icon" title="Remove Invalid">
<i class="mdui-icon material-icons">&#xe16c;</i>
</span>
<a href="{:url('admin/index/logout')}" class="mdui-btn mdui-btn-icon" title="Logout">
<i class="mdui-icon material-icons">&#xe879;</i>
</a>
</div>
</div>
<div class="mdui-container">
Expand Down Expand Up @@ -46,6 +54,33 @@
success: function(data){$('#ajax').html(data);mdui.mutation(); },
error: function (data, status, e){mdui.snackbar({message: e});}
});

$('#purge').on('click', function(){
mdui.confirm("Are you sure to purge all data?", function(){
mdui.prompt('Please input "YES" to procceed.',
function (value) {
if (value === 'YES'){
$.ajax({
url: "{:url('admin/info/purge')}", type: 'GET',
success: function (resp) {mdui.snackbar({message: 'Node deleted.', position: 'bottom'});reload_list();},
error: function (data, status, e) {mdui.snackbar({message: e, position: 'bottom'})}
});
} else {
mdui.snackbar({message: 'Cancelled.', position: 'bottom'});
}
}, function(){mdui.snackbar({message: 'Cancelled.', position: 'bottom'});});
});
});

$('#clear').on('click', function(){
mdui.confirm("Are you sure to clear invalid data?", function(){
$.ajax({
url: "{:url('admin/info/clear')}", type: 'GET',
success: function (resp) {mdui.snackbar({message: 'Cleared.', position: 'bottom'});reload_list();},
error: function (data, status, e) {mdui.snackbar({message: e, position: 'bottom'})}
});
});
});
</script>
</body>
</html>
9 changes: 5 additions & 4 deletions application/admin/view/index/index_ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{volist name="uuids" id="vo"}
<div class="mdui-list-item mdui-ripple mdui-p-l-0">
<span class="mdui-list-item-icon flag-icon flag-icon-{:empty($data[$key]['Country Code'])?'none':strtolower($data[$key]['Country Code'])}"></span>
<div class="mdui-list-item-content ajax-load" data-href="{:url('admin/info/index',['uuid'=>$key])}">
<div class="mdui-list-item-content ajax-load" onclick="mdui.alert('Name: {:empty($names[$key])?$vo:$names[$key]}<br>IP: {$vo}', 'Info');" data-href="{:url('admin/info/index',['uuid'=>$key])}">
<div class="mdui-list-item-title">
{:empty($names[$key])?$vo:$names[$key]}
</div>
Expand Down Expand Up @@ -61,18 +61,19 @@
var parent = $(this.parentNode.parentNode.parentNode), btn = $(this);
mdui.confirm("Are you sure to delete node: "+btn.attr("data-ip")+"?", function(){
$.ajax({
url: btn.attr("data-href"), 'type': 'DELETE', 'data': {},
url: btn.attr("data-href"), type: 'DELETE', data: {},
success: function (resp) {mdui.snackbar({message: 'Node deleted.', position: 'bottom'});parent.addClass("mdui-hidden");reload_list();},
error: function (data, status, e) {mdui.snackbar({message: e, position: 'bottom'})}
});
});
});

$('.rename').on('click', function(){
var parent = $(this.parentNode.parentNode.parentNode), btn = $(this);
mdui.prompt('Renaming node',
function (value){
$.ajax({
url: btn.attr("data-href"), 'type': 'PATCH', 'data': {'rename': value},
url: btn.attr("data-href"), type: 'PATCH', data: {'rename': value},
success: function (resp) {mdui.snackbar({message: 'Node renamed.', position: 'bottom'});reload_list();},
error: function (data, status, e) {mdui.snackbar({message: e, position: 'bottom'})}
});
Expand All @@ -83,7 +84,7 @@
var message = 'Hide.', btn = $(this), display = this.checked;
if (display){message = 'Display.'}
$.ajax({
url: btn.attr("data-href"), 'type': 'PATCH', 'data': {'display': display?1:0},
url: btn.attr("data-href"), type: 'PATCH', data: {'display': display?1:0},
success: function (resp) {mdui.snackbar({message: message, position: 'bottom'});},
error: function (data, status, e) {mdui.snackbar({message: e, position: 'bottom'});reload_list();}
});
Expand Down
27 changes: 27 additions & 0 deletions application/api/controller/Ping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace app\api\controller;


use app\common\lib\SystemMonitor;
use think\Controller;
use think\Exception;
use think\Response;

class Ping extends Controller
{
protected $middleware = ['FlowControl', 'CheckUUID', 'CheckJson'];

/**
* 显示资源列表
*
* @param string $uuid
* @return Response
* @throws Exception
*/
public function get($uuid = '')
{
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($uuid), 'Ping'));
}
}
69 changes: 55 additions & 14 deletions application/common/lib/SystemMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ static public function fetchIPInfo($ip)

static public function getUUIDs()
{
return Cache::remember('system_monitor:hashes', function () {
$data = Cache::remember('system_monitor:hashes', function () {
return Cache::store('redis')->handler()
->hGetAll("system_monitor:hashes");
}, self::$cacheTime);

return !empty($data)?$data:[];
}

static public function setUUID($uuid, $ip)
Expand Down Expand Up @@ -182,16 +184,34 @@ static public function collectionFormat($data, $name)
$value = [];

switch (strtolower($name)) {
case "thermal":
case "battery":
case "load":
case "ping":
case "thermal":
//in case of missing mounting point periodically.
$sensor_labels = [];
foreach ($data as $data_json => $_) {
$data_decode = json_decode($data_json, true);

if (!array_key_exists($name, $data_decode))
continue;

$sensor_labels = array_unique(array_merge($sensor_labels, array_keys(json_decode($data_json, true)[$name])));
}

foreach ($data as $data_json => $data_time) {
$sensors = json_decode($data_json, true)[$name];
if (count($sensors) > 0)
if (count($sensor_labels) > 0)
$time[] = date('m-d H:i', $data_time);

$data_decode = json_decode($data_json, true);

if (!array_key_exists($name, $data_decode))
continue;

$sensors = json_decode($data_json, true)[$name];
//extract $name from collection.
foreach ($sensors as $sensor => $sensor_value)
$value[$sensor][] = $sensor_value;
foreach ($sensor_labels as $_ => $sensor_label)
$value[$sensor_label][] = empty($sensors[$sensor_label])?-1:$sensors[$sensor_label];
}
break;

Expand All @@ -206,12 +226,9 @@ static public function collectionFormat($data, $name)
$time[] = date('m-d H:i', $data_time);
$disk_data = json_decode($data_json, true)[$name];

foreach ($mount_points as $_ => $mount_point) {
if (empty($disk_data[$mount_point]))
$value[$mount_point][] = 0;
else
$value[$mount_point][] = $disk_data[$mount_point]['used'];
}
foreach ($mount_points as $_ => $mount_point)
$value[$mount_point][] = empty($disk_data[$mount_point])?0:$value[$mount_point][] = $disk_data[$mount_point]['used'];

}
break;
case "network":
Expand Down Expand Up @@ -258,9 +275,18 @@ static public function getIPByUUID($uuid)
return $uuids[$uuid];
}

static public function deleteInfo($uuid)
static public function deleteInfo($uuid = "")
{
$ip = SystemMonitor::getIPByUUID($uuid);
if (empty($uuid)){
Cache::rm("system_monitor:collection");
Cache::rm("system_monitor:info");
Cache::rm("system_monitor:hashes");
Cache::store('redis')->rm("system_monitor:collection");
Cache::store('redis')->rm("system_monitor:info");
Cache::store('redis')->rm("system_monitor:hashes");
return;
}

Cache::rm("system_monitor:collection:$uuid");
Cache::rm("system_monitor:info:$uuid");
Cache::rm("system_monitor:hashes");
Expand All @@ -269,6 +295,21 @@ static public function deleteInfo($uuid)
Cache::store('redis')->handler()->hDel("system_monitor:hashes", $uuid);
}

static public function clearInfo()
{
$count = 0;
$uuids = self::getUUIDs();

foreach ($uuids as $uuid => $_) {
if(empty(self::getCollection($uuid))) {
self::deleteInfo($uuid);
$count++;
}
}

return $count;
}

static public function setHide($uuid, $hide)
{
if (!empty($hide))
Expand Down
75 changes: 68 additions & 7 deletions application/index/view/index/info_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ <h4>{:lang("IPV6")}</h4>
<p>{$info['IPV6']|default='None'}</p>
<h4>{:lang("Location")}</h4>
<p class="mdui-valign"><span class="mdui-list-item-icon flag-icon flag-icon-{:empty($info['Country Code'])?'none':strtolower($info['Country Code'])}"></span>{$info['Country']}</p>
<h4>{:lang("Agent Version")}</h4>
<p>{$info['Agent Version']|default='N/A'}</p>
</div>
<div class="mdui-col-xs-12 mdui-col-md-6">
<h4>{:lang("Process")}</h4>
Expand Down Expand Up @@ -98,7 +100,7 @@ <h4>{:lang("Update Time")}</h4>
</div>
<div class="mdui-panel-item-body">
<div class="mdui-row">
<div class="mdui-col-lg-3" style="overflow: scroll;max-height: 40vh;">
<div class="mdui-col-lg-3" style="overflow: scroll;">
{volist name="latest['Disk']" id="row"}
<div class="mdui-row">
<h4>{$key}</h4>
Expand Down Expand Up @@ -183,6 +185,21 @@ <h4>{:lang('Swap')}</h4>
</div>
</div>
</div>
<div class="mdui-panel-item mdui-panel-item-open">
<div class="mdui-panel-item-header">
<i class="mdui-icon material-icons" style="color: #900088;">&#xe01b;</i>
<div class="mdui-panel-item-title">{:lang("Ping")}</div>
<div class="mdui-panel-item-summary">{:lang("Ping View")}</div>
<i class="mdui-panel-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
</div>
<div class="mdui-panel-item-body">
<div class="mdui-col-xs-12">
<div id="ping-collection" class="graph">
<div class="mdui-spinner"></div>
</div>
</div>
</div>
</div>
</div>
<script>
var bar_config = {
Expand Down Expand Up @@ -245,7 +262,7 @@ <h4>{:lang('Swap')}</h4>
]
};

ctx_network = echarts.init(document.getElementById('network-collection'), null, {height: window.innerHeight * 0.5});
ctx_network = echarts.init(document.getElementById('network-collection'), null, {height: window.innerHeight * 0.6});
ctx_network.setOption(option);
},
error: function () {
Expand Down Expand Up @@ -282,7 +299,7 @@ <h4>{:lang('Swap')}</h4>
});
});

ctx_disk = echarts.init(document.getElementById('disk-collection'), null, {height: window.innerHeight * 0.5});
ctx_disk = echarts.init(document.getElementById('disk-collection'), null, {height: window.innerHeight * 0.6});
ctx_disk.setOption(option);
},
error: function () {document.getElementById('disk-collection').parentNode.innerHTML="Fail to load storage data.";}
Expand Down Expand Up @@ -389,7 +406,7 @@ <h4>{:lang('Swap')}</h4>
});
});

ctx_thermal = echarts.init(document.getElementById('thermal-collection'), null, {height: window.innerHeight * 0.5});
ctx_thermal = echarts.init(document.getElementById('thermal-collection'), null, {height: window.innerHeight * 0.6});
ctx_thermal.setOption(option);
},
error: function () {document.getElementById('thermal-collection').parentNode.innerHTML="Fail to load sensor data.";}
Expand Down Expand Up @@ -430,17 +447,61 @@ <h4>{:lang('Swap')}</h4>
});
});

ctx_battery = echarts.init(document.getElementById('battery-collection'), null, {height: window.innerHeight * 0.5});
ctx_battery = echarts.init(document.getElementById('battery-collection'), null, {height: window.innerHeight * 0.4});
ctx_battery.setOption(option);
},
error: function () {document.getElementById('battery-collection').parentNode.innerHTML="Fail to load sensor data.";}
});



$.ajax({
url: "{:url('api/ping/get', ['uuid' => $uuid])}", method: "get", dataType: 'json',
async: true,
success: function(e){
if (e.value.length === 0){
document.getElementById('ping-collection').innerHTML="No Ping View Available.";
return 0;
}

let option = {
tooltip: {trigger: 'axis'},
title: {left: 'center', text: '{:lang("Ping Sensor")}'},
xAxis: {type: 'category', boundaryGap: false, data: e.time},
yAxis: {
type: 'value',
axisLabel: {formatter: '{value} ms'}
},
dataZoom: [{type: 'inside', start: 70, end: 100}, {
start: 90, end: 100, handleSize: '80%',
handleStyle: {
color: '#fff', shadowBlur: 3, shadowColor: 'rgba(0, 0, 0, 0.6)',
shadowOffsetX: 2, shadowOffsetY: 2
}}],
series:[],
legend: {top: 30}
};

Object.keys(e.value).forEach(function(k){
option.series.push({
name: k, type: 'line', smooth: true, symbol: 'none', sampling: 'average',
areaStyle: {}, data: e.value[k],
});
});

ctx_ping = echarts.init(document.getElementById('ping-collection'), null, {height: window.innerHeight * 0.8});
ctx_ping.setOption(option);
},
error: function () {document.getElementById('ping-collection').parentNode.innerHTML="Fail to load ping view data.";}
});

window.onresize = function() {
ctx_battery && ctx_battery.resize();
ctx_cpu && ctx_cpu.resize();
ctx_disk && ctx_disk.resize();
ctx_mem && ctx_mem.resize();
ctx_thermal && ctx_thermal.resize();
ctx_network && ctx_network.resize();
ctx_battery && ctx_battery.resize();
ctx_ping && ctx_ping.resize();
ctx_thermal && ctx_thermal.resize();
};
</script>
Loading
Loading