Tuesday 27 August 2013

Google: Website / Internet Monitoring with Google Docs

Most company owners use website / Internet monitoring services to track the downtime and uptime of their sites / Internet.  These service offer free plans but you often have to upgrade for unlimited email  / SMS alerts or if you would like to monitor a large number of websites.

Good news is that you can now use Google Docs to monitor your website(s) and, unlike the commercial services, Google imposes no restrictions. You can track any number of websites with Google Docs and it will send email /text alerts in case of any issues. In addition, this DIY website monitoring tool is absolutely free.

Here’s how you quickly configure Google Docs to monitor the uptime /downtime of your website(s). This has to done just once and Google Docs will continuously monitor all your sites in the background.

Let’s get started:
  1. Sign-in to your Google account and then click here to copy this Google sheet into your Google Drive. You may use your Google Apps account as well.
  2. Put your website URLs in cell B2 (comma separated) and your email address in cell B3. If you wish to be alerted by text messages, just replace No with Yes in cell B4.
  3. You’ll find a new Website Monitor menu in your Google Docs toolbar. Click Initialize and you’ll get a pop-up asking for authorization. Grant the necessary access.
  4. Go to the Website Monitor menu again and choose “Start” to begin the monitoring process.  Close the Google Sheet.


That’s it. The Google Docs sheet will monitor your website in the background at 5-minute intervals and will send alerts whenever it has trouble accessing the website. If the issue is resolved, you’ll get another notification saying “all’s well.”

The uptime and downtime times get logged in the same Google Docs sheet so you can use that data to analyse the performance of your web hosting company.

Internally, there’s a simple Google Script attached to the Google Docs sheet that does the monitoring.

The script invokes itself every 5 minutes, with the magic of Script Triggers, and then tries to fetch your website using URLFetchApp.fetch (a Google function similar to wget or curl). If the HTTP response code is anything other than 200, it indicates that there’s an issue with the underlying website.

Google Apps Script can send email messages but the script employs a workaround for sending text messages.

It creates a regular event in your Google Calendar but with an SMS reminder  - the event is set to expire in 30 seconds and thus you get an instant text alert on your mobile. If you aren’t getting text alerts on your phone, please ensure that your phone number is associated with Google Calendar as detailed in this tutorial.




Google Apps Script:
/*
   Website Monitor 
   ===============
*/
function init() {
  if (ScriptApp.getScriptTriggers().length == 0) {
    // Set up a monitor that triggers every 5 minutes
    ScriptApp.newTrigger("websiteMonitor")
      .timeBased().everyMinutes(5).create();
  }
}

function websiteMonitor()
{
  var response, error, code, urls;

  // The script can monitor multiple website URLs (comma separated)
  urls = SpreadsheetApp.getActiveSheet().getRange("B2").getValue();
  urls = urls.replace(/\s/g, "").split(",");
 
  for (var i=0; i<urls.length; i++) {
   
    var url = urls[i];
     
    if (!ScriptProperties.getProperty(url)) {
        ScriptProperties.setProperty(url, 200);
    }
   
    // Trying to connect to the website URL
    try {  
      response = UrlFetchApp.fetch(url);
    } catch(error) {
      // If URLFetchApp fails, the site is probably down
      updateLog(url, -1);
      continue;
    }
   
    code = response.getResponseCode();
    updateLog(url, code);
   
  }
}

function updateLog(url, code) {
 
  if (ScriptProperties.getProperty(url) == code)
    return;
 
  ScriptProperties.setProperty(url, code);
 
  var sheet = SpreadsheetApp.getActiveSheet();
 
  var row   = sheet.getLastRow() + 1;  
  var time  = new Date();
  var msg = "Down";
 
  if (code == 200)
    msg = "Up";
 
  msg = "Website is " + msg + "  " + url;
 
  sheet.getRange(row,1).setValue(time);
  sheet.getRange(row,2).setValue(msg);
 
  // Send an email notification when the site status changes
  var email = sheet.getRange("B3").getValue();
  MailApp.sendEmail(email, msg, url);
 
  var now   = new Date(time.getTime() + 10000);

  // Create an event in Google Calendar with an SMS reminder
  if (sheet.getRange("B4").getValue().toLowerCase() == "yes")
    CalendarApp.createEvent(msg, now, now).addSmsReminder(0);
}


Reference:
1. Monitor your Website’s Uptime with Google Docs

No comments:

Post a Comment