Download Server Monitor(web-window)/core/model.js from mchuca/ServerMonitor: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/mchuca/ServerMonitor/resolve/main/Server%20Monitor(web-window)/core/model.js
- Command line
-
hf download 'hf://mchuca/ServerMonitor/Server Monitor(web-window)/core/model.js'
-
curl -L -o model.js 'https://huggingface.co/mchuca/ServerMonitor/resolve/main/Server%20Monitor(web-window)/core/model.js'
3.31 kB
| // Server configuration (servers.json has the same schema in every edition), rules and the values rules look at. | |
| const crypto = require('crypto'); | |
| const plat = require('./platform'); | |
| const RULE = { id: '', metric: 'cpu', op: '>', action: 'notify', arg: '', severity: 'warning', enabled: true, value: 90, duration: 60, cooldown: 300 }; | |
| const AUTOPILOT = { // history-driven automatic control (evaluated by the analyzer every minute) | |
| enabled: true, dryRun: false, restartFailed: true, diskCleanup: false, anomalyAlerts: true, maxRestartsPerHour: 3, runawayMinutes: 10, diskForecastDays: 3, | |
| runawayCpu: 90, diskCleanupAt: 92, leakMemPressure: 90, anomalyZ: 3, runawayMode: 'renice', leakMode: 'notify', diskCleanupCommand: plat.cleanup, | |
| }; | |
| const SERVER = { id: '', name: '', group: '', host: '', user: '', keyFile: '', notes: '', port: 22, interval: plat.ui.interval, useSudo: false, enabled: true }; | |
| const newId = () => crypto.randomUUID().slice(0, 12); | |
| const defaultRules = () => [['cpu', 90, 60, 'warning'], ['mem', 90, 60, 'warning'], ['swap', 80, 120, 'warning'], ['disk', 90, 0, 'critical'], | |
| ['temp', 85, 30, 'critical'], ['load', 150, 120, 'warning'], ['failed', 0, 0, 'warning']] | |
| .map(([metric, value, duration, severity]) => ({ ...RULE, id: newId(), metric, value, duration, severity })); | |
| // the fields of `def`, taken from `o` where present and converted to the default's type (strings trimmed) | |
| const pick = (def, o = {}) => Object.fromEntries(Object.entries(def).map(([k, d]) => [k, o[k] == null ? d | |
| : typeof d === 'number' ? (Number.isFinite(+o[k]) ? +o[k] : d) : typeof d === 'boolean' ? !!o[k] : String(o[k]).trim()])); | |
| // a complete ServerConfig from any object (stored JSON or a form sent by the page); the password is kept in clear in memory | |
| function make(o, password) { | |
| const c = { ...pick(SERVER, o), transport: o.transport === 0 ? 0 : 1, password, autopilot: pick(AUTOPILOT, o.autopilot) }; // 0 Local, 1 SSH | |
| c.rules = Array.isArray(o.rules) ? o.rules.map(r => pick(RULE, r)) : defaultRules(); | |
| for (const r of c.rules) r.id ||= newId(); | |
| c.interval = Math.min(300, Math.max(1, Math.round(c.interval))), c.id ||= newId(); | |
| return c; | |
| } | |
| const KEY = Buffer.from('ServerMonitor/obfuscation/v1'); | |
| const xor = (s, enc) => Buffer.from(Buffer.from(s, enc ? 'utf8' : 'base64').map((b, i) => b ^ KEY[i % KEY.length])).toString(enc ? 'base64' : 'utf8'); // NOT encryption | |
| const gpuMax = (m, k) => Math.max(0, ...m.gpus.map(g => g[k])); | |
| module.exports = { | |
| RULE, make, defaultRules, | |
| fromJson: o => make(o, xor(String(o.password || ''), false)), | |
| toJson: c => ({ ...c, password: xor(c.password, true) }), | |
| forPage: c => ({ ...c, password: undefined, hasPassword: !!c.password }), // stored passwords never leave the server | |
| address: c => c.transport === 0 ? 'this machine' : (c.user && c.user + '@') + c.host + (c.port !== 22 ? ':' + c.port : ''), | |
| gpuMax, | |
| metricValues: m => ({ | |
| cpu: m.cpuUsage, iowait: m.cpuIowait, load: m.load1 * 100 / Math.max(1, m.cpuCount), mem: m.memPct, swap: m.swapPct, disk: m.maxDiskPct, | |
| diskio: m.maxDiskUtil, temp: m.maxTemp, rx: m.netRxBps / 1e6, tx: m.netTxBps / 1e6, failed: m.servicesFailed, procs: m.procs.length, | |
| gpu: gpuMax(m, 'util'), gputemp: gpuMax(m, 'temp'), | |
| }), | |
| }; | |