February 7, 2014

Using Google Docs to check your websites uptime using SMS reminders


You can’t stay looking at your machine’s monitor to keep an eye on websites uptime. So, there are some cool sites which actually help you keep track of your sites uptime or downtime. Helping you cross check the reasons behind your site’s service downtime. As unavailability of site can hit your visitor count.
Although there are many websites which give uptime and downtime services they charge for SMS reminders. So here is a cool (Hack)setup provided by amit agarwal of Digital inspiration(Labnol) fame, to monitor website's uptime using Google Docs.

Procedure

First head to download this copy  of the Google Docs Spreadsheet. Then change the website URL to your website and the email address to your email. That's the email you'll be notified for uptime and downtime. Also change SMS notifications to yes.
  • 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.
  • 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.
  • Go to the Website Monitor menu again and choose “Start” to begin the monitoring process.  Close the Google Sheet.
Then in the Google Docs menu, you'll need to go to Tools –> Script Editor . Google uses JavaScript for this scripting. Then on script editor find // Setup trigger that runs every 5 minutes
code
You can change timer to 1 or 5 or 10 or 15.Save the trigger and authorize Google Docs, then re-save.
Now again go to Website Monitor menu in your Google Docs toolbar. Click Initialize and you’ll get a pop-up asking for authorization. Grant the necessary access.
Go to the Website Monitor menu again and choose “Start” to begin the monitoring process.  Close the Google Sheet.
Here is a snapshot how it looks. I had created a dummy url to check the uptime.
down
For more hacks and tutorials please visit : Amit Agarwal's Digital Inspiration
If you are receiving the SMS notifications then please ensure that your phone number is associated with Google Calendar. Here are the steps to associate your phone with Google Calendar

Register your mobile phone on Google Calendar

To register your mobile phone with Google Calendar, you need to obtain a verification code. Here's how:
  1. Click the gear icon at the top of any page and select Settings from the drop-down menu
  2. Click the Mobile Setup tab
  3. Select your country from the Country drop-down menu
  4. Enter your phone number in the Phone number field
  5. Select your carrier from the drop-down menu, if applicable. If you see the link See Help Center for supported providers, please check if your provider is supported before requesting your verification code.
  6. If your carrier is supported, click the Send Verification Code button, and you'll get a text message on your phone
  7. Once you receive this message, enter the code you received in the Verification code field and click the Finish setup button
  8. Click Save

Also check the entire code script which works behind in this hack
/**   Website Monitor 4.0 by Digital Inspiration    **/
/**   ==========================================    **/

/**   Published by Amit Agarwal on 02/14/2013       **/
/**   Last updated by @labnol on 10/15/2013         **/

/**   Details at http://labnol.org/?p=21060         **/

/**   Support: amit@labnol.org   twitter: @labnol   **/


function onOpen() {  
  
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  
  var menu = [ 
    {name: "Step 1: Initialize", functionName: "init"},
    {name: "Step 2: Start ", functionName: "init"},
    {name: "Uninstall (Stop)", functionName: "removeJobs"}
  ];  
  
  sheet.addMenu("Website Monitor", menu);    
}

function init() {    
  
  removeJobs(true);
  
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  
  var urls = sheet.getSheets()[0]
  .getRange("B2").getValue();
  
  urls = urls.replace(/\s/g, "").split(",");
  
  var time = (new Date()).getTime();
  
  var db = ScriptDb.getMyDb();
  
  for (i=0; i<urls.length; i++) {
    db.save({url: urls[i], status:200});    
  }   
  
  // Setup trigger that runs every 5 minutes
  
  ScriptApp.newTrigger("init")
  .forSpreadsheet(sheet)
  .onEdit()
  .create();
  
  ScriptApp.newTrigger("websiteMonitor")
  .timeBased()
  .everyMinutes(1)
  .create();
  
  sheet.toast("Google Docs will now monitor your website(s). "
              + "You can exit this sheet!", "Initialized", -1);
}

function websiteMonitor() {  
  
  var db = ScriptDb.getMyDb();  
  var results = db.query({});
  
  while (results.hasNext()) {
    
    var item = results.next();    
    var code = -1;
    
    try {    
      var response = UrlFetchApp.fetch(item.url);
      code = response.getResponseCode();    
    } catch(error) {}
    
    item.status = alertUser(item, code);     
    db.save(item);
    
  }
  
}

