CPU Monitor Library and CLI tool
npm install cpumonCpuMonitor class is based on standard NodeJS EventEmitter.
You can subscribe to cpudata events which would be generated
according to the provided sampling interval. The event would
also have the data of cpu usage percentage for each cpu core.
There is no CJS build provided. Only ESM is provided, so use import - not require.
``sh`
npx cpumon@latest
`sh`
npm i cpumon
`javascript
import { CpuMonitor } from 'cpumon';
const monitor = new CpuMonitor(1000);
monitor.on(
'cpudata',
(load) => console.log(load)
);
`
`javascript
import { CpuInfo, CpuMonitor } from 'cpumon';
const monitor = new CpuMonitor(1000);
monitor.on(
'cpudata',
(load: CpuInfo[]) => console.log(load)
);
`
You can stop monitoring cpu by calling .stopMonitor() method.
For example, to stop monitoring after 5 min, you can use the following code:
`javascript`
setTimeout(() => monitor.stopMonitor(), 5601000);
`javascript``
type CpuInfo = {
model: string;
idle: number;
load: number;
total: number;
loadRatio?: number;
loadPercentage?: number;
}