-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathreportHandler.js
More file actions
539 lines (507 loc) · 18.2 KB
/
reportHandler.js
File metadata and controls
539 lines (507 loc) · 18.2 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
const fs = require('fs');
const os = require('os');
const { errors, ipCheck } = require('arsenal');
const async = require('async');
const request = require('./request');
const config = require('../Config').config;
const { data } = require('../data/wrapper');
const metadata = require('../metadata/wrapper');
const monitoring = require('../utilities/monitoringHandler');
const vault = require('../auth/vault');
const REPORT_MODEL_VERSION = 1;
const ASYNCLIMIT = 5;
const REQ_PATHS = {
crrSchedules: '/_/crr/resume/all',
crrStatus: '/_/crr/status',
crrMetricPrefix: '/_/metrics/crr',
ingestionSchedules: '/_/ingestion/resume/all',
ingestionStatus: '/_/ingestion/status',
ingestionMetricPrefix: '/_/metrics/ingestion',
};
function hasWSOptionalDependencies() {
try {
const b = require('bufferutil');
const u = require('utf-8-validate');
return !!(b && u);
} catch {
return false;
}
}
function getCapabilities(cfg = config) {
const caps = cfg.capabilities || {
// Default capabilities, for backward compatibility. Should not be modified,
// changes are expected to be done through config.json file.
locationTypeAzure: true,
locationTypeGCP: true,
locationTypeDigitalOcean: true,
locationTypeS3Custom: true,
locationTypeSproxyd: true,
locationTypeNFS: true,
locationTypeHyperdriveV2: true,
locationTypeLocal: true,
preferredReadLocation: true,
managedLifecycle: true,
managedLifecycleTransition: true,
secureChannelOptimizedPath: true,
s3cIngestLocation: true,
nfsIngestLocation: false,
awsIngestLocation: false,
};
// Consistency & safety checks for capabilities that depend on other config values
const localVolumeCap = process.env.LOCAL_VOLUME_CAPABILITY || 'true';
caps.locationTypeLocal &&= (localVolumeCap === '1' || localVolumeCap.toLowerCase() === 'true');
caps.secureChannelOptimizedPath &&= hasWSOptionalDependencies();
caps.managedLifecycle &&= cfg.supportedLifecycleRules.includes('Expiration');
caps.managedLifecycleTransition &&= cfg.supportedLifecycleRules.includes('Transition');
caps.lifecycleRules &&= cfg.supportedLifecycleRules;
// Map locationTypes entries to the respective "legacy" capability flags
if (cfg.supportedLocationTypes) {
caps.locationTypeAzure &&= cfg.supportedLocationTypes.has('location-azure-v1');
caps.locationTypeGCP &&= cfg.supportedLocationTypes.has('location-gcp-v1');
caps.locationTypeDigitalOcean &&= cfg.supportedLocationTypes.has('location-do-spaces-v1');
caps.locationTypeSproxyd &&= cfg.supportedLocationTypes.has('location-scality-sproxyd-v1');
caps.locationTypeNFS &&= cfg.supportedLocationTypes.has('location-nfs-mount-v1');
caps.locationTypeHyperdriveV2 &&= cfg.supportedLocationTypes.has('location-scality-hdclient-v2');
caps.locationTypeLocal &&= cfg.supportedLocationTypes.has('location-file-v1');
}
delete caps.locationTypeCephRadosGW;
delete caps.cephIngestLocation;
return caps;
}
function cleanup(obj) {
return {
overlayVersion: obj.overlayVersion,
};
}
function isAuthorized(clientIP, req) {
return ipCheck.ipMatchCidrList(config.healthChecks.allowFrom, clientIP) &&
req.headers['x-scal-report-token'] === config.reportToken;
}
function getGitVersion(cb) {
fs.readFile('.git/HEAD', 'ascii', (err, val) => {
if (err && err.code === 'ENOENT') {
return cb(null, 'no-dot-git');
}
if (err) {
return cb(null, 'error-reading-dot-git');
}
return cb(null, val);
});
}
function getSystemStats() {
const cpuInfo = os.cpus();
const model = cpuInfo[0].model;
const speed = cpuInfo[0].speed;
const times = cpuInfo.
map(c => c.times).
reduce((prev, cur) =>
Object.assign({}, {
user: prev.user + cur.user,
nice: prev.nice + cur.nice,
sys: prev.sys + cur.sys,
idle: prev.idle + cur.idle,
irq: prev.irq + cur.irq,
}), {
user: 0,
nice: 0,
sys: 0,
idle: 0,
irq: 0,
});
return {
memory: {
total: os.totalmem(),
free: os.freemem(),
},
cpu: {
loadavg: os.loadavg(),
count: cpuInfo.length,
model,
speed,
times,
},
arch: os.arch(),
platform: os.platform(),
release: os.release(),
hostname: os.hostname(),
};
}
const _makeRequest = (endpoint, path, cb) => {
const url = `${endpoint}${path}`;
request.get(url, { json: true }, (error, response, body) => {
if (error) {
return cb(error);
}
if (response.statusCode >= 400) {
return cb('responseError', body);
}
if (body) {
return cb(null, body);
}
return cb(null, {});
});
};
function _crrMetricRequest(endpoint, site, log, cb) {
const path = `${REQ_PATHS.crrMetricPrefix}/${site}`;
return _makeRequest(endpoint, path, (err, res) => {
if (err) {
if (err === 'responseError') {
log.error('error response from backbeat api', {
error: res,
method: '_crrMetricRequest',
});
} else {
log.error('unable to perform request to backbeat api', {
error: err,
method: '_crrMetricRequest',
});
}
return cb(null, {});
}
const { completions, failures, backlog, throughput, pending } = res;
if (!completions || !failures || !backlog || !throughput || !pending) {
log.error('could not get metrics from backbeat', {
method: '_crrMetricRequest',
});
return cb(null, {});
}
const stats = {
completions: {
count: parseInt(completions.results.count, 10),
size: parseInt(completions.results.size, 10),
},
failures: {
count: parseInt(failures.results.count, 10),
size: parseInt(failures.results.size, 10),
},
backlog: {
count: parseInt(backlog.results.count, 10),
size: parseInt(backlog.results.size, 10),
},
throughput: {
count: parseInt(throughput.results.count, 10),
size: parseInt(throughput.results.size, 10),
},
pending: {
count: parseInt(pending.results.count, 10),
size: parseInt(pending.results.size, 10),
},
};
return cb(null, stats);
});
}
function _ingestionMetricRequest(endpoint, site, log, cb) {
const path = `${REQ_PATHS.ingestionMetricPrefix}/${site}`;
return _makeRequest(endpoint, path, (err, res) => {
if (err) {
if (err === 'responseError') {
log.error('error response from backbeat api', {
error: res,
method: '_ingestionMetricRequest',
});
} else {
log.error('unable to perform request to backbeat api', {
error: err,
method: '_ingestionMetricRequest',
});
}
return cb(null, {});
}
const { completions, throughput, pending } = res;
if (!completions || !throughput || !pending) {
log.error('could not get metrics from backbeat', {
method: '_ingestionMetricRequest',
});
return cb(null, {});
}
const stats = {
completions: {
count: parseInt(completions.results.count, 10),
},
throughput: {
count: parseInt(throughput.results.count, 10),
},
pending: {
count: parseInt(pending.results.count, 10),
},
};
return cb(null, stats);
});
}
function _getMetricsByLocation(endpoint, sites, requestMethod, log, cb) {
async.mapLimit(
sites,
ASYNCLIMIT,
(site, next) => requestMethod(endpoint, site, log, (err, res) => {
if (err) {
log.debug('Error in retrieving site metrics', {
method: '_getMetricsByLocation',
error: err,
site,
requestType: requestMethod.name,
});
return next(null, { site, stats: {} });
}
return next(null, { site, stats: res });
}),
(err, locStats) => {
if (err) {
log.error('failed to get stats for site', {
method: '_getMetricsByLocation',
error: err,
requestType: requestMethod.name,
});
return cb(null, {});
}
const retObj = {};
locStats.forEach(locStat => {
retObj[locStat.site] = locStat.stats;
});
return cb(null, retObj);
}
);
}
function _getMetrics(sites, requestMethod, log, cb, _testConfig) {
const conf = (_testConfig && _testConfig.backbeat) || config.backbeat;
const { host, port } = conf;
const endpoint = `http://${host}:${port}`;
return async.parallel({
all: done => requestMethod(endpoint, 'all', log, done),
byLocation: done => _getMetricsByLocation(endpoint, sites,
requestMethod, log, done),
}, (err, res) => {
if (err) {
return cb(err);
}
const all = (res && res.all) || {};
const byLocation = (res && res.byLocation) || {};
const retObj = Object.assign({}, all, { byLocation });
return cb(null, retObj);
});
}
function getCRRMetrics(log, cb, _testConfig) {
log.debug('request CRR metrics from backbeat api', {
method: 'getCRRMetrics',
});
const { replicationEndpoints } = _testConfig || config;
const sites = replicationEndpoints.map(endpoint => endpoint.site);
return _getMetrics(sites, _crrMetricRequest, log, (err, retObj) => {
if (err) {
log.error('failed to get CRR stats', {
method: 'getCRRMetrics',
error: err,
});
return cb(null, {});
}
return cb(null, retObj);
}, _testConfig);
}
function getIngestionMetrics(sites, log, cb, _testConfig) {
log.debug('request Ingestion metrics from backbeat api', {
method: 'getIngestionMetrics',
});
return _getMetrics(sites, _ingestionMetricRequest, log, (err, retObj) => {
if (err) {
log.error('failed to get Ingestion stats', {
method: 'getIngestionMetrics',
error: err,
});
return cb(null, {});
}
return cb(null, retObj);
}, _testConfig);
}
function _getStates(statusPath, schedulePath, log, cb, _testConfig) {
const conf = (_testConfig && _testConfig.backbeat) || config.backbeat;
const { host, port } = conf;
const endpoint = `http://${host}:${port}`;
async.parallel({
states: done => _makeRequest(endpoint, statusPath, done),
schedules: done => _makeRequest(endpoint, schedulePath, done),
}, (err, res) => {
if (err) {
return cb(err);
}
const locationSchedules = {};
Object.keys(res.schedules).forEach(loc => {
const val = res.schedules[loc];
if (!isNaN(Date.parse(val))) {
locationSchedules[loc] = new Date(val);
}
});
const retObj = {
states: res.states || {},
schedules: locationSchedules,
};
return cb(null, retObj);
});
}
function getReplicationStates(log, cb, _testConfig) {
log.debug('requesting replication location states from backbeat api', {
method: 'getReplicationStates',
});
const { crrStatus, crrSchedules } = REQ_PATHS;
return _getStates(crrStatus, crrSchedules, log, (err, res) => {
if (err) {
if (err === 'responseError') {
log.error('error response from backbeat api', {
error: res,
method: 'getReplicationStates',
service: 'replication',
});
} else {
log.error('unable to perform request to backbeat api', {
error: err,
method: 'getReplicationStates',
service: 'replication',
});
}
return cb(null, {});
}
return cb(null, res);
}, _testConfig);
}
function getIngestionStates(log, cb, _testConfig) {
log.debug('requesting location ingestion states from backbeat api', {
method: 'getIngestionStates',
});
const { ingestionStatus, ingestionSchedules } = REQ_PATHS;
return _getStates(ingestionStatus, ingestionSchedules, log, (err, res) => {
if (err) {
if (err === 'responseError') {
log.error('error response from backbeat api', {
error: res,
method: 'getIngestionStates',
service: 'ingestion',
});
} else {
log.error('unable to perform request to backbeat api', {
error: err,
method: 'getIngestionStates',
service: 'ingestion',
});
}
return cb(null, {});
}
return cb(null, res);
}, _testConfig);
}
function getIngestionInfo(log, cb, _testConfig) {
log.debug('requesting location ingestion info from backbeat api', {
method: 'getIngestionInfo',
});
async.waterfall([
done => getIngestionStates(log, done, _testConfig),
(stateObj, done) => {
// if getIngestionStates returned an error or the returned object
// did not return an expected response
if (Object.keys(stateObj).length === 0 || !stateObj.states) {
log.debug('no ingestion locations found', {
method: 'getIngestionInfo',
});
return done(null, stateObj, {});
}
const sites = Object.keys(stateObj.states);
return getIngestionMetrics(sites, log, (err, res) => {
if (err) {
log.error('failed to get Ingestion stats', {
method: 'getIngestionInfo',
error: err,
});
return done(null, stateObj, {});
}
return done(null, stateObj, res);
}, _testConfig);
},
], (err, stateObj, metricObj) => {
if (err) {
log.error('failed to get ingestion info', {
method: 'getIngestionInfo',
error: err,
});
return cb(null, {});
}
return cb(null, {
metrics: metricObj,
status: stateObj,
});
});
}
/**
* Sends back a report
*
* @param {string} clientIP - Client IP address for filtering
* @param {http~IncomingMessage} req - HTTP request object
* @param {http~ServerResponse} res - HTTP response object
* @param {werelogs~RequestLogger} log - request logger
*
* @return {undefined}
*/
function reportHandler(clientIP, req, res, log) {
// Attach the apiMethod method to the request, so it can used by monitoring in the server
// eslint-disable-next-line no-param-reassign
req.apiMethod = 'report';
if (!isAuthorized(clientIP, req)) {
res.writeHead(403);
res.write(JSON.stringify(errors.AccessDenied));
res.end();
return;
}
// TODO propagate value of req.headers['x-scal-report-skip-cache']
async.parallel({
getUUID: cb => metadata.getUUID(log, cb),
getMDDiskUsage: cb => metadata.getDiskUsage(log, cb),
getDataDiskUsage: cb => data.getDiskUsage(log, cb),
getVersion: cb => getGitVersion(cb),
getObjectCount: cb => metadata.countItems(log, cb),
getCRRMetrics: cb => getCRRMetrics(log, cb),
getReplicationStates: cb => getReplicationStates(log, cb),
getIngestionInfo: cb => getIngestionInfo(log, cb),
getVaultReport: cb => vault.report(log, cb),
},
(err, results) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(err));
log.errorEnd('could not gather report', { error: err });
} else {
const getObjectCount = results.getObjectCount;
const crrStatsObj = Object.assign({}, results.getCRRMetrics);
crrStatsObj.stalled = { count: getObjectCount.stalled || 0 };
delete getObjectCount.stalled;
const response = {
utcTime: new Date(),
uuid: results.getUUID,
reportModelVersion: REPORT_MODEL_VERSION,
mdDiskUsage: results.getMDDiskUsage,
dataDiskUsage: results.getDataDiskUsage,
serverVersion: results.getVersion,
systemStats: getSystemStats(),
itemCounts: getObjectCount,
crrStats: crrStatsObj,
repStatus: results.getReplicationStates,
config: cleanup(config),
capabilities: getCapabilities(),
ingestStats: results.getIngestionInfo.metrics,
ingestStatus: results.getIngestionInfo.status,
vaultReport: results.getVaultReport,
};
monitoring.crrCacheToProm(results);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(response));
log.end().debug('report handler finished');
}
res.end();
});
}
module.exports = {
getCapabilities,
reportHandler,
_crrMetricRequest,
getCRRMetrics,
getReplicationStates,
_ingestionMetricRequest,
getIngestionMetrics,
getIngestionStates,
getIngestionInfo,
};