
Research
Security News
Malicious npm Package Wipes Codebases with Remote Trigger
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
os-monitor
Advanced tools
A very simple monitor for the built-in os
, fs
modules in Node.js.
Allows you to observe some OS parameters, such as free memory available, load average or free disk space.
To install the latest stable version of os-monitor
:
npm install os-monitor
If you are using an old version of Node.js (older than v18.15.x), you might need the legacy version(1.x) of os-monitor
; it supports Node.js back to v0.10.x:
npm install os-monitor@legacy
const monitor = require("os-monitor");
// basic usage
monitor.start();
// more advanced usage with configs.
monitor.start({ delay: 3000 // interval in ms between monitor cycles
, freemem: 1000000000 // freemem under which event 'freemem' is triggered
, uptime: 1000000 // number of secs over which event 'uptime' is triggered
, diskfree: {
'/': 100000, // number of free blocks under which event 'diskfree' is triggered
'/home': 100000
}
, critical1: 0.7 // loadavg1 over which event 'loadavg1' is triggered
, critical5: 0.7 // loadavg5 over which event 'loadavg5' is triggered
, critical15: 0.7 // loadavg15 over which event 'loadavg15' is triggered
, silent: false // set true to mute event 'monitor'
, stream: false // set true to enable the monitor as a Readable Stream
, immediate: false // set true to execute a monitor cycle at start()
});
// define handler that will always fire every cycle
monitor.on('monitor', (event) => {
console.log(event.type, 'This event always happens on each monitor cycle!');
});
// define handler for a too high 1-minute load average
monitor.on('loadavg1', (event) => {
console.log(event.type, 'Load average is exceptionally high!');
});
// define handler for a too low free memory
monitor.on('freemem', (event) => {
console.log(event.type, 'Free memory is very low!');
});
// define a throttled handler
monitor.throttle('loadavg5', (event) => {
// whatever is done here will not happen
// more than once every 5 minutes(300000 ms)
}, monitor.minutes(5));
// change config while monitor is running
monitor.config({
freemem: 0.3 // alarm when 30% or less free memory available
});
// stop monitor
monitor.stop();
// check whether monitor is running or not
monitor.isRunning(); // -> true / false
// use as readable stream
monitor.start({stream: true}).pipe(process.stdout);
Delay in milliseconds between each monitor cycle. Default: 3000
Amount of memory in bytes under which event 'freemem' is triggered. Can also be a percentage of total memory. Default: 0
Number of seconds over which event 'uptime' is triggered. Default: undefined
Object containing free blocks values, for given file system paths, under which event 'diskfree' is triggered. Supported from Node.js v18.15.x and later. (ref.) Default: {}
Value of 1 minute load average over which event 'loadavg1' is triggered. Default: os.cpus().length
(A Unix-specific concept, the load average is a measure of system activity, calculated by the operating system and expressed as a fractional number. As a rule of thumb, the load average should ideally be less than the number of logical CPUs in the system. ref.: http://nodejs.org/api/os.html#os_os_loadavg)
Value of 5 minutes load average over which event 'loadavg5' is triggered. Default: os.cpus().length
Value of 15 minutes load average over which event 'loadavg15' is triggered. Default: os.cpus().length
Set true to mute event 'monitor'. Default: false
Set true to enable the monitor as a Readable Stream. Default: false
Set true to execute a monitor cycle at start(). Default: false
The monitor.version
property contains the os-monitor
version string.
Starts the monitor. Accepts an optional options object.
Stops the monitor.
Checks whether the monitor is running or not; returns a boolean.
Accepts an optional options object and updates monitor config. Always returns monitor config options.
Resets monitor config to its default values.
Adds a listener for the specified event type. Supported events are: 'monitor', 'uptime', 'freemem', 'diskfree', 'loadavg1', 'loadavg5', 'loadavg15', 'start', 'stop', 'config', 'reset', 'destroy'.
Adds a one-time listener for the specified event type. This listener is invoked only the next time the event is fired, after which it is removed.
Adds a throttled listener. The throttled listener will not be executed more than once every delay milliseconds.
Removes a throttled listener previously added using .throttle()
. handler
must be the original function.
Returns a Promise that resolves with an event object when eventType
is triggered.
Permanently stops and disables the monitor.
Convenience methods to get the right amount of milliseconds.
monitor.seconds(10); // -> 10000 ms
monitor.minutes(5); // -> 300000 ms
monitor.hours(1); // -> 3600000 ms
monitor.days(1); // -> 86400000 ms
// start with a delay of 5000 ms
monitor.start({ delay: monitor.seconds(5) });
Convenience method to get the right amount of file system blocks.
monitor.blocks(100000000, 4096); // -> 24415 blocks
// start by observing file system path `/filesystem`
monitor.start({
diskfree: {
'/filesystem': monitor.blocks(100000000, 4096)
}
});
There is some useful information in the provided event object:
{
type: 'monitor', // event type
loadavg: [ 0.4599609375, 0.53076171875, 0.4990234375 ], // load average values for 1, 5, 15 minutes
uptime: 1614056, // os uptime in seconds
freemem: 241262592, // free memory available in bytes
totalmem: 2147483648, // total memory available in bytes
timestamp: 1394766898 // UNIX Timestamp
}
All supported events are: 'monitor', 'uptime', 'freemem', 'diskfree', 'loadavg1', 'loadavg5', 'loadavg15', 'start', 'stop', 'config', 'reset', 'destroy'.
Note that os-monitor
is an instance of EventEmitter
.
Events API docs: nodejs.org/api/events
os-monitor
can also be used as a Readable Stream.
monitor.start({ stream: true });
// write to STDOUT
monitor.pipe(process.stdout);
// write to a file
let fs = require('fs'),
logFile = fs.createWriteStream('/tmp/log.txt', {flags: 'a'});
monitor.pipe(logFile);
os-monitor
supports Promise, async/await: using .when(eventType)
returns a Promise.
monitor.when('freemem').then(event => {
// ...
});
async function callback() {
let event = await monitor.when('uptime');
// ...
}
Need concurrent monitor instances? The monitor class is available from the os-monitor object:
const monitor = require('os-monitor');
let monitor1 = new monitor.Monitor();
let monitor2 = new monitor.Monitor();
let monitor3 = new monitor.Monitor();
FAQs
simple OS monitoring for Node.js
The npm package os-monitor receives a total of 3,513 weekly downloads. As such, os-monitor popularity was classified as popular.
We found that os-monitor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.
Security News
New CNA status enables OpenJS Foundation to assign CVEs for security vulnerabilities in projects like ESLint, Fastify, Electron, and others, while leaving disclosure responsibility with individual maintainers.