Merge branch 'datasource-refactor' into develop
This commit is contained in:
@@ -61,17 +61,18 @@ export class ZabbixAPIDatasource {
|
|||||||
* @return {Object} Grafana metrics object with timeseries data for each target.
|
* @return {Object} Grafana metrics object with timeseries data for each target.
|
||||||
*/
|
*/
|
||||||
query(options) {
|
query(options) {
|
||||||
var self = this;
|
|
||||||
|
|
||||||
// get from & to in seconds
|
|
||||||
var timeFrom = Math.ceil(dateMath.parse(options.range.from) / 1000);
|
var timeFrom = Math.ceil(dateMath.parse(options.range.from) / 1000);
|
||||||
var timeTo = Math.ceil(dateMath.parse(options.range.to) / 1000);
|
var timeTo = Math.ceil(dateMath.parse(options.range.to) / 1000);
|
||||||
|
|
||||||
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
|
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
|
||||||
var useTrends = (timeFrom < useTrendsFrom) && this.trends;
|
var useTrends = (timeFrom <= useTrendsFrom) && this.trends;
|
||||||
|
|
||||||
// Create request for each target
|
// Create request for each target
|
||||||
var promises = _.map(options.targets, target => {
|
var promises = _.map(options.targets, target => {
|
||||||
|
|
||||||
|
// Prevent changes of original object
|
||||||
|
target = _.cloneDeep(target);
|
||||||
|
|
||||||
if (target.mode !== 1) {
|
if (target.mode !== 1) {
|
||||||
|
|
||||||
// Migrate old targets
|
// Migrate old targets
|
||||||
@@ -83,21 +84,20 @@ export class ZabbixAPIDatasource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Replace templated variables
|
// Replace templated variables
|
||||||
var groupFilter = this.replaceTemplateVars(target.group.filter, options.scopedVars);
|
target.group.filter = this.replaceTemplateVars(target.group.filter, options.scopedVars);
|
||||||
var hostFilter = this.replaceTemplateVars(target.host.filter, options.scopedVars);
|
target.host.filter = this.replaceTemplateVars(target.host.filter, options.scopedVars);
|
||||||
var appFilter = this.replaceTemplateVars(target.application.filter, options.scopedVars);
|
target.application.filter = this.replaceTemplateVars(target.application.filter, options.scopedVars);
|
||||||
var itemFilter = this.replaceTemplateVars(target.item.filter, options.scopedVars);
|
target.item.filter = this.replaceTemplateVars(target.item.filter, options.scopedVars);
|
||||||
|
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
|
||||||
|
|
||||||
// Query numeric data
|
// Query numeric data
|
||||||
if (!target.mode || target.mode === 0) {
|
if (!target.mode || target.mode === 0) {
|
||||||
return self.queryNumericData(target, groupFilter, hostFilter, appFilter, itemFilter,
|
return this.queryNumericData(target, timeFrom, timeTo, useTrends);
|
||||||
timeFrom, timeTo, useTrends, options, self);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query text data
|
// Query text data
|
||||||
else if (target.mode === 2) {
|
else if (target.mode === 2) {
|
||||||
return self.queryTextData(target, groupFilter, hostFilter, appFilter, itemFilter,
|
return this.queryTextData(target, timeFrom, timeTo);
|
||||||
timeFrom, timeTo, options, self);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,11 +111,11 @@ export class ZabbixAPIDatasource {
|
|||||||
return this.zabbixAPI
|
return this.zabbixAPI
|
||||||
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
|
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
|
||||||
.then(slaObject => {
|
.then(slaObject => {
|
||||||
return self.queryProcessor
|
return this.queryProcessor
|
||||||
.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
|
.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, this);
|
});
|
||||||
|
|
||||||
// Data for panel (all targets)
|
// Data for panel (all targets)
|
||||||
return this.q.all(_.flatten(promises))
|
return this.q.all(_.flatten(promises))
|
||||||
@@ -134,10 +134,13 @@ export class ZabbixAPIDatasource {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
queryNumericData(target, groupFilter, hostFilter, appFilter, itemFilter, timeFrom, timeTo, useTrends, options, self) {
|
queryNumericData(target, timeFrom, timeTo, useTrends) {
|
||||||
// Build query in asynchronous manner
|
// Build query in asynchronous manner
|
||||||
return self.queryProcessor
|
return this.queryProcessor.build(target.group.filter,
|
||||||
.build(groupFilter, hostFilter, appFilter, itemFilter, 'num')
|
target.host.filter,
|
||||||
|
target.application.filter,
|
||||||
|
target.item.filter,
|
||||||
|
'num')
|
||||||
.then(items => {
|
.then(items => {
|
||||||
// Add hostname for items from multiple hosts
|
// Add hostname for items from multiple hosts
|
||||||
var addHostName = utils.isRegex(target.host.filter);
|
var addHostName = utils.isRegex(target.host.filter);
|
||||||
@@ -153,51 +156,43 @@ export class ZabbixAPIDatasource {
|
|||||||
});
|
});
|
||||||
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
|
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
|
||||||
|
|
||||||
getHistory = self.zabbixAPI
|
getHistory = this.zabbixAPI
|
||||||
.getTrend(items, timeFrom, timeTo)
|
.getTrend(items, timeFrom, timeTo)
|
||||||
.then(history => {
|
.then(history => {
|
||||||
return self.queryProcessor.handleTrends(history, items, addHostName, valueType);
|
return this.queryProcessor.handleTrends(history, items, addHostName, valueType);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use history
|
// Use history
|
||||||
else {
|
else {
|
||||||
getHistory = self.zabbixCache
|
getHistory = this.zabbixCache
|
||||||
.getHistory(items, timeFrom, timeTo)
|
.getHistory(items, timeFrom, timeTo)
|
||||||
.then(history => {
|
.then(history => {
|
||||||
return self.queryProcessor.handleHistory(history, items, addHostName);
|
return this.queryProcessor.handleHistory(history, items, addHostName);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return getHistory.then(timeseries_data => {
|
return getHistory.then(timeseries_data => {
|
||||||
|
let transformFunctions = bindFunctionDefs(target.functions, 'Transform');
|
||||||
|
let aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate');
|
||||||
|
let aliasFunctions = bindFunctionDefs(target.functions, 'Alias');
|
||||||
|
|
||||||
// Apply transformation functions
|
// Apply transformation functions
|
||||||
timeseries_data = _.map(timeseries_data, timeseries => {
|
timeseries_data = _.map(timeseries_data, timeseries => {
|
||||||
|
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
|
||||||
// Filter only transformation functions
|
|
||||||
var transformFunctions = bindFunctionDefs(target.functions, 'Transform', DataProcessor);
|
|
||||||
|
|
||||||
// Timeseries processing
|
|
||||||
var dp = timeseries.datapoints;
|
|
||||||
for (var i = 0; i < transformFunctions.length; i++) {
|
|
||||||
dp = transformFunctions[i](dp);
|
|
||||||
}
|
|
||||||
timeseries.datapoints = dp;
|
|
||||||
|
|
||||||
return timeseries;
|
return timeseries;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Apply aggregations
|
// Apply aggregations
|
||||||
var aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate', DataProcessor);
|
|
||||||
var dp = _.map(timeseries_data, 'datapoints');
|
|
||||||
if (aggregationFunctions.length) {
|
if (aggregationFunctions.length) {
|
||||||
for (var i = 0; i < aggregationFunctions.length; i++) {
|
let dp = _.map(timeseries_data, 'datapoints');
|
||||||
dp = aggregationFunctions[i](dp);
|
dp = sequence(aggregationFunctions)(dp);
|
||||||
}
|
|
||||||
var lastAgg = _.findLast(target.functions, func => {
|
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
|
||||||
return _.contains(
|
let lastAgg = _.findLast(target.functions, func => {
|
||||||
_.map(metricFunctions.getCategories()['Aggregate'], 'name'), func.def.name);
|
return _.contains(aggFuncNames, func.def.name);
|
||||||
});
|
});
|
||||||
|
|
||||||
timeseries_data = [
|
timeseries_data = [
|
||||||
{
|
{
|
||||||
target: lastAgg.text,
|
target: lastAgg.text,
|
||||||
@@ -207,47 +202,36 @@ export class ZabbixAPIDatasource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply alias functions
|
// Apply alias functions
|
||||||
var aliasFunctions = bindFunctionDefs(target.functions, 'Alias', DataProcessor);
|
_.each(timeseries_data, sequence(aliasFunctions));
|
||||||
for (var j = 0; j < aliasFunctions.length; j++) {
|
|
||||||
_.each(timeseries_data, aliasFunctions[j]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return timeseries_data;
|
return timeseries_data;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
queryTextData(target, groupFilter, hostFilter, appFilter, itemFilter, timeFrom, timeTo, options, self) {
|
queryTextData(target, timeFrom, timeTo) {
|
||||||
return self.queryProcessor
|
return this.queryProcessor.build(target.group.filter,
|
||||||
.build(groupFilter, hostFilter, appFilter, itemFilter, 'text')
|
target.host.filter,
|
||||||
|
target.application.filter,
|
||||||
|
target.item.filter,
|
||||||
|
'text')
|
||||||
.then(items => {
|
.then(items => {
|
||||||
if (items.length) {
|
if (items.length) {
|
||||||
return self.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
||||||
.then(history => {
|
.then(history => {
|
||||||
return self.queryProcessor.convertHistory(history, items, false, (point) => {
|
return this.queryProcessor.convertHistory(history, items, false, (point) => {
|
||||||
let extractedValue = point.value;
|
let value = point.value;
|
||||||
|
|
||||||
// Regex-based extractor
|
// Regex-based extractor
|
||||||
if (target.textFilter) {
|
if (target.textFilter) {
|
||||||
let text_extract_pattern = new RegExp(self.replaceTemplateVars(target.textFilter, options.scopedVars));
|
value = extractText(point.value, target.textFilter, target.useCaptureGroups);
|
||||||
extractedValue = text_extract_pattern.exec(point.value);
|
|
||||||
if (extractedValue) {
|
|
||||||
if (target.useCaptureGroups) {
|
|
||||||
extractedValue = extractedValue[1];
|
|
||||||
} else {
|
|
||||||
extractedValue = extractedValue[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [value, point.clock * 1000];
|
||||||
extractedValue,
|
|
||||||
point.clock * 1000
|
|
||||||
];
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return self.q.when([]);
|
return this.q.when([]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -304,12 +288,12 @@ export class ZabbixAPIDatasource {
|
|||||||
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
||||||
*/
|
*/
|
||||||
metricFindQuery(query) {
|
metricFindQuery(query) {
|
||||||
// Split query. Query structure:
|
let result;
|
||||||
// group.host.app.item
|
let parts = [];
|
||||||
var self = this;
|
|
||||||
var parts = [];
|
// Split query. Query structure: group.host.app.item
|
||||||
_.each(query.split('.'), function (part) {
|
_.each(query.split('.'), part => {
|
||||||
part = self.replaceTemplateVars(part, {});
|
part = this.replaceTemplateVars(part, {});
|
||||||
|
|
||||||
// Replace wildcard to regex
|
// Replace wildcard to regex
|
||||||
if (part === '*') {
|
if (part === '*') {
|
||||||
@@ -317,7 +301,7 @@ export class ZabbixAPIDatasource {
|
|||||||
}
|
}
|
||||||
parts.push(part);
|
parts.push(part);
|
||||||
});
|
});
|
||||||
var template = _.object(['group', 'host', 'app', 'item'], parts);
|
let template = _.object(['group', 'host', 'app', 'item'], parts);
|
||||||
|
|
||||||
// Get items
|
// Get items
|
||||||
if (parts.length === 4) {
|
if (parts.length === 4) {
|
||||||
@@ -325,41 +309,24 @@ export class ZabbixAPIDatasource {
|
|||||||
if (template.app === '/.*/') {
|
if (template.app === '/.*/') {
|
||||||
template.app = '';
|
template.app = '';
|
||||||
}
|
}
|
||||||
return this.queryProcessor
|
result = this.queryProcessor.getItems(template.group, template.host, template.app);
|
||||||
.getItems(template.group, template.host, template.app)
|
} else if (parts.length === 3) {
|
||||||
.then(items => {
|
|
||||||
return _.map(items, formatMetric);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Get applications
|
// Get applications
|
||||||
else if (parts.length === 3) {
|
result = this.queryProcessor.getApps(template.group, template.host);
|
||||||
return this.queryProcessor
|
} else if (parts.length === 2) {
|
||||||
.getApps(template.group, template.host)
|
|
||||||
.then(apps => {
|
|
||||||
return _.map(apps, formatMetric);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Get hosts
|
// Get hosts
|
||||||
else if (parts.length === 2) {
|
result = this.queryProcessor.getHosts(template.group);
|
||||||
return this.queryProcessor
|
} else if (parts.length === 1) {
|
||||||
.getHosts(template.group)
|
|
||||||
.then(hosts => {
|
|
||||||
return _.map(hosts, formatMetric);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Get groups
|
// Get groups
|
||||||
else if (parts.length === 1) {
|
result = this.zabbixCache.getGroups(template.group);
|
||||||
return this.zabbixCache
|
} else {
|
||||||
.getGroups(template.group)
|
result = this.q.when([]);
|
||||||
.then(groups => {
|
}
|
||||||
return _.map(groups, formatMetric);
|
|
||||||
|
return result.then(metrics => {
|
||||||
|
return _.map(metrics, formatMetric);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Return empty object for invalid request
|
|
||||||
else {
|
|
||||||
return this.q.when([]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////
|
/////////////////
|
||||||
// Annotations //
|
// Annotations //
|
||||||
@@ -438,8 +405,7 @@ export class ZabbixAPIDatasource {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function bindFunctionDefs(functionDefs, category, DataProcessor) {
|
function bindFunctionDefs(functionDefs, category) {
|
||||||
'use strict';
|
|
||||||
var aggregationFunctions = _.map(metricFunctions.getCategories()[category], 'name');
|
var aggregationFunctions = _.map(metricFunctions.getCategories()[category], 'name');
|
||||||
var aggFuncDefs = _.filter(functionDefs, function(func) {
|
var aggFuncDefs = _.filter(functionDefs, function(func) {
|
||||||
return _.contains(aggregationFunctions, func.def.name);
|
return _.contains(aggregationFunctions, func.def.name);
|
||||||
@@ -452,7 +418,6 @@ function bindFunctionDefs(functionDefs, category, DataProcessor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function formatMetric(metricObj) {
|
function formatMetric(metricObj) {
|
||||||
'use strict';
|
|
||||||
return {
|
return {
|
||||||
text: metricObj.name,
|
text: metricObj.name,
|
||||||
expandable: false
|
expandable: false
|
||||||
@@ -492,3 +457,27 @@ function replaceTemplateVars(templateSrv, target, scopedVars) {
|
|||||||
}
|
}
|
||||||
return replacedTarget;
|
return replacedTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractText(str, pattern, useCaptureGroups) {
|
||||||
|
let extractPattern = new RegExp(pattern);
|
||||||
|
let extractedValue = extractPattern.exec(str);
|
||||||
|
if (extractedValue) {
|
||||||
|
if (useCaptureGroups) {
|
||||||
|
extractedValue = extractedValue[1];
|
||||||
|
} else {
|
||||||
|
extractedValue = extractedValue[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return extractedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply function one by one:
|
||||||
|
// sequence([a(), b(), c()]) = c(b(a()));
|
||||||
|
function sequence(funcsArray) {
|
||||||
|
return function(result) {
|
||||||
|
for (var i = 0; i < funcsArray.length; i++) {
|
||||||
|
result = funcsArray[i].call(this, result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user