2.0.3 • Published 5 months ago

os-monitor v2.0.3

Weekly downloads
3,451
License
MIT
Repository
github
Last release
5 months ago

os-monitor

NPM

Node.js (install, build and test)

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.

Installation

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

Synopsis

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);

config options

delay

Delay in milliseconds between each monitor cycle. Default: 3000

freemem

Amount of memory in bytes under which event 'freemem' is triggered. Can also be a percentage of total memory. Default: 0

uptime

Number of seconds over which event 'uptime' is triggered. Default: undefined

diskfree

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: {}

critical1

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)

critical5

Value of 5 minutes load average over which event 'loadavg5' is triggered. Default: os.cpus().length

critical15

Value of 15 minutes load average over which event 'loadavg15' is triggered. Default: os.cpus().length

silent

Set true to mute event 'monitor'. Default: false

stream

Set true to enable the monitor as a Readable Stream. Default: false

immediate

Set true to execute a monitor cycle at start(). Default: false

API

.version

The monitor.version property contains the os-monitor version string.

.start( options )

Starts the monitor. Accepts an optional options object.

.stop( )

Stops the monitor.

.isRunning( )

Checks whether the monitor is running or not; returns a boolean.

.config( options )

Accepts an optional options object and updates monitor config. Always returns monitor config options.

.reset( )

Resets monitor config to its default values.

.on( eventType, handler ), .addListener( eventType, handler )

Adds a listener for the specified event type. Supported events are: 'monitor', 'uptime', 'freemem', 'diskfree', 'loadavg1', 'loadavg5', 'loadavg15', 'start', 'stop', 'config', 'reset', 'destroy'.

.once( eventType, handler )

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.

.throttle( eventType, handler, delay )

Adds a throttled listener. The throttled listener will not be executed more than once every delay milliseconds.

.unthrottle( eventType, handler )

Removes a throttled listener previously added using .throttle(). handler must be the original function.

.when( eventType )

Returns a Promise that resolves with an event object when eventType is triggered.

.destroy( )

Permanently stops and disables the monitor.

.seconds( n ), .minutes( n ), .hours( n ), .days( n )

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) });

.blocks( bytes, blockSize )

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)
  }
});

Event object

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

Using the monitor as a Readable Stream

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);

Promise

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');
    // ...
}

Monitor class

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();
2.0.3

5 months ago

2.0.2

5 months ago

2.0.1

6 months ago

2.0.0

6 months ago

1.3.3

6 months ago

1.3.2

6 months ago

2.0.0-edge.3

6 months ago

2.0.0-edge.4

6 months ago

1.3.1

12 months ago

2.0.0-edge.2

11 months ago

2.0.0-edge.0

12 months ago

2.0.0-edge.1

12 months ago

1.3.0

1 year ago

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.10

3 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

5 years ago

1.0.6

6 years ago

1.0.5

8 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.6

10 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago

0.0.10

12 years ago

0.0.9

12 years ago

0.0.8

12 years ago

0.0.7

12 years ago

0.0.6

12 years ago

0.0.5

12 years ago

0.0.4

12 years ago

0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago