Twist on Website Monitoring with Google Docs

I came across this neat Google Apps Script that monitor your website and send you an alert if it is down.

It was not a perfect fit for my needs so I made some modifications to the original script.

  • First, I have hardcoded URL (more than one in an array) in the script. Then I added a FOR loop that checks all the URLs.
  • My email is also hardcoded, it makes the spreadsheet neater.
  • Finally, I only need a flag (entry in the sheet and email) when a site is down. I don’t want to know if it’s up so I filtered out “200” responses.

One last thing I’ve done in Gmail is adding a filter to highlight the emails sent from the script (Mark as important, starred and add a specific Label).

The email once filtred in Gmail.

The email once filtred in Gmail.

Here is my version of the script, feel free to copy and change as you see fit!

/** Monitor Sites's Uptime **/
/** based on: Site's Uptime **/
/** By Amit Agarwal 26/03/2012 **/
/** **/

function isMySiteDown()
var url= new Array();
url[0] = "";
url[1] = "";
url[2] = "";

var response, error;

for (var n=url.length-1;n>=0;--n){
try {
response = UrlFetchApp.fetch(url[n]);
} catch(error)
var msg = "[UPTIME] " + url[n] + " is DOWN";
insertData(error, -1, msg);

var code = response.getResponseCode();

if (code!=200) {
var msg = "[UPTIME] " + url[n] + " is DOWN";
insertData(response.getContent()[0], code, msg);}

function insertData(error, code, msg) {

var sheet = SpreadsheetApp.getActiveSheet();
var email = "";
var row = sheet.getLastRow() + 1;

sheet.getRange(row,1).setValue(new Date());

if (code!=200)
MailApp.sendEmail(email, msg, msg+" "+error);

This entry was posted in Internet, Technology and tagged , , , , . Bookmark the permalink.

One Response to "Twist on Website Monitoring with Google Docs"