diff --git a/zabbix/datasource.js b/zabbix/datasource.js index f051866..e77b35e 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -1,4 +1,3 @@ -'use strict'; define([ 'angular', 'lodash', @@ -8,7 +7,7 @@ define([ './queryCtrl' ], function (angular, _, kbn) { - //'use strict'; + 'use strict'; var module = angular.module('grafana.services'); @@ -26,21 +25,56 @@ function (angular, _, kbn) { this.basicAuth = datasource.basicAuth; this.withCredentials = datasource.withCredentials; - // TODO: fix passing username and password from config.html - this.username = datasource.meta.username; - this.password = datasource.meta.password; + if (datasource.jsonData) { + this.username = datasource.jsonData.username; + this.password = datasource.jsonData.password; - // Use trends instead history since specified time - this.trends = datasource.meta.trends; - this.trendsFrom = datasource.meta.trendsFrom || '7d'; + // Use trends instead history since specified time + this.trends = datasource.jsonData.trends; + this.trendsFrom = datasource.jsonData.trendsFrom || '7d'; - // Limit metrics per panel for templated request - this.limitmetrics = datasource.meta.limitmetrics || 100; + // Limit metrics per panel for templated request + this.limitmetrics = datasource.jsonData.limitMetrics || 100; + } else { + // DEPRECATED. Loads settings from plugin.json file. + // For backward compatibility only. + this.username = datasource.meta.username; + this.password = datasource.meta.password; + this.trends = datasource.meta.trends; + this.trendsFrom = datasource.meta.trendsFrom || '7d'; + this.limitmetrics = datasource.meta.limitmetrics || 100; + } // Initialize Zabbix API this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials); } + /** + * Test connection to Zabbix API + * + * @return {object} Connection status and Zabbix API version + */ + ZabbixAPIDatasource.prototype.testDatasource = function() { + var self = this; + return this.zabbixAPI.getZabbixAPIVersion().then(function (apiVersion) { + return self.zabbixAPI.performZabbixAPILogin().then(function (auth) { + if (auth) { + return { + status: "success", + title: "Success", + message: "Zabbix API version: " + apiVersion + }; + } else { + return { + status: "error", + title: "Invalid user name or password", + message: "Zabbix API version: " + apiVersion + }; + } + }); + }); + }; + /** * Calls for each panel in dashboard. * diff --git a/zabbix/partials/config.html b/zabbix/partials/config.html index b9d893a..de32561 100644 --- a/zabbix/partials/config.html +++ b/zabbix/partials/config.html @@ -10,16 +10,44 @@ User
  • - +
  • Password
  • - + +
  • + +
    + +
    + +
    +
    +
    +
    - - diff --git a/zabbix/queryCtrl.js b/zabbix/queryCtrl.js index 053d4f4..0a02a58 100644 --- a/zabbix/queryCtrl.js +++ b/zabbix/queryCtrl.js @@ -14,10 +14,10 @@ function (angular, _) { $scope.init = function() { $scope.targetLetters = targetLetters; $scope.metric = { - hostGroupList: ["Loading..."], - hostList: ["Loading..."], - applicationList: ["Loading..."], - itemList: ["Loading..."] + hostGroupList: [], + hostList: [{name: '*', visible_name: 'All'}], + applicationList: [{name: '*', visible_name: 'All'}], + itemList: [{name: 'All'}] }; // Update host group, host, application and item lists @@ -120,10 +120,9 @@ function (angular, _) { * Update list of host groups */ $scope.updateGroupList = function() { - $scope.metric.groupList = [{name: '*', visible_name: 'All'}]; - addTemplatedVariables($scope.metric.groupList); - $scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) { + $scope.metric.groupList = [{name: '*', visible_name: 'All'}]; + addTemplatedVariables($scope.metric.groupList); $scope.metric.groupList = $scope.metric.groupList.concat(groups); }); }; @@ -132,51 +131,54 @@ function (angular, _) { * Update list of hosts */ $scope.updateHostList = function() { - $scope.metric.hostList = [{name: '*', visible_name: 'All'}]; - addTemplatedVariables($scope.metric.hostList); - var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; - $scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) { - $scope.metric.hostList = $scope.metric.hostList.concat(hosts); - }); + if (groups) { + $scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) { + $scope.metric.hostList = [{name: '*', visible_name: 'All'}]; + addTemplatedVariables($scope.metric.hostList); + $scope.metric.hostList = $scope.metric.hostList.concat(hosts); + }); + } }; /** * Update list of host applications */ $scope.updateAppList = function() { - $scope.metric.applicationList = [{name: '*', visible_name: 'All'}]; - addTemplatedVariables($scope.metric.applicationList); - var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; - $scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) { - apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { - return {name: appname}; + if (groups && hosts) { + $scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) { + apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { + return {name: appname}; + }); + $scope.metric.applicationList = [{name: '*', visible_name: 'All'}]; + addTemplatedVariables($scope.metric.applicationList); + $scope.metric.applicationList = $scope.metric.applicationList.concat(apps); }); - $scope.metric.applicationList = $scope.metric.applicationList.concat(apps); - }); + } }; /** * Update list of items */ $scope.updateItemList = function() { - $scope.metric.itemList = [{name: 'All'}]; - addTemplatedVariables($scope.metric.itemList); - var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; var apps = $scope.target.application ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined; - $scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps).then(function (items) { - // Show only unique item names - var uniq_items = _.map(_.uniq(items, function (item) { - return zabbixHelperSrv.expandItemName(item); - }), function (item) { - return {name: zabbixHelperSrv.expandItemName(item)}; + if (groups && hosts && apps) { + $scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps).then(function (items) { + // Show only unique item names + var uniq_items = _.map(_.uniq(items, function (item) { + return zabbixHelperSrv.expandItemName(item); + }), function (item) { + return {name: zabbixHelperSrv.expandItemName(item)}; + }); + $scope.metric.itemList = [{name: 'All'}]; + addTemplatedVariables($scope.metric.itemList); + $scope.metric.itemList = $scope.metric.itemList.concat(uniq_items); }); - $scope.metric.itemList = $scope.metric.itemList.concat(uniq_items); - }); + } }; /** diff --git a/zabbix/zabbixAPIWrapper.js b/zabbix/zabbixAPIWrapper.js index 9e9fb15..45a14d1 100644 --- a/zabbix/zabbixAPIWrapper.js +++ b/zabbix/zabbixAPIWrapper.js @@ -117,6 +117,42 @@ function (angular, _) { // API method wrappers // ///////////////////////// + /** + * Request version of the Zabbix API. + * + * @return {string} Zabbix API version + */ + p.getZabbixAPIVersion = function() { + var options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + url: this.url, + data: { + jsonrpc: '2.0', + method: 'apiinfo.version', + params: [], + id: 1 + } + }; + + if (this.basicAuth || this.withCredentials) { + options.withCredentials = true; + } + if (this.basicAuth) { + options.headers = options.headers || {}; + options.headers.Authorization = this.basicAuth; + } + + return backendSrv.datasourceRequest(options).then(function (result) { + if (!result.data) { + return null; + } + return result.data.result; + }); + }; + /** * Perform history query from Zabbix API * @@ -303,7 +339,7 @@ function (angular, _) { var params = { output: ['name'] }; - if (group[0] !== '*') { + if (group && group[0] !== '*') { params.filter = { name: group }; @@ -338,7 +374,7 @@ function (angular, _) { var params = { output: ['host', 'name'] }; - if (hostnames[0] !== '*') { + if (hostnames && hostnames[0] !== '*') { params.filter = { name: hostnames }; @@ -356,7 +392,7 @@ function (angular, _) { var params = { output: ['name'] }; - if (application[0] !== '*') { + if (application && application[0] !== '*') { params.filter = { name: application }; @@ -385,7 +421,7 @@ function (angular, _) { promises.push(this.getGroupByName(groups)); } // Get applicationids from names - if (apps) { + if (apps && apps[0] !== '*') { promises.push(this.getAppByName(apps)); } @@ -405,7 +441,7 @@ function (angular, _) { return object.hostid; }), 'hostid'); } - if (apps) { + if (apps && apps[0] !== '*') { applicationids = _.map(_.filter(results, function (object) { return object.applicationid; }), 'applicationid');