forked from statusdashboard/statusdashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
261 lines (220 loc) · 8.09 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var util = require('util'),
fs = require('fs'),
_ = require('underscore')._,
humanized_time_span = require(__dirname + '/lib/humanized_time_span.js'),
EventEmitter = require('events').EventEmitter,
controller = new EventEmitter();
var log;
/**
Set this module as an express.js controller
*/
module.exports = controller;
/**
Setup and configuration helper. Should be called once
*/
var settings;
var sources = {};
module.exports.setup = function (settings) {
module.exports.configure(settings);
log = settings.logger ? settings.logger : require('util').log;
/**
Load available plugins
*/
fs.readdir(__dirname + '/plugins', function(err, pluginDirectories) {
if (!err) {
_.each(pluginDirectories, function(directory) {
if (fs.statSync(__dirname + '/plugins/' + directory).isDirectory() && fs.statSync(__dirname + '/plugins/' + directory + '/' + directory + '_plugin.js').isFile()) {
return require(__dirname + '/plugins/' + directory + '/' + directory + '_plugin.js').create(controller, settings);
} else {
log("Excluding plugin: " + directory);
}
});
} else {
log("Error when creating plugin: " + err);
}
});
/**
Load available sources
*/
sourceFiles = fs.readdirSync(__dirname + '/lib/sources');
_.each(sourceFiles, function(source) {
source = source.substr(0, source.length - 3);
sources[source] = require(__dirname + '/lib/sources/' + source).check;
});
if (settings.plugins.external.enable) {
var file = settings.plugins.external.file || __dirname + '/plugins.json';
log('Loading external plugins defined in ' + file + ' file');
var plugins;
try {
plugins = require(file);
} catch (err) {
log('Can not load external plugins file ' + err);
return err;
}
console.log(plugins)
_.each(plugins, function(item) {
try {
log('Loading external plugin ' + item);
require(item)({api : controller, settings : settings});
} catch (err) {
log('Can not load external plugin ' + item + ' : ' + err);
}
})
} else {
log('External plugins are not enabled, change value in settings.plugins.external.enable if needed');
}
};
/**
Configuration manipulations helpers
*/
module.exports.configure = function (s) {
settings = s;
};
module.exports.addService = function (serviceCfg) {
log('Adding new service: ' + serviceCfg.name);
module.exports.removeService(serviceCfg.name);
settings.services.push(serviceCfg);
};
module.exports.removeService = function (name) {
for (var i = 0; i < settings.services.length; i++) {
if (settings.services[i].name === name) {
settings.services.splice(i, 1);
status.services.splice(i, 1);
return true;
}
}
return false;
};
/**
Updating Service display at a given interval
*/
var status = {};
status.lastupdate = new Date().toGMTString();
status.services = [];
module.exports.checkAllServices = function() {
status.lastupdate = new Date().toUTCString();
log('Refreshing status board...');
/**
Update the service status object, check the service
*/
for (var i = 0; i < settings.services.length; i++) {
status.services[i] = {};
status.services[i].name = settings.services[i].name;
status.services[i].group = settings.services[i].group;
status.services[i].label = settings.services[i].label;
if (settings.services[i].status) {
status.services[i].status = settings.services[i].status;
} else {
status.services[i].status = 'unknown';
}
status.services[i].statusCode = 0;
status.services[i].message = '';
if (status.services[i].status !== 'maintenance') {
sources[settings.services[i].check].call(null, settings.services[i], status.services[i], function(status, service) {
controller.emit(status, service);
});
}
}
/**
There should be something more elegant to do here than to double schedule...
*/
setTimeout(function() {
var statusTab = _.map(status.services, function(value, key) { return value; });
status.summarize = {};
status.summarize.lastupdate = status.lastupdate;
status.summarize.up = _.reduce(_.select(status.services, function(data){ return data.status == 'up'; }), function(memo, num){ return memo + 1; }, 0);
status.summarize.critical = _.reduce(_.select(status.services, function(data){ return data.status == 'critical'; }), function(memo, num){ return memo + 1; }, 0);
status.summarize.maintenance = _.reduce(_.select(status.services, function(data){ return data.status == 'maintenance'; }), function(memo, num){ return memo + 1; }, 0);
status.summarize.down = _.reduce(_.select(status.services, function(data){ return data.status == 'down'; }), function(memo, num){ return memo + 1; }, 0);
status.summarize.unknown = _.reduce(_.select(status.services, function(data){ return data.status == 'unknown'; }), function(memo, num){ return memo + 1; }, 0);
controller.emit("refresh", status);
}, settings.serviceDelay);
};
module.exports.startChecking = function () {
module.exports.serviceCheckIntervalPointer = setInterval(module.exports.checkAllServices, settings.serviceInterval);
module.exports.checkAllServices();
};
module.exports.stopChecking = function () {
clearInterval(module.exports.serviceCheckIntervalPointer);
};
/**
Returns a list of available services
*/
module.exports.services = function(req, res) {
res.send(JSON.stringify(status), 200);
};
module.exports.servicesElement = function(req, res, value) {
res.send(JSON.stringify(_.first(_.select(status.services, function(data){
return data.name == value;
}))), 200);
};
/**
Summary report
*/
module.exports.summarize = function(req, res) {
res.send({
up: status.summarize.up,
critical: status.summarize.critical,
down: status.summarize.down,
unknown: status.summarize.unknown
}, 200);
};
exports.getServicesElement = function(value) {
return status.services[value];
};
module.exports.configClient = function(req, res) {
res.send(JSON.stringify(settings.client), 200);
};
module.exports.pluginsClient = function(req, res) {
var plugins = _.map(_.select(_.map(settings.plugins, function(num, key) {
return { name:key, enable: num.enable, client: num.client }; }
), function(data) {
return (data.enable === true && data.client === true);
}),
function(num, key) {
return { name:num.name };
});
res.send(JSON.stringify(plugins), 200);
};
/**
Give access to the whole status object
*/
module.exports.getStatus = function() {
return status;
};
/**
Advertise uptime in human-readable format
*/
var startupTime = new Date().valueOf();
var date_formats = {
past: [
{ ceiling: 60, text: "$seconds seconds ago" },
{ ceiling: 3600, text: "$minutes minutes and $seconds seconds ago" },
{ ceiling: 86400, text: "$hours hours, $minutes minutes and $seconds seconds ago" },
{ ceiling: 2629744, text: "$days days, $hours hours, $minutes minutes and $seconds seconds ago" },
{ ceiling: 31556926, text: "$months months, $days days, $hours hours, $minutes minutes and $seconds seconds ago" },
{ ceiling: null, text: "$years years ago, $months months, $days days, $hours hours, $minutes minutes and $seconds seconds" }
],
future: [
{ ceiling: 60, text: "in $seconds seconds" },
{ ceiling: 3600, text: "in $minutes minutes" },
{ ceiling: 86400, text: "in $hours hours" },
{ ceiling: 2629744, text: "in $days days" },
{ ceiling: 31556926, text: "in $months months" },
{ ceiling: null, text: "in $years years" }
]
};
module.exports.uptime = function(req, res) {
var now = new Date().valueOf();
var uptime = now - startupTime;
var human = humanized_time_span.humanized_time_span(startupTime, now, date_formats);
res.send({ startupTime: startupTime, now: now, uptime: uptime, human: human}, 200);
};
/**
Advertise application information
*/
var pkginfo = require('pkginfo')(module, 'name', 'version', 'description');
var info = { description: module.exports.description, name: module.exports.name, version: module.exports.version };
module.exports.info = function(req, res) {
res.send(info, 200);
};