@@ -336,28 +336,48 @@ function diagnoseSystem() {
336336
337337// 获取容器运行时间
338338function getContainerUptime () {
339- // 尝试从容器启动时间计算运行时间
340339 $ start_time = null ;
341340
342- // 方法1: 从 /proc/1/stat 获取进程启动时间
341+ // 方法1: 从supervisord进程启动时间计算(PID 1)
343342 if (file_exists ('/proc/1/stat ' )) {
344343 $ stat_data = file_get_contents ('/proc/1/stat ' );
345344 if ($ stat_data ) {
346345 $ parts = explode (' ' , $ stat_data );
347346 if (count ($ parts ) >= 22 ) {
348- $ start_time = intval ($ parts [21 ]); // 进程启动时间(时钟滴答)
347+ $ start_ticks = intval ($ parts [21 ]); // 进程启动时间(时钟滴答)
349348 $ clock_ticks = intval (shell_exec ('getconf CLK_TCK 2>/dev/null ' ) ?: 100 );
350- $ start_time = $ start_time / $ clock_ticks ; // 转换为秒
349+ $ start_time = $ start_ticks / $ clock_ticks ; // 转换为秒
351350 }
352351 }
353352 }
354353
355- // 方法2: 从 /proc/uptime 获取系统运行时间(作为备用)
356- if (!$ start_time && file_exists ('/proc/uptime ' )) {
357- $ uptime_data = file_get_contents ('/proc/uptime ' );
358- if ($ uptime_data ) {
359- $ parts = explode (' ' , trim ($ uptime_data ));
360- $ start_time = time () - floatval ($ parts [0 ]); // 当前时间 - 系统运行时间
354+ // 方法2: 从环境变量获取容器启动时间
355+ if (!$ start_time ) {
356+ $ container_start = getenv ('CONTAINER_START_TIME ' );
357+ if ($ container_start ) {
358+ $ start_time = strtotime ($ container_start );
359+ }
360+ }
361+
362+ // 方法3: 从Docker inspect获取容器启动时间
363+ if (!$ start_time ) {
364+ $ container_id = getenv ('HOSTNAME ' );
365+ if ($ container_id ) {
366+ $ inspect_output = shell_exec ("docker inspect {$ container_id } 2>/dev/null | grep -o ' \"StartedAt \": \"[^ \"]* \"' | cut -d' \"' -f4 " );
367+ if ($ inspect_output ) {
368+ $ start_time = strtotime (trim ($ inspect_output ));
369+ }
370+ }
371+ }
372+
373+ // 方法4: 从容器内文件创建时间获取
374+ if (!$ start_time ) {
375+ $ container_files = ['/etc/hostname ' , '/etc/hosts ' , '/.dockerenv ' ];
376+ foreach ($ container_files as $ file ) {
377+ if (file_exists ($ file )) {
378+ $ start_time = filemtime ($ file );
379+ break ;
380+ }
361381 }
362382 }
363383
@@ -461,22 +481,18 @@ function getContainerInfo() {
461481
462482// 获取进程内存占用
463483function 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 ) {
484+ // 使用更简单的方法:直接使用 top 命令获取内存信息
485+ $ top_output = shell_exec ('top -bn1 2>/dev/null ' );
486+ if (!$ top_output ) {
470487 return '未运行 ' ;
471488 }
472489
473- $ lines = explode ("\n" , $ all_processes );
490+ $ lines = explode ("\n" , $ top_output );
474491 $ total_memory = 0 ;
475492 $ process_count = 0 ;
476493
477494 foreach ($ lines as $ line ) {
478495 if (trim ($ line )) {
479- // 根据进程名称匹配
480496 $ matched = false ;
481497
482498 switch ($ process_name ) {
@@ -503,11 +519,11 @@ function getProcessMemory($process_name) {
503519
504520 if ($ matched ) {
505521 $ parts = preg_split ('/\s+/ ' , trim ($ line ));
506- if (count ($ parts ) >= 6 ) {
507- // ps aux 格式: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
508- $ rss = intval ($ parts [5 ]); // RSS 在第6列
509- if ($ rss > 0 ) {
510- $ total_memory += $ rss ;
522+ if (count ($ parts ) >= 4 ) {
523+ // top 格式: PID USER %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
524+ $ vsz = intval ($ parts [4 ]); // VSZ 在第5列
525+ if ($ vsz > 0 ) {
526+ $ total_memory += $ vsz ;
511527 $ process_count ++;
512528 }
513529 }
0 commit comments