function alertUser(item, code) {
  
  var status = item.status;
  
  // Status unchanged
  if (code === status) {
    return code;
  }
  
  // Site was up previously but is now down
  if ((code === -1) && (status === 200)) {
    // Run another check after 1 minutes to prevent false positives
    quickCheck();
    return -2;
  }
  
  // Site was down previously but up on second check
  if ((code === 200) && (status === -2)) {
    return code;
  }
  
  // Site was down previously and is down again
  if ((code === -1) && (status === -2)) {
    quickCheck();
    logMessage(item.url, "Down");
    return code;
  }
  
  // Site was down previously but up again
  if ((code === 200) && (status === -1)) {
    logMessage(item.url, "Up");
    return code;
  }
  
  return 200;
}


function logMessage(url, message) {
  
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  var row   = sheet.getLastRow() + 1;    
  var time  = new Date();  
  
  sheet.getRange(row,1).setValue(time);
  sheet.getRange(row,2).setValue(message + " : " + url);
  
  var alert = "Site " + url + " is " + message.toLowerCase();
  
  MailApp.sendEmail(sheet.getRange("B3").getValue(), "Site " + message, alert);  
  
  if (sheet.getRange("B4").getValue().toLowerCase() == "yes") {
    time = new Date(time.getTime() + 15000);
    CalendarApp.createEvent(alert, time, time).addSmsReminder(0); 
  }
  
  return;
}

function secondCheck() {
  
  var triggers = ScriptApp.getProjectTriggers();
  for (var i=0; i<triggers.length; i++) {
    if (triggers[i].getHandlerFunction() == "secondCheck") {
      ScriptApp.deleteTrigger(triggers[i]);
    }
  }
  
  websiteMonitor();
  
}

function quickCheck() {
  ScriptApp.newTrigger("secondCheck").timeBased().after(120000).create();
  return;
}

function removeJobs(quiet) {    
  
  // Clean the database
  
  var db = ScriptDb.getMyDb();
  
  while (true) {
    var result = db.query({}); 
    if (result.getSize() == 0) {
      break;
    }
    while (result.hasNext()) {
      db.remove(result.next());
    }
  }
  
  // Delete all Script Triggers
  
  var triggers = ScriptApp.getScriptTriggers();
  
  for (i=0; i<triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
  
  
  // Inform the user, default is "YES"
  
  if (! quiet) {
    
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    
    sheet.toast("The program has stopped. You can choose Start under Website Monitor "
                + "menu anytime later to resume website monitoring!", "Uninstalled", -1);
    
  }    
}

// Written by Amit Agarwal amit@labnol.org
// Twitter: @labnol

4 comments:

Sunday said...

Invariably, this is a helpful information. Its important for the Internet marketer to monitor increase up-time and downtime of the website.

Time lost in downtime can affect the success of the site. Well, its good to have this tutorial shared.

It seems easy and thankfully it can be achieved with Google account. Google Docs and the corresponding SMS reminders could be time saving! Thanks for sharing this piece!

This comment was shared in kingged.com - the content syndication and social bookmarking website where this post was found and "kingged".

Sunday - kingged.com contributor

http://kingged.com/using-google-docs-to-check-your-websites-uptime-using-sms-reminders/

Nwosu Mavtrevor said...

I actually saw this tutorial on Amit Agarwal of Digital Inspiration's website sometime last year and implemented it on my website and since then i can easily get notified when my site is down so i can take actions to get it running once more.

it is very important as well to keep an eye on your host's server uptime so as to know how they perform for you to know if you will continue using them or leave for a better hosting.

I found this post on Kingged.com and also left a comment on it.

Anonymous said...

Any thoughts as to what this would look like if it were to include other return codes, such as PHP login codes like 300? Has anyone seen versions like this?

SUMIT KUMAR said...

Apple iPhone 7 Camera
Samsung Galaxy A9 Price
Snapchat for PC
Samsung Galaxy S7 Specification
www.gadgetlio.com