2017-03-11 16:03:20 +00:00
|
|
|
var async = require('../utils/async_extend.js');
|
2017-03-11 12:32:16 +00:00
|
|
|
|
2017-03-11 17:27:10 +00:00
|
|
|
// TODO: need to separate colors from states
|
|
|
|
// i.e use status: /on|off|warn/ not /red|green/
|
|
|
|
// it's up to the logger or console to determine the color
|
2017-03-11 03:00:30 +00:00
|
|
|
var ServicesMonitor = function(options) {
|
|
|
|
this.events = options.events;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.checkList = {};
|
|
|
|
this.checkTimers = {};
|
|
|
|
this.checkState = {};
|
2017-03-11 16:48:12 +00:00
|
|
|
this.working = false;
|
2017-03-11 03:00:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-11 16:48:12 +00:00
|
|
|
ServicesMonitor.prototype.initCheck = function(checkName) {
|
|
|
|
var self = this;
|
|
|
|
var check = this.checkList[checkName];
|
|
|
|
|
|
|
|
if (!check) { return false; }
|
|
|
|
|
|
|
|
self.events.on('check:' + checkName, function(obj) {
|
2017-03-11 17:27:10 +00:00
|
|
|
// TODO: see todo above
|
|
|
|
if (check && check.status === 'red' && obj.status === 'green') {
|
|
|
|
self.events.emit('check:backOnline:' + checkName);
|
|
|
|
}
|
|
|
|
if (check && check.status === 'green' && obj.status === 'red') {
|
|
|
|
self.events.emit('check:wentOffline:' + checkName);
|
|
|
|
}
|
2017-03-11 16:48:12 +00:00
|
|
|
self.checkState[checkName] = obj.name[obj.status];
|
2017-03-11 17:27:10 +00:00
|
|
|
check.status = obj.status;
|
2017-03-11 16:48:12 +00:00
|
|
|
self.events.emit("servicesState", self.checkState);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (check.interval !== 0) {
|
|
|
|
self.checkTimers[checkName] = setInterval(function() {
|
|
|
|
check.fn.call(check.fn, function(obj) {
|
|
|
|
self.events.emit('check:' + checkName, obj);
|
|
|
|
});
|
|
|
|
}, check.interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
check.fn.call(check.fn, function(obj) {
|
|
|
|
self.events.emit('check:' + checkName, obj);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ServicesMonitor.prototype.addCheck = function(checkName, checkFn, time) {
|
|
|
|
var self = this;
|
|
|
|
this.logger.trace('add check: ' + checkName);
|
|
|
|
this.checkList[checkName] = {fn: checkFn, interval: time || 5000};
|
|
|
|
|
|
|
|
if (this.working) {
|
|
|
|
this.initCheck(checkName);
|
|
|
|
}
|
2017-03-11 03:00:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-11 16:23:42 +00:00
|
|
|
ServicesMonitor.prototype.stopCheck = function(name) {
|
|
|
|
clearInterval(this.checkTimers[name]);
|
|
|
|
delete this.checkTimers[name];
|
|
|
|
delete this.checkList[name];
|
|
|
|
delete this.checkState[name];
|
|
|
|
};
|
|
|
|
|
2017-03-11 03:00:30 +00:00
|
|
|
ServicesMonitor.prototype.startMonitor = function() {
|
|
|
|
var self = this;
|
2017-03-11 16:48:12 +00:00
|
|
|
this.working = true;
|
|
|
|
this.logger.trace('startMonitor');
|
2017-03-11 03:00:30 +00:00
|
|
|
|
2017-03-11 12:32:16 +00:00
|
|
|
async.eachObject(this.checkList, function(checkName, check, callback) {
|
2017-03-11 16:48:12 +00:00
|
|
|
self.initCheck(checkName);
|
2017-03-11 16:17:52 +00:00
|
|
|
callback();
|
2017-03-11 12:32:16 +00:00
|
|
|
}, function(err) {
|
2017-03-11 16:17:52 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error("error running service check");
|
|
|
|
self.logger.error(err.message);
|
|
|
|
}
|
2017-03-11 12:32:16 +00:00
|
|
|
});
|
2017-03-11 03:00:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ServicesMonitor;
|