From b466cc4cbc620a3dab5638eca8f07aa81956fb4a Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 12 Jul 2015 19:12:21 +0300 Subject: [PATCH 1/8] Resolved #54 - Implemented testDatasource() method for test connection to zabbix server. --- zabbix/datasource.js | 15 +++++++++++++++ zabbix/zabbixAPIWrapper.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index f051866..656c2dd 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -41,6 +41,21 @@ function (angular, _, kbn) { 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() { + return this.zabbixAPI.getZabbixAPIVersion().then(function (apiVersion) { + return { + status: "success", + title: "Success", + message: "Zabbix API version: " + apiVersion + }; + }); + }; + /** * Calls for each panel in dashboard. * diff --git a/zabbix/zabbixAPIWrapper.js b/zabbix/zabbixAPIWrapper.js index 9e9fb15..78be787 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 * From ba48436a0a9b8a9987a59ec482deb0060daa1ec3 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 12 Jul 2015 20:55:56 +0300 Subject: [PATCH 2/8] Resolved #53 - Get settings from data source configuration page. --- zabbix/datasource.js | 11 +++++------ zabbix/partials/config.html | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 656c2dd..57883f6 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -26,16 +26,15 @@ 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; + this.username = datasource.jsonData.username || datasource.meta.username; + this.password = datasource.jsonData.password || datasource.meta.password; // Use trends instead history since specified time - this.trends = datasource.meta.trends; - this.trendsFrom = datasource.meta.trendsFrom || '7d'; + this.trends = datasource.jsonData.trends || datasource.meta.trends; + this.trendsFrom = datasource.jsonData.trendsFrom || datasource.meta.trendsFrom || '7d'; // Limit metrics per panel for templated request - this.limitmetrics = datasource.meta.limitmetrics || 100; + this.limitmetrics = datasource.jsonData.limitMetrics || datasource.meta.limitmetrics || 100; // Initialize Zabbix API this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials); 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
  • - + +
  • + +
    + +
    +
      +
    • + Trends +
    • +
    • + Enable  + + +
    • +
    • + Use trends from +
    • +
    • + +
    • +
    +
    +
    +
    +
      +
    • + Metrics limit +
    • +
    • +
    - - From 920e0df72948c145ea666c96bbe068b7cd0dd3b9 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 12 Jul 2015 22:59:33 +0300 Subject: [PATCH 3/8] iss #54 - Added authorization check by testDatasource() method. --- zabbix/datasource.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 57883f6..a9f03bd 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -46,12 +46,23 @@ function (angular, _, kbn) { * @return {object} Connection status and Zabbix API version */ ZabbixAPIDatasource.prototype.testDatasource = function() { + var self = this; return this.zabbixAPI.getZabbixAPIVersion().then(function (apiVersion) { - return { - status: "success", - title: "Success", - message: "Zabbix API version: " + 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 + }; + } + }); }); }; From 9d84a3e0e821822834af7d75c354dccc682e0f07 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 13 Jul 2015 19:25:23 +0300 Subject: [PATCH 4/8] Fixed issues with query editor after grafana update. --- zabbix/queryCtrl.js | 66 +++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 32 deletions(-) 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); - }); + } }; /** From f07b5cb476963d8751275e8bc5c0cbcdbfe42a12 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 16 Jul 2015 18:29:41 +0300 Subject: [PATCH 5/8] Added backward compatibility for plugin configuration. --- zabbix/datasource.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index a9f03bd..c75fa78 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -26,15 +26,25 @@ function (angular, _, kbn) { this.basicAuth = datasource.basicAuth; this.withCredentials = datasource.withCredentials; - this.username = datasource.jsonData.username || datasource.meta.username; - this.password = datasource.jsonData.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.jsonData.trends || datasource.meta.trends; - this.trendsFrom = datasource.jsonData.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.jsonData.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); From e72811cb2d7037e1343a1321176132c6750c1ba0 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 16 Jul 2015 18:38:42 +0300 Subject: [PATCH 6/8] iss #45 - Some additional checks for getGroupByName(), getHostByName() and getAppByName() methods. --- zabbix/zabbixAPIWrapper.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zabbix/zabbixAPIWrapper.js b/zabbix/zabbixAPIWrapper.js index 78be787..1e7d23e 100644 --- a/zabbix/zabbixAPIWrapper.js +++ b/zabbix/zabbixAPIWrapper.js @@ -339,7 +339,7 @@ function (angular, _) { var params = { output: ['name'] }; - if (group[0] !== '*') { + if (group && group[0] !== '*') { params.filter = { name: group }; @@ -374,7 +374,7 @@ function (angular, _) { var params = { output: ['host', 'name'] }; - if (hostnames[0] !== '*') { + if (hostnames && hostnames[0] !== '*') { params.filter = { name: hostnames }; @@ -392,7 +392,7 @@ function (angular, _) { var params = { output: ['name'] }; - if (application[0] !== '*') { + if (application && application[0] !== '*') { params.filter = { name: application }; From a657b0b7cd0508807ebf4bd0a6a62c21a5292046 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 20 Jul 2015 20:26:36 +0300 Subject: [PATCH 7/8] Fixed #45 - Item list don't works properly when All applications selected. --- zabbix/zabbixAPIWrapper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zabbix/zabbixAPIWrapper.js b/zabbix/zabbixAPIWrapper.js index 1e7d23e..45a14d1 100644 --- a/zabbix/zabbixAPIWrapper.js +++ b/zabbix/zabbixAPIWrapper.js @@ -421,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)); } @@ -441,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'); From 0df92cf580d1afbfaf564a1d041832b0d186854f Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 20 Jul 2015 20:36:41 +0300 Subject: [PATCH 8/8] Move 'use strict' into function in datasource.js --- zabbix/datasource.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index c75fa78..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');