From dc00766458978d60192a20eb04dddb3df4a63b00 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 11 Jan 2016 21:53:53 +0300 Subject: [PATCH 1/3] Added initial cache service for groups, hosts, applications and items caching on client side. --- plugins/datasource-zabbix/datasource.js | 8 ++- plugins/datasource-zabbix/zabbixAPIWrapper.js | 49 +++++++++++++++ plugins/datasource-zabbix/zabbixCacheSrv.js | 59 +++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 plugins/datasource-zabbix/zabbixCacheSrv.js diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 06a3b1a..3cfa693 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -5,6 +5,7 @@ define([ './directives', './zabbixAPIWrapper', './helperFunctions', + './zabbixCacheSrv', './queryCtrl' ], function (angular, _, dateMath) { @@ -12,7 +13,8 @@ function (angular, _, dateMath) { var module = angular.module('grafana.services'); - module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv, alertSrv, ZabbixAPI, zabbixHelperSrv) { + module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv, alertSrv, + ZabbixAPI, zabbixHelperSrv, ZabbixCache) { /** * Datasource initialization. Calls when you refresh page, add @@ -48,6 +50,10 @@ function (angular, _, dateMath) { // Initialize Zabbix API this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials); + + // Initialize cache service + this.zabbixCache = new ZabbixCache(this.zabbixAPI); + console.log(this.zabbixCache); } /** diff --git a/plugins/datasource-zabbix/zabbixAPIWrapper.js b/plugins/datasource-zabbix/zabbixAPIWrapper.js index 48220ea..51feb76 100644 --- a/plugins/datasource-zabbix/zabbixAPIWrapper.js +++ b/plugins/datasource-zabbix/zabbixAPIWrapper.js @@ -113,6 +113,55 @@ function (angular, _) { }); }; + ////////////////////////// + // High-level functions // + ////////////////////////// + + p.getGroups = function() { + var params = { + output: ['name'], + sortfield: 'name', + selectHosts: [] + }; + + return this.performZabbixAPIRequest('hostgroup.get', params); + }; + + p.getHosts = function() { + var params = { + output: ['name', 'host'], + sortfield: 'name' + }; + + return this.performZabbixAPIRequest('host.get', params); + }; + + p.getApplications = function() { + var params = { + output: ['name'], + sortfield: 'name' + }; + + return this.performZabbixAPIRequest('application.get', params); + }; + + p.getItems = function() { + var params = { + output: ['name', 'key_', 'value_type'], + sortfield: 'name', + //Include web items in the result + webitems: true, + // Return only numeric items + filter: { + value_type: [0, 3] + }, + // Return only enabled items + monitored: true + }; + + return this.performZabbixAPIRequest('item.get', params); + }; + ///////////////////////// // API method wrappers // ///////////////////////// diff --git a/plugins/datasource-zabbix/zabbixCacheSrv.js b/plugins/datasource-zabbix/zabbixCacheSrv.js new file mode 100644 index 0000000..fd6926b --- /dev/null +++ b/plugins/datasource-zabbix/zabbixCacheSrv.js @@ -0,0 +1,59 @@ +define([ + 'angular', + 'lodash' + ], +function (angular, _) { + 'use strict'; + + var module = angular.module('grafana.services'); + + module.factory('ZabbixCache', function($q, backendSrv) { + + function ZabbixCache(zabbixAPI, lifetime) { + var self = this; + this.zabbixAPI = zabbixAPI; + this.lifetime = lifetime; + + var promises = [ + this.zabbixAPI.getGroups(), + this.zabbixAPI.getHosts(), + this.zabbixAPI.getApplications(), + this.zabbixAPI.getItems() + ]; + + $q.all(promises).then(function (results) { + console.log(results); + if (results.length) { + self._groups = results[0]; + self._hosts = results[1]; + self._applications = groupApplications(results[2]); + self._items = results[3]; + } + }); + } + + var p = ZabbixCache.prototype; + + p.getHosts = function() { + return this._hosts; + }; + + /** + * Group Zabbix applications by name + * @param {[type]} applications [description] + * @return {[type]} [description] + */ + function groupApplications(applications) { + return _.map(_.groupBy(applications, 'name'), function (value, key) { + return { + name: key, + ids: _.map(value, 'applicationid') + }; + }); + } + + return ZabbixCache; + + }); + +}); From 7b9ddade2e06a2a5f72f577a893e91878939220b Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 11 Jan 2016 22:26:37 +0300 Subject: [PATCH 2/3] Added initial methods for getting groups, hosts apps and items. --- plugins/datasource-zabbix/zabbixCacheSrv.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugins/datasource-zabbix/zabbixCacheSrv.js b/plugins/datasource-zabbix/zabbixCacheSrv.js index fd6926b..75d92bc 100644 --- a/plugins/datasource-zabbix/zabbixCacheSrv.js +++ b/plugins/datasource-zabbix/zabbixCacheSrv.js @@ -34,10 +34,22 @@ function (angular, _) { var p = ZabbixCache.prototype; + p.getGroups = function() { + return this._groups; + }; + p.getHosts = function() { return this._hosts; }; + p.getApplications = function() { + return this._applications; + }; + + p.getItems = function() { + return this._items; + }; + /** * Group Zabbix applications by name * @param {[type]} applications [description] From 8d3a00d7837a04b221665a7044e524c616d0b51e Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sat, 16 Jan 2016 19:34:45 +0300 Subject: [PATCH 3/3] Format api response. --- plugins/datasource-zabbix/zabbixAPIWrapper.js | 20 ++++------ plugins/datasource-zabbix/zabbixCacheSrv.js | 38 ++++++++++++++----- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/plugins/datasource-zabbix/zabbixAPIWrapper.js b/plugins/datasource-zabbix/zabbixAPIWrapper.js index 51feb76..9fe2ff2 100644 --- a/plugins/datasource-zabbix/zabbixAPIWrapper.js +++ b/plugins/datasource-zabbix/zabbixAPIWrapper.js @@ -120,8 +120,7 @@ function (angular, _) { p.getGroups = function() { var params = { output: ['name'], - sortfield: 'name', - selectHosts: [] + sortfield: 'name' }; return this.performZabbixAPIRequest('hostgroup.get', params); @@ -130,7 +129,8 @@ function (angular, _) { p.getHosts = function() { var params = { output: ['name', 'host'], - sortfield: 'name' + sortfield: 'name', + selectGroups: [] }; return this.performZabbixAPIRequest('host.get', params); @@ -139,7 +139,8 @@ function (angular, _) { p.getApplications = function() { var params = { output: ['name'], - sortfield: 'name' + sortfield: 'name', + selectHosts: [] }; return this.performZabbixAPIRequest('application.get', params); @@ -147,16 +148,9 @@ function (angular, _) { p.getItems = function() { var params = { - output: ['name', 'key_', 'value_type'], + output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'], sortfield: 'name', - //Include web items in the result - webitems: true, - // Return only numeric items - filter: { - value_type: [0, 3] - }, - // Return only enabled items - monitored: true + selectApplications: [] }; return this.performZabbixAPIRequest('item.get', params); diff --git a/plugins/datasource-zabbix/zabbixCacheSrv.js b/plugins/datasource-zabbix/zabbixCacheSrv.js index 75d92bc..d65212a 100644 --- a/plugins/datasource-zabbix/zabbixCacheSrv.js +++ b/plugins/datasource-zabbix/zabbixCacheSrv.js @@ -1,7 +1,7 @@ define([ 'angular', 'lodash' - ], +], function (angular, _) { 'use strict'; @@ -11,9 +11,22 @@ function (angular, _) { function ZabbixCache(zabbixAPI, lifetime) { var self = this; + this.zabbixAPI = zabbixAPI; this.lifetime = lifetime; + this._groups = []; + this._hosts = []; + this._applications = []; + this._items = []; + + this.refresh(); + } + + var p = ZabbixCache.prototype; + + p.refresh = function () { + var self = this; var promises = [ this.zabbixAPI.getGroups(), this.zabbixAPI.getHosts(), @@ -22,17 +35,23 @@ function (angular, _) { ]; $q.all(promises).then(function (results) { - console.log(results); if (results.length) { self._groups = results[0]; - self._hosts = results[1]; + + self._hosts = _.forEach(results[1], function(host) { + host.groups = _.map(host.groups, 'groupid'); + return host; + }); + self._applications = groupApplications(results[2]); - self._items = results[3]; + + self._items = _.forEach(results[3], function(item) { + item.applications = _.map(item.applications, 'applicationid'); + return item; + }); } }); - } - - var p = ZabbixCache.prototype; + }; p.getGroups = function() { return this._groups; @@ -52,14 +71,13 @@ function (angular, _) { /** * Group Zabbix applications by name - * @param {[type]} applications [description] - * @return {[type]} [description] */ function groupApplications(applications) { return _.map(_.groupBy(applications, 'name'), function (value, key) { return { name: key, - ids: _.map(value, 'applicationid') + applicationids: _.map(value, 'applicationid'), + hostids: _.uniq(_.map(_.flatten(value, 'hosts'), 'hostid')) }; }); }