Add templated items and apps search.

This commit is contained in:
Alexander Zobnin
2015-05-23 17:08:12 +03:00
parent 841b47edb5
commit 7bc24fc2a0
2 changed files with 150 additions and 25 deletions

View File

@@ -183,7 +183,7 @@ function (angular, _, kbn) {
// Get the list of hosts // Get the list of hosts
ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupid) { ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupid) {
var params = { var params = {
output: ['name'], output: ['name', 'host'],
sortfield: 'name' sortfield: 'name'
}; };
// Return only hosts in given group // Return only hosts in given group
@@ -229,22 +229,65 @@ function (angular, _, kbn) {
}; };
ZabbixAPIDatasource.prototype.findZabbixGroup = function (group) {
var params = {
output: ['name'],
search: {
name: group
},
searchWildcardsEnabled: true
}
return this.performZabbixAPIRequest('hostgroup.get', params);
};
ZabbixAPIDatasource.prototype.findZabbixHost = function (hostname) {
var params = {
output: ['host', 'name'],
search: {
host: hostname,
name: hostname
},
searchWildcardsEnabled: true,
searchByAny: true
}
return this.performZabbixAPIRequest('host.get', params);
};
ZabbixAPIDatasource.prototype.findZabbixApp = function (application) {
var params = {
output: ['name'],
search: {
name: application
},
searchWildcardsEnabled: true
}
return this.performZabbixAPIRequest('application.get', params);
};
// For templated query // For templated query
ZabbixAPIDatasource.prototype.metricFindQuery = function (query) { ZabbixAPIDatasource.prototype.metricFindQuery = function (query) {
var interpolated; var interpolated;
try { try {
interpolated = templateSrv.replace(query); interpolated = encodeURIComponent(templateSrv.replace(query));
} }
catch (err) { catch (err) {
return $q.reject(err); return $q.reject(err);
} }
var parts = interpolated.split('.'); // Split query. Query structure:
var template = { // group.host.app.key
'group': parts[0], var parts = [];
'host': parts[1], _.each(query.split('.'), function (part) {
'item': parts[2] part = templateSrv.replace(part);
}; if (part[0] === '{') {
parts.push(part.slice(1, -1).split(','));
} else {
parts.push(part);
}
});
var template = _.object(['group', 'host', 'app', 'key'], parts)
var params = { var params = {
output: ['name'], output: ['name'],
@@ -255,23 +298,97 @@ function (angular, _, kbn) {
} }
}; };
var self = this; // Get items
return this.performZabbixAPIRequest('hostgroup.get', params) if (parts.length === 4) {
.then(function (result) { var params = {
var groupid = null; output: ['name', 'key_'],
if (result.length && template.group) { sortfield: 'name',
groupid = result[0].groupid; };
} if (template.group != '*' && template.group) {
return self.performHostSuggestQuery(groupid) params.group = template.group;
.then(function (result) { }
return _.map(result, function (host) { if (template.host != '*' && template.host) {
return { params.host = template.host;
text: host.name, }
expandable: false if (template.application != '*' && template.application) {
}; params.application = template.app;
}); }
return this.performZabbixAPIRequest('item.get', params)
.then(function (result) {
return _.map(result, function (item) {
return {
text: item.key_,
expandable: false
};
}); });
}); });
}
// Get applications
else if (parts.length === 3) {
return this.appFindQuery(template);
}
// Return empty object
else {
var d = $q.defer();
d.resolve({ data: [] });
return d.promise;
}
};
ZabbixAPIDatasource.prototype.appFindQuery = function(template) {
var params = {
output: ['name'],
sortfield: 'name'
};
var promises = [];
if (template.group != '*' && template.group) {
if (_.isArray(template.group)) {
_.each(template.group, function (group) {
promises.push(this.findZabbixGroup(group));
}, this);
} else {
promises.push(this.findZabbixGroup(template.group));
}
}
if (template.host != '*' && template.host) {
if (_.isArray(template.host)) {
_.each(template.host, function (host) {
promises.push(this.findZabbixHost(host));
}, this);
} else {
promises.push(this.findZabbixHost(template.host));
}
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
var hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
var params = {
output: ['name']
}
if (hostids) {
params.hostids = hostids;
} else if (groupids) {
params.groupids = groupids;
}
return self.performZabbixAPIRequest('application.get', params)
.then(function (result) {
return _.map(result, function (app) {
return {
text: app.name,
expandable: false
};
});
});
});
}; };

View File

@@ -8,7 +8,7 @@ function (angular, _) {
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
module.controller('ZabbixAPIQueryCtrl', function($scope) { module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv) {
$scope.init = function() { $scope.init = function() {
$scope.targetLetters = targetLetters; $scope.targetLetters = targetLetters;
@@ -162,6 +162,14 @@ function (angular, _) {
$scope.datasource.performHostSuggestQuery(groupid).then(function (series) { $scope.datasource.performHostSuggestQuery(groupid).then(function (series) {
$scope.metric.hostList = series; $scope.metric.hostList = series;
// Add templated variables
_.each(templateSrv.variables, function(variable) {
$scope.metric.hostList.push({
'name': '$' + variable.name,
'hostid': 0
})
});
if ($scope.target.host) { if ($scope.target.host) {
$scope.target.host = $scope.metric.hostList.filter(function (item, index, array) { $scope.target.host = $scope.metric.hostList.filter(function (item, index, array) {
// Find selected host in metric.hostList // Find selected host in metric.hostList