From 037b4b523afee9391c40943b0a1c7daf6adeabb2 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sat, 23 Jan 2016 13:28:24 +0300 Subject: [PATCH] Changing zabbix api wrapper for working with ZabbixAPIService. --- plugins/datasource-zabbix/zabbixAPIService.js | 8 ++++ plugins/datasource-zabbix/zabbixAPIWrapper.js | 45 ++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/plugins/datasource-zabbix/zabbixAPIService.js b/plugins/datasource-zabbix/zabbixAPIService.js index 21c9ec7..32e5687 100644 --- a/plugins/datasource-zabbix/zabbixAPIService.js +++ b/plugins/datasource-zabbix/zabbixAPIService.js @@ -73,6 +73,14 @@ function (angular) { return this._request(api_url, 'user.login', params, options, null); }; + /** + * Get Zabbix API version + * 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); + }; + }); // Define zabbix API exception type diff --git a/plugins/datasource-zabbix/zabbixAPIWrapper.js b/plugins/datasource-zabbix/zabbixAPIWrapper.js index 9fe2ff2..4b3dff3 100644 --- a/plugins/datasource-zabbix/zabbixAPIWrapper.js +++ b/plugins/datasource-zabbix/zabbixAPIWrapper.js @@ -1,21 +1,26 @@ define([ 'angular', - 'lodash' + 'lodash', + './zabbixAPIService' ], function (angular, _) { 'use strict'; var module = angular.module('grafana.services'); - module.factory('ZabbixAPI', function($q, backendSrv) { + module.factory('ZabbixAPI', function($q, backendSrv, ZabbixAPIService) { + // Initialize Zabbix API. function ZabbixAPI(api_url, username, password, basicAuth, withCredentials) { - // Initialize API parameters. this.url = api_url; this.username = username; this.password = password; - this.basicAuth = basicAuth; - this.withCredentials = withCredentials; + this.auth = null; + + this.requestOptions = { + basicAuth: basicAuth, + withCredentials: withCredentials + }; } var p = ZabbixAPI.prototype; @@ -24,6 +29,21 @@ function (angular, _) { // Core methods // ////////////////// + p.request = function(method, params) { + var self = this; + if (this.auth) { + return ZabbixAPIService._request(this.url, method, params, this.requestOptions, this.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); + }); + } + }; + /** * Request data from Zabbix API * @@ -123,7 +143,10 @@ function (angular, _) { sortfield: 'name' }; - return this.performZabbixAPIRequest('hostgroup.get', params); + return this.request('hostgroup.get', params).then(function(result) { + console.log("getGroups", result); + return result; + }); }; p.getHosts = function() { @@ -133,7 +156,7 @@ function (angular, _) { selectGroups: [] }; - return this.performZabbixAPIRequest('host.get', params); + return this.request('host.get', params); }; p.getApplications = function() { @@ -143,7 +166,7 @@ function (angular, _) { selectHosts: [] }; - return this.performZabbixAPIRequest('application.get', params); + return this.request('application.get', params); }; p.getItems = function() { @@ -153,7 +176,7 @@ function (angular, _) { selectApplications: [] }; - return this.performZabbixAPIRequest('item.get', params); + return this.request('item.get', params); }; ///////////////////////// @@ -225,7 +248,7 @@ function (angular, _) { params.time_till = end; } - return this.performZabbixAPIRequest('history.get', params); + return this.request('history.get', params); }, this)).then(function (results) { return _.flatten(results); }); @@ -261,7 +284,7 @@ function (angular, _) { params.time_till = end; } - return this.performZabbixAPIRequest('trend.get', params); + return this.request('trend.get', params); }, this)).then(function (results) { return _.flatten(results); });