This article will show how to deploy a simple monitoring application in the Cloud in a couple of minutes using statusdashboard and Heroku.
I already introduced StatusDashboard several months ago with a small websocket client I wrote allowing to connect to the web application and streaming monitoring data directly in the terminal. The monitoring data is produced by the server which is periodically trying to call a set of defined services (HTTP, HTTPs, FTP, TCP, UDP, …).
In the last version, you can now embed StatusDashboard in any node application with a really simple to use API. This is especially nice to customize it for your needs, for example, getting settings from a configuration file, from a remote service or whatever:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"title": "Platform Status Sample", | |
"hostname": "0.0.0.0", | |
"port": 8080, | |
"client": { | |
"transports": [] | |
}, | |
"services": [ | |
{ | |
"name": "BLOG", | |
"label": "BLOG", | |
"check": "http", | |
"host": "chamerling.org", | |
"port": 80, | |
"path": "/" | |
}, | |
], | |
"plugins" : { | |
"console" : { | |
"enable": false | |
}, | |
"irc" : { | |
"enable": false, | |
"server": "irc.freenode.net", | |
"nick": "mystatusdashboardsample", | |
"options": { | |
"debug": false, | |
"channels": ["#mystatusdashboardsample"] | |
} | |
}, | |
"heartbeat": { | |
"enable": true, | |
"period": 20000 | |
} | |
}, | |
"serviceInterval": 20000, | |
"serviceDelay": 500 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs') | |
, util = require('util') | |
, server = require('statusdashboard/server') | |
var input = fs.readFileSync('./config.json', 'utf-8'); | |
var settings = JSON.parse(input); | |
// update for Heroku | |
settings.port = process.env.PORT || settings.port; | |
process.on('exit', function () { | |
util.log('Bye bye Statusdashboard.'); | |
}); | |
process.on('uncaughtException', function(err) { | |
util.log(err.stack.replace(/^ /gm, ' ')); | |
}); | |
var dashboard = new server.dashboard(settings, function() { | |
console.log('Server is started with settings', settings); | |
}); |
Let’s deploy it on Heroku…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git clone http://github.com/chamerling/statusdashboard-server.git | |
cd statusdashboard-server | |
heroku login | |
# … | |
heroku create | |
# Will create all the required stuff on heroku (git repository) and add a remote in your git repository. Takes 5 seconds. | |
git push heroku master | |
# Some hooks will detect the nodejs app, launch npm… Takes 30 seconds. | |
heroku open |
There is one problem with the current approach: When running such an application on Heroku with the default Heroku Plan, it will ends when nobody browse it. This will stop the monitoring loop which is not really useful for a monitoring app… In order to avoid such behavior, I added a heartbeat mechanism last night which is configured from the application settings. It also needs to define some environment variable with the heroku client. Let’s assume that your application is running on http://YOURAPP.herokuapp.com, you have to define this variable like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
heroku config:add HEARTBEAT_URL=http://YOURAPP.herokuapp.com |
It will restart your application (if not, restart it manually), will start to ping itself and keep the application alive.
The code of this article is available at https://github.com/chamerling/statusdashboard-server. You get fork/clone/whatever and start monitoring your services in the next minutes…
And the proof that it works in less than 2 minutes (with a poor Internet connection, and taking time to browse source code…)