Advertisement
Guest User

Untitled

a guest
May 24th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. http_createServer = require("http").createServer;
  2. crypto_randombytes = require("crypto").pseudoRandomBytes;
  3. parseUrl = require("url").parse;
  4. exec = require("child_process").exec;
  5. os_hostname = require("os").hostname;
  6.  
  7.  
  8. function onRequest(request, response) {
  9.     var queryFragments,
  10.         queryParams,
  11.         auth,
  12.         hostName;
  13.  
  14.     queryFragments = parseUrl(request.url, true);
  15.     queryParams = queryFragments.query;
  16.     auth = queryParams.auth
  17.     hostName = queryFragments.hostname || "";
  18.  
  19.     if (auth && auth !== "secret") {
  20.         returnErrorPage("408", response);
  21.     }
  22.     else {
  23.         exec("rrd.sh graph '" + hostName + "'", { encoding: "binary", maxBuffer: 10000000, timeout: 20000 }, handleGraph.bind(null, response, hostName));
  24.     }
  25. }
  26.  
  27. function returnErrorPage(status, response) {
  28.     response.writeHead(status);
  29.     response.end();
  30. }
  31.  
  32. function handleGraph(response, hostName, err, stdout, stderr) {
  33.     if (err !== null) {
  34.         response.writeHead (500);
  35.         response.end(err + stderr);
  36.     }
  37.     else {
  38.         response.setTimeout (10000);
  39.         response.writeHead (200, { "content-type": "image/png", "content-length": stdout.length, "content-disposition": "inline; filename=\"" + hostName + ".png\"" } );
  40.         response.end(stdout, "binary");
  41.     }
  42. }
  43.  
  44. var server = http_createServer(onRequest);
  45. server.listen(8000);
  46.  
  47. console.info (new Date().toISOString() + "\thttpd started");
  48. console.log("Server running at http://127.0.0.1:8000/");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement