mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-06 10:02:49 +08:00
refactor(admin): 重构系统状态模块以支持模块化结构
This commit is contained in:
221
extend/base/admin/controller/system/StatusBase.php
Normal file
221
extend/base/admin/controller/system/StatusBase.php
Normal file
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
|
||||
namespace base\admin\controller\system;
|
||||
|
||||
use app\admin\service\annotation\ControllerAnnotation;
|
||||
use app\admin\service\annotation\NodeAnotation;
|
||||
use app\common\command\admin\Version;
|
||||
use app\common\controller\AdminController;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* @ControllerAnnotation(title="system_status")
|
||||
*/
|
||||
class StatusBase extends AdminController
|
||||
{
|
||||
/**
|
||||
* @NodeAnotation(title="列表")
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$systemInfo = $this->getSystemInfo();
|
||||
$serverInfo = $this->getServerInfo();
|
||||
$databaseInfo = $this->getDatabaseInfo();
|
||||
$cacheInfo = $this->getCacheInfo();
|
||||
$storageInfo = $this->getStorageInfo();
|
||||
$phpConfigInfo = $this->getPhpConfigInfo();
|
||||
$performanceInfo = $this->getPerformanceInfo();
|
||||
|
||||
$this->assign('systemInfo', $systemInfo);
|
||||
$this->assign('serverInfo', $serverInfo);
|
||||
$this->assign('databaseInfo', $databaseInfo);
|
||||
$this->assign('cacheInfo', $cacheInfo);
|
||||
$this->assign('storageInfo', $storageInfo);
|
||||
$this->assign('phpConfigInfo', $phpConfigInfo);
|
||||
$this->assign('performanceInfo', $performanceInfo);
|
||||
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
protected function getSystemInfo()
|
||||
{
|
||||
return [
|
||||
'framework' => 'Ulthon Admin',
|
||||
'version' => Version::VERSION,
|
||||
'product_version' => Version::PRODUCT_VERSION ?: Version::VERSION,
|
||||
'layui_version' => Version::LAYUI_VERSION,
|
||||
'thinkphp_version' => app()->version(),
|
||||
'php_version' => PHP_VERSION,
|
||||
'server_time' => date('Y-m-d H:i:s'),
|
||||
'timezone' => date_default_timezone_get(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getServerInfo()
|
||||
{
|
||||
$serverSoftware = $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown';
|
||||
$os = PHP_OS;
|
||||
$sapi = php_sapi_name();
|
||||
$ip = $_SERVER['SERVER_ADDR'] ?? '127.0.0.1';
|
||||
$port = $_SERVER['SERVER_PORT'] ?? '80';
|
||||
|
||||
return [
|
||||
'server_software' => $serverSoftware,
|
||||
'os' => $os,
|
||||
'sapi' => $sapi,
|
||||
'ip' => $ip,
|
||||
'port' => $port,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getDatabaseInfo()
|
||||
{
|
||||
$connectionName = Config::get('database.default', 'mysql');
|
||||
$dbConfig = Config::get('database.connections.' . $connectionName);
|
||||
$info = [
|
||||
'type' => $dbConfig['type'] ?? 'Unknown',
|
||||
'hostname' => $dbConfig['hostname'] ?? 'Unknown',
|
||||
'database' => $dbConfig['database'] ?? 'Unknown',
|
||||
'prefix' => $dbConfig['prefix'] ?? '',
|
||||
'charset' => $dbConfig['charset'] ?? 'Unknown',
|
||||
'status' => 'unknown',
|
||||
'version' => 'Unknown',
|
||||
'table_count' => 0,
|
||||
];
|
||||
|
||||
try {
|
||||
$connection = Db::connect($connectionName);
|
||||
$version = $connection->query('SELECT VERSION() as version')[0]['version'] ?? 'Unknown';
|
||||
|
||||
$prefix = $dbConfig['prefix'] ?? '';
|
||||
$tableCount = 0;
|
||||
if ($prefix) {
|
||||
$tables = $connection->query("SHOW TABLES LIKE '{$prefix}%'");
|
||||
$tableCount = count($tables);
|
||||
} else {
|
||||
$tables = $connection->query('SHOW TABLES');
|
||||
$tableCount = count($tables);
|
||||
}
|
||||
|
||||
$info['status'] = 'connected';
|
||||
$info['version'] = $version;
|
||||
$info['table_count'] = $tableCount;
|
||||
} catch (\Exception $e) {
|
||||
$info['status'] = 'error';
|
||||
$info['error'] = $e->getMessage();
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
protected function getCacheInfo()
|
||||
{
|
||||
$defaultDriver = Config::get('cache.default');
|
||||
$cacheConfig = Config::get('cache.stores.' . $defaultDriver);
|
||||
|
||||
$info = [
|
||||
'default_driver' => $defaultDriver,
|
||||
'type' => $cacheConfig['type'] ?? 'Unknown',
|
||||
'path' => $cacheConfig['path'] ?? '',
|
||||
'prefix' => $cacheConfig['prefix'] ?? '',
|
||||
'expire' => $cacheConfig['expire'] ?? 0,
|
||||
'status' => 'unknown',
|
||||
];
|
||||
|
||||
try {
|
||||
$testKey = 'system_status_test_' . time();
|
||||
Cache::set($testKey, 'test', 10);
|
||||
$testValue = Cache::get($testKey);
|
||||
Cache::delete($testKey);
|
||||
|
||||
if ($testValue === 'test') {
|
||||
$info['status'] = 'available';
|
||||
} else {
|
||||
$info['status'] = 'error';
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$info['status'] = 'error';
|
||||
$info['error'] = $e->getMessage();
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
protected function getStorageInfo()
|
||||
{
|
||||
$rootPath = app()->getRootPath();
|
||||
$freeBytes = disk_free_space($rootPath);
|
||||
$totalBytes = disk_total_space($rootPath);
|
||||
$usedBytes = $totalBytes - $freeBytes;
|
||||
$usagePercent = ($usedBytes / $totalBytes) * 100;
|
||||
|
||||
return [
|
||||
'root_path' => $rootPath,
|
||||
'total' => $this->formatBytes($totalBytes),
|
||||
'free' => $this->formatBytes($freeBytes),
|
||||
'used' => $this->formatBytes($usedBytes),
|
||||
'usage_percent' => round($usagePercent, 2),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getPhpConfigInfo()
|
||||
{
|
||||
return [
|
||||
'upload_max_filesize' => ini_get('upload_max_filesize'),
|
||||
'post_max_size' => ini_get('post_max_size'),
|
||||
'memory_limit' => ini_get('memory_limit'),
|
||||
'max_execution_time' => ini_get('max_execution_time') . 's',
|
||||
'max_input_time' => ini_get('max_input_time') . 's',
|
||||
'safe_mode' => ini_get('safe_mode') ? 'On' : 'Off',
|
||||
'allow_url_fopen' => ini_get('allow_url_fopen') ? 'On' : 'Off',
|
||||
'file_uploads' => ini_get('file_uploads') ? 'On' : 'Off',
|
||||
'disabled_functions' => ini_get('disable_functions') ?: 'None',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getPerformanceInfo()
|
||||
{
|
||||
$memoryUsage = memory_get_usage(true);
|
||||
$memoryLimit = $this->convertToBytes(ini_get('memory_limit'));
|
||||
$memoryPercent = $memoryLimit > 0 ? ($memoryUsage / $memoryLimit) * 100 : 0;
|
||||
|
||||
return [
|
||||
'memory_usage' => $this->formatBytes($memoryUsage),
|
||||
'memory_limit' => $this->formatBytes($memoryLimit),
|
||||
'memory_percent' => round($memoryPercent, 2),
|
||||
'peak_memory' => $this->formatBytes(memory_get_peak_usage(true)),
|
||||
'load_average' => function_exists('sys_getloadavg') ? implode(', ', sys_getloadavg()) : 'N/A',
|
||||
];
|
||||
}
|
||||
|
||||
protected function formatBytes($bytes, $precision = 2)
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
|
||||
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
|
||||
$bytes /= 1024;
|
||||
}
|
||||
|
||||
return round($bytes, $precision) . ' ' . $units[$i];
|
||||
}
|
||||
|
||||
protected function convertToBytes($value)
|
||||
{
|
||||
$value = trim($value);
|
||||
$unit = strtolower($value[strlen($value) - 1]);
|
||||
$value = (float) $value;
|
||||
|
||||
switch ($unit) {
|
||||
case 'g':
|
||||
$value *= 1024;
|
||||
case 'm':
|
||||
$value *= 1024;
|
||||
case 'k':
|
||||
$value *= 1024;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user