From 0ca6dfe5a7ef4f7d83943a94eea216cd137aa92e Mon Sep 17 00:00:00 2001 From: Yu Yon Date: Thu, 8 Jan 2026 23:15:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E9=95=BF=E6=98=BE=E7=A4=BA=EF=BC=88?= =?UTF-8?q?=E4=BD=BF=E7=94=A8/proc=E8=AE=A1=E7=AE=97=E5=AE=9E=E9=99=85?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E8=BF=90=E8=A1=8C=E6=97=B6=E9=97=B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/admin_api/core.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/routes/admin_api/core.py b/routes/admin_api/core.py index 83a8434..a744c4d 100644 --- a/routes/admin_api/core.py +++ b/routes/admin_api/core.py @@ -570,10 +570,34 @@ def get_docker_stats(): logger.debug(f"读取CPU信息失败: {e}") try: - result = subprocess.check_output(["uptime", "-p"]).decode("utf-8").strip() - docker_status["uptime"] = result.replace("up ", "") + # 读取系统运行时间 + with open('/proc/uptime', 'r') as f: + system_uptime = float(f.read().split()[0]) + + # 读取 PID 1 的启动时间 (jiffies) + with open('/proc/1/stat', 'r') as f: + stat = f.read().split() + starttime_jiffies = int(stat[21]) + + # 获取 CLK_TCK (通常是 100) + clk_tck = os.sysconf(os.sysconf_names['SC_CLK_TCK']) + + # 计算容器运行时长(秒) + container_uptime_seconds = system_uptime - (starttime_jiffies / clk_tck) + + # 格式化为可读字符串 + days = int(container_uptime_seconds // 86400) + hours = int((container_uptime_seconds % 86400) // 3600) + minutes = int((container_uptime_seconds % 3600) // 60) + + if days > 0: + docker_status["uptime"] = f"{days}天{hours}小时{minutes}分钟" + elif hours > 0: + docker_status["uptime"] = f"{hours}小时{minutes}分钟" + else: + docker_status["uptime"] = f"{minutes}分钟" except Exception as e: - logger.debug(f"获取运行时间失败: {e}") + logger.debug(f"获取容器运行时间失败: {e}") docker_status["status"] = "Running"