服务状态显示内存/CPU/磁盘负载,版本升到2.1.0

This commit is contained in:
2026-09-22 17:25:02 +08:00
parent 7aeb31f935
commit bcc1367291
4 changed files with 101 additions and 9 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "agent-app",
"version": "2.0.0",
"version": "2.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "agent-app",
"version": "2.0.0",
"version": "2.1.0",
"dependencies": {
"better-sqlite3": "11.3.0",
"cron-parser": "5.10.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "agent-app",
"version": "2.0.0",
"version": "2.1.0",
"description": "自用通用 Agent 系统:多模型调度 + 任务目录 + 工具调用 + H5 Web UI",
"main": "server/server.js",
"scripts": {
+37 -4
View File
@@ -396,16 +396,49 @@
function kvPlain(label, v) {
return '<div class="cfg-kv"><label>' + esc(label) + '</label><b>' + esc(dispVal(v)) + '</b></div>';
}
// 容量格式化(内存/磁盘量级到 GB/TB,fmtSize 只到 MB 不够用);数字与单位不留空格
function fmtCap(n) {
if (n == null || !Number.isFinite(n)) return '?';
if (n < 1024) return n + 'B';
const units = ['KB', 'MB', 'GB', 'TB'];
let i = -1;
do { n /= 1024; i++; } while (n >= 1024 && i < units.length - 1);
return (n >= 100 ? Math.round(n) : n.toFixed(1)) + units[i];
}
// 带阈值的 kv 行:warn 时值标红(接收拼好的纯文本,内部自行 esc)
function kvWarn(label, text, warn) {
return '<div class="cfg-kv"><label>' + esc(label) + '</label><b' +
(warn ? ' style="color:#c0392b"' : '') + '>' + esc(text) + '</b></div>';
}
// 服务状态里的负载三行(内存/CPU/磁盘),数据来自 /api/system/status 的 sys 字段
function svcLoadHtml(st) {
const sys = st.sys || {};
let html = '';
const mem = sys.mem;
if (mem && mem.total_bytes) {
// 「剩余」= 主机 MemAvailable(含可回收 cache),不是容器配额剩余额度
html += kvWarn('内存', fmtCap(mem.avail_bytes) + ' / ' + fmtCap(mem.total_bytes) +
' (剩余 ' + Math.round(mem.avail_bytes / mem.total_bytes * 100) + '%)', mem.avail_bytes / mem.total_bytes < 0.15);
}
const cpu = sys.cpu;
if (cpu && cpu.cores) {
html += kvWarn('CPU', 'load ' + cpu.load1 + ' / ' + cpu.load5 + ' / ' + cpu.load15 +
' · ' + cpu.cores + ' 核' + (cpu.quota_cores ? '(配额 ' + cpu.quota_cores + ' 核)' : ''),
cpu.load1 > cpu.cores);
}
for (const d of (sys.disks || [])) {
const pct = d.total_bytes ? Math.round((1 - d.free_bytes / d.total_bytes) * 100) : 0;
html += kvWarn('磁盘', fmtCap(d.free_bytes) + ' / ' + fmtCap(d.total_bytes) + ' (已用 ' + pct + '%)', pct >= 90);
}
return html;
}
function svcCardHtml(st) {
let html = '<div class="settings-card"><h4>服务状态' +
(SET.pendingKeys.size ? ' <span class="badge held">待重启生效</span>' : '') + '</h4>';
html += kvPlain('版本 / Node', st.version + ' / ' + st.node);
html += kvPlain('已运行', fmtUptime(st.uptime_ms) + '(启动于 ' + new Date(st.started_at).toLocaleString() + ')');
html += kvPlain('端口', st.port);
html += kvPlain('配置文件', st.config_path);
html += kvPlain('工作区', st.workspace_root);
html += kvPlain('数据目录', st.data_root);
html += kvPlain('技能目录', st.skills_root);
html += svcLoadHtml(st);
html += kvPlain('命令执行', st.executor_url ? st.executor_url + '(远端)' : '本地');
if (SET.pendingKeys.size) html += kvPlain('待生效改动', Array.from(SET.pendingKeys).join(', '));
html += '<button class="primary-btn" id="restart-btn" style="margin-top:8px" onclick="sUI.restart()">重启服务</button>' +
+61 -2
View File
@@ -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):前端模式选择器据此渲染;