|
|
@@ -0,0 +1,73 @@ |
|
|
function init() { |
|
|
if (ScriptApp.getProjectTriggers().length == 0) { |
|
|
ScriptApp.newTrigger("websiteMonitor").timeBased().everyMinutes(0.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.29.4/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\nChúc các sếp may mắn lần sau"; |
|
|
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 UP after "+duration+" minutes\nNhanh nhanh vào đăng kí môn nào"; |
|
|
|
|
|
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]); |
|
|
} |