Download Server Monitor(web-linux)/core/metrics.js from mchuca/ServerMonitor: direct link, hf CLI and curl.
- Browser
- Download file 3.89 kB
-
https://huggingface.co/mchuca/ServerMonitor/resolve/main/Server%20Monitor(web-linux)/core/metrics.js
- Command line
-
hf download 'hf://mchuca/ServerMonitor/Server Monitor(web-linux)/core/metrics.js'
-
curl -L -o metrics.js 'https://huggingface.co/mchuca/ServerMonitor/resolve/main/Server%20Monitor(web-linux)/core/metrics.js'
3.89 kB
| // One parsed sample (Metrics) and the frame helpers shared by the platform parsers. | |
| const num = s => +s || 0; | |
| const ZERO = ('cpuCount loggedUsers procsRunning procsBlocked threadsTotal socketsUsed servicesFailed servicesRunning uptime load1 load5 load15 ' + | |
| 'cpuUsage cpuUser cpuSystem cpuIowait cpuSteal cpuIrq ctxtPerSec intrPerSec forksPerSec avgMhz memTotal memFree memAvailable memUsed buffers cached ' + | |
| 'shared slab dirty swapTotal swapUsed memPct swapPct diskReadBps diskWriteBps maxDiskPct maxDiskUtil netRxBps netTxBps maxTemp').split(' '); | |
| const blank = now => ({ | |
| valid: false, timestamp: now, hostname: '', os: '', kernel: '', arch: '', cpuModel: '', virt: '', fullestMount: '', busiestDisk: '', | |
| ...Object.fromEntries(ZERO.map(k => [k, 0])), cores: [], mounts: [], disks: [], nets: [], tcp: {}, procs: [], services: [], temps: [], gpus: [], | |
| }); | |
| // "@@S <section>" + rows -> { section: [rows] } | |
| function sections(frame) { | |
| const sec = {}; | |
| let cur; | |
| for (const raw of frame.split('\n')) { | |
| const l = raw.replace(/\r$/, ''); | |
| if (l.startsWith('@@S ')) cur = sec[l.slice(4).trim()] ??= []; | |
| else if (cur && l && !l.startsWith('@@')) cur.push(l); | |
| } | |
| return sec; | |
| } | |
| const kv = (lines = [], into = {}) => { | |
| for (const l of lines) { | |
| const e = l.indexOf('='); | |
| if (e > 0) into[l.slice(0, e)] = l.slice(e + 1).trim(); | |
| } | |
| return into; | |
| }; | |
| // per-second rates of counter vectors ("net:eth0" -> [rx, tx, ...]); the frame's counters replace the previous ones | |
| function rater(st, dt) { | |
| const prev = st.ctr || {}, next = st.ctr = {}; | |
| return (key, v) => { | |
| const p = prev[key]; | |
| next[key] = v; | |
| return v.map((x, i) => dt > 0.05 && p?.length === v.length ? Math.max(0, (x - p[i]) / dt) : 0); | |
| }; | |
| } | |
| function addDisk(m, d, counted) { | |
| if (counted) { | |
| m.diskReadBps += d.readBps, m.diskWriteBps += d.writeBps; | |
| if (d.util >= m.maxDiskUtil) m.maxDiskUtil = d.util, m.busiestDisk = d.name; | |
| } | |
| m.disks.push(d); | |
| } | |
| function addMount(m, mt) { | |
| mt.pct = mt.used + mt.avail > 0 ? mt.used * 100 / (mt.used + mt.avail) : 0; | |
| if (mt.pct >= m.maxDiskPct) m.maxDiskPct = mt.pct, m.fullestMount = mt.mount; | |
| m.mounts.push(mt); | |
| } | |
| // what both parsers do last: derived values, sorting, cached GPU / service lists, temperatures ("name|value", value × scale = °C) | |
| function finish(m, st, sec, tempScale) { | |
| m.memUsed = Math.max(0, m.memTotal - m.memAvailable); | |
| m.memPct = m.memTotal > 0 ? m.memUsed * 100 / m.memTotal : 0; | |
| m.swapPct = m.swapTotal > 0 ? m.swapUsed * 100 / m.swapTotal : 0; | |
| m.nets.sort((a, b) => a.virt - b.virt || (a.name < b.name ? -1 : +(a.name > b.name))); | |
| for (const n of m.nets) if (!n.virt) m.netRxBps += n.rxBps, m.netTxBps += n.txBps; | |
| for (const p of m.procs) p.memPct = m.memTotal > 0 ? p.rss * 100 / m.memTotal : 0, m.threadsTotal += p.threads; | |
| m.procs.sort((a, b) => b.cpu - a.cpu || b.rss - a.rss); | |
| if (sec.gpu) | |
| st.gpus = sec.gpu.map(l => l.split(',').map(x => x.trim())).filter(f => f.length >= 8) | |
| .map(f => ({ index: Math.trunc(num(f[0])), name: f[1], util: num(f[2]), memUsed: num(f[3]), memTotal: num(f[4]), temp: num(f[5]), power: num(f[6]), fan: num(f[7]) })); | |
| m.gpus = st.gpus || [], m.services = st.services || []; | |
| for (const s of m.services) m.servicesFailed += s.active === 'failed', m.servicesRunning += s.sub === 'running'; | |
| for (const l of sec.thermal || []) { | |
| const bar = l.lastIndexOf('|'), name = l.slice(0, Math.max(0, bar)).trim(), celsius = num(l.slice(bar + 1)) * tempScale; | |
| if (bar <= 0 || celsius <= 0 || celsius > 150 || m.temps.some(t => t.name === name)) continue; | |
| m.temps.push({ name, celsius }), m.maxTemp = Math.max(m.maxTemp, celsius); | |
| } | |
| } | |
| module.exports = { num, blank, sections, kv, rater, addDisk, addMount, finish }; | |