Skip to content

Instantly share code, notes, and snippets.

@earino
Created November 23, 2012 22:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save earino/4137612 to your computer and use it in GitHub Desktop.
Save earino/4137612 to your computer and use it in GitHub Desktop.
Poor Man's Nagios
/**
* This function fetches a url and gets some of the instrumentation data.
*
* @param url the Url to fetch and instrument
* @return an object which contains the status code, latency, and contentLength.
*/
function fetchUrl(url) {
try {
var start = new Date().getTime();
var resp = UrlFetchApp.fetch(url);
var total_time = new Date().getTime() - start;
var code = resp.getResponseCode();
return {
code: code,
latency: total_time,
contentLength: resp.getContentText().length
};
} catch (e) {
throw e;
}
}
/**
* This function sets the background color of a cell according to the good
* statuses in the map. Currently only check for 200, but this could be
* extended easilly.
*
* @param cell The cell to write status to
* @param status The status code to write to the cell (and test for goodness)
*/
function manageStatus(cell, status) {
var goodStatuses = { 200: 1};
if (goodStatuses[status]) {
cell.setBackground("green")
}
else {
cell.setBackground("red");
}
cell.setValue(status);
}
/**
* Helper function to set the cell contents and optinally color.
*
* @param cell The cell to work on
* @param cellContents The contents to set
* @param cellColor *optional* a color to set the cell
*/
function setCellContents(cell, cellContents, cellColor) {
cell.setValue(cellContents);
if (cellColor != null) {
cell.setBackground(cellColor);
}
}
/**
* Function to check to see if a URL is down for everyone or just the
* google data center running this app script. It screen scrapes the
* downforeveryoneorjustme.com website and tests it simply.
*
* @param url the url to ask downforeveryoneorjustme.com about
* @return boolean on if it's down for everyone or just you :-)
*/
function downForEveryone(url) {
var newUrl = url.replace("http://", "");
try {
var resp = UrlFetchApp.fetch("http://www.downforeveryoneorjustme.com/"+ url);
var contents = resp.getContentText();
var re = /It's just you/;
if (re.test(contents)) {
return false;
}
}
catch (e) {
Logger.log("Error in downForEveryone, reason: " + JSON.stringify(e));
}
return true;
}
/**
* Main driver function. Rerieves the active spreadsheet, gets all the URLs on the
* leftmost column, and from there tries to see if they are up and if they need
* to be instrumented. Triggered from the UI dropdown as well as optinally from a
* timer.
*/
function readRows() {
var initial = new Date().getTime();
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
rows.setBackground("white");
for (var i = 1; i <= numRows - 1; i++) {
var row = values[i];
var url = row.toString().split(',')[0];
try {
var urlStatus = fetchUrl(url);
var cell = sheet.setActiveCell("B" + (i + 1));
manageStatus(cell, urlStatus.code);
setCellContents(sheet.setActiveCell("C" + (i + 1)),
urlStatus.latency, "white");
setCellContents(sheet.setActiveCell("D" + (i + 1)),
urlStatus.contentLength);
}
catch (e) {
Logger.log("UNABLE TO RETRIEVE URL: " + url + " reason: " + JSON.stringify(e));
var cell = sheet.setActiveCell("B" + (i + 1));
manageStatus(cell, 500);
if (downForEveryone(url)) {
setCellContents(sheet.setActiveCell("E" + (i + 1)),
"EVERYONE", "red");
}
else {
setCellContents(sheet.setActiveCell("E" + (i + 1)),
"JUST YOU", "yellow");
}
}
}
cell = sheet.setActiveCell("F1");
cell.setValue(new Date());
};
/**
* Handler to add the "Check URLs" menu item to the Script Center Menu dropdown.
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Check URLs",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};
@ChristianKrausse
Copy link

Could you please provide some Info how to use this? This looks interesting to me, but I do not know how to use this. How to include this in a working html page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment