fc_monitor_epl.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // monitor EPL and send an email if unreachable
  2. // Prerequisites
  3. var http = require('http');
  4. var Mailer = require('./mailer.js');
  5. // globals
  6. var mailto = ['info@finalsclub.org', 'snow@sleepless.com'];
  7. var msgs = [];
  8. function dlog(msg) {
  9. msgs.push(msg);
  10. console.log(msg);
  11. }
  12. function main() {
  13. var numRetries = 2;
  14. var fc1Opts = {
  15. host: 'finalsclub.org',
  16. port: 9001,
  17. path: '/',
  18. method: 'GET',
  19. timeout: 15 * 1000
  20. };
  21. var errs = [];
  22. var date = new Date().toString();
  23. var url = 'http://' + fc1Opts.host + ':' + fc1Opts.port + fc1Opts.path;
  24. dlog('FinalsClub EPL health check monitor');
  25. dlog('date: ' + date);
  26. dlog('url: ' + url);
  27. dlog('');
  28. checkAlive(fc1Opts, numRetries, function (success, errMsg) {
  29. var url = fc1Opts.host + ':' + fc1Opts.port + fc1Opts.path;
  30. if (success) {
  31. dlog('host is alive');
  32. dlog('');
  33. } else {
  34. dlog('FAILED');
  35. dlog('host is dead - final error: ' + errMsg);
  36. dlog('');
  37. sendEmailAlert(url, msgs, date);
  38. }
  39. });
  40. }
  41. function checkAlive(httpOptions, retries, cb) {
  42. var errs = [];
  43. checkAlive2(httpOptions, retries, errs, cb);
  44. }
  45. function checkAlive2(httpOptions, retries, errs, cb) {
  46. checkAliveWorker(httpOptions, function (success, errMsg) {
  47. if (success || retries <= 0) {
  48. cb(success, errMsg);
  49. } else {
  50. dlog('Error: ' + errMsg + '\n\nretrying...');
  51. checkAlive2(httpOptions, retries - 1, errs, cb);
  52. }
  53. });
  54. }
  55. function checkAliveWorker(httpOptions, cb) {
  56. var timeoutDelayMS = httpOptions.timeout || 30 * 1000;
  57. // declare req var before using it's reference in timeout handler
  58. var req = null;
  59. // init request timeout handler
  60. var timeoutId = setTimeout(function () {
  61. clearTimeout(timeoutId);
  62. if (cb) {
  63. cb(false, 'timeout');
  64. cb = null;
  65. }
  66. req.abort();
  67. }, timeoutDelayMS);
  68. // init request now
  69. req = http.request(httpOptions, function (res) {
  70. // console.log('STATUS: ' + res.statusCode);
  71. // console.log('HEADERS: ' + JSON.stringify(res.headers));
  72. res.setEncoding('utf8');
  73. res.on('data', function (chunk) {
  74. // console.log('BODY: ' + chunk);
  75. if (timeoutId) {
  76. clearTimeout(timeoutId);
  77. timeoutId = null;
  78. }
  79. if (cb) { cb(true, null); }
  80. cb = null;
  81. });
  82. if (res.statusCode != 200) {
  83. if (timeoutId) {
  84. clearTimeout(timeoutId);
  85. timeoutId = null;
  86. }
  87. var msg = ['invalid response code',
  88. 'status: ' + res.statusCode,
  89. 'headers: ' + JSON.stringify(res.headers)];
  90. if (cb) { cb(false, msg.join('\n')); }
  91. cb = null;
  92. }
  93. });
  94. req.on('error', function (e) {
  95. console.log('problem with request: ' + e.message);
  96. if (timeoutId) {
  97. clearTimeout(timeoutId);
  98. timeoutId = null;
  99. }
  100. if (cb) { cb(false, e.message); }
  101. cb = null;
  102. });
  103. // close the request
  104. req.end();
  105. }
  106. function sendEmailAlert(url, msgs, date) {
  107. var awsAccessKey = process.env.AWS_ACCESS_KEY_ID;
  108. var awsSecretKey = process.env.AWS_SECRET_ACCESS_KEY;
  109. var mailer = new Mailer(awsAccessKey, awsSecretKey);
  110. for (var i in mailto) {
  111. var email = mailto[i];
  112. dlog('sending email alert to: ' + email);
  113. var details = msgs.join('\n');
  114. var message = {
  115. 'to': email,
  116. 'subject': 'FinalsClub.org EPL Monitor Warning',
  117. 'template': 'systemEPLMonitorFailed',
  118. 'locals': {
  119. 'url': url,
  120. 'msgs': details,
  121. 'date': date
  122. }
  123. };
  124. mailer.send(message, function (err, result) {
  125. if (err) {
  126. dlog('Error sending email\nError Message: ' + err.Message);
  127. } else {
  128. dlog('Successfully sent email.');
  129. }
  130. });
  131. }
  132. };
  133. main();