forked from qawemlilo/node-ping
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var Ping = require('./lib/ping'), | ||
websites = require('./websites'), | ||
http = require('http'), | ||
server, | ||
port = process.env.PORT || 3008, | ||
monitors = []; | ||
|
||
|
||
websites.forEach(function (website) { | ||
var monitor = new Ping ({ | ||
website: website.url, | ||
timeout: website.timeout | ||
}); | ||
|
||
monitors.push(monitor); | ||
}); | ||
|
||
|
||
server = http.createServer(function (req, res) { | ||
var data = "Monitoring the following websites: \n \n" + websites.join("\n"); | ||
|
||
res.end(data); | ||
}); | ||
|
||
|
||
server.listen(port); | ||
console.log('Listening to port %s', port); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
email: '[email protected]', | ||
password: 'xxxxxxxxxxxx' | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
var nodemailer = require('nodemailer'), | ||
config = require('../config'), | ||
mailer; | ||
|
||
mailer = function (opts, fn) { | ||
|
||
var mailOpts, smtpTrans; | ||
|
||
// nodemailer configuration | ||
try { | ||
smtpTrans = nodemailer.createTransport('SMTP', { | ||
service: 'Gmail', | ||
auth: { | ||
user: config.email, | ||
pass: config.password | ||
} | ||
}); | ||
} | ||
catch (err) { | ||
fn('Nodemailer could not create Transport', ''); | ||
return; | ||
} | ||
|
||
// mailing options | ||
mailOpts = { | ||
from: opts.from, | ||
replyTo: opts.from, | ||
to: opts.to, | ||
subject: opts.subject, | ||
html: opts.body | ||
}; | ||
|
||
// Send maail | ||
try { | ||
smtpTrans.sendMail(mailOpts, function (error, response) { | ||
//if sending fails | ||
if (error) { | ||
fn(true, error); | ||
} | ||
//Yay!! message sent | ||
else { | ||
fn(false, response.message); | ||
} | ||
}); | ||
} | ||
catch (err) { | ||
fn('Nodemailer could not send Mail', ''); | ||
} | ||
}; | ||
|
||
module.exports = mailer; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
var request = require('request'), | ||
statusCodes = require('http').STATUS_CODES; | ||
|
||
/* | ||
Ping Constructor | ||
*/ | ||
function Ping (opts) { | ||
// holds website to be monitored | ||
this.website = ''; | ||
|
||
// ping intervals in minutes | ||
this.timeout = 15; | ||
|
||
// interval handler | ||
this.handle = null; | ||
|
||
// initialize the app | ||
this.init(opts) | ||
} | ||
|
||
/* | ||
Methods | ||
*/ | ||
|
||
Ping.prototype = { | ||
|
||
init: function (opts) { | ||
var self = this; | ||
|
||
self.website = opts.website; | ||
|
||
self.timeout = (opts.timeout * (60 * 1000)); | ||
|
||
// start monitoring | ||
self.start(); | ||
}, | ||
|
||
|
||
|
||
|
||
start: function () { | ||
var self = this, | ||
time = Date.now(); | ||
|
||
console.log("\nLoading... " + self.website + "\nTime: " + self.getFormatedDate(time) + "\n"); | ||
|
||
// create an interval for pings | ||
self.handle = setInterval(function () { | ||
self.ping(); | ||
}, self.timeout); | ||
}, | ||
|
||
|
||
|
||
ping: function () { | ||
var self = this, currentTime = Date.now(); | ||
|
||
try { | ||
// send request | ||
request(self.website, function (error, res, body) { | ||
// Website is up | ||
if (!error && res.statusCode === 200) { | ||
self.isOk(); | ||
} | ||
|
||
// No error but website not ok | ||
else if (!error) { | ||
self.isNotOk(res.statusCode); | ||
} | ||
|
||
// Loading error | ||
else { | ||
self.isNotOk(); | ||
} | ||
}); | ||
} | ||
catch (err) { | ||
self.isNotOk(); | ||
} | ||
}, | ||
|
||
|
||
|
||
|
||
isOk: function () { | ||
this.log('UP', 'OK'); | ||
}, | ||
|
||
|
||
|
||
|
||
isNotOk: function (statusCode) { | ||
var time = Date.now(), | ||
self = this, | ||
time = self.getFormatedDate(time), | ||
msg = statusCodes[statusCode + ''], | ||
|
||
htmlMsg = '<p>Time: ' + time; | ||
htmlMsg +='</p><p>Website: ' + self.website; | ||
htmlMsg += '</p><p>Message: ' + msg + '</p>'; | ||
|
||
this.log('DOWN', msg); | ||
|
||
// Send admin and email | ||
mailer({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: self.website + ' is down', | ||
body: htmlMsg | ||
}, function (error, res) { | ||
if (error) { | ||
console.log(error); | ||
} | ||
else { | ||
console.log(res.message || 'Failed to send email'); | ||
} | ||
}); | ||
}, | ||
|
||
|
||
|
||
|
||
log: function (status, msg) { | ||
var self = this, | ||
time = Date.now(), | ||
output = ''; | ||
|
||
output += "\nWebsite: " + self.website; | ||
output += "\nTime: " + time; | ||
output += "\nStatus: " + status; | ||
output += "\nMessage:" + msg + "\n"; | ||
|
||
console.log(output); | ||
}, | ||
|
||
|
||
|
||
|
||
getFormatedDate: function (time) { | ||
var currentDate = new Date(time); | ||
|
||
currentDate = currentDate.toISOString(); | ||
currentDate = currentDate.replace(/T/, ' '); | ||
currentDate = currentDate.replace(/\..+/, ''); | ||
|
||
return currentDate; | ||
} | ||
} | ||
|
||
module.exports = Ping; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
List of websites to be monitored | ||
*/ | ||
module.exports = [ | ||
{ | ||
url: 'http://www.rflab.co.za', | ||
timeout: 15 | ||
}, | ||
|
||
{ | ||
url: 'http://www.bookmarkmanager.co.za', | ||
timeout: 15 | ||
}, | ||
|
||
{ | ||
url: 'http://crushit-compiler.herokuapp.com', | ||
timeout: 15 | ||
}, | ||
|
||
{ | ||
url: 'http://node-ping.herokuapp.com', | ||
timeout: 5 | ||
}, | ||
{ | ||
url: 'http://www.sanatural.co.za/home/', | ||
timeout: 15 | ||
} | ||
]; |