Created
February 19, 2013 05:48
-
-
Save josher19/4983413 to your computer and use it in GitHub Desktop.
webcheck.ls LiveScript code (easily converted to CoffeeScript) to check the title of a website and notify if the site is down. Requires: growl (npm install growl).
This file contains hidden or 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
#!/media/DATA/Mobile/git/josher19/LiveScript/bin/livescript | |
request = require 'request' | |
fs = require 'fs' | |
growl = require('growl') | |
docheck = (url, expectedTitle, cb = barf) -> | |
return (err, req, body) -> | |
try | |
return cb(err) if err; | |
return cb("Request not processed") if !req; | |
return cb({url: url, statusCode:req.statusCode,length:req.body && req.body.length}) if req.statusCode != 200 | |
return cb("No Body") if !body; | |
m=body.match(/<title>(.*?)<\/title>/i); | |
return cb("No match", m) if !m || !m.length; | |
cb {url: url, 'got':m[1], 'expected':expectedTitle} if m[1] != expectedTitle | |
cb null, true | |
catch e | |
cb e | |
barf = (err, obj) -> | |
console.log err, obj # if err | |
panic(err, obj) if obj != true | |
panic = (err, obj) -> | |
# send email with error message | |
console.log "Panic!" | |
# TODO: include URL in msg | |
msg = JSON.stringify {"error":err,"object":obj}, null, 2 | |
fs.writeFileSync('/tmp/lasterr.json', msg) | |
growl(msg, {sticky:true, image:'html'}); | |
console.warn(err); | |
process.exit(1); | |
err; | |
# request('http://www.greasemonkey.cn/', docheck('油猴汽车快修')); | |
# request('http://www.greasemonkey.cn/en/', docheck('Welcome to Grease Monkey')); | |
### | |
/* | |
* webchecker: check website at URL is online with given title and Status Code of 200 (OK). | |
* - url: String such as 'http://www.greasemonkey.cn/' | |
* - title: String such as '油猴汽车快修' | |
* - cb: callback(error, results) to run when completed | |
*/ | |
### | |
webchecker = (url, title, cb) -> | |
try | |
request(url, docheck(url, title, cb)); | |
catch e | |
cb e | |
webchecker('http://www.greasemonkey.cn/', '油猴汽车快修') | |
webchecker('http://www.greasemonkey.cn/en/', 'Welcome to Grease Monkey') | |
webchecker('http://www.greasemonkeychina.com/', '油猴汽车快修 ') | |
# webchecker('http://www.greasemonkeyintl.com/', 'Welcome to Grease Monkey') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment