fix: 修复容器运行时长显示(使用/proc计算实际容器运行时间)
This commit is contained in:
@@ -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"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user