","https://gist.github.com/earino/4137612","https://gist.github.com/4137612.git","@ChristianKrausse","https://avatars.githubusercontent.com/u/947665","Poor Man's Nagios · GitHub","Skip to content","\n All gists\n\n Back to GitHub\n\n \n Sign in\n\n \n Sign up\n","\n \n Sign in\n \n Sign up\n ","You signed in with another tab or window. Reload to refresh your session.","You signed out in another tab or window. Reload to refresh your session.","You switched accounts on another tab or window. Reload to refresh your session.","Dismiss alert","{{ message }}","\n Instantly share code, notes, and snippets.\n ","\n earino/script.js\n ","\n Created\n ","November 23, 2012 22:42","Show Gist options","\n Download ZIP\n","Star","\n (4)\n 4\n ","You must be signed in to star a gist","Fork","\n (1)\n 1\n ","You must be signed in to fork a gist","Embed","\n \n Embed\n \n Embed this gist in your website.\n \n","\n \n Share\n \n Copy sharable link for this gist.\n \n","\n \n Clone via HTTPS\n \n Clone using the web URL.\n \n","\n Learn more about clone URLs\n\n","\n Clone this repository at <script src="https://gist.github.com/earino/4137612.js"></script>\n","Save earino/4137612 to your computer and use it in GitHub Desktop.","\n Code\n","\n Revisions\n ","\n Stars\n ","\n Forks\n ","Download ZIP","\n Poor Man's Nagios\n ","Raw","\n script.js\n ","\n 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.\n Learn more about bidirectional Unicode characters\n "," Show hidden characters\n","/**"," * 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://\", \"\");"," 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)); "," manageStatus(cell, 500);"," if (downForEveryone(url)) {"," setCellContents(sheet.setActiveCell(\"E\" + (i + 1)),"," \"EVERYONE\", \"red\"); "," }"," else {"," \"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);","\n \n Copy link\n\n","ChristianKrausse","\n\n \n\n commented \n\n\n ","Feb 6, 2016","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?","\n Sorry, something went wrong.\n ","\n \n Sign up for free\n to join this conversation on GitHub.\n Already have an account?\n Sign in to comment\n\n\n \n","Footer","\n © 2024 GitHub, Inc.\n ","Footer navigation","Terms","Privacy","Security","Status","Docs","Contact","\n Manage cookies\n ","\n Do not share my personal information\n ","\n You can’t perform that action at this time.\n "]}
Skip to content

Instantly share code, notes, and snippets.

@earino
Created November 23, 2012 22:42
Show Gist options
  • 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