1 // monitor EPL and send an email if unreachable
5 var http = require('http');
6 var Mailer = require('./mailer.js');
9 var mailto = ['info@finalsclub.org', 'snow@sleepless.com'];
22 host: 'finalsclub.org',
31 var date = new Date().toString();
32 var url = 'http://' + fc1Opts.host + ':' + fc1Opts.port + fc1Opts.path;
33 dlog('FinalsClub EPL health check monitor');
34 dlog('date: ' + date);
38 checkAlive(fc1Opts, numRetries, function (success, errMsg) {
39 var url = fc1Opts.host + ':' + fc1Opts.port + fc1Opts.path;
41 dlog('host is alive');
45 dlog('host is dead - final error: ' + errMsg);
48 sendEmailAlert(url, msgs, date);
53 function checkAlive(httpOptions, retries, cb) {
55 checkAlive2(httpOptions, retries, errs, cb);
58 function checkAlive2(httpOptions, retries, errs, cb) {
59 checkAliveWorker(httpOptions, function (success, errMsg) {
60 if (success || retries <= 0) {
63 dlog('Error: ' + errMsg + '\n\nretrying...');
64 checkAlive2(httpOptions, retries - 1, errs, cb);
69 function checkAliveWorker(httpOptions, cb) {
71 var timeoutDelayMS = httpOptions.timeout || 30 * 1000;
73 // declare req var before using it's reference in timeout handler
76 // init request timeout handler
77 var timeoutId = setTimeout(function () {
78 clearTimeout(timeoutId);
88 req = http.request(httpOptions, function (res) {
89 // console.log('STATUS: ' + res.statusCode);
90 // console.log('HEADERS: ' + JSON.stringify(res.headers));
91 res.setEncoding('utf8');
92 res.on('data', function (chunk) {
93 // console.log('BODY: ' + chunk);
95 clearTimeout(timeoutId);
99 if (cb) { cb(true, null); }
103 if (res.statusCode != 200) {
105 clearTimeout(timeoutId);
109 var msg = ['invalid response code',
110 'status: ' + res.statusCode,
111 'headers: ' + JSON.stringify(res.headers)];
113 if (cb) { cb(false, msg.join('\n')); }
118 req.on('error', function (e) {
119 console.log('problem with request: ' + e.message);
121 clearTimeout(timeoutId);
125 if (cb) { cb(false, e.message); }
134 function sendEmailAlert(url, msgs, date) {
135 var awsAccessKey = process.env.AWS_ACCESS_KEY_ID;
136 var awsSecretKey = process.env.AWS_SECRET_ACCESS_KEY;
137 var mailer = new Mailer(awsAccessKey, awsSecretKey);
139 for (var i in mailto) {
140 var email = mailto[i];
141 dlog('sending email alert to: ' + email);
142 var details = msgs.join('\n');
145 'subject': 'FinalsClub.org EPL Monitor Warning',
146 'template': 'systemEPLMonitorFailed',
155 mailer.send(message, function (err, result) {
157 dlog('Error sending email\nError Message: ' + err.Message);
159 dlog('Successfully sent email.');