Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
1. optimize code
2. adding battery suppot
  • Loading branch information
LittleJake committed Oct 29, 2024
1 parent 48f0779 commit 87f9662
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 121 deletions.
105 changes: 88 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
Server Monitor
==========

![Apache2.0](https://img.shields.io/badge/License-Apache2.0-green)

![think v5.1.40](https://img.shields.io/packagist/php-v/topthink/think/v5.1.40)

![GitHub Repo stars](https://img.shields.io/github/stars/LittleJake/server-monitor?style=social)

![GitHub followers](https://img.shields.io/github/followers/LittleJake?style=social)
<img alt="Apache 2.0" src="https://img.shields.io/badge/License-Apache2.0-green">
<img alt="think v5.1.40" src="https://img.shields.io/packagist/php-v/topthink/think/v5.1.41">
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/LittleJake/server-monitor?style=social">
<img alt="GitHub followers" src="https://img.shields.io/github/followers/LittleJake?style=social">

基于ThinkPHP5.1的服务器监控平台,需要配合服务端python脚本,使用Redis存储相关数据。

Expand All @@ -22,7 +19,7 @@ composer install
cp .env.example .env
```

#### 上报节点密钥操作(增/删/查)
#### API上报节点密钥操作(增/删/查)(TODO

```bash
php think token help
Expand All @@ -34,38 +31,112 @@ php think token help
vim .env
```

#### 刷新数据源缓存(加速网站访问)
#### 刷新数据缓存

```bash
php think refresh:cache
```

### 界面演示

![首页](img/20230113152258.png)
![首页](img/1705648177498.png)

![Node](img/1705648247270.png)

![CPU](img/20230113152320.png)
![Node2](img/1705648323851.png)

![Thermal](img/20230113152437.png)
![Other](img/1705648396126.png)

### Demo

[测试网站](https://monitor.littlejake.net)
[Demo](https://monitor.littlejake.net)

### 数据结构

#### Collection

```json
{
"Disk": {
// Based on different mountpoint.
"mountpoint": {
"total": "0.00",
"used": "0.00",
"free": "0.00",
"percent": 0
},
},
"Memory": {
"Mem": {
"total": "0.00",
"used": "0.00",
"free": "0.00",
"percent": 0.0
},
"Swap": {
"total": "0.00",
"used": "0.00",
"free": "0.00",
"percent": 0.0
}
},
"Load": {
// Metrics based on platform.
"metric": 0.0
},
"Network": {
"RX": {
"bytes": 0,
"packets": 0
},
"TX": {
"bytes": 0,
"packets": 0
}
},
"Thermal": {
// Celsius
"sensor": 0.0,
},
"Battery": {
"percent": 0.0,
}
}
```

#### Info

```json
{
"CPU": "",
"System Version": "",
"IPV4": "masked ipv4",
"IPV6": "masked ipv6",
"Uptime": "time in readable form",
"Connection": "",
"Process": "",
"Load Average": "",
"Update Time": "",
"Country": "extract from ip-api.com",
"Country Code": "extract from ip-api.com",
"Throughput": "Gigabytes",
}
```

### 开源协议

[Apache 2.0](LICENSE)

### 鸣谢

MDUI
[MDUI](https://mdui.org)

ThinkPHP
[ThinkPHP](https://www.thinkphp.cn/)

ip-api.com
[ip-api.com](https://ip-api.com)

### Sponsors

Thanks for the free VM server (opensource) provided by [DartNode](https://dartnode.com?via=1).
Thanks for the amazing VM server provided by [DartNode](https://dartnode.com?via=1).

<a href="https://dartnode.com?via=1"><img src="https://raw.githubusercontent.com/LittleJake/LittleJake/master/images/dartnode.png" width="150" align='left' ></a>
25 changes: 25 additions & 0 deletions application/api/controller/Battery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace app\api\controller;

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

class Battery 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), 'Battery'));
}
}
2 changes: 1 addition & 1 deletion application/api/controller/Disk.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Disk extends Controller
*/
public function get($uuid = '')
{
return json(SystemMonitor::diskFormat(SystemMonitor::getCollection($uuid)));
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($uuid), 'Disk'));
}
}
2 changes: 1 addition & 1 deletion application/api/controller/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Memory extends Controller
*/
public function get($uuid = '')
{
return json(SystemMonitor::memoryFormat(SystemMonitor::getCollection($uuid)));
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($uuid), "Memory"));
}
}
2 changes: 1 addition & 1 deletion application/api/controller/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Network extends Controller
*/
public function get($uuid = '')
{
return json(SystemMonitor::networkFormat(SystemMonitor::getCollection($uuid)));
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($uuid), "Network"));
}
}
145 changes: 62 additions & 83 deletions application/common/lib/SystemMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,100 +160,79 @@ static public function setCollection($uuid, $data)
}

static public function collectionFormat($data, $name)
{
$k = [];
$v = [];

foreach ($data as $kk => $vv) {
$k[] = date('m-d H:i', $vv);
foreach (json_decode($kk, true)[$name] as $kkk => $vvv)
$v[$kkk][] = $vvv;
}

return [
'time' => $k,
'value' => $v
];
}

static public function diskFormat($data)
{
$time = [];
$value = [];

//in case of missing mounting point periodically.
$mount_points = [];
foreach ($data as $kk => $_)
$mount_points = array_unique(array_merge($mount_points, array_keys(json_decode($kk, true)['Disk'])));

foreach ($data as $kk => $date) {
$time[] = date('m-d H:i', $date);
$json = json_decode($kk, true);

foreach ($mount_points as $_ => $mount_point) {
if (empty($json['Disk'][$mount_point]))
$value[$mount_point][] = 0;
else
$value[$mount_point][] = $json['Disk'][$mount_point]['used'];
}
switch (strtolower($name)) {
case "thermal":
case "battery":
case "load":
foreach ($data as $data_json => $data_time) {
$sensors = json_decode($data_json, true)[$name];
if (count($sensors) > 0)
$time[] = date('m-d H:i', $data_time);
//extract $name from collection.
foreach ($sensors as $sensor => $sensor_value)
$value[$sensor][] = $sensor_value;
}
break;

case "disk":
case "memory":
//in case of missing mounting point periodically.
$mount_points = [];
foreach ($data as $data_json => $_)
$mount_points = array_unique(array_merge($mount_points, array_keys(json_decode($data_json, true)[$name])));

foreach ($data as $data_json => $data_time) {
$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'];
}
}
break;
case "network":
$rx_packets = [];
$rx_megabytes = [];
$tx_packets = [];
$tx_megabytes = [];

foreach ($data as $data_json => $data_time) {
$time[] = date('m-d H:i', $data_time);
$network_data = json_decode($data_json, true)[$name];
$rx_packets[] = (intval($network_data['RX']['packets']) * 1.0) / 1000;
$tx_packets[] = (intval($network_data['TX']['packets']) * 1.0) / 1000;
$rx_megabytes[] = intval(intval(intval($network_data['RX']['bytes']) * 100.00) / 1048576) * 1.0 / 100;
$tx_megabytes[] = intval(intval(intval($network_data['TX']['bytes']) * 100.00) / 1048576) * 1.0 / 100;
}

return [
'RX' => [
'time' => $time,
'packets' => $rx_packets,
'megabytes' => $rx_megabytes
],
'TX' => [
'time' => $time,
'packets' => $tx_packets,
'megabytes' => $tx_megabytes
]
];
}


return [
'time' => $time,
'value' => $value
];
}


static public function memoryFormat($data)
{
$k = [];
$v = [];

foreach ($data as $kk => $vv) {
$k[] = date('m-d H:i', $vv);
foreach (json_decode($kk, true)['Memory'] as $kkk => $vvv)
$v[$kkk][] = $vvv['used'];
}

return [
'time' => $k,
'value' => $v
];
}

static public function networkFormat($data)
{
$k = [];
$rx_packets = [];
$rx_megabytes = [];
$tx_packets = [];
$tx_megabytes = [];

foreach ($data as $kk => $vv) {
$k[] = date('m-d H:i', $vv);
$j = json_decode($kk, true)['Network'];
// $arr = explode(',',json_decode($kk, true)['value']);
$rx_packets[] = (intval($j['RX']['packets']) * 1.0) / 1000;
$tx_packets[] = (intval($j['TX']['packets']) * 1.0) / 1000;
$rx_megabytes[] = intval(intval(intval($j['RX']['bytes']) * 100.00) / 1048576) * 1.0 / 100;
$tx_megabytes[] = intval(intval(intval($j['TX']['bytes']) * 100.00) / 1048576) * 1.0 / 100;
}

return [
'RX' => [
'time' => $k,
'packets' => $rx_packets,
'megabytes' => $rx_megabytes
],
'TX' => [
'time' => $k,
'packets' => $tx_packets,
'megabytes' => $tx_megabytes
]
];
}

static public function getIPByUUID($uuid)
{
$uuids = SystemMonitor::getUUIDs();
Expand Down Expand Up @@ -294,7 +273,7 @@ static public function setDisplayName($uuid, $name)
Cache::store('redis')->handler()
->hDel("system_monitor:name", $uuid);
}

Cache::rm("system_monitor:name");
}

Expand Down
Loading

0 comments on commit 87f9662

Please sign in to comment.