/
redfish-job.js
executable file
·579 lines (550 loc) · 20.1 KB
/
redfish-job.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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright 2016, EMC, Inc.
'use strict';
var di = require('di'),
urlParse = require('url-parse');
module.exports = redfishJobFactory;
di.annotate(redfishJobFactory, new di.Provide('Job.Redfish'));
di.annotate(redfishJobFactory, new di.Inject(
'Job.Base',
'Logger',
'Util',
'Assert',
'Promise',
'_',
'Services.Waterline',
'JobUtils.RedfishTool',
'Constants'
));
function redfishJobFactory(
BaseJob,
Logger,
util,
assert,
Promise,
_,
waterline,
RedfishTool,
Constants
) {
var logger = Logger.initialize(redfishJobFactory);
/**
*
* @param {Object} options
* @param {Object} context
* @param {String} taskId
* @constructor
*/
function RedfishJob(options, context, taskId) {
RedfishJob.super_.call(this, logger, options, context, taskId);
this.routingKey = this.context.graphId;
assert.uuid(this.routingKey) ;
this.concurrent = {};
this.maxConcurrent = 1;
this.commandOptions = {};
}
util.inherits(RedfishJob, BaseJob);
RedfishJob.prototype.initClient = function(settings) {
var redfish = new RedfishTool();
redfish.settings = settings;
return redfish;
};
/**
* @function _run
* @description the jobs internal run method
*/
RedfishJob.prototype._run = function run() {
var self = this;
return waterline.workitems.update({name: "Pollers.Redfish"}, {failureCount: 0})
.then(function() {
self._subscribeRedfishCommand(self.routingKey, function(data) {
self.commandOptions = data;
if (self.concurrentRequests(data.node, data.workItemId)) {
return;
}
self.addConcurrentRequest(data.node, data.workItemId);
assert.object(data.config,
'Redfish Poller Data Config');
return waterline.obms.findByNode(data.node, 'redfish-obm-service', true)
.then(function (obmSetting) {
if (obmSetting) {
return [
obmSetting.config,
data.config.command
];
}
})
.spread(function (config, command) {
return self.collectData(
config,
command
);
})
.then(function (result) {
assert.object(result,
'Expected Result Object For: ' + data.config.command
);
data[data.config.command] = result;
return self._publishRedfishCommandResult(
self.routingKey,
data.config.command,
data
);
})
.then(function () {
return waterline.workitems.findOne({id: data.workItemId});
})
.then(function (workitem) {
return waterline.workitems.setSucceeded(null, null, workitem);
})
.catch(function (err) {
logger.error("Error Retrieving Redfish Data.", {
data: data,
error: err
});
})
.finally(function () {
self.removeConcurrentRequest(data.node, data.workItemId);
});
});
})
.catch(function(err) {
logger.error("Failed to initialize job", {
error:err
});
self._done(err);
});
};
/**
* @function concurrentRequests
* @description manage concurrent work item command requests
*/
RedfishJob.prototype.concurrentRequests = function(node, workItemId) {
assert.string(node);
assert.string(workItemId);
if(!_.has(this.concurrent, node)){
this.concurrent[node] = {};
}
if(!_.has(this.concurrent[node], workItemId)){
this.concurrent[node][workItemId] = 0;
}
if(this.concurrent[node][workItemId] >= this.maxConcurrent){
return true;
} else {
return false;
}
};
/**
* @function addConcurrentRequest
* @description add a new command request for this work item
*/
RedfishJob.prototype.addConcurrentRequest = function(node, type) {
assert.object(this.concurrent[node]);
assert.number(this.concurrent[node][type]);
this.concurrent[node][type] += 1;
};
/**
* @function removeConcurrentRequest
* @description remove a completed command request for this work item
*/
RedfishJob.prototype.removeConcurrentRequest = function(node, type) {
assert.object(this.concurrent[node]);
assert.number(this.concurrent[node][type]);
this.concurrent[node][type] -= 1;
};
/**
* Collect Redfish Log Service Entries for Systems resource member
* @param redfish client utility
* @param system resource path to collect entries from
*/
RedfishJob.prototype.collectSystemLogEntries = function(redfish, resource) {
return redfish.clientRequest(resource)
.then(function(res) {
assert.ok(_.has(res.body, 'Members'),
'Has LogServices Members');
return res.body.Members;
})
.map(function(member) {
assert.object(member, 'LogService Member');
return redfish.clientRequest(member['@odata.id']);
})
.map(function(res) {
assert.ok(_.has(res.body, 'Entries'),
'Has LogService Entries');
return redfish.clientRequest(res.body.Entries['@odata.id']);
})
.map(function(res) {
assert.ok(_.has(res.body, 'Members'),
'Has Entry Members');
return res.body.Members;
})
.map(function(memberArr) {
var promises = [];
_.forEach(memberArr, function(member) {
promises.push(redfish.clientRequest(member['@odata.id']));
});
return Promise.all(promises);
})
.map(function(resArr) {
var data = [];
_.forEach(resArr, function(res) {
data.push(res.body);
});
return data;
});
};
/**
* Collect Redfish Log Service Entries for Managers resource member
* @param redfish client utility
*/
RedfishJob.prototype.collectManagersLogEntries = function(redfish) {
var parse = urlParse(redfish.settings.uri);
var rootPath = parse.pathname + '/';
// Managers is a root resource
return redfish.clientRequest(rootPath)
.then(function(res) {
assert.ok(_.has(res.body, 'Managers'),
'Managers Resource');
return redfish.clientRequest(res.body.Managers['@odata.id']);
})
.then(function(res) {
assert.ok(_.has(res.body, 'Members'),
'Has Manager Members');
return res.body.Members;
})
.map(function(member) {
assert.object(member, 'Manager Member');
return redfish.clientRequest(member['@odata.id']);
})
.map(function(res) {
assert.ok(_.has(res.body, 'LogServices'),
'Has LogServices Resource');
return redfish.clientRequest(res.body.LogServices['@odata.id']);
})
.map(function(res) {
assert.ok(_.has(res.body, 'Members'),
'Has LogService Members');
return res.body.Members;
})
.map(function(member) {
assert.object(member, 'Log Service Member');
return redfish.clientRequest(member[0]['@odata.id']);
})
.map(function(res) {
assert.ok(_.has(res.body, 'Entries'),
'Has LogServices Entries');
return redfish.clientRequest(res.body.Entries['@odata.id']);
})
.map(function(res) {
assert.ok(_.has(res.body, 'Members'),
'Has Entry Members');
return res.body.Members;
})
.map(function(memberArr) {
var promises = [];
_.forEach(memberArr, function(member) {
promises.push(redfish.clientRequest(member['@odata.id']));
});
return Promise.all(promises);
})
.map(function(resArr) {
var data = [];
_.forEach(resArr, function(res) {
data.push(res.body);
});
return data;
});
};
RedfishJob.prototype._formatFabricServiceAlert = function _formatFabricServiceAlert(alert) {
/*
* alert format:
{ pollerName: 'FabricService',
data: [ { EndPointName: 'epName', Available: true } ],
EventType: 'ResourceUpdated'
}
* expected event format:
{ type: 'polleralert',
action: 'fabricservice.updated',
typeId: '5858c89bca4349fa0409ba8d',
nodeId: '5858c7f7f6f0ce7d08a7298b',
severity: 'information',
data: {
eventType: 'ResourceUpdated',
resource: [ { EndPointName: 'epName', Available: true } ]
}
}
*/
var self = this;
return {
type: 'polleralert',
action: 'fabricservice.updated',
typeId: self.commandOptions.workItemId,
nodeId: self.commandOptions.node,
severity: "information",
data: {
eventType: alert.EventType,
resource: alert.data
}
};
};
/**
* Check fabric service status and generate alert on status change
* @param redfish client utility
*/
RedfishJob.prototype.fabricServiceDataAlert = function(redfish, currentData) {
var self = this;
var alert = {
pollerName: 'FabricService',
data: []
};
var eventTypes = Constants.Redfish.EventTypes;
return Promise.resolve()
.then(function() {
if(self.fabricDataCache) {
assert.ok(_.has(self.fabricDataCache, 'EndPoints'),
'Last Endpoint Data'
);
assert.ok(_.has(currentData, 'EndPoints'),
'Current Endpoint Data'
);
var currentEndpoints = currentData.EndPoints;
var lastEndpoints = self.fabricDataCache.EndPoints;
if(JSON.stringify(currentEndpoints) !== JSON.stringify(lastEndpoints)) {
_.forEach(currentEndpoints, function(endpoint) {
var match = _.find(lastEndpoints,
_.matches({EndPointName:endpoint.EndPointName}));
if(_.isUndefined(match)) {
alert.data.push(endpoint);
alert.EventType = eventTypes.ResourceAdded;
} else {
if(match.Available !== endpoint.Available) {
alert.data.push(endpoint);
alert.EventType = eventTypes.ResourceUpdated;
}
}
});
_.forEach(lastEndpoints, function(endpoint) {
var match = _.find(currentEndpoints,
_.matches({EndPointName:endpoint.EndPointName}));
if(_.isUndefined(match)) {
alert.data.push(endpoint);
alert.EventType = eventTypes.ResourceRemoved;
}
});
}
if(alert.data.length) {
logger.debug('FabricService Status Alert', {alert:alert});
return self._publishPollerAlert(self._formatFabricServiceAlert(alert));
}
return;
}
})
.then(function() {
self.fabricDataCache = currentData;
return currentData;
});
};
/**
* Collect Emc OEM Redfish FabricService resource data
* @param redfish client utility
*/
RedfishJob.prototype.collectFabricServiceData = function(redfish) {
var self = this;
var parse = urlParse(redfish.settings.uri);
var rootPath = parse.pathname + '/';
// FabricService is a root Oem resource
return redfish.clientRequest(rootPath)
.then(function(res) {
assert.ok(_.has(res.body, 'Oem.Emc.FabricService'),
'Has FabricService Resource');
return redfish.clientRequest(
res.body.Oem.Emc.FabricService['@odata.id']
);
})
.then(function(res) {
return self.fabricServiceDataAlert(redfish, res.body);
});
};
/**
* Collect Emc OEM Redfish Elements Power/Thermal resource data
* @param redfish client utility
* @param type the resource type Power or Thermal
*/
RedfishJob.prototype.collectOemElementsData = function(redfish, type) {
var self = this;
var parse = urlParse(redfish.settings.root);
return redfish.clientRequest(parse)
.then(function (res) {
assert.ok(_.has(res.body, 'Oem.Emc.Elements'),
'Has Oem Elements Resource');
var elements = _.get(res.body, 'Oem.Emc.Elements');
return elements['@odata.id'];
})
.then(function (id) {
assert.string(id, 'Element Identifier');
return redfish.clientRequest(id);
})
.then(function (res) {
assert.object(res, 'Element Resource Object');
var elements = _.get(res, 'body.Members');
return elements;
})
.map(function (element) {
return redfish.clientRequest(element['@odata.id']);
})
.map(function (element) {
assert.object(element.body);
return element.body;
})
.map(function (element) {
return redfish.clientRequest(element['@odata.id'])
.then(function (element) {
return element;
});
})
.map(function (element) {
assert.ok(_.has(element.body, type), 'Has Thermal Resource');
var id = _.get(element.body, type, {})['@odata.id'];
return redfish.clientRequest(id)
.then(function(res) {
assert.object(res.body, type + ' data');
return(res.body);
});
});
};
/**
* Collect Emc OEM Redfish Spine Modules Power/Thermal resource data
* @param redfish client utility
* @param type the resource type Power or Thermal
*/
RedfishJob.prototype.collectOemSpineData = function(redfish, type) {
return redfish.clientRequest().then(function(res) {
var spineModId = _.get(res.body, 'Oem.Emc.SpineModules', {})['@odata.id'];
if (_.isUndefined(spineModId)) {
throw new Error('Missing SpineModules Resource');
}
return redfish.clientRequest(spineModId).then(function(res) {
var members = _.get(res, 'body.Members', {});
assert.ok(Array.isArray(members), 'SpineModules Member List');
return members;
});
}).map(function(member) {
return redfish.clientRequest(member['@odata.id']);
}).map(function(res) {
var id = _.get(res.body, type, {})['@odata.id'];
return redfish.clientRequest(id);
}).map(function(res) {
return res.body;
});
};
/**
* Collect Emc OEM Redfish Aggregator Power/Thermal resource data
* @param redfish client utility
* @param type the resource type Power or Thermal
*/
RedfishJob.prototype.collectOemAggregatorData = function(redfish, type) {
return redfish.clientRequest().then(function(res) {
var spineModId = _.get(res.body, 'Oem.Emc.Aggregators', {})['@odata.id'];
if (_.isUndefined(spineModId)) {
throw new Error('Missing Aggregators Resource');
}
return redfish.clientRequest(spineModId).then(function(res) {
var members = _.get(res, 'body.Members', {});
assert.ok(Array.isArray(members), 'Aggregators Member List');
return members;
});
}).map(function(member) {
return redfish.clientRequest(member['@odata.id']);
}).map(function(res) {
var id = _.get(res.body, type, {})['@odata.id'];
return redfish.clientRequest(id);
}).map(function(res) {
return res.body;
});
};
/**
* Collect Redfish telemetry data for specified Redfish command
* @param config the redfish configuration settings object
* @param command the redfish command
*/
RedfishJob.prototype.collectData = function(config, command) {
var self = this;
assert.string(command, 'Redfish Command');
var redfish = self.initClient(config);
return redfish.clientRequest(config.root)
.then(function(response) {
return response.body;
})
.then(function(member) {
switch(command) {
case 'power':
assert.ok(_.has(member, 'Power'),
'Power Resource for ' + member.Name);
return redfish.clientRequest(
member.Power['@odata.id']
);
case 'thermal':
assert.ok(_.has(member, 'Thermal'),
'Thermal Resource for ' + member.Name);
return redfish.clientRequest(
member.Thermal['@odata.id']
);
case 'systems.logservices':
assert.ok(_.has(member, 'LogServices'),
'LogService for ' + member.Name);
return self.collectSystemLogEntries(
redfish, member.LogServices['@odata.id']
);
case 'managers.logservices':
return self.collectManagersLogEntries(
redfish // starts at root
);
case 'fabricservice':
return self.collectFabricServiceData(
redfish // starts at root
);
case 'elements.thermal':
case 'elements.power':
return self.collectOemElementsData(
redfish,
(command === 'elements.thermal') ? 'Thermal' : 'Power'
);
case 'spine.thermal':
case 'spine.power':
return self.collectOemSpineData(
redfish,
(command === 'spine.thermal') ? 'Thermal' : 'Power'
);
case 'aggregator.thermal':
case 'aggregator.power':
return self.collectOemAggregatorData(
redfish,
(command === 'aggregator.thermal') ? 'Thermal' : 'Power'
);
default:
throw new Error(
'Unsupported Redfish Command: ' + command
);
}
})
.then(function(data) {
if (!data) {
throw new Error(
'No Data Found For Command: ' + command
);
}
if (data.body) {
data = data.body;
}
return data;
})
.catch(function(err) {
if(err.message !== 'Unsupported Redfish Command: ' + command &&
err.message !== 'No Data Found For Command: ' + command ) {
logger.warning('Collect Data', {error:err});
return {error:err}; // publish exceptions as data
}
throw err;
});
};
return RedfishJob;
}