diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 0c6172f..add049c 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -19,7 +19,7 @@ function (angular, _, dateMath, utils, metricFunctions) { /** @ngInject */ function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv, - ZabbixAPI, ZabbixCache, QueryProcessor, DataProcessingService) { + ZabbixAPI, ZabbixCachingProxy, QueryProcessor, DataProcessingService) { // General data source settings this.name = instanceSettings.name; @@ -39,7 +39,7 @@ function (angular, _, dateMath, utils, metricFunctions) { this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials); // Initialize cache service - this.zabbixCache = new ZabbixCache(this.zabbixAPI); + this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI); // Initialize query builder this.queryProcessor = new QueryProcessor(this.zabbixCache); @@ -140,7 +140,7 @@ function (angular, _, dateMath, utils, metricFunctions) { } else { // Use history - getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) { + getHistory = self.zabbixCache.getHistory(items, from, to).then(function(history) { return self.queryProcessor.handleHistory(history, addHostName); }); } @@ -277,8 +277,6 @@ function (angular, _, dateMath, utils, metricFunctions) { * of metrics in "{metric1,metcic2,...,metricN}" format. */ this.metricFindQuery = function (query) { - var metrics; - // Split query. Query structure: // group.host.app.item var parts = []; @@ -296,31 +294,36 @@ function (angular, _, dateMath, utils, metricFunctions) { // Get items if (parts.length === 4) { - var items = this.queryProcessor.filterItems(template.host, template.app, true); - metrics = _.map(items, formatMetric); + //var items = this.queryProcessor.filterItems(template.host, template.app, true); + return this.queryProcessor.filterItems(template.host, template.app, true) + .then(function(items) { + return _.map(items, formatMetric); + }); } // Get applications else if (parts.length === 3) { - var apps = this.queryProcessor.filterApplications(template.host); - metrics = _.map(apps, formatMetric); + return this.queryProcessor.filterApplications(template.host) + .then(function(apps) { + return _.map(apps, formatMetric); + }); } // Get hosts else if (parts.length === 2) { - var hosts = this.queryProcessor.filterHosts(template.group); - metrics = _.map(hosts, formatMetric); + return this.queryProcessor.filterHosts(template.group) + .then(function(hosts) { + return _.map(hosts, formatMetric); + }); } // Get groups else if (parts.length === 1) { - metrics = _.map(this.zabbixCache.getGroups(template.group), formatMetric); + return this.zabbixCache.getGroups(template.group).then(function(groups) { + return _.map(groups, formatMetric); + }); } // Return empty object for invalid request else { - var d = $q.defer(); - d.resolve([]); - return d.promise; + return $q.when([]); } - - return $q.when(metrics); }; function formatMetric(metricObj) { diff --git a/plugins/datasource-zabbix/queryCtrl.js b/plugins/datasource-zabbix/queryCtrl.js index 6c95110..d5dc69f 100644 --- a/plugins/datasource-zabbix/queryCtrl.js +++ b/plugins/datasource-zabbix/queryCtrl.js @@ -10,13 +10,16 @@ define([ var module = angular.module('grafana.controllers'); var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; - module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) { + module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, $q, templateSrv) { var zabbixCache = $scope.datasource.zabbixCache; $scope.init = function () { $scope.targetLetters = targetLetters; - $scope.metric = {}; + + if (!$scope.metric) { + $scope.metric = {}; + } // Load default values var targetDefaults = { @@ -49,17 +52,9 @@ define([ } // Load metrics from cache - if (zabbixCache._initialized) { - $scope.getMetricsFromCache(); + $scope.getMetricsFromCache().then(function() { $scope.initFilters(); - //console.log("Cached", $scope.metric); - } else { - zabbixCache.refresh().then(function () { - $scope.getMetricsFromCache(); - $scope.initFilters(); - //console.log("From server", $scope.metric); - }); - } + }); } else if ($scope.target.mode === 1) { $scope.slaPropertyList = [ @@ -75,19 +70,27 @@ define([ }; $scope.initFilters = function () { - $scope.metric.filteredHosts = $scope.filterHosts(); - $scope.metric.filteredApplications = $scope.filterApplications(); - $scope.metric.filteredItems = $scope.filterItems(); + $scope.filterHosts(); + $scope.filterApplications(); + $scope.filterItems(); }; - $scope.getMetricsFromCache = function () { + $scope.getMetricsFromCache = function() { var item_type = $scope.editorModes[$scope.target.mode]; - $scope.metric = { - groupList: zabbixCache.getGroups(), - hostList: zabbixCache.getHosts(), - applicationList: zabbixCache.getApplications(), - itemList: zabbixCache.getItems(item_type) - }; + 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 @@ -102,130 +105,26 @@ define([ $scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems'); $scope.filterHosts = function () { - var group = $scope.target.group; - var groups = []; - var hosts = []; - - // Filter groups by regex - if (group.isRegex) { - var filterPattern = Utils.buildRegex(group.filter); - groups = _.filter($scope.metric.groupList, function (groupObj) { - return filterPattern.test(groupObj.name); - }); - } - // Find hosts in selected group - else { - var finded = _.find($scope.metric.groupList, {'name': group.filter}); - if (finded) { - groups.push(finded); - } else { - groups = undefined; - } - } - - if (groups) { - var groupids = _.map(groups, 'groupid'); - hosts = _.filter($scope.metric.hostList, function (hostObj) { - return _.intersection(groupids, hostObj.groups).length; - }); - } - return hosts; + var groupFilter = templateSrv.replace($scope.target.group.filter); + $scope.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) { + $scope.metric.filteredHosts = hosts; + }); }; $scope.filterApplications = function () { - var host = $scope.target.host; - var hosts = []; - var apps = []; - - // Filter hosts by regex - if (host.isRegex) { - var filterPattern = Utils.buildRegex(host.filter); - hosts = _.filter($scope.metric.hostList, function (hostObj) { - return filterPattern.test(hostObj.name); - }); - } - // Find applications in selected host - else { - var finded = _.find($scope.metric.hostList, {'name': host.filter}); - if (finded) { - hosts.push(finded); - } else { - hosts = undefined; - } - } - - if (hosts) { - var hostsids = _.map(hosts, 'hostid'); - apps = _.filter($scope.metric.applicationList, function (appObj) { - return _.intersection(hostsids, appObj.hosts).length; - }); - } - - return apps; + var hostFilter = templateSrv.replace($scope.target.host.filter); + $scope.datasource.queryProcessor.filterApplications(hostFilter).then(function(apps) { + $scope.metric.filteredApplications = apps; + }); }; $scope.filterItems = function () { - var app = $scope.target.application; - var host = $scope.target.host; - var hosts = []; - var apps = []; - var items = []; - - // Filter hosts by regex - if (host.isRegex) { - var hostFilterPattern = Utils.buildRegex(host.filter); - hosts = _.filter($scope.metric.hostList, function (hostObj) { - return hostFilterPattern.test(hostObj.name); + var hostFilter = templateSrv.replace($scope.target.host.filter); + var appFilter = templateSrv.replace($scope.target.application.filter); + $scope.datasource.queryProcessor.filterItems(hostFilter, appFilter, $scope.target.showDisabledItems) + .then(function(items) { + $scope.metric.filteredItems = items; }); - } - else { - var findedHosts = _.find($scope.metric.hostList, {'name': host.filter}); - if (findedHosts) { - hosts.push(findedHosts); - } else { - hosts = undefined; - } - } - - // Filter applications by regex - if (app.isRegex) { - var filterPattern = Utils.buildRegex(app.filter); - apps = _.filter($scope.metric.applicationList, function (appObj) { - return filterPattern.test(appObj.name); - }); - } - // Find items in selected application - else if (app.filter) { - var finded = _.find($scope.metric.applicationList, {'name': app.filter}); - if (finded) { - apps.push(finded); - } else { - apps = undefined; - } - } else { - apps = undefined; - if (hosts) { - items = _.filter($scope.metric.itemList, function (itemObj) { - return _.find(hosts, {'hostid': itemObj.hostid }); - }); - } - } - - if (apps) { - var appids = _.flatten(_.map(apps, 'applicationids')); - items = _.filter($scope.metric.itemList, function (itemObj) { - return _.intersection(appids, itemObj.applications).length; - }); - items = _.filter(items, function (itemObj) { - return _.find(hosts, {'hostid': itemObj.hostid }); - }); - } - - if (!$scope.target.showDisabledItems) { - items = _.filter(items, {'status': '0'}); - } - - return items; }; $scope.onTargetPartChange = function (targetPart) { @@ -236,21 +135,21 @@ define([ // Handle group blur and filter hosts $scope.onGroupBlur = function() { - $scope.metric.filteredHosts = $scope.filterHosts(); + $scope.filterHosts(); $scope.parseTarget(); $scope.get_data(); }; // Handle host blur and filter applications $scope.onHostBlur = function() { - $scope.metric.filteredApplications = $scope.filterApplications(); + $scope.filterApplications(); $scope.parseTarget(); $scope.get_data(); }; // Handle application blur and filter items $scope.onApplicationBlur = function() { - $scope.metric.filteredItems = $scope.filterItems(); + $scope.filterItems(); $scope.parseTarget(); $scope.get_data(); }; diff --git a/plugins/datasource-zabbix/queryProcessor.js b/plugins/datasource-zabbix/queryProcessor.js index aa6cf7b..9947a17 100644 --- a/plugins/datasource-zabbix/queryProcessor.js +++ b/plugins/datasource-zabbix/queryProcessor.js @@ -31,128 +31,150 @@ function (angular, _, utils) { this.filterHosts = function(groupFilter) { var groups = []; var hosts = []; - var groupList = self.cache.getGroups(); - // Filter groups by regex - if (utils.isRegex(groupFilter)) { - var filterPattern = utils.buildRegex(groupFilter); - groups = _.filter(groupList, function (groupObj) { - return filterPattern.test(groupObj.name); - }); - } - // Find hosts in selected group - else { - var finded = _.find(groupList, {'name': groupFilter}); - if (finded) { - groups.push(finded); - } else { - groups = undefined; + return self.cache.getGroups().then(function(groupList) { + // Filter groups by regex + if (utils.isRegex(groupFilter)) { + var filterPattern = utils.buildRegex(groupFilter); + groups = _.filter(groupList, function (groupObj) { + return filterPattern.test(groupObj.name); + }); + } + // Find hosts in selected group + else { + var finded = _.find(groupList, {'name': groupFilter}); + if (finded) { + groups.push(finded); + } else { + groups = undefined; + } } - } - if (groups) { - var groupids = _.map(groups, 'groupid'); - hosts = _.filter(self.cache.getHosts(), function (hostObj) { - return _.intersection(groupids, hostObj.groups).length; - }); - } - return hosts; + if (groups) { + var groupids = _.map(groups, 'groupid'); + return self.cache.getHosts().then(function(hosts) { + return _.filter(hosts, function (hostObj) { + return _.intersection(groupids, hostObj.groups).length; + }); + }); + } else { + return hosts; + } + }); }; this.filterApplications = function(hostFilter) { var hosts = []; var apps = []; - var hostList = this.cache.getHosts(); - // Filter hosts by regex - if (utils.isRegex(hostFilter)) { - var filterPattern = utils.buildRegex(hostFilter); - hosts = _.filter(hostList, function (hostObj) { - return filterPattern.test(hostObj.name); - }); - } - // Find applications in selected host - else { - var finded = _.find(hostList, {'name': hostFilter}); - if (finded) { - hosts.push(finded); - } else { - hosts = undefined; + var promises = [ + this.cache.getHosts(), + this.cache.getApplications() + ]; + + return $q.all(promises).then(function(results) { + var hostList = results[0]; + var applicationList = results[1]; + + // Filter hosts by regex + if (utils.isRegex(hostFilter)) { + var filterPattern = utils.buildRegex(hostFilter); + hosts = _.filter(hostList, function (hostObj) { + return filterPattern.test(hostObj.name); + }); + } + // Find applications in selected host + else { + var finded = _.find(hostList, {'name': hostFilter}); + if (finded) { + hosts.push(finded); + } else { + hosts = undefined; + } } - } - if (hosts) { - var hostsids = _.map(hosts, 'hostid'); - apps = _.filter(this.cache.getApplications(), function (appObj) { - return _.intersection(hostsids, appObj.hosts).length; - }); - } - - return apps; + if (hosts) { + var hostsids = _.map(hosts, 'hostid'); + apps = _.filter(applicationList, function (appObj) { + return _.intersection(hostsids, appObj.hosts).length; + }); + } + return apps; + }); }; this.filterItems = function (hostFilter, appFilter, showDisabledItems) { var hosts = []; var apps = []; var items = []; - var hostList = this.cache.getHosts(); - var applicationList = this.cache.getApplications(); - // Filter hosts by regex - if (utils.isRegex(hostFilter)) { - var hostFilterPattern = utils.buildRegex(hostFilter); - hosts = _.filter(hostList, function (hostObj) { - return hostFilterPattern.test(hostObj.name); - }); - } - else { - var findedHosts = _.find(hostList, {'name': hostFilter}); - if (findedHosts) { - hosts.push(findedHosts); - } else { - hosts = undefined; + var promises = [ + this.cache.getHosts(), + this.cache.getApplications(), + this.cache.getItems() + ]; + + return $q.all(promises).then(function(results) { + var hostList = results[0]; + var applicationList = results[1]; + var cachedItems = results[2]; + + // Filter hosts by regex + if (utils.isRegex(hostFilter)) { + var hostFilterPattern = utils.buildRegex(hostFilter); + hosts = _.filter(hostList, function (hostObj) { + return hostFilterPattern.test(hostObj.name); + }); + } + else { + var findedHosts = _.find(hostList, {'name': hostFilter}); + if (findedHosts) { + hosts.push(findedHosts); + } else { + hosts = undefined; + } } - } - // Filter applications by regex - if (utils.isRegex(appFilter)) { - var filterPattern = utils.buildRegex(appFilter); - apps = _.filter(applicationList, function (appObj) { - return filterPattern.test(appObj.name); - }); - } - // Find items in selected application - else if (appFilter) { - var finded = _.find(applicationList, {'name': appFilter}); - if (finded) { - apps.push(finded); + // Filter applications by regex + if (utils.isRegex(appFilter)) { + var filterPattern = utils.buildRegex(appFilter); + apps = _.filter(applicationList, function (appObj) { + return filterPattern.test(appObj.name); + }); + } + // Find items in selected application + else if (appFilter) { + var finded = _.find(applicationList, {'name': appFilter}); + if (finded) { + apps.push(finded); + } else { + apps = undefined; + } } else { apps = undefined; + if (hosts) { + items = _.filter(cachedItems, function (itemObj) { + return _.find(hosts, {'hostid': itemObj.hostid }); + }); + } } - } else { - apps = undefined; - if (hosts) { - items = _.filter(this.cache.getItems(), function (itemObj) { + + if (apps) { + var appids = _.flatten(_.map(apps, 'applicationids')); + items = _.filter(cachedItems, function (itemObj) { + return _.intersection(appids, itemObj.applications).length; + }); + items = _.filter(items, function (itemObj) { return _.find(hosts, {'hostid': itemObj.hostid }); }); } - } - if (apps) { - var appids = _.flatten(_.map(apps, 'applicationids')); - items = _.filter(this.cache.getItems(), function (itemObj) { - return _.intersection(appids, itemObj.applications).length; - }); - items = _.filter(items, function (itemObj) { - return _.find(hosts, {'hostid': itemObj.hostid }); - }); - } + if (!showDisabledItems) { + items = _.filter(items, {'status': '0'}); + } - if (!showDisabledItems) { - items = _.filter(items, {'status': '0'}); - } - - return items; + return items; + }); }; /** @@ -165,107 +187,120 @@ function (angular, _, utils) { var hosts = []; var apps = []; var items = []; + var promises = [ + this.cache.getGroups(), + this.cache.getHosts(), + this.cache.getApplications(), + this.cache.getItems() + ]; - if (utils.isRegex(hostFilter)) { + return $q.all(promises).then(function(results) { + var cachedGroups = results[0]; + var cachedHosts = results[1]; + var cachedApps = results[2]; + var cachedItems = results[3]; - // Filter groups - if (utils.isRegex(groupFilter)) { - var groupPattern = utils.buildRegex(groupFilter); - groups = _.filter(this.cache.getGroups(), function (groupObj) { - return groupPattern.test(groupObj.name); - }); - } else { - var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter}); - if (findedGroup) { - groups.push(findedGroup); + if (utils.isRegex(hostFilter)) { + + // Filter groups + if (utils.isRegex(groupFilter)) { + var groupPattern = utils.buildRegex(groupFilter); + groups = _.filter(cachedGroups, function (groupObj) { + return groupPattern.test(groupObj.name); + }); } else { - groups = undefined; + var findedGroup = _.find(cachedGroups, {'name': groupFilter}); + if (findedGroup) { + groups.push(findedGroup); + } else { + groups = undefined; + } } - } - if (groups) { - var groupids = _.map(groups, 'groupid'); - hosts = _.filter(this.cache.getHosts(), function (hostObj) { - return _.intersection(groupids, hostObj.groups).length; - }); - } else { - // No groups finded - return []; - } - - // Filter hosts - var hostPattern = utils.buildRegex(hostFilter); - hosts = _.filter(hosts, function (hostObj) { - return hostPattern.test(hostObj.name); - }); - } else { - var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter}); - if (findedHost) { - hosts.push(findedHost); - } else { - // No hosts finded - return []; - } - } - - // Find items belongs to selected hosts - items = _.filter(this.cache.getItems(), function (itemObj) { - return _.contains(_.map(hosts, 'hostid'), itemObj.hostid); - }); - - if (utils.isRegex(itemFilter)) { - - // Filter applications - if (utils.isRegex(appFilter)) { - var appPattern = utils.buildRegex(appFilter); - apps = _.filter(this.cache.getApplications(), function (appObj) { - return appPattern.test(appObj.name); - }); - } - // Don't use application filter if it empty - else if (appFilter === "") { - apps = undefined; - } - else { - var findedApp = _.find(this.cache.getApplications(), {'name': appFilter}); - if (findedApp) { - apps.push(findedApp); + if (groups) { + var groupids = _.map(groups, 'groupid'); + hosts = _.filter(cachedHosts, function (hostObj) { + return _.intersection(groupids, hostObj.groups).length; + }); } else { - // No applications finded + // No groups finded + return []; + } + + // Filter hosts + var hostPattern = utils.buildRegex(hostFilter); + hosts = _.filter(hosts, function (hostObj) { + return hostPattern.test(hostObj.name); + }); + } else { + var findedHost = _.find(cachedHosts, {'name': hostFilter}); + if (findedHost) { + hosts.push(findedHost); + } else { + // No hosts finded return []; } } - // Find items belongs to selected applications - if (apps) { - var appids = _.flatten(_.map(apps, 'applicationids')); - items = _.filter(items, function (itemObj) { - return _.intersection(appids, itemObj.applications).length; - }); - } + // Find items belongs to selected hosts + items = _.filter(cachedItems, function (itemObj) { + return _.contains(_.map(hosts, 'hostid'), itemObj.hostid); + }); - if (items) { - var itemPattern = utils.buildRegex(itemFilter); - items = _.filter(items, function (itemObj) { - return itemPattern.test(itemObj.name); - }); + if (utils.isRegex(itemFilter)) { + + // Filter applications + if (utils.isRegex(appFilter)) { + var appPattern = utils.buildRegex(appFilter); + apps = _.filter(cachedApps, function (appObj) { + return appPattern.test(appObj.name); + }); + } + // Don't use application filter if it empty + else if (appFilter === "") { + apps = undefined; + } + else { + var findedApp = _.find(cachedApps, {'name': appFilter}); + if (findedApp) { + apps.push(findedApp); + } else { + // No applications finded + return []; + } + } + + // Find items belongs to selected applications + if (apps) { + var appids = _.flatten(_.map(apps, 'applicationids')); + items = _.filter(items, function (itemObj) { + return _.intersection(appids, itemObj.applications).length; + }); + } + + if (items) { + var itemPattern = utils.buildRegex(itemFilter); + items = _.filter(items, function (itemObj) { + return itemPattern.test(itemObj.name); + }); + } else { + // No items finded + return []; + } } else { - // No items finded - return []; + items = _.filter(items, {'name': itemFilter}); + if (!items.length) { + // No items finded + return []; + } } - } else { - items = _.filter(items, {'name': itemFilter}); - if (!items.length) { - // No items finded - return []; - } - } - // Set host as host name for each item - items = _.each(items, function (itemObj) { - itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name; + // Set host as host name for each item + items = _.each(items, function (itemObj) { + itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name; + }); + + return items; }); - - return items; }; /** diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js index 00b41dd..c84d829 100644 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ b/plugins/datasource-zabbix/zabbixAPI.js @@ -8,6 +8,11 @@ function (angular, _) { var module = angular.module('grafana.services'); + /** + * Zabbix API Wrapper. + * Creates Zabbix API instance with given parameters (url, credentials and other). + * Wraps API calls and provides high-level methods. + */ module.factory('ZabbixAPI', function($q, backendSrv, ZabbixAPIService) { // Initialize Zabbix API. @@ -15,12 +20,14 @@ function (angular, _) { this.url = api_url; this.username = username; this.password = password; - this.auth = null; + this.auth = ""; this.requestOptions = { basicAuth: basicAuth, withCredentials: withCredentials }; + + this.loginPromise = null; } var p = ZabbixAPI.prototype; @@ -31,31 +38,49 @@ function (angular, _) { p.request = function(method, params) { var self = this; - if (this.auth) { - return ZabbixAPIService._request(this.url, method, params, this.requestOptions, this.auth) - .then(function(result) { - return result; - }, + return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth) + .then(function(result) { + return result; + }, - // Handle errors - function(error) { - if (error.message === "Session terminated, re-login, please.") { - return ZabbixAPIService.login(self.url, self.username, self.password, self.requestOptions) - .then(function(auth) { - self.auth = auth; - return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth); - }); - } - }); + // Handle errors + function(error) { + if (isAuthError(error.data)) { + return self.loginOnce().then(function() { + return self.request(method, params); + }); + } + }); + }; + + function isAuthError(message) { + return ( + message === "Session terminated, re-login, please." || + message === "Not authorised." || + message === "Not authorized." + ); + } + + /** + * When API unauthenticated or auth token expired each request produce login() + * call. But auth token is common to all requests. This function wraps login() method + * and call it once. If login() already called just wait for it (return its promise). + * @return login promise + */ + p.loginOnce = function() { + var self = this; + var deferred = $q.defer(); + if (!self.loginPromise) { + self.loginPromise = deferred.promise; + self.login().then(function(auth) { + self.loginPromise = null; + self.auth = auth; + deferred.resolve(auth); + }); } else { - - // Login first - return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions) - .then(function(auth) { - self.auth = auth; - return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth); - }); + return self.loginPromise; } + return deferred.promise; }; /** diff --git a/plugins/datasource-zabbix/zabbixAPIService.js b/plugins/datasource-zabbix/zabbixAPIService.js index 2212a5a..03f7789 100644 --- a/plugins/datasource-zabbix/zabbixAPIService.js +++ b/plugins/datasource-zabbix/zabbixAPIService.js @@ -16,7 +16,8 @@ function (angular) { * Request data from Zabbix API * @return {object} response.result */ - this._request = function(api_url, method, params, options, auth) { + this.request = function(api_url, method, params, options, auth) { + var deferred = $q.defer(); var requestData = { jsonrpc: '2.0', method: method, @@ -24,8 +25,12 @@ function (angular) { id: 1 }; - // Set auth parameter only if it needed - if (auth) { + if (auth === "") { + // Reject immediately if not authenticated + deferred.reject({data: "Not authorised."}); + return deferred.promise; + } else if (auth) { + // Set auth parameter only if it needed requestData.auth = auth; } @@ -46,19 +51,20 @@ function (angular) { requestOptions.headers.Authorization = options.basicAuth; } - return backendSrv.datasourceRequest(requestOptions).then(function (response) { + backendSrv.datasourceRequest(requestOptions).then(function (response) { // General connection issues if (!response.data) { - return []; + deferred.reject(response); } // Handle Zabbix API errors else if (response.data.error) { - throw new ZabbixException(response.data.error); + deferred.reject(response.data.error); } - return response.data.result; + deferred.resolve(response.data.result); }); + return deferred.promise; }; /** @@ -70,7 +76,7 @@ function (angular) { user: username, password: password }; - return this._request(api_url, 'user.login', params, options, null); + return this.request(api_url, 'user.login', params, options, null); }; /** @@ -78,7 +84,7 @@ function (angular) { * Matches the version of Zabbix starting from Zabbix 2.0.4 */ this.getVersion = function(api_url, options) { - return this._request(api_url, 'apiinfo.version', [], options); + return this.request(api_url, 'apiinfo.version', [], options); }; }); diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index 57405cc..bc9f6ca 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -10,9 +10,9 @@ function (angular, _, utils) { // Use factory() instead service() for multiple datasources support. // Each datasource instance must initialize its own cache. - module.factory('ZabbixCache', function($q) { + module.factory('ZabbixCachingProxy', function($q) { - function ZabbixCache(zabbixAPI, ttl) { + function ZabbixCachingProxy(zabbixAPI, ttl) { this.zabbixAPI = zabbixAPI; this.ttl = ttl; @@ -21,14 +21,23 @@ function (angular, _, utils) { this._hosts = undefined; this._applications = undefined; this._items = undefined; + this.storage = { + history: {}, + trends: {} + }; // Check is a service initialized or not this._initialized = undefined; + + this.refreshPromise = false; + + // Wrap _refresh() method to call it once. + this.refresh = callOnce(p._refresh, this.refreshPromise); } - var p = ZabbixCache.prototype; + var p = ZabbixCachingProxy.prototype; - p.refresh = function () { + p._refresh = function() { var self = this; var promises = [ this.zabbixAPI.getGroups(), @@ -37,7 +46,7 @@ function (angular, _, utils) { this.zabbixAPI.getItems() ]; - return $q.all(promises).then(function (results) { + return $q.all(promises).then(function(results) { if (results.length) { self._groups = results[0]; self._hosts = convertHosts(results[1]); @@ -49,33 +58,107 @@ function (angular, _, utils) { }; p.getGroups = function() { - return this._groups; + var self = this; + if (this._groups) { + return $q.when(self._groups); + } else { + return this.refresh().then(function() { + return self._groups; + }); + } }; p.getHosts = function() { - return this._hosts; + var self = this; + if (this._hosts) { + return $q.when(self._hosts); + } else { + return this.refresh().then(function() { + return self._hosts; + }); + } }; p.getApplications = function() { - return this._applications; + var self = this; + if (this._applications) { + return $q.when(self._applications); + } else { + return this.refresh().then(function() { + return self._applications; + }); + } }; p.getItems = function(type) { + var self = this; + if (this._items) { + return $q.when(filterItems(self._items, type)); + } else { + return this.refresh().then(function() { + return filterItems(self._items, type); + }); + } + }; + + function filterItems(items, type) { switch (type) { case 'num': - return _.filter(this._items, function(item) { + return _.filter(items, function(item) { return (item.value_type === '0' || item.value_type === '3'); }); case 'text': - return _.filter(this._items, function(item) { + return _.filter(items, function(item) { return (item.value_type === '1' || item.value_type === '2' || item.value_type === '4'); }); default: - return this._items; + return items; } + } + + p.getHistory = function(items, time_from, time_till) { + var itemids = _.map(arguments[0], 'itemid'); + var stamp = itemids.join() + arguments[1] + arguments[2]; + //console.log(arguments, stamp); + return this.zabbixAPI.getHistory(items, time_from, time_till); + }; + + p.getHistory_ = function(items, time_from, time_till) { + var deferred = $q.defer(); + var historyStorage = this.storage.history; + var full_history; + var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) { + return !historyStorage[itemid]; + }); + if (expired.length) { + this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) { + var grouped_history = _.groupBy(history, 'itemid'); + _.forEach(expired, function(item) { + var itemid = item.itemid; + historyStorage[itemid] = item; + historyStorage[itemid].time_from = time_from; + historyStorage[itemid].time_till = time_till; + historyStorage[itemid].history = grouped_history[itemid]; + }); + full_history = _.map(items, function(item) { + return historyStorage[item.itemid].history; + }); + deferred.resolve(_.flatten(full_history, true)); + }); + } else { + full_history = _.map(items, function(item) { + return historyStorage[item.itemid].history; + }); + deferred.resolve(_.flatten(full_history, true)); + } + return deferred.promise; + }; + + p.getHistoryFromAPI = function(items, time_from, time_till) { + return this.zabbixAPI.getHistory(items, time_from, time_till); }; p.getHost = function(hostid) { @@ -102,7 +185,7 @@ function (angular, _, utils) { * host.hosts - array of host ids */ function convertApplications(applications) { - return _.map(_.groupBy(applications, 'name'), function (value, key) { + return _.map(_.groupBy(applications, 'name'), function(value, key) { return { name: key, applicationids: _.map(value, 'applicationid'), @@ -126,7 +209,23 @@ function (angular, _, utils) { }); } - return ZabbixCache; + function callOnce(func, promiseKeeper) { + return function() { + var deferred = $q.defer(); + if (!promiseKeeper) { + promiseKeeper = deferred.promise; + func.apply(this, arguments).then(function(result) { + deferred.resolve(result); + promiseKeeper = null; + }); + } else { + return promiseKeeper; + } + return deferred.promise; + }; + } + + return ZabbixCachingProxy; });