(Roll Your Own) Website Monitoring in Azure

Web Endpoint Status

A new ‘web endpoint status’ feature recently appeared in the Azure portal to automatically monitor Azure Websites for you.

Untitled

The system will poll up to two endpoints for you, every 5 minutes from up  to 3 locations (i.e. other data centres).

Clicking on the name of your endpoint (‘Home’ in this case) will give you the last 5 ‘ping’ times from each location:

Untitled

You will get a warning in the portal if your application takes more than 30 seconds to respond.

Your website must be running in ‘reserved’ mode to enable this feature.

For me, 30 seconds is a long time. If your website is taking that long to respond, you’ve got a problem. I’d also like to monitor websites running in the free and shared modes. There are services out there which offer comprehensive monitoring, pingdom being the obvious example. However, I thought it would be interesting to roll my own..

Roll Your Own Monitoring

Azure Mobile Services allows you to schedule a task every 15 minutes. This task is actually a Node.js process, and the ‘request‘ library is one of the few packages available. This is my simple monitor:

 

var request = require('request');

function CheckWebsiteHealth() {
    var options = {
    	uri: "http://XYZ.azurewebsites.net/",
    	timeout : 3 * 1000 //ms
    }

    var startTime = new Date().getTime();
    request(options, function(error, response, body){
    	var endTime = new Date().getTime();
    	if (error){
    		console.error(error);
    	} else {
    		console.log("ping response in " + (endTime - startTime) + "ms");
    	}
    });
}

I have chosen to only allow 3 seconds for the site to respond, this is just an option you can choose what is appropriate for you. You could also extend this code to monitor multiple sites.

It writes out to the Mobile Services log every time it makes the request. The entry will show up as an error if the site returns an error.

Untitled

One advantage here is that you get far more history than just the 5 most recent pings.

It would be fairly straight forward to call send an email using SendGrid or simliar service. Alternatively you could write this data out to table storage, and hook up a nice application with a graph…

Advertisements