SystemInfo.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace App\Module\Admin\Utils;
  3. use Illuminate\Support\Facades\DB;
  4. /**
  5. * 系统信息工具类
  6. */
  7. class SystemInfo
  8. {
  9. /**
  10. * 获取服务器信息
  11. *
  12. * @return array
  13. */
  14. public function getServerInfo(): array
  15. {
  16. return [
  17. 'os' => php_uname('s'),
  18. 'os_version' => php_uname('r'),
  19. 'hostname' => php_uname('n'),
  20. 'architecture' => php_uname('m'),
  21. 'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
  22. 'server_ip' => $_SERVER['SERVER_ADDR'] ?? 'Unknown',
  23. 'server_port' => $_SERVER['SERVER_PORT'] ?? 'Unknown',
  24. 'document_root' => $_SERVER['DOCUMENT_ROOT'] ?? 'Unknown',
  25. ];
  26. }
  27. /**
  28. * 获取PHP信息
  29. *
  30. * @return array
  31. */
  32. public function getPhpInfo(): array
  33. {
  34. return [
  35. 'version' => PHP_VERSION,
  36. 'sapi' => php_sapi_name(),
  37. 'memory_limit' => ini_get('memory_limit'),
  38. 'max_execution_time' => ini_get('max_execution_time'),
  39. 'upload_max_filesize' => ini_get('upload_max_filesize'),
  40. 'post_max_size' => ini_get('post_max_size'),
  41. 'timezone' => date_default_timezone_get(),
  42. 'extensions' => $this->getLoadedExtensions(),
  43. ];
  44. }
  45. /**
  46. * 获取已加载的PHP扩展
  47. *
  48. * @return array
  49. */
  50. protected function getLoadedExtensions(): array
  51. {
  52. $extensions = get_loaded_extensions();
  53. sort($extensions);
  54. return $extensions;
  55. }
  56. /**
  57. * 获取数据库信息
  58. *
  59. * @return array
  60. */
  61. public function getDatabaseInfo(): array
  62. {
  63. try {
  64. $connection = DB::connection();
  65. $pdo = $connection->getPdo();
  66. return [
  67. 'driver' => $connection->getDriverName(),
  68. 'version' => $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION),
  69. 'database' => $connection->getDatabaseName(),
  70. 'charset' => $connection->getConfig('charset'),
  71. 'collation' => $connection->getConfig('collation'),
  72. 'prefix' => $connection->getConfig('prefix'),
  73. 'status' => 'connected',
  74. ];
  75. } catch (\Exception $e) {
  76. return [
  77. 'status' => 'error',
  78. 'error' => $e->getMessage(),
  79. ];
  80. }
  81. }
  82. /**
  83. * 获取内存使用情况
  84. *
  85. * @return array
  86. */
  87. public function getMemoryUsage(): array
  88. {
  89. $memoryUsage = memory_get_usage(true);
  90. $memoryPeak = memory_get_peak_usage(true);
  91. $memoryLimit = $this->parseMemoryLimit(ini_get('memory_limit'));
  92. return [
  93. 'current' => $this->formatBytes($memoryUsage),
  94. 'peak' => $this->formatBytes($memoryPeak),
  95. 'limit' => $this->formatBytes($memoryLimit),
  96. 'percentage' => $memoryLimit > 0 ? round(($memoryUsage / $memoryLimit) * 100, 2) : 0,
  97. 'current_bytes' => $memoryUsage,
  98. 'peak_bytes' => $memoryPeak,
  99. 'limit_bytes' => $memoryLimit,
  100. ];
  101. }
  102. /**
  103. * 解析内存限制
  104. *
  105. * @param string $memoryLimit
  106. * @return int
  107. */
  108. protected function parseMemoryLimit(string $memoryLimit): int
  109. {
  110. if ($memoryLimit === '-1') {
  111. return 0; // 无限制
  112. }
  113. $unit = strtolower(substr($memoryLimit, -1));
  114. $value = (int) substr($memoryLimit, 0, -1);
  115. switch ($unit) {
  116. case 'g':
  117. return $value * 1024 * 1024 * 1024;
  118. case 'm':
  119. return $value * 1024 * 1024;
  120. case 'k':
  121. return $value * 1024;
  122. default:
  123. return (int) $memoryLimit;
  124. }
  125. }
  126. /**
  127. * 格式化字节数
  128. *
  129. * @param int $bytes
  130. * @return string
  131. */
  132. protected function formatBytes(int $bytes): string
  133. {
  134. if ($bytes === 0) {
  135. return '0 B';
  136. }
  137. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  138. $power = floor(log($bytes, 1024));
  139. return round($bytes / pow(1024, $power), 2) . ' ' . $units[$power];
  140. }
  141. /**
  142. * 获取磁盘使用情况
  143. *
  144. * @return array
  145. */
  146. public function getDiskUsage(): array
  147. {
  148. $path = base_path();
  149. $totalBytes = disk_total_space($path);
  150. $freeBytes = disk_free_space($path);
  151. $usedBytes = $totalBytes - $freeBytes;
  152. return [
  153. 'total' => $this->formatBytes($totalBytes),
  154. 'used' => $this->formatBytes($usedBytes),
  155. 'free' => $this->formatBytes($freeBytes),
  156. 'percentage' => $totalBytes > 0 ? round(($usedBytes / $totalBytes) * 100, 2) : 0,
  157. 'total_bytes' => $totalBytes,
  158. 'used_bytes' => $usedBytes,
  159. 'free_bytes' => $freeBytes,
  160. ];
  161. }
  162. /**
  163. * 获取CPU使用率(Linux系统)
  164. *
  165. * @return float
  166. */
  167. public function getCpuUsage(): float
  168. {
  169. if (PHP_OS_FAMILY !== 'Linux') {
  170. return 0.0;
  171. }
  172. try {
  173. $load = sys_getloadavg();
  174. return $load ? round($load[0] * 100, 2) : 0.0;
  175. } catch (\Exception $e) {
  176. return 0.0;
  177. }
  178. }
  179. /**
  180. * 获取系统负载平均值
  181. *
  182. * @return array
  183. */
  184. public function getLoadAverage(): array
  185. {
  186. if (function_exists('sys_getloadavg')) {
  187. $load = sys_getloadavg();
  188. return [
  189. '1min' => round($load[0], 2),
  190. '5min' => round($load[1], 2),
  191. '15min' => round($load[2], 2),
  192. ];
  193. }
  194. return [
  195. '1min' => 0,
  196. '5min' => 0,
  197. '15min' => 0,
  198. ];
  199. }
  200. /**
  201. * 获取系统运行时间
  202. *
  203. * @return string
  204. */
  205. public function getUptime(): string
  206. {
  207. if (PHP_OS_FAMILY === 'Linux') {
  208. try {
  209. $uptime = file_get_contents('/proc/uptime');
  210. $seconds = (int) explode(' ', $uptime)[0];
  211. $days = floor($seconds / 86400);
  212. $hours = floor(($seconds % 86400) / 3600);
  213. $minutes = floor(($seconds % 3600) / 60);
  214. return "{$days}天 {$hours}小时 {$minutes}分钟";
  215. } catch (\Exception $e) {
  216. return 'Unknown';
  217. }
  218. }
  219. return 'Unknown';
  220. }
  221. /**
  222. * 获取数据库状态
  223. *
  224. * @return array
  225. */
  226. public function getDatabaseStatus(): array
  227. {
  228. try {
  229. $startTime = microtime(true);
  230. DB::select('SELECT 1');
  231. $responseTime = round((microtime(true) - $startTime) * 1000, 2);
  232. return [
  233. 'status' => 'healthy',
  234. 'response_time' => $responseTime . 'ms',
  235. ];
  236. } catch (\Exception $e) {
  237. return [
  238. 'status' => 'error',
  239. 'error' => $e->getMessage(),
  240. ];
  241. }
  242. }
  243. /**
  244. * 获取性能指标
  245. *
  246. * @return array
  247. */
  248. public function getPerformanceMetrics(): array
  249. {
  250. return [
  251. 'memory' => $this->getMemoryUsage(),
  252. 'disk' => $this->getDiskUsage(),
  253. 'cpu_usage' => $this->getCpuUsage(),
  254. 'load_average' => $this->getLoadAverage(),
  255. 'database' => $this->getDatabaseStatus(),
  256. ];
  257. }
  258. /**
  259. * 获取Laravel应用信息
  260. *
  261. * @return array
  262. */
  263. public function getApplicationInfo(): array
  264. {
  265. return [
  266. 'name' => config('app.name'),
  267. 'env' => config('app.env'),
  268. 'debug' => config('app.debug'),
  269. 'url' => config('app.url'),
  270. 'timezone' => config('app.timezone'),
  271. 'locale' => config('app.locale'),
  272. 'laravel_version' => app()->version(),
  273. 'php_version' => PHP_VERSION,
  274. ];
  275. }
  276. }