系统信息功能 后端(1/3)

This commit is contained in:
Chenx221 2024-04-09 19:01:44 +08:00
parent e1f07d922e
commit 7265ad3ed8
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021

View File

@ -14,6 +14,7 @@ use app\models\User;
use app\models\UserSearch;
use app\utils\AdminSword;
use app\utils\FileSizeHelper;
use DateTime;
use OTPHP\TOTP;
use RuntimeException;
use Throwable;
@ -588,4 +589,91 @@ class AdminController extends Controller
return $this->render('sysinfo');
}
/**
* Get server status
* 只兼容Windows和Linux
* other不考虑
* @return void
*/
public function actionGetServerStatus(): void
{
//需要收集的信息
//hostname
//os
//cpu
//memory
//server time
//server uptime
//is windows?
//server load
//server cpu usage
//server memory usage
//server swap usage
//storage data drive
//file system
//drive size
//drive used & free
//dns server
//gateway
//network interface(status,speed,ipv4v6 address)
//All users number
//active users number(24h)
//share number,collection number
//php version,memory limit,max execution time,max upload size,max post size,extension
//database type,version,size
//get server hostname
$hostname = php_uname('n');
//get os
$os = php_uname('s') . ' ' . php_uname('r') . ' ' . php_uname('v') . ' ' . php_uname('m');
// is windows?
$is_windows = stripos($os, 'windows') !== false;
if ($is_windows) {
//get cpu model for windows
$cpu = implode("\n", array_slice(explode("\n", trim(shell_exec('wmic cpu get name'))), 1)) . ' (' . implode("\n", array_slice(explode("\n", trim(shell_exec('wmic cpu get NumberOfCores'))), 1)) . ' cores)';
//get memory for windows
$memory_str = shell_exec('wmic MEMORYCHIP get Capacity');
$memoryLines = explode("\n", trim($memory_str));
unset($memoryLines[0]);
$totalMemory = 0;
foreach ($memoryLines as $mem) {
$totalMemory += intval($mem);
}
$memory = FileSizeHelper::formatBytes($totalMemory);
//get server uptime for windows
$uptime = shell_exec('net statistics workstation | find "Statistics since"');
$uptime = explode("since", $uptime, 2)[1];
$bootTime = DateTime::createFromFormat('m/d/Y H:i:s A', trim($uptime));
$now = new DateTime();
$interval = $bootTime->diff($now);
echo $interval->format('%a days %h hours %i minutes %s seconds');
//get server cpu usage for windows
$cpu_usage = implode("\n", array_slice(explode("\n", trim(shell_exec('wmic cpu get loadpercentage'))), 1));
if($cpu_usage === '') {
$cpu_usage = '0';
}
//get server memory usage for windows
// $memory_usage = shell_exec('wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /Value');
//TODO
} else {
//get cpu model
$cpu = shell_exec('cat /proc/cpuinfo | grep "model name" | uniq | awk -F": " \'{print $2}\'');
//get memory
$memory_kb = intval(shell_exec("grep MemTotal /proc/meminfo | awk '{print $2}'"));
$memory = FileSizeHelper::formatBytes($memory_kb * 1024);
//get server uptime
$uptime = str_replace("up ", "", trim(shell_exec('uptime -p')));
//get server cpu usage
$cpu_usage = shell_exec('top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1"%"}\'');
}
//get server time
$server_time = date('Y-m-d H:i:s T');
//get server load (only for linux)
$load = $is_windows ? null : sys_getloadavg();
}
}