Created
November 22, 2011 16:06
-
-
Save bsiegel/1386015 to your computer and use it in GitHub Desktop.
IsTFSOK
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.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var url = require('url'); | |
var exec = require('child_process').exec; | |
var init = 'C:\\cygwin\\bin\\wget.exe --cookies=on --keep-session-cookies --save-cookies="C:\\Users\\bsiegel\\Documents\\My Web Sites\\istfsok\\cookie.txt" -qO- http://nagiosxi/nagiosxi' | |
var cmd = 'C:\\cygwin\\bin\\wget.exe --referer=http://nagiosxi/nagiosxi/index.php --cookies=on --load-cookies="C:\\Users\\bsiegel\\Documents\\My Web Sites\\istfsok\\cookie.txt" --keep-session-cookies -qO- "http://nagiosxi/nagiosxi/includes/components/xicore/status.php?show=hostdetail&host=TFS%20Server%20-%20AtNet"' | |
http.createServer(function (req, res) { | |
var url_parts = url.parse(req.url); | |
if (url_parts.pathname == '/') { | |
console.log('Serving request'); | |
var child = exec(init, function (error, stdout, stderr) { | |
console.log('Init request complete'); | |
var child = exec(cmd, function (error, stdout, stderr) { | |
console.log('Cmd request complete'); | |
if (error !== null) { | |
console.log('error'); | |
res.writeHead(500, { 'Content-Type': 'text/html' }); | |
res.end('Error when contacting nagios: ' + error); | |
} else { | |
var txt = stdout.replace(/\n/g, ''); | |
console.log('Regexing'); | |
var matches = txt.match(/hoststatusdetailinfotext">(.+?) -/i); | |
console.log(matches[1]); | |
var outp = (matches[1] == "OK" || matches[1] == "UP") ? "YEP" : (matches[1] == "WARNING" ? "KINDA" : "NOPE"); | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end('<html><body><center><div style="float:left;height:50%;margin-bottom:-120px"></div><div style="clear:both;height:240px;position:relative;font-size:240px">' + outp + '</div></center></body></html>'); | |
} | |
}); | |
}); | |
} else { | |
res.writeHead(404, {'Content-Type': 'text/html'}); | |
res.end("<h1>404 Not Found</h1>"); | |
} | |
}).listen(process.env.PORT || 8080); |
Also, the WARNING status was actually what was happening more often than TFS just being down. The link to the co-lo was very lossy sometimes and TFS operations would time out or take forever to complete.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Due to limitations of the current version of Node on Windows (at the time), I could not use a node package to do the HTTP requests and ended up having to shell out to wget. Also Nagios has some weirdness with cookies and referrers, so I had to first request the main Nagios page and save the cookies, and then request the page I actually wanted with the saved cookies.