Skip to content

Commit

Permalink
code tut ready
Browse files Browse the repository at this point in the history
  • Loading branch information
qawemlilo committed Feb 18, 2013
1 parent 494ad28 commit fed1c66
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app.js
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);
4 changes: 4 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
email: '[email protected]',
password: 'xxxxxxxxxxxx'
};
51 changes: 51 additions & 0 deletions lib/mailer.js
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;
150 changes: 150 additions & 0 deletions lib/ping.js
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;
28 changes: 28 additions & 0 deletions websites.js
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
}
];

0 comments on commit fed1c66

Please sign in to comment.