diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 4ca75eb..0cf5c94 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,7 @@ function (angular, _, dateMath) { /** @ngInject */ function ZabbixAPIDatasource(instanceSettings, $q, backendSrv, templateSrv, alertSrv, - ZabbixAPI, zabbixHelperSrv) { + ZabbixAPI, zabbixHelperSrv, ZabbixCache) { // General data source settings this.name = instanceSettings.name; @@ -31,6 +32,9 @@ 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); + /** * Test connection to Zabbix API * diff --git a/plugins/datasource-zabbix/zabbixAPIWrapper.js b/plugins/datasource-zabbix/zabbixAPIWrapper.js index 48220ea..9fe2ff2 100644 --- a/plugins/datasource-zabbix/zabbixAPIWrapper.js +++ b/plugins/datasource-zabbix/zabbixAPIWrapper.js @@ -113,6 +113,49 @@ function (angular, _) { }); }; + ////////////////////////// + // High-level functions // + ////////////////////////// + + p.getGroups = function() { + var params = { + output: ['name'], + sortfield: 'name' + }; + + return this.performZabbixAPIRequest('hostgroup.get', params); + }; + + p.getHosts = function() { + var params = { + output: ['name', 'host'], + sortfield: 'name', + selectGroups: [] + }; + + return this.performZabbixAPIRequest('host.get', params); + }; + + p.getApplications = function() { + var params = { + output: ['name'], + sortfield: 'name', + selectHosts: [] + }; + + return this.performZabbixAPIRequest('application.get', params); + }; + + p.getItems = function() { + var params = { + output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'], + sortfield: 'name', + selectApplications: [] + }; + + 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..d65212a --- /dev/null +++ b/plugins/datasource-zabbix/zabbixCacheSrv.js @@ -0,0 +1,89 @@ +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; + + 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(), + this.zabbixAPI.getApplications(), + this.zabbixAPI.getItems() + ]; + + $q.all(promises).then(function (results) { + if (results.length) { + self._groups = results[0]; + + self._hosts = _.forEach(results[1], function(host) { + host.groups = _.map(host.groups, 'groupid'); + return host; + }); + + self._applications = groupApplications(results[2]); + + self._items = _.forEach(results[3], function(item) { + item.applications = _.map(item.applications, 'applicationid'); + return item; + }); + } + }); + }; + + 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 + */ + function groupApplications(applications) { + return _.map(_.groupBy(applications, 'name'), function (value, key) { + return { + name: key, + applicationids: _.map(value, 'applicationid'), + hostids: _.uniq(_.map(_.flatten(value, 'hosts'), 'hostid')) + }; + }); + } + + return ZabbixCache; + + }); + +});