Added initial cache service for groups, hosts, applications and
items caching on client side.
This commit is contained in:
@@ -5,6 +5,7 @@ define([
|
|||||||
'./directives',
|
'./directives',
|
||||||
'./zabbixAPIWrapper',
|
'./zabbixAPIWrapper',
|
||||||
'./helperFunctions',
|
'./helperFunctions',
|
||||||
|
'./zabbixCacheSrv',
|
||||||
'./queryCtrl'
|
'./queryCtrl'
|
||||||
],
|
],
|
||||||
function (angular, _, dateMath) {
|
function (angular, _, dateMath) {
|
||||||
@@ -12,7 +13,8 @@ function (angular, _, dateMath) {
|
|||||||
|
|
||||||
var module = angular.module('grafana.services');
|
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
|
* Datasource initialization. Calls when you refresh page, add
|
||||||
@@ -48,6 +50,10 @@ function (angular, _, dateMath) {
|
|||||||
|
|
||||||
// Initialize Zabbix API
|
// Initialize Zabbix API
|
||||||
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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 //
|
// API method wrappers //
|
||||||
/////////////////////////
|
/////////////////////////
|
||||||
|
|||||||
59
plugins/datasource-zabbix/zabbixCacheSrv.js
Normal file
59
plugins/datasource-zabbix/zabbixCacheSrv.js
Normal file
@@ -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;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user