Last active
November 19, 2017 17:23
-
-
Save aarondicks/e145bdcfad3012f5e25b54f75aab88dc to your computer and use it in GitHub Desktop.
Companion snippet for https://www.impression.co.uk/blog/8753/monitoring-website-uptime-google-sheets-email-alerts/
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
/** | |
* Website uptime checker for clients at Impression | |
* Inspired by Labnol's solution (https://lifehacker.com/5896830/use-google-docs-to-monitor-your-websites-uptime) | |
* By aaron@impression.co.uk for https://www.impression.co.uk | |
*/ | |
function init() { | |
if (ScriptApp.getProjectTriggers().length == 0) { | |
ScriptApp.newTrigger("websiteMonitor").timeBased().everyMinutes(1).create(); | |
} | |
} | |
function websiteMonitor() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Config"); | |
var lastRow = sheet.getLastRow(); | |
Logger.log('last'+lastRow); | |
var configuration = sheet.getRange(2, 1, lastRow-1, 3); | |
var config_vars = configuration.getValues(); | |
var response, code, msg, status; | |
var time = new Date(); | |
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js').getContentText()); | |
for (var i in config_vars) { | |
var emails = config_vars[i][1].replace(/\s/g, "").split(","); | |
var url = config_vars[i][0]; | |
try { | |
response = UrlFetchApp.fetch(url); | |
code = response.getResponseCode(); | |
} catch(error) { | |
// If URLFetchApp fails, the site is probably down | |
code = 404; | |
} | |
if (parseInt(code) > 200) { | |
msg = url+" is down"; | |
status = "DOWN"; | |
if (PropertiesService.getScriptProperties().getProperty(url) ) { | |
// Still down | |
// logSiteChanges(url, 'DOWN_STILL'); | |
} else { | |
// Just gone down | |
PropertiesService.getScriptProperties().setProperty(url, time); | |
logSiteChanges(url, 'DOWN'); | |
for (var e in emails) { | |
MailApp.sendEmail(emails[e], msg, msg); | |
} | |
} | |
} else { | |
msg = url+" is up"; | |
status = "UP"; | |
if (PropertiesService.getScriptProperties().getProperty(url) ) { | |
var time_down = moment(PropertiesService.getScriptProperties().getProperty(url)); | |
var duration = moment.duration(moment().diff(time_down)).asMinutes(); | |
logSiteChanges(url, 'UP after '+duration+' minutes'); | |
msg = url+" is after "+duration+" minutes"; | |
logSiteChanges(url, 'UP after '+duration+' minutes'); | |
// No longer down | |
PropertiesService.getScriptProperties().deleteProperty(url); | |
for (var e in emails) { | |
MailApp.sendEmail(emails[e], msg, msg); | |
} | |
} | |
} | |
sheet.getRange(parseInt(i)+2, 3).setValue(status+": "+url); | |
} | |
} | |
function logSiteChanges(url, status) { | |
var time = new Date(); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Log"); | |
sheet.appendRow([time, url, status]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment