Last active
August 29, 2015 14:00
-
-
Save greenido/11392864 to your computer and use it in GitHub Desktop.
Part 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
function blogMonitor() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[2]; | |
var curUrl = sheet.getRange("B1").getValue(); | |
var curStatus = sheet.getRange("B3").getValue(); | |
// monitorObj contain the url we are checking and its status (200 or 503 or any other HTML return code). | |
var monitorObj = {url: curUrl, status: curStatus}; | |
// We will start as if the blog is down and hope for the best. | |
var code = 503; | |
try { | |
// Fetch the url of our site | |
var response = UrlFetchApp.fetch(monitorObj.url); | |
// Get the content (=HTML) in order to check for anchors | |
// that will validate the site is up and running | |
var checkRes = response.getContentText(); | |
// You can put here any string that will let you know the site is up. | |
if (checkRes.indexOf("ToDo - some string from the h1 or any other anchor on the site") < 1) { | |
// The site doesn't render our validation string | |
code = 503; | |
Logger.log("Our site on: " + monitorObj.url + " is not OK"); | |
} | |
else { | |
// If it's something else let's get the HTML return code | |
code = response.getResponseCode(); | |
Logger.log("Our site on: " + monitorObj.url + " is OK. Code: "+ code); | |
} | |
} catch(error) { | |
Logger.log("Err: during blogMonitor() we got: " + error); | |
} | |
// monitorObj contain two properties: site-url and its status. | |
// The status will be the return code. E.g. 200 - we are good. | |
monitorObj.status = checkAndNotify(monitorObj, code); | |
// store the obj in our meta data sheet or if you wish | |
// you can use ScriptDB | |
saveMonitorObj(monitorObj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment