Last active
August 29, 2015 14:06
-
-
Save ibaaj/a723d697328aac165b7e to your computer and use it in GitHub Desktop.
Page Monitor NodeJS - usage: node pageMonitor.js http://url.com/ 300
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 crypto = require('crypto'); | |
if(process.argv.length != 4) | |
{ | |
console.log('Syntax ::'); | |
console.log('node pagemonitor.js http://my-url-to-monitor.com 300'); | |
console.log('with 300 the interval check (in seconds (300s = 5min)).'); | |
process.exit(1); | |
} | |
var urlInput = process.argv[2], interval = parseInt(process.argv[3]); | |
var urlRegex = new RegExp(/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi); | |
if (!urlInput.match(urlRegex)) | |
{ | |
console.log("this:" + url + " is not a url"); | |
process.exit(1); | |
} | |
var urlParts = url.parse(urlInput, true); | |
if(isNaN(interval)) | |
{ | |
console.log(process.argv[3] + " is not a number."); | |
process.exit(1); | |
} | |
var firstDate, firstMd5; | |
var changementAlert = function(){ | |
console.log("CHANGEMENT"); | |
console.log('\u0007'); | |
process.exit(0); | |
}; | |
var intervalCheck = function(){ | |
var optionsSec = { method: 'GET', host: urlParts.host, port: urlParts.port, path: urlParts.path }; | |
var requestSec = http.request(optionsSec, function(res) { | |
var data = ''; | |
if(Date.parse(res.headers["last-modified"]) != firstDate) | |
changementAlert(); | |
res.on('data', function(chunk) { | |
data += chunk.toString(); | |
}); | |
res.on('end', function(){ | |
if(crypto.createHash('md5').update(data).digest('hex') != firstMd5) | |
changementAlert(); | |
}); | |
} | |
).end(); | |
}; | |
var options = { method: 'GET', host: urlParts.host, port: urlParts.port, path: urlParts.path }; | |
var req = http.request(options, function(res) { | |
firstDate = Date.parse(res.headers["last-modified"]); | |
var data = ''; | |
res.on('data', function(chunk) { | |
data += chunk.toString(); | |
}); | |
res.on('end', function(){ | |
firstMd5 = crypto.createHash('md5').update(data).digest('hex'); | |
}); | |
} | |
); | |
req.on('finish', function(){ | |
setInterval(intervalCheck, interval); | |
}); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment