Merge branch 'develop'

This commit is contained in:
Alexander Zobnin
2016-09-24 10:07:29 +03:00
11 changed files with 377 additions and 169 deletions

View File

@@ -49,8 +49,6 @@ export class ZabbixAPIDatasource {
// Use custom format for template variables
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
console.log(this.zabbixCache);
}
////////////////////////
@@ -63,17 +61,18 @@ export class ZabbixAPIDatasource {
* @return {Object} Grafana metrics object with timeseries data for each target.
*/
query(options) {
var self = this;
// get from & to in seconds
var timeFrom = Math.ceil(dateMath.parse(options.range.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.range.to) / 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
var promises = _.map(options.targets, target => {
// Prevent changes of original object
target = _.cloneDeep(target);
if (target.mode !== 1) {
// Migrate old targets
@@ -85,21 +84,26 @@ export class ZabbixAPIDatasource {
}
// Replace templated variables
var groupFilter = this.replaceTemplateVars(target.group.filter, options.scopedVars);
var hostFilter = this.replaceTemplateVars(target.host.filter, options.scopedVars);
var appFilter = this.replaceTemplateVars(target.application.filter, options.scopedVars);
var itemFilter = this.replaceTemplateVars(target.item.filter, options.scopedVars);
target.group.filter = this.replaceTemplateVars(target.group.filter, options.scopedVars);
target.host.filter = this.replaceTemplateVars(target.host.filter, options.scopedVars);
target.application.filter = this.replaceTemplateVars(target.application.filter, options.scopedVars);
target.item.filter = this.replaceTemplateVars(target.item.filter, options.scopedVars);
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
_.forEach(target.functions, func => {
func.params = _.map(func.params, param => {
return this.templateSrv.replace(param, options.scopedVars);
});
});
// Query numeric data
if (!target.mode || target.mode === 0) {
return self.queryNumericData(target, groupFilter, hostFilter, appFilter, itemFilter,
timeFrom, timeTo, useTrends, options, self);
return this.queryNumericData(target, timeFrom, timeTo, useTrends);
}
// Query text data
else if (target.mode === 2) {
return self.queryTextData(target, groupFilter, hostFilter, appFilter, itemFilter,
timeFrom, timeTo, options, self);
return this.queryTextData(target, timeFrom, timeTo);
}
}
@@ -113,11 +117,11 @@ export class ZabbixAPIDatasource {
return this.zabbixAPI
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
.then(slaObject => {
return self.queryProcessor
return this.queryProcessor
.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
}
}, this);
});
// Data for panel (all targets)
return this.q.all(_.flatten(promises))
@@ -136,10 +140,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
return self.queryProcessor
.build(groupFilter, hostFilter, appFilter, itemFilter, 'num')
return this.queryProcessor.build(target.group.filter,
target.host.filter,
target.application.filter,
target.item.filter,
'num')
.then(items => {
// Add hostname for items from multiple hosts
var addHostName = utils.isRegex(target.host.filter);
@@ -151,55 +158,47 @@ export class ZabbixAPIDatasource {
// Find trendValue() function and get specified trend value
var trendFunctions = _.map(metricFunctions.getCategories()['Trends'], 'name');
var trendValueFunc = _.find(target.functions, func => {
return _.contains(trendFunctions, func.def.name);
return _.includes(trendFunctions, func.def.name);
});
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
getHistory = self.zabbixAPI
getHistory = this.zabbixAPI
.getTrend(items, timeFrom, timeTo)
.then(history => {
return self.queryProcessor.handleTrends(history, items, addHostName, valueType);
return this.queryProcessor.handleTrends(history, items, addHostName, valueType);
});
}
// Use history
else {
getHistory = self.zabbixCache
getHistory = this.zabbixCache
.getHistory(items, timeFrom, timeTo)
.then(history => {
return self.queryProcessor.handleHistory(history, items, addHostName);
return this.queryProcessor.handleHistory(history, items, addHostName);
});
}
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
timeseries_data = _.map(timeseries_data, timeseries => {
// 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;
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
return timeseries;
});
// Apply aggregations
var aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate', DataProcessor);
var dp = _.map(timeseries_data, 'datapoints');
if (aggregationFunctions.length) {
for (var i = 0; i < aggregationFunctions.length; i++) {
dp = aggregationFunctions[i](dp);
}
var lastAgg = _.findLast(target.functions, func => {
return _.contains(
_.map(metricFunctions.getCategories()['Aggregate'], 'name'), func.def.name);
let dp = _.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
let lastAgg = _.findLast(target.functions, func => {
return _.includes(aggFuncNames, func.def.name);
});
timeseries_data = [
{
target: lastAgg.text,
@@ -209,49 +208,36 @@ export class ZabbixAPIDatasource {
}
// Apply alias functions
var aliasFunctions = bindFunctionDefs(target.functions, 'Alias', DataProcessor);
for (var j = 0; j < aliasFunctions.length; j++) {
_.each(timeseries_data, aliasFunctions[j]);
}
_.each(timeseries_data, sequence(aliasFunctions));
return timeseries_data;
});
});
}
queryTextData(target, groupFilter, hostFilter, appFilter, itemFilter, timeFrom, timeTo, options, self) {
return self.queryProcessor
.build(groupFilter, hostFilter, appFilter, itemFilter, 'text')
queryTextData(target, timeFrom, timeTo) {
return this.queryProcessor.build(target.group.filter,
target.host.filter,
target.application.filter,
target.item.filter,
'text')
.then(items => {
if (items.length) {
var textItemsPromises = _.map(items, item => {
return self.zabbixAPI.getLastValue(item.itemid);
});
return self.q.all(textItemsPromises)
.then(result => {
return _.map(result, (lastvalue, index) => {
var extractedValue;
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
.then(history => {
return this.queryProcessor.convertHistory(history, items, false, (point) => {
let value = point.value;
// Regex-based extractor
if (target.textFilter) {
var text_extract_pattern = new RegExp(self.replaceTemplateVars(target.textFilter, options.scopedVars));
extractedValue = text_extract_pattern.exec(lastvalue);
if (extractedValue) {
if (target.useCaptureGroups) {
extractedValue = extractedValue[1];
} else {
extractedValue = extractedValue[0];
}
}
} else {
extractedValue = lastvalue;
value = extractText(point.value, target.textFilter, target.useCaptureGroups);
}
return {
target: items[index].name,
datapoints: [[extractedValue, timeTo * 1000]]
};
return [value, point.clock * 1000];
});
});
} else {
return self.q.when([]);
return this.q.when([]);
}
});
}
@@ -308,12 +294,12 @@ export class ZabbixAPIDatasource {
* of metrics in "{metric1,metcic2,...,metricN}" format.
*/
metricFindQuery(query) {
// Split query. Query structure:
// group.host.app.item
var self = this;
var parts = [];
_.each(query.split('.'), function (part) {
part = self.replaceTemplateVars(part, {});
let result;
let parts = [];
// Split query. Query structure: group.host.app.item
_.each(query.split('.'), part => {
part = this.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
@@ -321,7 +307,7 @@ export class ZabbixAPIDatasource {
}
parts.push(part);
});
var template = _.object(['group', 'host', 'app', 'item'], parts);
let template = _.zipObject(['group', 'host', 'app', 'item'], parts);
// Get items
if (parts.length === 4) {
@@ -329,40 +315,23 @@ export class ZabbixAPIDatasource {
if (template.app === '/.*/') {
template.app = '';
}
return this.queryProcessor
.getItems(template.group, template.host, template.app)
.then(items => {
return _.map(items, formatMetric);
});
}
// Get applications
else if (parts.length === 3) {
return this.queryProcessor
.getApps(template.group, template.host)
.then(apps => {
return _.map(apps, formatMetric);
});
}
// Get hosts
else if (parts.length === 2) {
return this.queryProcessor
.getHosts(template.group)
.then(hosts => {
return _.map(hosts, formatMetric);
});
}
// Get groups
else if (parts.length === 1) {
return this.zabbixCache
.getGroups(template.group)
.then(groups => {
return _.map(groups, formatMetric);
});
}
// Return empty object for invalid request
else {
return this.q.when([]);
result = this.queryProcessor.getItems(template.group, template.host, template.app);
} else if (parts.length === 3) {
// Get applications
result = this.queryProcessor.getApps(template.group, template.host);
} else if (parts.length === 2) {
// Get hosts
result = this.queryProcessor.getHosts(template.group);
} else if (parts.length === 1) {
// Get groups
result = this.zabbixCache.getGroups(template.group);
} else {
result = this.q.when([]);
}
return result.then(metrics => {
return _.map(metrics, formatMetric);
});
}
/////////////////
@@ -409,7 +378,7 @@ export class ZabbixAPIDatasource {
return self.zabbixAPI
.getEvents(objectids, timeFrom, timeTo, showOkEvents)
.then(events => {
var indexedTriggers = _.indexBy(triggers, 'triggerid');
var indexedTriggers = _.groupBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
if (annotation.hideAcknowledged) {
@@ -442,11 +411,10 @@ export class ZabbixAPIDatasource {
}
function bindFunctionDefs(functionDefs, category, DataProcessor) {
'use strict';
function bindFunctionDefs(functionDefs, category) {
var aggregationFunctions = _.map(metricFunctions.getCategories()[category], 'name');
var aggFuncDefs = _.filter(functionDefs, function(func) {
return _.contains(aggregationFunctions, func.def.name);
return _.includes(aggregationFunctions, func.def.name);
});
return _.map(aggFuncDefs, function(func) {
@@ -455,8 +423,14 @@ function bindFunctionDefs(functionDefs, category, DataProcessor) {
});
}
function filterFunctionDefs(funcs, category) {
let filteredFuncs = _.map(metricFunctions.getCategories()[category]);
return _.filter(funcs, func => {
return _.includes(filteredFuncs, func.def.name);
});
}
function formatMetric(metricObj) {
'use strict';
return {
text: metricObj.name,
expandable: false
@@ -496,3 +470,27 @@ function replaceTemplateVars(templateSrv, target, scopedVars) {
}
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;
};
}

View File

@@ -230,7 +230,7 @@ angular.module('grafana.services').factory('QueryProcessor', function($q) {
// Group history by itemid
var grouped_history = _.groupBy(history, 'itemid');
var hosts = _.indexBy(_.flatten(_.map(items, 'hosts')), 'hostid');
var hosts = _.groupBy(_.flatten(_.map(items, 'hosts')), 'hostid');
return _.map(grouped_history, function(hist, itemid) {
var item = _.find(items, {'itemid': itemid});

View File

@@ -35,7 +35,7 @@ export function isTemplateVariable(str, templateVariables) {
var variables = _.map(templateVariables, variable => {
return '$' + variable.name;
});
return _.contains(variables, str);
return _.includes(variables, str);
} else {
return false;
}

View File

@@ -114,6 +114,15 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
// Zabbix API method wrappers //
////////////////////////////////
acknowledgeEvent(eventid, message) {
var params = {
eventids: eventid,
message: message
};
return this.request('event.acknowledge', params);
}
getGroups() {
var params = {
output: ['name'],

View File

@@ -117,7 +117,7 @@ angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $i
var deferred = this.$q.defer();
var historyStorage = this.storage.history;
var full_history;
var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) {
var expired = _.filter(_.groupBy(items, 'itemid'), function(item, itemid) {
return !historyStorage[itemid];
});
if (expired.length) {