Sunday, July 21, 2013

How To Monitor Website And Get SMS Reports

Do you really know at what time your website is down. I know most of people doesn't know that their website is gets down. For this problem I have great solution which is monitor your site by using google docs. By using Google Docs you can monitor your site and get SMS reports straight to your mobile sms inbox.

Here i made one small JavaScript which help you to monitor your website and give you SMS reports when website is down. Using Google Docs you can monitor your website’s up-time and down-time. The latest version of this Google Docs JavaScript based on website monitoring tool, and it is much much better than any other website monitoring site. It uses Google SMS channel to serve SMS so you don't need to pay for it. It also supports multiple websites and blogs. This website monitoring Script also supports Email Reports.



How To Do:
  1. Click here and get a sheet.
  2. Now go to file and make copy of it.
  3. You’ll see a new Website Monitor menu in the toolbar. Click Initialize and you’ll get a pop-up asking for authorization. Just say Yes.
  4. Put your Website URL and email address
  5. You can put multiple URLs by adding comma.
  6. Go to the Website Monitor menu again and choose “Start Monitoring”.

Full Source Code:
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; ii++) {
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);
}
Note: If you do not wish to receive SMS alerts, simply change the value of cell B4 from Yes to No.