From 5ead2950b8e85a74e0c0926b42340f3d2be2d3b4 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 1 Feb 2016 14:31:20 +0300 Subject: [PATCH 1/4] Fixed item filtering by types. --- plugins/datasource-zabbix/datasource.js | 10 +++--- plugins/datasource-zabbix/queryCtrl.js | 40 ++++++++------------- plugins/datasource-zabbix/queryProcessor.js | 10 ++++-- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 03e93e9..1cd35ad 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -294,11 +294,11 @@ function (angular, _, dateMath, utils, metricFunctions) { // Get items if (parts.length === 4) { - //var items = this.queryProcessor.filterItems(template.host, template.app, true); - return this.queryProcessor.filterItems(template.group, template.host, template.app, true) - .then(function(items) { - return _.map(items, formatMetric); - }); + return this.queryProcessor.filterItems(template.group, template.host, + template.app, 'all', true) + .then(function(items) { + return _.map(items, formatMetric); + }); } // Get applications else if (parts.length === 3) { diff --git a/plugins/datasource-zabbix/queryCtrl.js b/plugins/datasource-zabbix/queryCtrl.js index 8aea25f..56d5401 100644 --- a/plugins/datasource-zabbix/queryCtrl.js +++ b/plugins/datasource-zabbix/queryCtrl.js @@ -17,9 +17,10 @@ define([ $scope.init = function () { $scope.targetLetters = targetLetters; - if (!$scope.metric) { - $scope.metric = {}; - } + var scopeDefaults = { + metric: {} + }; + _.defaults($scope, scopeDefaults); // Load default values var targetDefaults = { @@ -51,10 +52,7 @@ define([ $scope.target.downsampleFunction = $scope.downsampleFunctionList[0]; } - // Load metrics from cache - $scope.getMetricsFromCache().then(function() { - $scope.initFilters(); - }); + $scope.initFilters(); } else if ($scope.target.mode === 1) { $scope.slaPropertyList = [ @@ -70,29 +68,12 @@ define([ }; $scope.initFilters = function () { + $scope.filterGroups(); $scope.filterHosts(); $scope.filterApplications(); $scope.filterItems(); }; - $scope.getMetricsFromCache = function() { - var item_type = $scope.editorModes[$scope.target.mode]; - var promises = [ - zabbixCache.getGroups(), - zabbixCache.getHosts(), - zabbixCache.getApplications(), - zabbixCache.getItems(item_type) - ]; - return $q.all(promises).then(function(results) { - $scope.metric = { - groupList: results[0], - hostList: results[1], - applicationList: results[2], - itemList: results[3] - }; - }); - }; - // Get list of metric names for bs-typeahead directive function getMetricNames(scope, metricList) { return _.uniq(_.map(scope.metric[metricList], 'name')); @@ -111,6 +92,12 @@ define([ }); }; + $scope.filterGroups = function() { + $scope.datasource.queryProcessor.filterGroups().then(function(groups) { + $scope.metric.groupList = groups; + }); + }; + $scope.filterApplications = function () { var groupFilter = templateSrv.replace($scope.target.group.filter); var hostFilter = templateSrv.replace($scope.target.host.filter); @@ -121,11 +108,12 @@ define([ }; $scope.filterItems = function () { + var item_type = $scope.editorModes[$scope.target.mode]; var groupFilter = templateSrv.replace($scope.target.group.filter); var hostFilter = templateSrv.replace($scope.target.host.filter); var appFilter = templateSrv.replace($scope.target.application.filter); $scope.datasource.queryProcessor.filterItems(groupFilter, hostFilter, appFilter, - $scope.target.showDisabledItems).then(function(items) { + item_type, $scope.target.showDisabledItems).then(function(items) { $scope.metric.filteredItems = items; }); }; diff --git a/plugins/datasource-zabbix/queryProcessor.js b/plugins/datasource-zabbix/queryProcessor.js index fc54ae0..d28e2f4 100644 --- a/plugins/datasource-zabbix/queryProcessor.js +++ b/plugins/datasource-zabbix/queryProcessor.js @@ -28,6 +28,12 @@ function (angular, _, utils) { } }; + this.filterGroups = function(groupFilter) { + return self.cache.getGroups().then(function(groupList) { + return groupList; + }); + }; + this.filterHosts = function(groupFilter) { var groups = []; var hosts = []; @@ -103,7 +109,7 @@ function (angular, _, utils) { }); }; - this.filterItems = function (groupFilter, hostFilter, appFilter, showDisabledItems) { + this.filterItems = function (groupFilter, hostFilter, appFilter, itemType, showDisabledItems) { var hosts = []; var apps = []; var items = []; @@ -111,7 +117,7 @@ function (angular, _, utils) { var promises = [ this.filterHosts(groupFilter), this.filterApplications(groupFilter, hostFilter), - this.cache.getItems(), + this.cache.getItems(itemType), ]; return $q.all(promises).then(function(results) { From 2b91f417f47628335aa2cef343139d8c287de590 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 1 Feb 2016 15:42:50 +0300 Subject: [PATCH 2/4] Fixed text metric query. --- plugins/datasource-zabbix/datasource.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 1cd35ad..98555ec 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -193,12 +193,9 @@ function (angular, _, dateMath, utils, metricFunctions) { else if (target.mode === 2) { // Find items by item names and perform queries - return this.zabbixAPI.itemFindQuery(groups, hosts, apps, "text") - .then(function (items) { - items = _.filter(items, function (item) { - return _.contains(itemnames, zabbixHelperSrv.expandItemName(item)); - }); - return self.zabbixAPI.getHistory(items, from, to).then(function(history) { + return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter) + .then(function(items) { + return self.zabbixCache.getHistory(items, from, to).then(function(history) { return { target: target.item.name, datapoints: _.map(history, function (p) { From efc51fdcf6730f2af0c690f4fe4a61f15f0b4b1b Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 1 Feb 2016 16:22:11 +0300 Subject: [PATCH 3/4] Impoved query for text items - get only last value. --- plugins/datasource-zabbix/datasource.js | 42 +++++++++++++------------ plugins/datasource-zabbix/zabbixAPI.js | 22 ++++++++++++- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 98555ec..2b0d24f 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -191,31 +191,33 @@ function (angular, _, dateMath, utils, metricFunctions) { // Query text data else if (target.mode === 2) { - - // Find items by item names and perform queries return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter) .then(function(items) { - return self.zabbixCache.getHistory(items, from, to).then(function(history) { - return { - target: target.item.name, - datapoints: _.map(history, function (p) { - var value = p.value; - if (target.textFilter) { - var text_extract_pattern = new RegExp(templateSrv.replace(target.textFilter, options.scopedVars)); - //var text_extract_pattern = new RegExp(target.textFilter); - var result = text_extract_pattern.exec(value); - if (result) { - if (target.useCaptureGroups) { - value = result[1]; - } else { - value = result[0]; - } + var deferred = $q.defer(); + if (items.length) { + self.zabbixAPI.getLastValue(items[0].itemid).then(function(lastvalue) { + if (target.textFilter) { + var text_extract_pattern = new RegExp(templateSrv.replace(target.textFilter, options.scopedVars)); + var result = text_extract_pattern.exec(lastvalue); + if (result) { + if (target.useCaptureGroups) { + result = result[1]; } else { - value = null; + result = result[0]; } } - return [value, p.clock * 1000]; - }) + deferred.resolve(result); + } else { + deferred.resolve(lastvalue); + } + }); + } else { + deferred.resolve(null); + } + return deferred.promise.then(function(text) { + return { + target: target.item.name, + datapoints: [[text, to * 1000]] }; }); }); diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js index c84d829..fb1efca 100644 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ b/plugins/datasource-zabbix/zabbixAPI.js @@ -132,7 +132,13 @@ function (angular, _) { p.getItems = function() { var params = { - output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'], + output: [ + 'name', 'key_', + 'value_type', + 'hostid', + 'status', + 'state' + ], sortfield: 'name', selectApplications: [] }; @@ -140,6 +146,20 @@ function (angular, _) { return this.request('item.get', params); }; + p.getLastValue = function(itemid) { + var params = { + output: ['lastvalue'], + itemids: itemid + }; + return this.request('item.get', params).then(function(items) { + if (items.length) { + return items[0].lastvalue; + } else { + return null; + } + }); + }; + /** * Perform history query from Zabbix API * From be405c47dc246df2f731cbeebe4e9861f8c4a03e Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 1 Feb 2016 16:36:03 +0300 Subject: [PATCH 4/4] Some UI fixes for text query editor. --- .../partials/query.editor.html | 31 +++---------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/plugins/datasource-zabbix/partials/query.editor.html b/plugins/datasource-zabbix/partials/query.editor.html index b5ff671..0bed21c 100644 --- a/plugins/datasource-zabbix/partials/query.editor.html +++ b/plugins/datasource-zabbix/partials/query.editor.html @@ -11,6 +11,7 @@