[ Short link to this article if you need it - http://goo.gl/7FYpwr - or retweet me ]
I recently set up a new web server at home using Node.js. Since it would be used in production I wanted to keep an eye on CPU/Memory load etc to check for any capacity issues on the machine. To do this I used the IBM SDK for Node.js and the IBM Health Center. In this document I'm going to describe how you can do the same!
Install the IBM SDK for Node.js
Don't be scared by the name, this is fundamentally just the name for the IBM distribution of the runtime. It runs in the same way as Joyent's. The main differences being that it the installation is performed using InstallAnywhere, and there are also additional versions for POWER (Linux and AIX) and Z (Linux) architectures, and has some hooks for the Health Center and other IBM tools.
You can download the IBM SDK for Node.js from developerWorks.
Note that the installer requires a JRE to be available on your system. If you've never used InstallAnywhere to install a product, the install documentation is available here but it boils down to to three options (make sure you run "chmod u+x" against the downloaded file first if you're on an *IX platform):
- Execute the downloaded file and it'll fire up a GUI to perform the install if a suitable display is available
- Execute the downloaded file with the "-i console" parameter to perform an interactive text mode install (this will happen automatically if no display is available)
Run the installer with "-i silent" to perform a silent installation. For this to work you'll need to create installer.properties in the current directory (the name can be overridden) containing the path to install, and an indication that you have read and accepted the license agreement e.g.
If you ever want to remove it, then navigate to "_node_installation" in the installed directory and run "./uninstall" - including "-i silent" as a parameter if you want it done non-interactively.
Install and run an MQTT broker
An MQTT broker is used between Node and the Health Center. Data is fed into the broker and the Health Center connects to it and reads the data. The same broker can be reused for multiple instances of Node.js.
The two simplest options for getting an MQTT broker are:
- "npm install mosca" - this installs the mosca MQTT broker which is written in node.js. You can then start it with node_modules/mosca/bin/mosca
- Install and run the Apache mosquitto broker which is written in C. Packages are available for several distributions, or you can download the source and compile it yourself.
Either of these options will start the broker listening on port 1883, which conveniently is where the IBM SDK for Node.js defaults to.
Start your Node.js application with the agent
The Health Center Agent is the piece of code included with the IBM SDK for Node.js which sends the statistics data to the MQTT broker. It can be configured via ./lib/node_modules/healthcenter/healthcenter.properties which is a plain text file you can edit. By default it will try to connect to a broker on localhost:1883. If you're broker is running elsewhere or requires authentication, edit the file to include the appropriate details.
Once you have it configured, start up your application with "node-hc" instead of "node" which will trigger the agent to load. There is a very small overhead of using the agent, which is why it is not started by default.
Install the Health Center UI
The Health Center is part of the IBM Monitoing and Diagnostic suite of tools which were originally written for java, but now support Node.js as well from version 3. It runs on top of the Eclipse UI, so head over to http://www.eclipse.org/downloads/ and grab a version for your platform if you don't already have it (It doesn't matter too much which variant, but the "Eclipse IDE for Java developers" is the one I usually pick.
Once you've got it, extract and launch it (eclipse/eclipse), give it a workspace directory to play in, then go to "Help" -> "Eclipse Marketplace" variant, search for "Health Center" and you should see the IBM one show up as per the screen shot below. Click "Install" to go through the plugin install, then restart when prompted
You can now go to "Window" -> "Open Perspective" -> "Other" -> "Health Center Status Summary" to switch to the Health Center perspective which will then show on the left of the screen. Go to "File" -> "New Connection" and input the host and port details of your broker - be sure to click the "MQTT" tab instead of "JMX". It will detect all agents connected to the broker and you can select the one you want. Then you can select the view you want. Here is an example of the CPU view:
Granted, this particular view isn't particularly exciting (The node process indicated by the red line at the bottom wasn't particularly active when I took this trace) but that shows the sorts of data you can view. The green line was overall activity on the system. You can use the tabs at the top to switch to showing memory usage, garbage collection data, or profiling, which I'll aim to talk about in a future article.