Files
wekan/server/lib/ramMonitor.js
T
Lauri Ojansivu cfd9a62466 Swap alone is not high RAM usage, and resource monitors are not named like vulnerabilities.
A developer laptop with many programs open showed in Admin Panel /
Problems / Memory usage: "RamBleed, high, high RAM usage started
(>= 90%): RAM 16.1/30.9 GB (52%), swap 18.7/30.9 GB (60%)". Swap at or
above 25% started a high-memory period by itself, made it high severity,
and the text still said ">= 90%". Idle pages in swap while RAM is free
are not pressure.

- Swap now counts only while RAM is also at or above the low threshold
  (80%), and a period ends when RAM is back under it.
- The start row says what started it: (RAM >= 90%) or (swap >= 25% with
  RAM >= 80%). Only RAM at 95% or more is high severity.
- The RAM, CPU, disk and speed monitors named their rows like Hall of
  Fame entries (RamBleed, CpuBleed, DiskBleed, SlowBleed) with CWE-400.
  They are HighRamUsage, HighCpuUsage, HighDiskUsage and SlowRequest now,
  without a CWE, and stored rows are renamed once at startup.

tests/ramHighTracker.test.cjs replays the laptop's numbers as a
negative case; tests/resourceStreamNames.test.cjs pins the names.

Thanks to xet7 !
2026-11-19 00:21:33 +02:00

107 lines
4.4 KiB
JavaScript

// ============================================================================
// WeKan RAM-usage monitor (Admin Panel → Problems → RAM usage).
// docs/Features/Admin-Panel/Problems/RAM-usage.md
// ----------------------------------------------------------------------------
// Samples system-wide memory and swap on an interval and records only the
// START and END of each sustained high-usage period to the 'ram' event stream,
// with WeKan's own memory and what it was doing. Observe-and-report only:
// nothing is slowed down or freed. Best-effort and never throws; without
// /proc/meminfo (not Linux) it uses what Node reports and has no swap.
// ============================================================================
import { Meteor } from 'meteor/meteor';
import os from 'os';
import fs from 'fs';
const { intEnv } = require('/models/lib/intEnv');
const { parseMeminfo, memoryUsage, HighRamTracker } = require('/models/lib/ramHighTracker');
const ENABLED = String(process.env.WEKAN_RAM_MONITOR || 'true').toLowerCase() !== 'false';
const INTERVAL_MS = Math.max(1000, intEnv('WEKAN_RAM_SAMPLE_INTERVAL_MS', 5000));
const HIGH_PCT = intEnv('WEKAN_RAM_HIGH_PERCENT', 90);
const LOW_PCT = intEnv('WEKAN_RAM_LOW_PERCENT', 80);
const SWAP_HIGH_PCT = intEnv('WEKAN_SWAP_HIGH_PERCENT', 25);
const ENTER_SAMPLES = Math.max(1, intEnv('WEKAN_RAM_HIGH_SAMPLES', 3));
const EXIT_SAMPLES = Math.max(1, intEnv('WEKAN_RAM_LOW_SAMPLES', 3));
const tracker = new HighRamTracker({
highPct: HIGH_PCT, lowPct: LOW_PCT, swapHighPct: SWAP_HIGH_PCT,
enterSamples: ENTER_SAMPLES, exitSamples: EXIT_SAMPLES,
});
let last = null;
function readMeminfo() {
try {
return parseMeminfo(fs.readFileSync('/proc/meminfo', 'utf8'));
} catch (e) {
return {};
}
}
export function sampleMemory() {
return memoryUsage({ totalmem: os.totalmem(), freemem: os.freemem(), meminfo: readMeminfo() });
}
const gb = bytes => (bytes / (1024 ** 3)).toFixed(1);
function describe(sample) {
const parts = [`RAM ${gb(sample.ramUsed)}/${gb(sample.ramTotal)} GB (${sample.ramPct}%)`];
parts.push(sample.swapPct == null
? 'no swap'
: `swap ${gb(sample.swapUsed)}/${gb(sample.swapTotal)} GB (${sample.swapPct}%)`);
parts.push(`WeKan RSS ${gb(process.memoryUsage().rss)} GB`);
try {
const { currentActivityLabel } = require('/server/lib/cpuMonitor');
const activity = currentActivityLabel();
if (activity) parts.push(`WeKan: ${activity}`);
} catch (e) { /* the CPU monitor is optional */ }
return parts.join(', ');
}
// For Admin Panel → Problems → RAM usage: the latest sample, and whether a
// high period is open. Reads module state; with the monitor off it samples now.
export function getCurrentMemory() {
const sample = ENABLED && last ? last : sampleMemory();
return { ...sample, high: tracker.isHigh(), highPercent: HIGH_PCT, swapHighPercent: SWAP_HIGH_PCT, monitored: ENABLED };
}
if (Meteor.isServer && ENABLED) {
Meteor.startup(() => {
const { record } = require('/server/lib/ramLog');
Meteor.setInterval(() => {
try {
const sample = sampleMemory();
if (sample.ramPct == null) return;
last = sample;
const now = Date.now();
const res = tracker.update(sample, now);
if (res.event === 'start') {
// Say what started it: RAM over its threshold, or swap while RAM is
// also tight. Only RAM near full is `high`; swap is not (ramHighTracker.js).
const reason = sample.ramPct >= HIGH_PCT
? `RAM >= ${HIGH_PCT}%`
: `swap >= ${SWAP_HIGH_PCT}% with RAM >= ${LOW_PCT}%`;
record({
action: 'detected',
severity: sample.ramPct >= 95 ? 'high' : 'medium',
detail: `high RAM usage started (${reason}): ${describe(sample)}`,
});
} else if (res.event === 'end') {
const secs = Math.round((res.durationMs || 0) / 1000);
const peakSwap = res.peakSwap == null ? '' : `, peak swap ${res.peakSwap}%`;
record({
action: 'remediated',
severity: 'info',
at: new Date(now),
detail: `high RAM usage ended after ${secs}s (peak RAM ${res.peakRam}%${peakSwap}, back under ${LOW_PCT}%): ${describe(sample)}`,
});
}
} catch (e) {
if (process.env.DEBUG === 'true') console.warn('ramMonitor sample failed:', e && e.message);
}
}, INTERVAL_MS);
});
}
export default { getCurrentMemory, sampleMemory };