Monitor Node.js request count with Cacti or Munin
30/04/2012 | Blog
Last time I wrote, how amazing Node.js is in terms of performance. This time I’m going to share a guide that may or may not be useful to those who moved away from Apache, Nginx or any other web server for that matter.
The problem that arises from switching to Node.js is that one looses the ability to monitor web server’s request count. Fortunately there’s a really simple way to get it back. The first step is to modify your web server’s source code to store request count and the second one is to get Cacti or Munin to read that value. Note: This guide is for Linux only (but I think it can be ported to other os’es).
STEP 1
Let’s begin with the first step. And this is really easy. Here’s the code for single Node.js instance:
var http = require('http'); var fs = require('fs'); var request_count = 0; http.createServer(function (req, res) { res.writeHead(200); res.end('Hello World\n'); request_count++; }).listen(8000); setInterval(function() { fs.writeFile('/dev/shm/nodejs_requests', request_count, function (error) { if (error) { throw error; } }); }, 5000); |
As you can see, we just increase counter on every http request and records it into memory (no need for an actual file on disk) with a set interval. In this case, the value is stored every 5 seconds. Currently it doesn’t make much sense to everly increase a single value, but bear with me – soon everything will become clear. But before that, take a look at the example with multiple Node.js instances (using cluster module):
var cluster = require('cluster'); var http = require('http'); var fs = require('fs'); var request_count = 0; if (cluster.isMaster) { for (var i = 0; i < 2; i++) { var worker = cluster.fork(); worker.on('message', function (msg) { if (msg.cmd && msg.cmd == 'notifyRequest') { request_count++; } }); } setInterval(function() { fs.writeFile('/dev/shm/nodejs_requests', request_count, function (error) { if (error) { throw error; } }); }, 5000); } else { http.Server(function (req, res) { res.writeHead(200); res.end("Hello World\n"); process.send({cmd: 'notifyRequest'}); }).listen(8000); } |
This is (shamelessly) taken and modified from the official Node.js cluster module documentation. As you can see, there’s little difference. The only changes are the addition of cluster and messaging method to notify about a request. So now that we are done with this part, lets move on to Cacti configuration which was provided by Emanuelis Norbutas. All credits for this part go to him.
STEP 2
The first thing to do is to create a shell script file, which we can store anywhere we want (for example /home/someusername/node_snmpd.sh). The content fo the file should look like this:
#!/bin/sh echo ".1.3.6.1.4.1.29121.11" echo "Counter" cat /dev/shm/nodejs_requests exit 0 |
The second line from the beginning should be the same as was used in Node.js. Now lets go to /etc/snmp/snmpd.conf (may depend on Linux distribution you are using) and add a new line:
pass .1.3.6.1.4.1.29121.11 /home/someusername/node_snmpd.sh |
Finally, lets go to Cacti and add a new template with parameters as follows:
- Template: SNMP Generic OID Template
- Data Source Type: COUNTER
- OID: .1.3.6.1.4.1.29121.11
STEP 2 ALTERNATIVE (MUNIN)
The following snippet was provided by Tomas Okmanas. All credit for this goes to him.
If you use Munin, you can monitor Node.js request count just as easily. The only thing you need to do is create a sh script at /etc/munin/nodejs_requests with the following content:
#!/bin/sh # -*- sh -*- if [ "$1" = "config" ]; then echo 'graph_title Node.js requests' echo 'graph_args --base 1000 -l 0' echo 'graph_vlabel req/s' echo 'graph_scale no' echo 'graph_category system' echo 'req.label req/s' echo 'req.type COUNTER' exit 0 fi echo -n "req.value " cat /dev/shm/nodejs_requests |
and then restart munin node with a command: `sudo /etc/init.d/munin-node restart`.
Now start your Node.js and hit the F5 in the browser multiple times. If everything was setup correctly, you should be seeing graph with the requests being drawn (wait for a few minutes). If not – check /dev/shm/nodejs_requests. Does it have a value? Is that value is higher than 0? If not, there’s something wrong with the Node.js part. If yes, recheck Cacti or Munin configuration.
If everything is working, but you are still worried that this is not the correct way to log request count, I can assure, that your worries are unfounded. Cacti Counter data source type is very smart. Even if you restart your Node.js or a variable overflow will occur (quite possible with 32-bit systems), it will still display data correctly.
So there it is. A simple way (I hope) to track your Node.js request count. Of course, this is just a basic example and you could expand this to monitor separate Node.js instances instead of all of them. Or you can make your custom tracking mechanism instead of using Cacti or Munin.
I hope you enjoyed this guide and if you have any questions – please don’t hesitate to ask.
Related posts:

Pingback: Monitor Node.js request count with Cacti or Munin | Node.js and JavaScript | Scoop.it