forked from statusdashboard/statusdashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
365 lines (338 loc) · 13 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var http = require('http');
var https = require('https');
var net = require('net');
var fs = require('fs');
var logger = require('util');
var _ = require('underscore')._;
var settings = require('./settings').create();
var humanized_time_span = require(__dirname + '/lib/humanized_time_span.js');
var EventEmitter = require('events').EventEmitter;
var controller = new EventEmitter();
module.exports = controller;
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 {
logger.log("Excluding plugin: " + directory);
}
});
} else {
logger.log("Error when creating plugin: " + err);
}
});
var status = {};
status.lastupdate = new Date().toGMTString();
status.services = [];
updatingServices = function() {
status.lastupdate = new Date().toUTCString();
logger.log('Refreshing status board...');
for (var i = 0; i < settings.services.length; i++) {
status.services[i] = {};
status.services[i].name = settings.services[i].name;
status.services[i].label = settings.services[i].label;
status.services[i].status = 'unknown';
status.services[i].statusCode = 0;
status.services[i].message = '';
commands[settings.services[i].check].call(null, settings.services[i], status.services[i]);
}
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.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);
};
var checkHttpStatusCode = function(response, service) {
if (response.statusCode == 200) {
service.status = "up";
} else if (response.statusCode == 301 || response.statusCode == 302) {
service.status = "unknown";
service.message = "Redirected to " + response.headers.location;
} else if (response.statusCode == 503 || response.statusCode == 404) {
service.status = "down";
} else {
service.status = "critical";
}
service.statusCode = response.statusCode;
};
var checkRange = function(min, max, value) {
if (min && max && value >= min && value < max) {
return true;
} else if ( (min && value >= min) || (max && value < max) ) {
return true;
}
return false;
};
var checkStatusDashboardResponse = function(response, serviceDefinition, service) {
if(response.statusCode === 200){
response.on('data', function(chunk){
summary = JSON.parse(chunk);
if(summary.critical === 0 && summary.down === 0 && summary.unknown ===0){
service.status = 'up';
}else {
service.status = 'down';
service.message = "Services up: "+ summary.up + ", Services down: "+ summary.down + ", Services critical: "+summary.critical + ", Services unknown: "+summary.unknown;
}
controller.emit(service.status, service);
});
}
};
var checkHttpValueResponse = function(response, serviceDefinition, service) {
if (response.statusCode == 200 && (serviceDefinition.checkFixedValueResponse || serviceDefinition.checkRangeValuesResponse)) {
response.on('data', function (chunk) {
var value = ('' + chunk).substring(0, chunk.length - 1);
if (serviceDefinition.checkFixedValueResponse) {
if (serviceDefinition.checkFixedValueResponse[value]) {
service.status = serviceDefinition.checkFixedValueResponse[value];
} else {
service.status = "critical";
service.message = "Unexpected value " + value;
}
} else {
if (serviceDefinition.checkRangeValuesResponse) {
if (serviceDefinition.checkRangeValuesResponse.length == 0) {
service.status = "critical";
service.message = "No range defined!";
} else {
var found = false;
for(var i = 0; i < serviceDefinition.checkRangeValuesResponse.length; i++) {
var range = serviceDefinition.checkRangeValuesResponse[i];
if (checkRange(range.min, range.max, parseInt(value))) {
service.status = range.status;
found = true;
break;
}
}
if (!found) {
service.status = "critical";
service.message = "No range matches";
}
}
}
}
controller.emit(service.status, service);
});
} else {
controller.emit(service.status, service);
}
};
var commands = {
http : function(serviceDefinition, service) {
var options = {
host: serviceDefinition.host,
port: serviceDefinition.port,
path: serviceDefinition.path,
headers: serviceDefinition.headers
};
http.get(options, function(response) {
service.message = '';
checkHttpStatusCode(response, service);
checkHttpValueResponse(response, serviceDefinition, service);
})
.on('error', function(e) {
service.status = "down";
service.statusCode = 0;
service.message = e.message;
controller.emit(service.status, service);
});
},
https : function(serviceDefinition, service) {
var options = {
host: serviceDefinition.host,
port: serviceDefinition.port,
path: serviceDefinition.path,
headers: serviceDefinition.headers
};
https.get(options, function(response) {
service.message = '';
checkHttpStatusCode(response, service);
checkHttpValueResponse(response, serviceDefinition, service);
})
.on('error', function(e) {
service.status = "down";
service.statusCode = 0;
service.message = e.message;
controller.emit(service.status, service);
});
},
tcp : function(serviceDefinition, service) {
var stream = net.createConnection(serviceDefinition.port, serviceDefinition.host);
stream.addListener('data', function (buffer) {
if (!serviceDefinition.rcv || serviceDefinition.rcv == buffer) {
service.status = "up";
service.statusCode = 0;
service.message = "";
} else {
service.status = "critical";
service.statusCode = 0;
service.message = "Expected " + serviceDefinition.rcv + " but was " + buffer;
}
stream.end();
controller.emit(service.status, service);
});
stream.addListener('connect', function () {
stream.write(serviceDefinition.cmd);
});
stream.addListener('end', function () {
stream.end();
});
stream.addListener('error', function (e) {
service.status = "down";
service.statusCode = e.errno;
service.message = e.message;
controller.emit(service.status, service);
});
},
ftp : function(serviceDefinition, service) {
service.status = "unknown";
service.statusCode = 0;
service.message = 'FTP check in progress...';
var stream = net.createConnection(serviceDefinition.port, serviceDefinition.host);
stream.addListener('connect', function () {
var ftpCommand = function(command, callback) {
stream.write(command + '\r\n');
var dataCallback = function(data) {
stream.removeListener('data', dataCallback);
var status = data.toString().match(/^\d\d\d/);
if (parseInt(status[0]) > 399) {
ftpError(status[0], data.toString());
} else {
return callback(data);
}
};
stream.addListener('data', dataCallback);
};
ftpCommand('USER ' + service.username, function(data) {
ftpCommand('PASS ' + service.password, function(data) {
service.status = "up";
service.statusCode = 0;
service.message = "";
controller.emit(service.status, service);
});
});
});
var ftpError = function(status, message) {
service.status = "down";
service.statusCode = status;
service.message = message;
controller.emit(service.status, service);
};
stream.addListener('error', function (e) {
service.status = "down";
service.statusCode = 0;
service.message = e.message;
controller.emit(service.status, service);
});
},
pidfile : function(serviceDefinition, service) {
service.status = "unknown";
service.statusCode = 0;
service.message = '';
var pidfiles = [serviceDefinition.pidfile];
if (serviceDefinition.pidfile.constructor === Array) {
pidfiles = serviceDefinition.pidfile;
}
for (var index = 0; index < pidfiles.length; index++) {
try {
var pid = fs.readFileSync(pidfiles[index]);
} catch (e) {
service.status = "unknown";
service.statusCode = e.errno;
service.message = e.message;
controller.emit(service.status, service);
return;
}
try {
var pidInt = parseInt(pid);
if (isNaN(pidInt)) {
service.status = "critical";
service.message = 'pid \'' + pid + '\' is not a number';
return;
} else {
process.kill(pidInt, 0);
}
} catch (e) {
service.status = "down";
service.statusCode = e.errno;
service.message = e.message;
controller.emit(service.status, service);
return;
}
}
service.status = "up";
controller.emit(service.status, service);
},
statusdashboard : function(serviceDefinition, service) {
var options = {
host: serviceDefinition.host,
port: serviceDefinition.port,
path: '/api/summarize'
};
http.get(options, function(response) {
service.message = '';
checkHttpStatusCode(response, service);
checkStatusDashboardResponse(response, serviceDefinition, service);
})
.on('error', function(e) {
service.status = "down";
service.statusCode = 0;
service.message = e.message;
controller.emit(service.status, service);
});
}
};
setInterval(updatingServices, settings.serviceInterval);
updatingServices();
module.exports.services = function(req, res) {
res.send(200, {}, JSON.stringify(status));
};
module.exports.servicesElement = function(req, res, value) {
res.send(200, {}, JSON.stringify(_.first(_.select(status.services, function(data){ return data.name == value; }))));
};
module.exports.summarize = function(req, res) {
res.send(200, {}, { up: status.summarize.up, critical: status.summarize.critical, down: status.summarize.down, unknown: status.summarize.unknown });
};
exports.getServicesElement = function(value) {
return status.services[value];
};
module.exports.configClient = function(req, res) {
res.send(200, {}, JSON.stringify(settings.client));
}
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(200, {}, JSON.stringify(plugins));
}
module.exports.getStatus = function() {
return status;
};
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(200, {}, { startupTime: startupTime, now: now, uptime: uptime, human: human});
}