OS Monitor

os-monitor (GitHub: lfortin/node-os-monitor, License: MIT) by Laurent Fortin is a very handy utility module to help you monitor basic stats of the server app is running on. It functions as both event emitter and a stream so you can plug this in literally anywhere.

 

1
npm install os-monitor

Usage

Example here is based on a custom writable stream which allows you to capture all events, process them and possible pipe them to a logger or just STDOUT.

var monitor = require('os-monitor');
var stream  = require('stream');
var util    = require('util');


function EchoStream () {
  stream.Writable.call(this);
};

util.inherits(EchoStream, stream.Writable);

EchoStream.prototype._write = function (event, encoding, done) {
  event = JSON.parse(event.toString());
  console.log(new Date(event.timestamp * 1000).toString(), JSON.stringify(event));
  done();
}

monitor.start({
  delay      : 3000,
  freemem    : 1000000000,
  uptime     : 1000000,
  critical1  : 0.7,
  critical5  : 0.7,
  critical15 : 0.7,
  silent     : false,
  stream     : true,
  immediate  : false,
})
.pipe(new EchoStream());

This would produce something like:

Mon Jan 19 2015 16:11:35 GMT-0800 (PST) {"timestamp":1421712695,"type":"config","options":{"delay":3000,"freemem":1000000000,"uptime":1000000,"critical1":0.7,"critical5":0.7,"critical15":0.7,"silent":false,"stream":true,"immediate":false}}
Mon Jan 19 2015 16:11:35 GMT-0800 (PST) {"timestamp":1421712695,"type":"start"}
Mon Jan 19 2015 16:11:38 GMT-0800 (PST) {"timestamp":1421712698,"type":"monitor","loadavg":[1.95068359375,2.49609375,2.50634765625],"uptime":20909,"freemem":22675456,"totalmem":4294967296}
Mon Jan 19 2015 16:11:38 GMT-0800 (PST) {"timestamp":1421712698,"type":"loadavg1","loadavg":[1.95068359375,2.49609375,2.50634765625],"uptime":20909,"freemem":22675456,"totalmem":4294967296}
Mon Jan 19 2015 16:11:38 GMT-0800 (PST) {"timestamp":1421712698,"type":"loadavg5","loadavg":[1.95068359375,2.49609375,2.50634765625],"uptime":20909,"freemem":22675456,"totalmem":4294967296}
Mon Jan 19 2015 16:11:38 GMT-0800 (PST) {"timestamp":1421712698,"type":"loadavg15","loadavg":[1.95068359375,2.49609375,2.50634765625],"uptime":20909,"freemem":22675456,"totalmem":4294967296}
Mon Jan 19 2015 16:11:38 GMT-0800 (PST) {"timestamp":1421712698,"type":"freemem","loadavg":[1.95068359375,2.49609375,2.50634765625],"uptime":20909,"freemem":22675456,"totalmem":4294967296}

What Else?

Checkout the runnable example and github example repository. The Github project has more examples and full documentation.