Nodejitsu

Save time managing and deploying your node.js app

Nodejitsu has joined GoDaddy

We are excited to join GoDaddy to help spearhead their largest Node.js product: Website Builder. Please read our blog post to learn more about customer transition plans and our continued support for Open Source within the node community.

Want to know all the details?
Read the full blog post or read the GoDaddy Announcement.

npmawesome: basic OS monitoring with os-monitor

About the author

Name
Location
Worldwide
nodejitsu nodejitsu

Other popular posts

- Scaling Isomorphic Javascript Code - Keep a node.js server up with Forever - Package.json dependencies done right
- npm cheat sheet - 6 Must Have Node.js Modules

This is a guest post from Alex Gorbatchev over at npmawesome.com. Nodejitsu loved what npmawesome.com was doing and is now supporting the project.

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.

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.