updated github path
[oweals/finalsclub.git] / fc_monitor_epl.js
1 // monitor EPL and send an email if unreachable
2
3
4 // Prerequisites
5 var http = require('http');
6 var Mailer = require('./mailer.js');
7
8 // globals
9 var mailto = ['info@finalsclub.org', 'snow@sleepless.com'];
10
11 var msgs = [];
12 function dlog(msg) {
13         msgs.push(msg);
14         console.log(msg);
15 }
16
17
18 function main() {
19
20         var numRetries = 2;
21         var fc1Opts = {
22                 host: 'finalsclub.org',
23                 port: 9001,
24                 path: '/',
25                 method: 'GET',
26                 timeout: 15 * 1000
27         };
28
29         var errs = [];
30
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);
35         dlog('url: ' + url);
36         dlog('');
37
38         checkAlive(fc1Opts, numRetries, function (success, errMsg) {
39                 var url = fc1Opts.host + ':' + fc1Opts.port + fc1Opts.path;
40                 if (success) {
41                         dlog('host is alive');
42                         dlog('');
43                 } else {
44                         dlog('FAILED');
45                         dlog('host is dead - final error: ' + errMsg);
46                         dlog('');
47
48                         sendEmailAlert(url, msgs, date);
49                 }
50         });
51 }
52
53 function checkAlive(httpOptions, retries, cb) {
54         var errs = [];
55         checkAlive2(httpOptions, retries, errs, cb);
56 }
57
58 function checkAlive2(httpOptions, retries, errs, cb) {
59         checkAliveWorker(httpOptions, function (success, errMsg) {
60                 if (success || retries <= 0) {
61                         cb(success, errMsg);
62                 } else {
63                         dlog('Error: ' + errMsg + '\n\nretrying...');
64                         checkAlive2(httpOptions, retries - 1, errs, cb);
65                 }
66         });
67 }
68
69 function checkAliveWorker(httpOptions, cb) {
70
71         var timeoutDelayMS = httpOptions.timeout || 30 * 1000;
72
73         // declare req var before using it's reference in timeout handler
74         var req = null;
75
76         // init request timeout handler
77         var timeoutId = setTimeout(function () {
78                 clearTimeout(timeoutId);
79                 
80                 if (cb) {
81                         cb(false, 'timeout');
82                         cb = null;
83                 }
84                 req.abort();
85         }, timeoutDelayMS);
86
87         // init request now
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);
94                         if (timeoutId) {
95                                 clearTimeout(timeoutId);
96                                 timeoutId = null;
97                         }
98
99                         if (cb) { cb(true, null); }
100                         cb = null;
101                 });
102
103                 if (res.statusCode != 200) {
104                         if (timeoutId) {
105                                 clearTimeout(timeoutId);
106                                 timeoutId = null;
107                         }
108
109                         var msg = ['invalid response code',
110                                                 'status: ' + res.statusCode,
111                                                 'headers: ' + JSON.stringify(res.headers)];
112
113                         if (cb) { cb(false, msg.join('\n')); }
114                         cb = null;
115                 }
116         });
117
118         req.on('error', function (e) {
119                 console.log('problem with request: ' + e.message);
120                 if (timeoutId) {
121                         clearTimeout(timeoutId);
122                         timeoutId = null;
123                 }
124
125                 if (cb) { cb(false, e.message); }
126                 cb = null;
127         });
128
129         // close the request
130         req.end();
131 }
132
133
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);
138
139         for (var i in mailto) {
140                 var email = mailto[i];
141                 dlog('sending email alert to: ' + email);
142                 var details = msgs.join('\n');  
143                 var message = {
144                         'to': email,
145                         'subject': 'FinalsClub.org EPL Monitor Warning',
146                         'template': 'systemEPLMonitorFailed',
147                         'locals': {
148                                 'url': url,
149                                 'msgs': details,
150                                 'date': date
151                         }
152                 };
153
154         
155                 mailer.send(message, function (err, result) {
156                         if (err) {
157                                 dlog('Error sending email\nError Message: ' + err.Message);
158                         } else {
159                                 dlog('Successfully sent email.');
160                         }
161                 });
162         }
163 };
164
165 main();
166