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).
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 **/
/** http://labnol.org/?p=33232 **/
function isMySiteDown()
{
var url= new Array();
url[0] = "http://one.example.com";
url[1] = "http://two-example.net";
url[2] = "http://www.google.com";
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);
return;
}
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 = "youremail@address.net";
var row = sheet.getLastRow() + 1;
sheet.getRange(row,1).setValue(new Date());
sheet.getRange(row,2).setValue(error);
sheet.getRange(row,3).setValue(code);
sheet.getRange(row,4).setValue(msg);
if (code!=200)
MailApp.sendEmail(email, msg, msg+" "+error);
}




