@stordata/vsphere-soapify
Version:
A NodeJS abstraction layer for the vSphere SOAP API
63 lines (51 loc) • 2.42 kB
JavaScript
;
const _ = require('lodash'),
ManagedEntity = require('./ManagedEntity'),
PerformanceDescription = require('../data/PerformanceDescription'),
PerfProviderSummary = require('../data/PerfProviderSummary'),
PerfEntityMetric = require('../data/PerfEntityMetric'),
PerfInterval = require('../data/PerfInterval'),
PerfCounterInfo = require('../data/PerfCounterInfo'),
// Taken as-is from the VMWare documentation
GROUPS = 'cpu|mem|managementAgent|disk|net|sys|power|storageAdapter|virtualDisk|datastore',
ROLLUP_TYPES = 'average|latest|maximum|minimum|none|summation',
UNITS = 'percent|megaHertz|millisecond|number|megaBytes|megaBytesPerSecond|kiloBytes|kiloBytesPerSecond|second|watt|joule|microsecond|teraBytes|celsius',
// Gets used below to split a counter name, and support dots
COUNTER_NAME_REGEX = new RegExp(`^(${GROUPS})\\.(.+)\\.(${ROLLUP_TYPES})(?:\\.(${UNITS}))?$`);
module.exports = class PerformanceManager extends ManagedEntity {
queryPerf(querySpec) {
return this.call('QueryPerfAsync', { querySpec }, PerfEntityMetric.ArrayOf);
}
queryPerfProviderSummary(entity) {
return this.call('QueryPerfProviderSummaryAsync', { entity }, PerfProviderSummary);
}
preparePerfCounters(counters) {
function findCounter({ groupInfo, nameInfo, rollupType, unitInfo }, counter) {
const matches = COUNTER_NAME_REGEX.exec(counter); /* eslint-disable one-var */
if (!matches) {
return false;
}
const [, group, name, rollup, unit] = matches;
return groupInfo.key === group && nameInfo.key === name && rollupType === rollup && (!unit || unitInfo.key === unit);
}
return this.withProperties()
.then(({ perfCounter, historicalInterval }) => _.reduce(counters, (accumulator, instance, counter) => {
const found = _.find(perfCounter, item => findCounter(item, counter));
if (found) {
accumulator.counterToId[counter] = {
counterId: found.key,
instance
};
accumulator.idToCounter[found.key] = counter;
}
return accumulator;
}, { counterToId: {}, idToCounter: {}, historicalInterval }));
}
static mappings() {
return {
description: PerformanceDescription,
historicalInterval: PerfInterval.ArrayOf,
perfCounter: PerfCounterInfo.ArrayOf
};
}
};