Skip to content

Commit c7e55c1

Browse files
committed
1.1.60
1 parent 1344a74 commit c7e55c1

File tree

2 files changed

+139
-89
lines changed

2 files changed

+139
-89
lines changed

src/admin.php

Lines changed: 138 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -336,39 +336,51 @@ function diagnoseSystem() {
336336

337337
// 获取容器运行时间
338338
function getContainerUptime() {
339-
// 尝试从 /proc/uptime 获取容器运行时间
340-
$uptime_file = '/proc/uptime';
341-
if (file_exists($uptime_file)) {
342-
$uptime_data = file_get_contents($uptime_file);
339+
// 尝试从容器启动时间计算运行时间
340+
$start_time = null;
341+
342+
// 方法1: 从 /proc/1/stat 获取进程启动时间
343+
if (file_exists('/proc/1/stat')) {
344+
$stat_data = file_get_contents('/proc/1/stat');
345+
if ($stat_data) {
346+
$parts = explode(' ', $stat_data);
347+
if (count($parts) >= 22) {
348+
$start_time = intval($parts[21]); // 进程启动时间(时钟滴答)
349+
$clock_ticks = intval(shell_exec('getconf CLK_TCK 2>/dev/null') ?: 100);
350+
$start_time = $start_time / $clock_ticks; // 转换为秒
351+
}
352+
}
353+
}
354+
355+
// 方法2: 从 /proc/uptime 获取系统运行时间(作为备用)
356+
if (!$start_time && file_exists('/proc/uptime')) {
357+
$uptime_data = file_get_contents('/proc/uptime');
343358
if ($uptime_data) {
344359
$parts = explode(' ', trim($uptime_data));
345-
$uptime_seconds = floatval($parts[0]);
346-
347-
// 转换为可读格式
348-
$days = floor($uptime_seconds / 86400);
349-
$hours = floor(($uptime_seconds % 86400) / 3600);
350-
$minutes = floor(($uptime_seconds % 3600) / 60);
351-
$seconds = floor($uptime_seconds % 60);
352-
353-
if ($days > 0) {
354-
return "容器运行时间: {$days}{$hours}小时 {$minutes}分钟";
355-
} elseif ($hours > 0) {
356-
return "容器运行时间: {$hours}小时 {$minutes}分钟";
357-
} elseif ($minutes > 0) {
358-
return "容器运行时间: {$minutes}分钟 {$seconds}";
359-
} else {
360-
return "容器运行时间: {$seconds}";
361-
}
360+
$start_time = time() - floatval($parts[0]); // 当前时间 - 系统运行时间
362361
}
363362
}
364363

365-
// 备用方案:尝试使用 uptime 命令但显示为容器信息
366-
$uptime = shell_exec('uptime 2>&1');
367-
if ($uptime) {
368-
return "容器状态: " . trim($uptime);
364+
if ($start_time) {
365+
$uptime_seconds = time() - $start_time;
366+
367+
// 转换为可读格式
368+
$days = floor($uptime_seconds / 86400);
369+
$hours = floor(($uptime_seconds % 86400) / 3600);
370+
$minutes = floor(($uptime_seconds % 3600) / 60);
371+
372+
if ($days > 0) {
373+
return "{$days}{$hours}小时 {$minutes}分钟";
374+
} elseif ($hours > 0) {
375+
return "{$hours}小时 {$minutes}分钟";
376+
} elseif ($minutes > 0) {
377+
return "{$minutes}分钟";
378+
} else {
379+
return "刚刚启动";
380+
}
369381
}
370382

371-
return "无法获取容器运行时间";
383+
return "无法获取";
372384
}
373385

374386
// 获取容器 ID
@@ -401,78 +413,103 @@ function getContainerId() {
401413
return "未知";
402414
}
403415

404-
// 获取进程内存占用
405-
function getProcessMemory($process_name) {
406-
// 根据进程名称设置不同的搜索模式
407-
$search_patterns = [];
408-
409-
switch ($process_name) {
410-
case 'php-fpm84':
411-
$search_patterns = [
412-
"ps aux | grep 'php-fpm' | grep -v grep 2>/dev/null",
413-
"ps aux | grep '{php-fpm84}' | grep -v grep 2>/dev/null",
414-
"pgrep -f 'php-fpm' | xargs ps -o pid,rss,comm --no-headers 2>/dev/null"
415-
];
416-
break;
417-
case 'nginx':
418-
$search_patterns = [
419-
"ps aux | grep 'nginx:' | grep -v grep 2>/dev/null",
420-
"ps aux | grep 'nginx' | grep -v grep 2>/dev/null",
421-
"pgrep -f 'nginx' | xargs ps -o pid,rss,comm --no-headers 2>/dev/null"
422-
];
423-
break;
424-
case 'redis':
425-
$search_patterns = [
426-
"ps aux | grep 'redis-server' | grep -v grep 2>/dev/null",
427-
"ps aux | grep 'redis' | grep -v grep 2>/dev/null",
428-
"pgrep -f 'redis' | xargs ps -o pid,rss,comm --no-headers 2>/dev/null"
429-
];
430-
break;
431-
default:
432-
$search_patterns = [
433-
"ps aux | grep '{$process_name}' | grep -v grep 2>/dev/null",
434-
"pgrep -f '{$process_name}' | xargs ps -o pid,rss,comm --no-headers 2>/dev/null"
435-
];
436-
}
416+
// 获取容器信息
417+
function getContainerInfo() {
418+
$info = [];
437419

438-
$memory_info = '';
439-
foreach ($search_patterns as $cmd) {
440-
$memory_info = shell_exec($cmd);
441-
if ($memory_info && trim($memory_info)) {
442-
break;
420+
// 获取容器ID
421+
$container_id = "无法获取";
422+
423+
// 方法1: 从 /proc/self/cgroup 获取
424+
$cgroup_file = '/proc/self/cgroup';
425+
if (file_exists($cgroup_file)) {
426+
$cgroup_data = file_get_contents($cgroup_file);
427+
if ($cgroup_data) {
428+
$lines = explode("\n", $cgroup_data);
429+
foreach ($lines as $line) {
430+
if (strpos($line, 'docker') !== false || strpos($line, 'containerd') !== false) {
431+
$parts = explode('/', $line);
432+
if (count($parts) >= 3) {
433+
$container_id = $parts[2];
434+
// 截取前12位作为短ID
435+
$container_id = substr($container_id, 0, 12);
436+
break;
437+
}
438+
}
439+
}
443440
}
444441
}
445442

446-
if (!$memory_info || !trim($memory_info)) {
447-
// 如果还是找不到,尝试使用supervisorctl检查状态
448-
$supervisor_status = shell_exec("supervisorctl status 2>/dev/null");
449-
if ($supervisor_status && strpos($supervisor_status, 'RUNNING') !== false) {
450-
return '运行中';
451-
}
443+
// 方法2: 从环境变量获取
444+
if ($container_id === "无法获取") {
445+
$container_id = getenv('HOSTNAME') ?: getenv('CONTAINER_ID') ?: "无法获取";
446+
}
447+
448+
$info['id'] = $container_id;
449+
450+
// 获取容器名称
451+
$info['name'] = getenv('CONTAINER_NAME') ?: getenv('HOSTNAME') ?: "未知";
452+
453+
// 获取镜像信息
454+
$info['image'] = getenv('CONTAINER_IMAGE') ?: "未知";
455+
456+
// 获取容器状态
457+
$info['status'] = "运行中";
458+
459+
return $info;
460+
}
461+
462+
// 获取进程内存占用
463+
function getProcessMemory($process_name) {
464+
// 使用更简单直接的方法获取进程信息
465+
$memory_info = '';
466+
467+
// 直接使用 ps aux 获取所有进程,然后过滤
468+
$all_processes = shell_exec('ps aux 2>/dev/null');
469+
if (!$all_processes) {
452470
return '未运行';
453471
}
454472

455-
$lines = explode("\n", trim($memory_info));
473+
$lines = explode("\n", $all_processes);
456474
$total_memory = 0;
457475
$process_count = 0;
458476

459477
foreach ($lines as $line) {
460478
if (trim($line)) {
461-
$parts = preg_split('/\s+/', trim($line));
462-
if (count($parts) >= 2) {
463-
// 处理不同的 ps 输出格式
464-
$rss_index = 1;
465-
if (count($parts) >= 3 && is_numeric($parts[1]) && is_numeric($parts[2])) {
479+
// 根据进程名称匹配
480+
$matched = false;
481+
482+
switch ($process_name) {
483+
case 'php-fpm84':
484+
if (strpos($line, 'php-fpm') !== false || strpos($line, '{php-fpm84}') !== false) {
485+
$matched = true;
486+
}
487+
break;
488+
case 'nginx':
489+
if (strpos($line, 'nginx') !== false) {
490+
$matched = true;
491+
}
492+
break;
493+
case 'redis':
494+
if (strpos($line, 'redis-server') !== false || strpos($line, 'redis') !== false) {
495+
$matched = true;
496+
}
497+
break;
498+
default:
499+
if (strpos($line, $process_name) !== false) {
500+
$matched = true;
501+
}
502+
}
503+
504+
if ($matched) {
505+
$parts = preg_split('/\s+/', trim($line));
506+
if (count($parts) >= 6) {
466507
// ps aux 格式: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
467-
$rss_index = 5; // RSS 在第6列
468-
} elseif (count($parts) >= 2 && is_numeric($parts[1])) {
469-
// ps -o 格式: PID RSS COMM
470-
$rss_index = 1; // RSS 在第2列
471-
}
472-
473-
if (isset($parts[$rss_index]) && is_numeric($parts[$rss_index])) {
474-
$total_memory += intval($parts[$rss_index]); // RSS in KB
475-
$process_count++;
508+
$rss = intval($parts[5]); // RSS 在第6列
509+
if ($rss > 0) {
510+
$total_memory += $rss;
511+
$process_count++;
512+
}
476513
}
477514
}
478515
}
@@ -484,6 +521,12 @@ function getProcessMemory($process_name) {
484521
return "{$memory_mb}MB ({$process_count}个进程)";
485522
}
486523

524+
// 如果还是找不到,尝试使用supervisorctl检查状态
525+
$supervisor_status = shell_exec("supervisorctl status 2>/dev/null");
526+
if ($supervisor_status && strpos($supervisor_status, 'RUNNING') !== false) {
527+
return '运行中';
528+
}
529+
487530
return '未运行';
488531
}
489532

@@ -500,9 +543,13 @@ function getSystemInfo() {
500543
// Nginx 信息
501544
$info['nginx_memory_usage'] = getProcessMemory('nginx');
502545

503-
// 系统信息
546+
// 容器信息
547+
$container_info = getContainerInfo();
504548
$info['uptime'] = getContainerUptime();
505-
$info['container_id'] = getContainerId();
549+
$info['container_id'] = $container_info['id'];
550+
$info['container_name'] = $container_info['name'];
551+
$info['container_image'] = $container_info['image'];
552+
$info['container_status'] = $container_info['status'];
506553
$info['memory_usage'] = shell_exec('free -h 2>&1') ?: '无法获取';
507554
$info['disk_usage'] = shell_exec('df -h / 2>&1') ?: '无法获取';
508555

@@ -815,8 +862,11 @@ function getSystemInfo() {
815862
<h3 class="font-semibold text-purple-800 mb-2">
816863
<i class="fas fa-info-circle text-purple-600 mr-1"></i>容器状态
817864
</h3>
818-
<p class="text-sm text-purple-700"><?php echo $systemInfo['uptime']; ?></p>
865+
<p class="text-sm text-purple-700">运行时间: <?php echo $systemInfo['uptime']; ?></p>
819866
<p class="text-sm text-purple-700">容器 ID: <?php echo $systemInfo['container_id']; ?></p>
867+
<p class="text-sm text-purple-700">容器名称: <?php echo $systemInfo['container_name']; ?></p>
868+
<p class="text-sm text-purple-700">镜像: <?php echo $systemInfo['container_image']; ?></p>
869+
<p class="text-sm text-purple-700">状态: <?php echo $systemInfo['container_status']; ?></p>
820870
</div>
821871
</div>
822872
</div>

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
php-nginx:1.1.59
1+
php-nginx:1.1.60

0 commit comments

Comments
 (0)