Monitoring Home Computers with Nodejs

After spending a couple of hours with nodejs today, here are my impressions.

  1. Very fast to get up and running, especially if you already know javascript.
  2. Function closures yes!  (Java needs these *SO BAD*).
  3. Like writing a shell script except it’s a real programming language.
  4. I like knowing that there’s no blocking I/O and no locks–let’s you focus on what you care about: moving junk around.
  5. Excellent public shared package registry with easy automatic install.  (Java needs this too–we have a clumsy version of it with Maven–but it’s sweet when it’s designed right into the platform.).
  6. Not clear which packages in the registry work on which platforms.  I had to hack the package I needed to get it to work on Windows.
  7. Unclear error messages–hard to debug.  Good for simple system integration components, but I think complex logic is better housed in a strictly typed language where you can find out what the hell’s going on when it doesn’t work.

If you’re curious about the specific thing I did with nodejs today, read on…

I live in a house with a lot of people and a lot of computers.  I’m always on people’s cases to turn their computers off when they’re not using them.  And then I thought, rather than nagging them all the time, I should just monitor when the computers are left on and  charge them for their electricity.

I looked around for a tool that would let me gather uptime stats on all the computers in our house.  Most of the tools I checked out were Linux based.  I needed a Windows one.  The most promising Windows tool was something called “Network Monitor” from Microsoft.  But the helpful folks over at Microsoft kindly told me that even it wasn’t a good fit.

So I gave up looking and rolled my own.  There’s been a lot of buzz recently about nodejs so I decided to give it a try.

To my surprise, I was able to get something up and running in a couple of hours.  It’s fast and powerful.

Here’s the program I ended up writing:

var fs = require("fs");
var ping = require('ping');
var dateutil = require('dateutil');
var hosts = [ 'dopey', 'sneezy', 'happy', 'sleepy', 'grumpy',
 'bashful', 'doc' ];
var logfilename = 'monitor_log.txt';
var log = fs.createWriteStream(logfilename, {'flags': 'a'});
function getDateString() {
 var now = new Date();
 return now.toDateString() + " " + now.toLocaleTimeString();
console.log("Started " + getDateString() + "\n");
console.log("Writing output to "+logfilename);
log.write("Started " + getDateString() + "\n");
function checkHosts() {
  hosts.forEach(function(host) {
    ping.sys.probe(host, function(isAlive) {
       var status = isAlive ? 'up' : 'down';
       log.write(getDateString() + '\t' + host + "\t" + status + "\n");
setInterval(checkHosts, 15*60*1000);

It was almost perfect out of the box.  The thing that took me the longest was finding a ping client that worked on Windows.  The nodejs registry is really easy to use and contains multiple ping clients, but the listings don’t indicate which modules work on which platforms.  It looks to me like nodejs was written to run on Linux/Mac platforms and then ported to Windows as an afterthought.  None of the ping clients in the registry worked on Windows.  What I ended up doing was installing the “ping” module and then hacking it to run on Windows.  I changed:

var ls = spawn('/bin/ping', ['-n', '-w 2', '-c 1', addr]);


var ls    = spawn('C:/windows/system32/ping.exe', ['-n', '1', '-w', '5000', addr]);

and then it worked.  But until I fixed it, the module failed silently and it took me a while to find out why it wasn’t working.  I submitted an issue in the hopes someone might improve the module to work on Windows.

If you have a small network application you need to run, I suggest giving nodejs a try!


It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

1 thought on “Monitoring Home Computers with Nodejs

  1. Alexei Guevara
    Alexei Guevara

    polyglot programming any one ? 😉 nice short and sweet post! now I have a taste of node.js

Leave a Reply