Last active
August 29, 2015 14:00
-
-
Save greenido/11393153 to your computer and use it in GitHub Desktop.
art of a tutorial on greenido.wordpress.com to show the power of Apps script as a simple site monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// The main logic to check the site and do a quick 2nd check in case we | |
// did not get 200 as return code | |
// | |
function checkAndNotify(monitorObj, curCode) { | |
var status = monitorObj.status; | |
if (curCode === 200) { | |
if (status === 503) { | |
// Site was down previously but up on second check | |
logToSheet(monitorObj.url, "The site is Up"); | |
} | |
// We are cool | |
return curCode; | |
} | |
// Site was up previously but is now down | |
if (curCode === 503 ) { | |
if (status === 200) { | |
// Run another check after 1 minutes to prevent false positives | |
quickCheck(); | |
} | |
else if (status === 503 ) { | |
// Site was down previously and is down again | |
quickCheck(); | |
logToSheet(monitorObj.url, "Process is Down"); | |
} | |
return curCode; | |
} | |
Logger.log("WARN: We did not catch code: " + curCode + " - let's check why"); | |
return curCode; | |
} | |
// | |
// Log our alerts to the main sheet and get info from | |
// The 'Form' sheet in order to mute the alerts. | |
// | |
function logToSheet(url, message) { | |
var formSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1]; | |
// We wish to see what is the last response from the 'mute alerts' form | |
var toStopEmailsRow = formSheet.getLastRow(); | |
var toStopEmails = formSheet.getRange(toStopEmailsRow, 2).getValue(); | |
// The logger sheet | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; | |
var row = sheet.getLastRow() + 1; | |
var time = new Date(); | |
sheet.getRange(row,1).setValue(time); | |
sheet.getRange(row,2).setValue(message + " : " + url); | |
var alert = "At: " + time + " Our site on: " + url + | |
" is " + message.toLowerCase() + | |
" | To stop emails: http://bit.ly/please-stop-email-now"; | |
if (toStopEmails === "Yes") { | |
Logger.log(alert + " But with no email"); | |
} | |
else { | |
// Send an email with the alert | |
Logger.log(alert); | |
MailApp.sendEmail(sheet.getRange("B3").getValue(), "Site " + message, alert); | |
} | |
} | |
// | |
// Let's check the site again to confirm that it's down | |
// | |
function secondCheck() { | |
// first we clean all the triggers to keep our house clean | |
var checksTriggers = ScriptApp.getProjectTriggers(); | |
for (var i=0; i< checksTriggers.length; i++) { | |
if (checksTriggers[i].getHandlerFunction() == "secondCheck") { | |
ScriptApp.deleteTrigger(checksTriggers[i]); | |
} | |
} | |
// Back to our main function | |
blogMonitor(); | |
} | |
// | |
// Our const - you should put it at another file that contain all your | |
// configurations. | |
// | |
var HALFMinute = 30000; | |
// | |
// Wait 30sec and check again | |
// | |
function quickCheck() { | |
ScriptApp.newTrigger("secondCheck").timeBased().after(HALFMinute).create(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment