Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
1. Using 32 bit UUID to identify each machine but not the IP address.
2. add some icons color.
3. fix "divided by zero" error when no data show.
  • Loading branch information
LittleJake committed Dec 16, 2021
1 parent fc821bf commit 85223f6
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 113 deletions.
4 changes: 1 addition & 3 deletions application/api/controller/Cpu.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Cpu extends Controller
*/
public function get($token = '')
{
$ip = SystemMonitor::getIPByHash($token);

return json(SystemMonitor::collectionFormat(SystemMonitor::getCpuCollection($ip)));
return json(SystemMonitor::collectionFormat(SystemMonitor::getCpuCollection($token)));
}
}
6 changes: 2 additions & 4 deletions application/api/controller/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ class Memory extends Controller
*/
public function get($token = '')
{
$ip = SystemMonitor::getIPByHash($token);

return json([
'swap' => SystemMonitor::collectionFormat(SystemMonitor::getSwapCollection($ip)),
'memory' => SystemMonitor::collectionFormat(SystemMonitor::getMemoryCollection($ip))
'swap' => SystemMonitor::collectionFormat(SystemMonitor::getSwapCollection($token)),
'memory' => SystemMonitor::collectionFormat(SystemMonitor::getMemoryCollection($token))
]);
}
}
5 changes: 2 additions & 3 deletions application/api/controller/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ class Network extends Controller
*/
public function get($token = '')
{
$ip = SystemMonitor::getIPByHash($token);
return json([
'RX' => SystemMonitor::networkFormat(SystemMonitor::getNetworkRXCollection($ip)),
'TX' => SystemMonitor::networkFormat(SystemMonitor::getNetworkTXCollection($ip))
'RX' => SystemMonitor::networkFormat(SystemMonitor::getNetworkRXCollection($token)),
'TX' => SystemMonitor::networkFormat(SystemMonitor::getNetworkTXCollection($token))
]);
}
}
3 changes: 1 addition & 2 deletions application/api/controller/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Storage extends Controller
*/
public function get($token = '')
{
$ip = SystemMonitor::getIPByHash($token);
return json(SystemMonitor::diskFormat(SystemMonitor::getDiskCollection($ip)));
return json(SystemMonitor::diskFormat(SystemMonitor::getDiskCollection($token)));
}
}
4 changes: 1 addition & 3 deletions application/api/controller/Thermal.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class Thermal extends Controller
*/
public function get($token = '')
{
$ip = SystemMonitor::getIPByHash($token);

return json(SystemMonitor::collectionFormat(SystemMonitor::getThermalCollection($ip)));
return json(SystemMonitor::collectionFormat(SystemMonitor::getThermalCollection($token)));
}
}
24 changes: 18 additions & 6 deletions application/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@
// 应用公共文件
function getIcon($desp = ''){
$url = "https://cdn.jsdelivr.net/npm/simple-icons@v3/icons/";
$icon = ['redhat', 'centos', 'ubuntu', 'debian', 'windows', 'intel', 'amd',
'qemu', 'linux', 'android', 'qualcomm', 'mediatek'];
$icon = [
'redhat' => "#EE0000",
'centos' => "#262577",
'ubuntu' => "#E95420",
'debian' => "#A81D33",
'windows' => "#0078D6",
'intel' => "#0071C5",
'amd' => "#ED1C24",
'qemu' => "#FF6600",
'linux' => "#FCC624",
'android' => "#3DDC84",
'qualcomm' => "#3253DC",
'mediatek' => "#EC9430"
];

foreach ($icon as $v)
if(stristr($desp, $v))
return $url."$v.svg";
foreach ($icon as $k => $v)
if(stristr($desp, $k))
return [$k, $url."$k.svg", $v];

return $url."linux.svg";
return ['linux', $url."linux.svg", $icon['linux']];
}
143 changes: 72 additions & 71 deletions application/common/lib/SystemMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SystemMonitor
{
static private $url = "http://ip-api.com/json/";
static public function fetchIPInfo($ip){
$info = [];
if(is_array($ip)){
foreach ($ip as $v){
$tmp = !empty(strstr($v, '/', true))?strstr($v, '/', true):$v;
Expand Down Expand Up @@ -51,9 +52,10 @@ static public function getHashes(){
return $hash;
}

static public function getStat($ip) {
if(is_array($ip)){
foreach ($ip as $v){
static public function getStat($hash) {
$stat = [];
if(is_array($hash)){
foreach ($hash as $v){
if(Cache::has("system_monitor:stat:$v")
&& !empty(Cache::get("system_monitor:stat:$v"))){
$stat[$v] = Cache::get("system_monitor:stat:$v");
Expand All @@ -65,22 +67,23 @@ static public function getStat($ip) {
}
}
}else{
if(Cache::has("system_monitor:stat:$ip")
&& !empty(Cache::get("system_monitor:stat:$ip"))){
$stat = Cache::get("system_monitor:stat:$ip");
if(Cache::has("system_monitor:stat:$hash")
&& !empty(Cache::get("system_monitor:stat:$hash"))){
$stat = Cache::get("system_monitor:stat:$hash");
} else{
$stat = json_decode(Cache::store('redis')->handler()
->get("system_monitor:stat:$ip"),true);
Cache::set("system_monitor:stat:$ip", $stat);
->get("system_monitor:stat:$hash"),true);
Cache::set("system_monitor:stat:$hash", $stat);
}
}

return $stat;
}

static public function getInfo($ip) {
if(is_array($ip)){
foreach ($ip as $v){
static public function getInfo($hash) {
$info = [];
if(is_array($hash)){
foreach ($hash as $v){
if(Cache::has("system_monitor:info:$v")
&& !empty(Cache::get("system_monitor:info:$v"))){
$info[$v] = Cache::get("system_monitor:info:$v");
Expand All @@ -92,13 +95,13 @@ static public function getInfo($ip) {
}
}
}else{
if(Cache::has("system_monitor:info:$ip")
&& !empty(Cache::get("system_monitor:info:$ip"))){
$info = Cache::get("system_monitor:info:$ip");
if(Cache::has("system_monitor:info:$hash")
&& !empty(Cache::get("system_monitor:info:$hash"))){
$info = Cache::get("system_monitor:info:$hash");
} else{
$info = Cache::store('redis')->handler()
->hGetAll("system_monitor:info:$ip");
Cache::set("system_monitor:info:$ip", $info);
->hGetAll("system_monitor:info:$hash");
Cache::set("system_monitor:info:$hash", $info);
}
}

Expand Down Expand Up @@ -141,98 +144,98 @@ static private function curl_get($url){
return $data;
}

static public function getCpuCollection($ip){
static public function getCpuCollection($hash){
$time = time();
if(!Cache::has("system_monitor:collection:cpu:$ip")){
if(!Cache::has("system_monitor:collection:cpu:$hash")){
$data = Cache::store('redis')->handler()
->zRangeByScore("system_monitor:collection:cpu:$ip", 0, $time
->zRangeByScore("system_monitor:collection:cpu:$hash", 0, $time
, ['withscores' => TRUE]);
Cache::set("system_monitor:collection:cpu:$ip",$data,150);
Cache::set("system_monitor:collection:cpu:$hash",$data,150);
} else {
$data = Cache::get("system_monitor:collection:cpu:$ip");
$data = Cache::get("system_monitor:collection:cpu:$hash");
}
return $data;
}

static public function getSwapCollection($ip){
static public function getSwapCollection($hash){
$time = time();
if(!Cache::has("system_monitor:collection:swap:$ip")){
if(!Cache::has("system_monitor:collection:swap:$hash")){
$data = Cache::store('redis')->handler()
->zRangeByScore("system_monitor:collection:swap:$ip", 0, $time
->zRangeByScore("system_monitor:collection:swap:$hash", 0, $time
, ['withscores' => TRUE]);
Cache::set("system_monitor:collection:swap:$ip",$data,150);
Cache::set("system_monitor:collection:swap:$hash",$data,150);
} else {
$data = Cache::get("system_monitor:collection:swap:$ip");
$data = Cache::get("system_monitor:collection:swap:$hash");
}
return $data;
}
static public function getThermalCollection($ip){
static public function getThermalCollection($hash){
$time = time();

if(!Cache::has("system_monitor:collection:thermal:$ip")){
if(!Cache::has("system_monitor:collection:thermal:$hash")){
$data = Cache::store('redis')->handler()
->zRangeByScore("system_monitor:collection:thermal:$ip", 0, $time
->zRangeByScore("system_monitor:collection:thermal:$hash", 0, $time
, ['withscores' => TRUE]);
Cache::set("system_monitor:collection:thermal:$ip",$data,150);
Cache::set("system_monitor:collection:thermal:$hash",$data,150);
} else {
$data = Cache::get("system_monitor:collection:thermal:$ip");
$data = Cache::get("system_monitor:collection:thermal:$hash");
}
return $data;
}
static public function getMemoryCollection($ip){
static public function getMemoryCollection($hash){
$time = time();

if(!Cache::has("system_monitor:collection:memory:$ip")){
if(!Cache::has("system_monitor:collection:memory:$hash")){
$data = Cache::store('redis')->handler()
->zRangeByScore("system_monitor:collection:memory:$ip", 0, $time
->zRangeByScore("system_monitor:collection:memory:$hash", 0, $time
, ['withscores' => TRUE]);
Cache::set("system_monitor:collection:memory:$ip",$data,150);
Cache::set("system_monitor:collection:memory:$hash",$data,150);
} else {
$data = Cache::get("system_monitor:collection:memory:$ip");
$data = Cache::get("system_monitor:collection:memory:$hash");
}
return $data;
}

static public function getDiskCollection($ip){
static public function getDiskCollection($hash){
$time = time();

if(!Cache::has("system_monitor:collection:disk:$ip")){
if(!Cache::has("system_monitor:collection:disk:$hash")){
$data = Cache::store('redis')->handler()
->zRangeByScore("system_monitor:collection:disk:$ip", 0, $time
->zRangeByScore("system_monitor:collection:disk:$hash", 0, $time
, ['withscores' => TRUE]);
Cache::set("system_monitor:collection:disk:$ip",$data,150);
Cache::set("system_monitor:collection:disk:$hash",$data,150);
} else {
$data = Cache::get("system_monitor:collection:disk:$ip");
$data = Cache::get("system_monitor:collection:disk:$hash");
}

return $data;
}

static public function getNetworkRXCollection($ip){
static public function getNetworkRXCollection($hash){
$time = time();

if(!Cache::has("system_monitor:collection:network:RX:$ip")){
if(!Cache::has("system_monitor:collection:network:RX:$hash")){
$data = Cache::store('redis')->handler()
->zRangeByScore("system_monitor:collection:network:RX:$ip", 0, $time
->zRangeByScore("system_monitor:collection:network:RX:$hash", 0, $time
, ['withscores' => TRUE]);
Cache::set("system_monitor:collection:network:RX:$ip",$data,150);
Cache::set("system_monitor:collection:network:RX:$hash",$data,150);
} else {
$data = Cache::get("system_monitor:collection:network:RX:$ip");
$data = Cache::get("system_monitor:collection:network:RX:$hash");
}

return $data;
}

static public function getNetworkTXCollection($ip){
static public function getNetworkTXCollection($hash){
$time = time();

if(!Cache::has("system_monitor:collection:network:TX:$ip")){
if(!Cache::has("system_monitor:collection:network:TX:$hash")){
$data = Cache::store('redis')->handler()
->zRangeByScore("system_monitor:collection:network:TX:$ip", 0, $time
->zRangeByScore("system_monitor:collection:network:TX:$hash", 0, $time
, ['withscores' => TRUE]);
Cache::set("system_monitor:collection:network:TX:$ip",$data,150);
Cache::set("system_monitor:collection:network:TX:$hash",$data,150);
} else {
$data = Cache::get("system_monitor:collection:network:TX:$ip");
$data = Cache::get("system_monitor:collection:network:TX:$hash");
}

return $data;
Expand Down Expand Up @@ -298,32 +301,30 @@ static public function getIPByHash($token){
}

static public function deleteInfo($hash){
$ip = self::getIPByHash($hash);
Cache::rm("system_monitor:collection:cpu:$ip");
Cache::rm("system_monitor:collection:disk:$ip");
Cache::rm("system_monitor:collection:memory:$ip");
Cache::rm("system_monitor:collection:swap:$ip");
Cache::rm("system_monitor:collection:network:RX:$ip");
Cache::rm("system_monitor:collection:network:TX:$ip");
Cache::rm("system_monitor:collection:network:tmp:$ip");
Cache::rm("system_monitor:collection:info:$ip");
Cache::rm("system_monitor:stat:$ip");
Cache::rm("system_monitor:collection:cpu:$hash");
Cache::rm("system_monitor:collection:disk:$hash");
Cache::rm("system_monitor:collection:memory:$hash");
Cache::rm("system_monitor:collection:swap:$hash");
Cache::rm("system_monitor:collection:network:RX:$hash");
Cache::rm("system_monitor:collection:network:TX:$hash");
Cache::rm("system_monitor:collection:network:tmp:$hash");
Cache::rm("system_monitor:collection:info:$hash");
Cache::rm("system_monitor:stat:$hash");
Cache::rm("system_monitor:hashes");
Cache::store('redis')->rm("system_monitor:collection:cpu:$ip");
Cache::store('redis')->rm("system_monitor:collection:disk:$ip");
Cache::store('redis')->rm("system_monitor:collection:memory:$ip");
Cache::store('redis')->rm("system_monitor:collection:swap:$ip");
Cache::store('redis')->rm("system_monitor:collection:network:RX:$ip");
Cache::store('redis')->rm("system_monitor:collection:network:TX:$ip");
Cache::store('redis')->rm("system_monitor:collection:network:tmp:$ip");
Cache::store('redis')->rm("system_monitor:collection:info:$ip");
Cache::store('redis')->rm("system_monitor:stat:$ip");
Cache::store('redis')->rm("system_monitor:collection:cpu:$hash");
Cache::store('redis')->rm("system_monitor:collection:disk:$hash");
Cache::store('redis')->rm("system_monitor:collection:memory:$hash");
Cache::store('redis')->rm("system_monitor:collection:swap:$hash");
Cache::store('redis')->rm("system_monitor:collection:network:RX:$hash");
Cache::store('redis')->rm("system_monitor:collection:network:TX:$hash");
Cache::store('redis')->rm("system_monitor:collection:network:tmp:$hash");
Cache::store('redis')->rm("system_monitor:collection:info:$hash");
Cache::store('redis')->rm("system_monitor:stat:$hash");
Cache::store('redis')->handler()->sRem("system_monitor:nodes", $ip);
Cache::store('redis')->handler()->hDel("system_monitor:hashes", $hash);
}

public static function setDisplay($hash, $display){
self::getIPByHash($hash);
if (!empty($display))
Cache::store('redis')->handler()
->sRem("system_monitor:hide", $hash);
Expand Down
3 changes: 1 addition & 2 deletions application/http/middleware/CheckToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class CheckToken
public function handle($request, \Closure $next)
{
$token = $request->param('token');

if(strlen($token) != 64)
if(strlen($token) != 32)
throw new Exception("Wrong Token", 403);


Expand Down
13 changes: 4 additions & 9 deletions application/index/controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public function index()
{
$hash = SystemMonitor::getHashes();
$ip = SystemMonitor::fetchIPInfo(array_values($hash));
$info = SystemMonitor::getInfo(array_values($hash));
$info = SystemMonitor::getInfo(array_keys($hash));
$hide = array_flip(SystemMonitor::getHide());


asort($hash);

$this->assign("hash", $hash);
$this->assign("hide", $hide);
$this->assign("ip", $ip);
Expand All @@ -28,14 +28,9 @@ public function index()
}

public function info($token = ''){
if(strlen($token) != 64)
$this->error("Wrong Token");

$ip = SystemMonitor::getIPByHash($token);

$json = SystemMonitor::getStat($ip);
$json = SystemMonitor::getStat($token);
$uptime_str = SystemMonitor::timeFormat(intval($json['Uptime']));
$info = SystemMonitor::getInfo($ip);
$info = SystemMonitor::getInfo($token);
ksort($info);
$this->assign("json", $json);
$this->assign("uptime", $uptime_str);
Expand Down
Loading

0 comments on commit 85223f6

Please sign in to comment.