Skip to content

Instantly share code, notes, and snippets.

@lukmdo
Created July 29, 2012 12:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lukmdo/3198236 to your computer and use it in GitHub Desktop.
Save lukmdo/3198236 to your computer and use it in GitHub Desktop.
google apps script=js
var ONE_S = 1000,
ONE_M = 60*ONE_S,
ONE_H = 60*ONE_M,
RUN_EVERY = 5*ONE_M,
SLEEP_TIME = 30*ONE_M,
CHECK_URL = 'http://www.SITE.com',
NOTIFY_MAIL = 'EMAIL@EMAIL.com',
NOTIFY_CALENDAR = 'ID_OF_YOUR_GOOGLE_CALENDAR@group.calendar.google.com';
function main() {
try {
testRunner(NOTIFY_CALENDAR, NOTIFY_MAIL, CHECK_URL);
} catch(error) {
MailApp.sendEmail(NOTIFY_MAIL, '[siteUptimeMonitor] ERROR testing ' + CHECK_URL, error);
}
}
function testRunner(calID, mail, url) {
var current_timestamp = new Date().getTime(),
cal = CalendarApp.getCalendarById(calID),
events = cal.getEvents(new Date(current_timestamp-ONE_H), new Date(current_timestamp)),
last_run = events.length ? events[events.length-1] : '',
last_run_status = last_run ? JSON.parse(last_run.getDescription()) : '';
Logger.log(cal.getName());
Logger.log(last_run);
if (last_run && last_run_status.is_success) {
if (current_timestamp - last_run_status.timestamp < SLEEP_TIME) {
// sleep time
return;
}
}
var status = testPage(url);
saveEvent(calID, mail, last_run, last_run_status, current_timestamp, status);
}
function testPage(url){
var status = {url: url, is_success: true, msg: 'OK', code: '', timestamp: new Date().getTime()};
try {
response = UrlFetchApp.fetch(url);
} catch(error) {
status.is_success = false;
status.msg = error
return status;
}
status.code = response.getResponseCode();
if (status.code != 200) { // 2xx + 3xx ?
status.is_success = false;
status.msg = response.getContentText();
}
return status;
}
function saveEvent(calID, mail, last_run, last_run_status, current_timestamp, status) {
var d = new Date(current_timestamp),
dateString = ""+ d.getFullYear() +"-"+ (d.getMonth()+1) +"-"+ d.getDate() +" "+ d.getHours() +":"+ d.getMinutes() +":"+ d.getSeconds(),
cal = CalendarApp.getCalendarById(calID),
title = '',
body = '';
status.run_at = dateString;
/* Mail:
on any fail OR on status change */
if (!status.is_success) {
title = '[siteUptimeMonitor] ' + status.url + ' !!!is DOWN!!!';
body = 'Run at: ' + dateString; +'\nCode: '+ status.code +'\nMessage: '+ status.msg;
MailApp.sendEmail(mail, title, body);
} else if (last_run && last_run_status.is_success != true) {
title = '[siteUptimeMonitor] ' + status.url + ' is back OK';
body = 'Run at: ' + dateString; +'\nCode: '+ status.code;
MailApp.sendEmail(mail, title, body);
}
/* Calendar:
new event on status change OR update previous */
title = status.is_success ? status.msg : '' + status.code + '!!!';
body = JSON.stringify(status);
if (!last_run || last_run_status.is_success != status.is_success) {
var startTime = new Date(current_timestamp),
endTime = status.is_success ? new Date(current_timestamp+SLEEP_TIME) : new Date(current_timestamp+RUN_EVERY);
var event = cal.createEvent(title, startTime, endTime, {description: body});
if (last_run) {
event.addSmsReminder(0);
}
} else {
// - different errors 404, 501... - just offer write with most recent
var startTime = last_run.getStartTime(),
endTime = new Date(current_timestamp+RUN_EVERY);
var event = cal.createEvent(title, startTime, endTime, {description: body});
last_run.deleteEvent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment