datasource: implement itemid query mode

This commit is contained in:
Alexander Zobnin
2017-07-25 18:40:13 +03:00
parent a48da2cda6
commit fc1748a087
24 changed files with 373 additions and 214 deletions

View File

@@ -140,6 +140,11 @@ var ZabbixAPIDatasource = function () {
// Create request for each target
var promises = _lodash2.default.map(options.targets, function (t) {
// Don't request undefined and hidden targets
if (t.hide) {
return [];
}
var timeFrom = Math.ceil(dateMath.parse(options.range.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.range.to) / 1000);
@@ -163,7 +168,7 @@ var ZabbixAPIDatasource = function () {
var useTrends = _this.isUseTrends(timeRange);
// Metrics or Text query mode
if (target.mode !== c.MODE_ITSERVICE) {
if (target.mode === c.MODE_METRICS || target.mode === c.MODE_TEXT || target.mode === c.MODE_ITEMID) {
// Migrate old targets
target = migrations.migrate(target);
@@ -176,13 +181,13 @@ var ZabbixAPIDatasource = function () {
return _this.queryNumericData(target, timeRange, useTrends, options);
} else if (target.mode === c.MODE_TEXT) {
return _this.queryTextData(target, timeRange);
} else if (target.mode === c.MODE_ITEMID) {
return _this.queryItemIdData(target, timeRange, useTrends, options);
}
} else if (target.mode === c.MODE_ITSERVICE) {
// IT services mode
return _this.queryITServiceData(target, timeRange, options);
}
// IT services mode
else if (target.mode === c.MODE_ITSERVICE) {
return _this.queryITServiceData(target, timeRange, options);
}
});
// Data for panel (all targets)
@@ -191,55 +196,50 @@ var ZabbixAPIDatasource = function () {
});
}
}, {
key: 'queryNumericData',
value: function queryNumericData(target, timeRange, useTrends, options) {
key: 'queryNumericDataForItems',
value: function queryNumericDataForItems(items, target, timeRange, useTrends, options) {
var _this2 = this;
var _timeRange = _slicedToArray(timeRange, 2),
timeFrom = _timeRange[0],
timeTo = _timeRange[1];
var getItemOptions = {
itemtype: 'num'
};
return this.zabbix.getItemsFromTarget(target, getItemOptions).then(function (items) {
var getHistoryPromise = void 0;
options.consolidateBy = getConsolidateBy(target);
var getHistoryPromise = void 0;
options.consolidateBy = getConsolidateBy(target);
if (useTrends) {
if (_this2.enableDirectDBConnection) {
getHistoryPromise = _this2.zabbix.getTrendsDB(items, timeFrom, timeTo, options).then(function (history) {
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
});
} else {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
}).then(function (timeseries) {
// Sort trend data, issue #202
_lodash2.default.forEach(timeseries, function (series) {
series.datapoints = _lodash2.default.sortBy(series.datapoints, function (point) {
return point[c.DATAPOINT_TS];
});
});
return timeseries;
});
}
if (useTrends) {
if (this.enableDirectDBConnection) {
getHistoryPromise = this.zabbix.getTrendsDB(items, timeFrom, timeTo, options).then(function (history) {
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
});
} else {
// Use history
if (_this2.enableDirectDBConnection) {
getHistoryPromise = _this2.zabbix.getHistoryDB(items, timeFrom, timeTo, options).then(function (history) {
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
var valueType = this.getTrendValueType(target);
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
}).then(function (timeseries) {
// Sort trend data, issue #202
_lodash2.default.forEach(timeseries, function (series) {
series.datapoints = _lodash2.default.sortBy(series.datapoints, function (point) {
return point[c.DATAPOINT_TS];
});
});
} else {
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleHistory(history, items);
});
}
return timeseries;
});
}
} else {
// Use history
if (this.enableDirectDBConnection) {
getHistoryPromise = this.zabbix.getHistoryDB(items, timeFrom, timeTo, options).then(function (history) {
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
});
} else {
getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleHistory(history, items);
});
}
}
return getHistoryPromise;
}).then(function (timeseries) {
return getHistoryPromise.then(function (timeseries) {
return _this2.applyDataProcessingFunctions(timeseries, target);
}).then(function (timeseries) {
return downsampleSeries(timeseries, options);
@@ -248,6 +248,18 @@ var ZabbixAPIDatasource = function () {
return [];
});
}
}, {
key: 'queryNumericData',
value: function queryNumericData(target, timeRange, useTrends, options) {
var _this3 = this;
var getItemOptions = {
itemtype: 'num'
};
return this.zabbix.getItemsFromTarget(target, getItemOptions).then(function (items) {
return _this3.queryNumericDataForItems(items, target, timeRange, useTrends, options);
});
}
}, {
key: 'getTrendValueType',
value: function getTrendValueType(target) {
@@ -319,7 +331,7 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'queryTextData',
value: function queryTextData(target, timeRange) {
var _this3 = this;
var _this4 = this;
var _timeRange2 = _slicedToArray(timeRange, 2),
timeFrom = _timeRange2[0],
@@ -330,7 +342,7 @@ var ZabbixAPIDatasource = function () {
};
return this.zabbix.getItemsFromTarget(target, options).then(function (items) {
if (items.length) {
return _this3.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _this4.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleText(history, items, target);
});
} else {
@@ -338,10 +350,28 @@ var ZabbixAPIDatasource = function () {
}
});
}
}, {
key: 'queryItemIdData',
value: function queryItemIdData(target, timeRange, useTrends, options) {
var _this5 = this;
var itemids = target.itemids;
itemids = _lodash2.default.map(itemids.split(','), function (itemid) {
return itemid.trim();
});
if (!itemids) {
return [];
}
return this.zabbix.getItemsByIDs(itemids).then(function (items) {
return _this5.queryNumericDataForItems(items, target, timeRange, useTrends, options);
});
}
}, {
key: 'queryITServiceData',
value: function queryITServiceData(target, timeRange, options) {
var _this4 = this;
var _this6 = this;
// Don't show undefined and hidden targets
if (target.hide || !target.itservice && !target.itServiceFilter || !target.slaProperty) {
@@ -369,7 +399,7 @@ var ZabbixAPIDatasource = function () {
itServiceIds = _lodash2.default.map(itServices, 'serviceid');
return itServiceIds;
}).then(function (serviceids) {
return _this4.zabbix.getSLA(serviceids, timeRange);
return _this6.zabbix.getSLA(serviceids, timeRange);
}).then(function (slaResponse) {
return _lodash2.default.map(itServiceIds, function (serviceid) {
var itservice = _lodash2.default.find(itServices, { 'serviceid': serviceid });
@@ -386,15 +416,15 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'testDatasource',
value: function testDatasource() {
var _this5 = this;
var _this7 = this;
var zabbixVersion = void 0;
return this.zabbix.getVersion().then(function (version) {
zabbixVersion = version;
return _this5.zabbix.login();
return _this7.zabbix.login();
}).then(function () {
if (_this5.enableDirectDBConnection) {
return _this5.zabbix.dbConnector.testSQLDataSource();
if (_this7.enableDirectDBConnection) {
return _this7.zabbix.dbConnector.testSQLDataSource();
} else {
return Promise.resolve();
}
@@ -442,14 +472,14 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'metricFindQuery',
value: function metricFindQuery(query) {
var _this6 = this;
var _this8 = this;
var result = void 0;
var parts = [];
// Split query. Query structure: group.host.app.item
_lodash2.default.each(utils.splitTemplateQuery(query), function (part) {
part = _this6.replaceTemplateVars(part, {});
part = _this8.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
@@ -491,7 +521,7 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'annotationQuery',
value: function annotationQuery(options) {
var _this7 = this;
var _this9 = this;
var timeFrom = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
@@ -509,7 +539,7 @@ var ZabbixAPIDatasource = function () {
return getTriggers.then(function (triggers) {
// Filter triggers by description
var triggerName = _this7.replaceTemplateVars(annotation.trigger, {});
var triggerName = _this9.replaceTemplateVars(annotation.trigger, {});
if (utils.isRegex(triggerName)) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return utils.buildRegex(triggerName).test(trigger.description);
@@ -526,7 +556,7 @@ var ZabbixAPIDatasource = function () {
});
var objectids = _lodash2.default.map(triggers, 'triggerid');
return _this7.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
return _this9.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
var indexedTriggers = _lodash2.default.keyBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
@@ -567,23 +597,23 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'alertQuery',
value: function alertQuery(options) {
var _this8 = this;
var _this10 = this;
var enabled_targets = filterEnabledTargets(options.targets);
var getPanelItems = _lodash2.default.map(enabled_targets, function (t) {
var target = _lodash2.default.cloneDeep(t);
_this8.replaceTargetVariables(target, options);
return _this8.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
_this10.replaceTargetVariables(target, options);
return _this10.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
});
return Promise.all(getPanelItems).then(function (results) {
var items = _lodash2.default.flatten(results);
var itemids = _lodash2.default.map(items, 'itemid');
return _this8.zabbix.getAlerts(itemids);
return _this10.zabbix.getAlerts(itemids);
}).then(function (triggers) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return trigger.priority >= _this8.alertingMinSeverity;
return trigger.priority >= _this10.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
@@ -614,12 +644,12 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'replaceTargetVariables',
value: function replaceTargetVariables(target, options) {
var _this9 = this;
var _this11 = this;
var parts = ['group', 'host', 'application', 'item'];
_lodash2.default.forEach(parts, function (p) {
if (target[p] && target[p].filter) {
target[p].filter = _this9.replaceTemplateVars(target[p].filter, options.scopedVars);
target[p].filter = _this11.replaceTemplateVars(target[p].filter, options.scopedVars);
}
});
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
@@ -627,9 +657,9 @@ var ZabbixAPIDatasource = function () {
_lodash2.default.forEach(target.functions, function (func) {
func.params = _lodash2.default.map(func.params, function (param) {
if (typeof param === 'number') {
return +_this9.templateSrv.replace(param.toString(), options.scopedVars);
return +_this11.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return _this9.templateSrv.replace(param, options.scopedVars);
return _this11.templateSrv.replace(param, options.scopedVars);
}
});
});

View File

@@ -70,6 +70,7 @@ var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl
_this.getApplicationNames = _lodash2.default.bind(_this.getMetricNames, _this, 'appList');
_this.getItemNames = _lodash2.default.bind(_this.getMetricNames, _this, 'itemList');
_this.getITServices = _lodash2.default.bind(_this.getMetricNames, _this, 'itServiceList');
_this.getVariables = _lodash2.default.bind(_this.getTemplateVariables, _this);
// Update metric suggestion when template variable was changed
$rootScope.$on('template-variable-value-updated', function () {
@@ -153,8 +154,8 @@ var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl
return metrics;
}
}, {
key: 'getVariables',
value: function getVariables() {
key: 'getTemplateVariables',
value: function getTemplateVariables() {
return _lodash2.default.map(this.templateSrv.variables, function (variable) {
return '$' + variable.name;
});

View File

@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
});
exports.regexPattern = undefined;
exports.expandItemName = expandItemName;
exports.expandItems = expandItems;
exports.containsMacro = containsMacro;
exports.replaceMacro = replaceMacro;
exports.splitTemplateQuery = splitTemplateQuery;
@@ -49,6 +50,15 @@ function expandItemName(name, key) {
return name;
}
function expandItems(items) {
_lodash2.default.forEach(items, function (item) {
item.item = item.name;
item.name = expandItemName(item.item, item.key_);
return item;
});
return items;
}
function splitKeyParams(paramStr) {
var params = [];
var quoted = false;

View File

@@ -64,6 +64,7 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
// Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getMacros = this.cachingProxy.getMacros.bind(this.cachingProxy);
this.getItemsByIDs = this.cachingProxy.getItemsByIDs.bind(this.cachingProxy);
if (enableDirectDBConnection) {
this.getHistoryDB = this.cachingProxy.getHistoryDB.bind(this.cachingProxy);

View File

@@ -217,16 +217,19 @@ function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
params.filter.value_type = [1, 2, 4];
}
return this.request('item.get', params).then(expandItems);
return this.request('item.get', params).then(utils.expandItems);
}
}, {
key: 'getItemsByIDs',
value: function getItemsByIDs(itemids) {
var params = {
itemids: itemids,
output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'],
webitems: true,
selectHosts: ['hostid', 'name']
};
function expandItems(items) {
_lodash2.default.forEach(items, function (item) {
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
return items;
}
return this.request('item.get', params).then(utils.expandItems);
}
}, {
key: 'getMacros',

View File

@@ -66,6 +66,9 @@ function ZabbixCachingProxyFactory() {
this.itemPromises = {};
this.getItemsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getItems, this.zabbixAPI), this.itemPromises, getRequestHash);
this.itemByIdPromises = {};
this.getItemsByIdOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getItemsByIDs, this.zabbixAPI), this.itemPromises, getRequestHash);
this.itServicesPromises = {};
this.getITServicesOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getITService, this.zabbixAPI), this.itServicesPromises, getRequestHash);
@@ -129,6 +132,12 @@ function ZabbixCachingProxyFactory() {
var params = [hostids, appids, itemtype];
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
}
}, {
key: 'getItemsByIDs',
value: function getItemsByIDs(itemids) {
var params = [itemids];
return this.proxyRequest(this.getItemsByIdOnce, params, this.cache.items);
}
}, {
key: 'getITServices',
value: function getITServices() {