服务状态显示内存/CPU/磁盘负载,版本升到2.1.0
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
// restart 策略(unless-stopped)自动拉起。不依赖 docker.sock、无需提权。
|
||||
// 非 docker 运行(如 npm start)时进程直接退出,不会自动重启,需自行启动。
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const engine = require('../engine');
|
||||
const { loadConfig } = require('../config');
|
||||
const versionInfo = require('../version');
|
||||
@@ -11,8 +13,9 @@ const log = require('../log');
|
||||
const router = express.Router();
|
||||
|
||||
const STARTED_AT = Date.now();
|
||||
let VERSION = '2.0.0';
|
||||
try { VERSION = require('../../package.json').version || VERSION; } catch (_) {}
|
||||
// 版本以本文件字面量为准:容器部署只挂载 app/server,镜像里的 package.json 可能是旧的,
|
||||
// 读它会一直显示发版时的版本号(之前 2.1.0 改完仍显 2.0.0 即此因)。发版时记得同步这里。
|
||||
const VERSION = '2.1.0';
|
||||
// git 提交号单独返回(提交时 post-commit 钩子自动生成),前端只显示简洁版本号
|
||||
const VERSION_COMMIT = versionInfo.commit || '';
|
||||
|
||||
@@ -26,6 +29,60 @@ function wrap(fn) {
|
||||
};
|
||||
}
|
||||
|
||||
// ---------- 运行负载采集 ----------
|
||||
// 内存读 /proc/meminfo(主机口径,容器内同样可见);CPU 配额优先读 cgroup 拿本容器自己的限制。
|
||||
function readCgroup(...paths) {
|
||||
for (const p of paths) {
|
||||
try { const s = fs.readFileSync(p, 'utf8').trim(); if (s) return s; } catch (_) {}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function collectMem() {
|
||||
// 「空闲内存」用 MemAvailable(含可回收 cache,符合直觉且贴近能用多少),
|
||||
// 而非 MemFree/cgroup 配额减法:后者会把容器视角的几百 MB 空闲误报成上 GB。
|
||||
// 读不到 /proc/meminfo(如 mac 本地调试)退回 os.*。
|
||||
let total = null, avail = null;
|
||||
try {
|
||||
const t = fs.readFileSync('/proc/meminfo', 'utf8');
|
||||
const mt = t.match(/MemTotal:\s+(\d+)\s*kB/);
|
||||
const ma = t.match(/MemAvailable:\s+(\d+)\s*kB/);
|
||||
if (mt) total = parseInt(mt[1], 10) * 1024;
|
||||
if (ma) avail = parseInt(ma[1], 10) * 1024;
|
||||
} catch (_) {}
|
||||
return {
|
||||
total_bytes: total || os.totalmem(),
|
||||
avail_bytes: avail != null ? avail : os.freemem(),
|
||||
rss_bytes: process.memoryUsage.rss(), // 本进程占用,供判断大头是不是自己
|
||||
};
|
||||
}
|
||||
function collectCpu() {
|
||||
let quota = null; // 配额换算核数(docker --cpus);无限制为 null
|
||||
const v2 = readCgroup('/sys/fs/cgroup/cpu.max'); // "<quota> <period>" 或 "max <period>"
|
||||
if (v2 != null) {
|
||||
const [q, p] = v2.split(/\s+/);
|
||||
if (q && q !== 'max') {
|
||||
const qq = parseInt(q, 10), pp = parseInt(p, 10);
|
||||
if (qq > 0 && pp > 0) quota = qq / pp;
|
||||
}
|
||||
} else {
|
||||
const q = parseInt(readCgroup('/sys/fs/cgroup/cpu/cpu.cfs_quota_us') || '', 10);
|
||||
const p = parseInt(readCgroup('/sys/fs/cgroup/cpu/cpu.cfs_period_us') || '', 10);
|
||||
if (q > 0 && p > 0) quota = q / p;
|
||||
}
|
||||
const load = os.loadavg();
|
||||
const r2 = v => Math.round(v * 100) / 100;
|
||||
return { cores: os.cpus().length, quota_cores: quota != null ? r2(quota) : null, load1: r2(load[0]), load5: r2(load[1]), load15: r2(load[2]) };
|
||||
}
|
||||
// 数据盘容量(只统计 data_root:tasks 与工作区同盘或关系不大,用户明确不要 tasks 行);目录异常则返回空数组
|
||||
function collectDisks(cfg) {
|
||||
const out = [];
|
||||
try {
|
||||
const s = fs.statfsSync(cfg.data_root);
|
||||
out.push({ path: cfg.data_root, total_bytes: s.blocks * s.bsize, free_bytes: s.bavail * s.bsize });
|
||||
} catch (_) {}
|
||||
return out;
|
||||
}
|
||||
|
||||
// 运行状态:设置页展示 + 前端重启后轮询等待恢复
|
||||
router.get('/status', wrap((req, res) => {
|
||||
const cfg = engine.getConfig();
|
||||
@@ -45,6 +102,8 @@ router.get('/status', wrap((req, res) => {
|
||||
db_path: cfg.db_path,
|
||||
executor_url: (cfg.executor && cfg.executor.url) || '',
|
||||
port: cfg.port,
|
||||
// 服务器负载(设置页服务卡片):内存/CPU 优先容器限额口径,磁盘为 data/workspace 所在盘
|
||||
sys: { mem: collectMem(), cpu: collectCpu(), disks: collectDisks(cfg) },
|
||||
auth_enabled: !!(cfg.auth && cfg.auth.users && cfg.auth.users.length),
|
||||
running_sessions: engine.activeRuns(),
|
||||
// 可用的 prompt+tools 组合名(config.yml 顶层 model_profiles):前端模式选择器据此渲染;
|
||||
|
||||
Reference in New Issue
Block a user