UNPKG

@znode/os-usage

Version:

os cpu/memory/disk usage

63 lines (62 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCPUCount = exports.cpu = void 0; /** * Inpsired by os-utils */ const os = require("os"); const execa_1 = require("@znode/execa"); async function cpu() { const count = await getCPUCount(); const stats1 = await getCPUInfo(); const startIdle = stats1.idle; const startTotal = stats1.total; return new Promise((resolve) => { setTimeout(async function () { const stats2 = await getCPUInfo(); const endIdle = stats2.idle; const endTotal = stats2.total; const idle = endIdle - startIdle; const total = endTotal - startTotal; const free = ~~((idle / total) * 100).toFixed(1); resolve({ count, free, used: 100 - free, }); }, 500); }); } exports.cpu = cpu; async function getCPUInfo() { const cpus = os.cpus(); let user = 0; let nice = 0; let sys = 0; let idle = 0; let irq = 0; let total = 0; for (const cpu in cpus) { if (!cpus.hasOwnProperty(cpu)) continue; user += cpus[cpu].times.user; nice += cpus[cpu].times.nice; sys += cpus[cpu].times.sys; irq += cpus[cpu].times.irq; idle += cpus[cpu].times.idle; } total = user + nice + sys + idle + irq; return { idle, total, }; } async function getCPUCount() { try { return +(await execa_1.default `cat /proc/cpuinfo | grep processor | wc -l`); } catch (error) { return -1; } } exports.getCPUCount = getCPUCount;