http://techawakening.org/?p=988
Piccola modifica per evitare di ricevere troppi messaggi di spam.
Il piccolo script prevede di mandare/creare l'evento di down solo quando il sito è offline e salva in tutti i casi il log sul foglio sul drive.
function isMySiteDown() {
var tot_site=5;
for (i=3;i<=tot_site;i++) {
// In cell E3 enter the URL of the site you want to monitor
var url = SpreadsheetApp.getActiveSheet().getRange("F"+i).getValue();
if(url.length) {
var response, error;
try {
response = UrlFetchApp.fetch(url);
} catch(error) {
insertData(error, -1, url, i, "Website down");
continue;
}
var code = response.getResponseCode();
if (code != 200) {
insertData(response.getContent()[0], code,url, i,"Website DOWN");
}else{
insertData(response.getContent()[0], code,url, i,"Website UP");
}
}
}
}
function insertData(error, code, url, i, msg) {
var sheet = SpreadsheetApp.getActiveSheet();
var email = sheet.getRange("F8").getValue();
var row = sheet.getLastRow() + 1;
var now = new Date().getTime();
sheet.getRange(row,1).setValue(new Date());
sheet.getRange(row,2).setValue(url);
sheet.getRange(row,3).setValue(error);
sheet.getRange(row,4).setValue(code);
if(code!=200){
CalendarApp.createEvent( url + ' is DOWN' , new Date(now+40000), new Date(now+40000)).addSmsReminder(0);
MailApp.sendEmail(email, msg, error);
}
}