For a while now I’ve been working on a node.js based tracker. This is basically a pixel that you attach to any website and it captures information about the visitors. With this development comes the (not small, not irrelevant) task of monitoring the app and the server to be able to define tests as “successful” or “in need of more testing”.
So, I have built a small piece of code that goes within the tracker server, and it repeatedly captures system information and stores it in a Redis database. Another script returns a set of graphs that feed from the Redis information and present the relevant numbers in a visual fashion.
What it does:
- Sets a loop to be executed every 5 seconds.
- Reads information from system resource files.
- Stores it in a Redis database.
- Purges expired stats records.
The other limb of this code is the script which presents the information to the end-user browser. These two pieces of code can be put together into one script, but in my case I needed to capture some information of the actual process being monitored, thus monitoring code went to one script and stats into another.
What’s happening here is:
- If the request is /live.html, return the static file.
- If not, return the stats from Redis db.
I’ve done some hacking to be able to return the stats in a json readable way.
Also, I’ve been checking around and trying Marcos Brasil’s code. He’s building a node monitor that resembles mine in some ways, but is currently focusing on CPU usage relying on internal node.js objects.
I must say he did a very good job at keeping the code and implementation clean. I’m in the process of making my node.js production tidier, so I’ll follow examples like these which make install as simple as “npm install”.
That said, I think some graphs and visual organization of the info is critical for the user to understand what this means…
- I’ve built the stats (live.html file) using the highcharts.com library. I highly recommend giving it a try.
- You should check out nodetime.com. A very impressive real-time monitoring app that is going through beta stage.
- I’m sharing all this code (including live.html) in this repo, for you to fork.
- I’ll soon update the code to alter the way I fetch CPU usage. I must say I prefer the internal node.js objects to the Linux’ info flat file.