Fixed #42 - multiple Zabbix datasources at one dashboard. Zabbix API implement as factory.
This commit is contained in:
@@ -11,7 +11,7 @@ function (angular, _, kbn) {
|
||||
|
||||
var module = angular.module('grafana.services');
|
||||
|
||||
module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv, zabbix) {
|
||||
module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv, ZabbixAPI) {
|
||||
|
||||
/**
|
||||
* Datasource initialization. Calls when you refresh page, add
|
||||
@@ -35,7 +35,7 @@ function (angular, _, kbn) {
|
||||
this.limitmetrics = datasource.meta.limitmetrics || 50;
|
||||
|
||||
// Initialize Zabbix API
|
||||
zabbix.init(this.url, this.username, this.password);
|
||||
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +83,7 @@ function (angular, _, kbn) {
|
||||
|
||||
// Find items by item names and perform queries
|
||||
var self = this;
|
||||
return zabbix.itemFindQuery(groups, hosts, apps)
|
||||
return this.zabbixAPI.itemFindQuery(groups, hosts, apps)
|
||||
.then(function (items) {
|
||||
|
||||
// Filter hosts by regex
|
||||
@@ -98,13 +98,13 @@ function (angular, _, kbn) {
|
||||
}
|
||||
}
|
||||
|
||||
if (itemnames === 'All') {
|
||||
if (itemnames == 'All') {
|
||||
|
||||
// Filter items by regex
|
||||
if (target.itemFilter) {
|
||||
var item_pattern = new RegExp(target.itemFilter);
|
||||
return _.filter(items, function (item) {
|
||||
return item_pattern.test(zabbix.expandItemName(item));
|
||||
return item_pattern.test(self.zabbixAPI.expandItemName(item));
|
||||
});
|
||||
} else {
|
||||
return items;
|
||||
@@ -113,7 +113,7 @@ function (angular, _, kbn) {
|
||||
|
||||
// Filtering items
|
||||
return _.filter(items, function (item) {
|
||||
return _.contains(itemnames, zabbix.expandItemName(item));
|
||||
return _.contains(itemnames, self.zabbixAPI.expandItemName(item));
|
||||
});
|
||||
}
|
||||
}).then(function (items) {
|
||||
@@ -129,11 +129,11 @@ function (angular, _, kbn) {
|
||||
var alias = items.length > 1 ? undefined : templateSrv.replace(target.alias);
|
||||
|
||||
if ((from < useTrendsFrom) && self.trends) {
|
||||
return zabbix.getTrends(items, from, to)
|
||||
.then(_.partial(self.handleTrendResponse, items, alias, target.scale));
|
||||
return self.zabbixAPI.getTrends(items, from, to)
|
||||
.then(_.bind(self.handleTrendResponse, self, items, alias, target.scale));
|
||||
} else {
|
||||
return zabbix.getHistory(items, from, to)
|
||||
.then(_.partial(self.handleHistoryResponse, items, alias, target.scale));
|
||||
return self.zabbixAPI.getHistory(items, from, to)
|
||||
.then(_.bind(self.handleHistoryResponse, self, items, alias, target.scale));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -160,10 +160,12 @@ function (angular, _, kbn) {
|
||||
var indexed_items = _.indexBy(items, 'itemid');
|
||||
var grouped_history = _.groupBy(trends, 'itemid');
|
||||
|
||||
var self = this;
|
||||
return $q.when(_.map(grouped_history, function (trends, itemid) {
|
||||
var item = indexed_items[itemid];
|
||||
var series = {
|
||||
target: (item.hosts ? item.hosts[0].name+': ' : '') + (alias ? alias : zabbix.expandItemName(item)),
|
||||
target: (item.hosts ? item.hosts[0].name+': ' : '')
|
||||
+ (alias ? alias : self.zabbixAPI.expandItemName(item)),
|
||||
datapoints: _.map(trends, function (p) {
|
||||
|
||||
// Value must be a number for properly work
|
||||
@@ -213,10 +215,12 @@ function (angular, _, kbn) {
|
||||
var indexed_items = _.indexBy(items, 'itemid');
|
||||
var grouped_history = _.groupBy(history, 'itemid');
|
||||
|
||||
var self = this;
|
||||
return $q.when(_.map(grouped_history, function (history, itemid) {
|
||||
var item = indexed_items[itemid];
|
||||
var series = {
|
||||
target: (item.hosts ? item.hosts[0].name+': ' : '') + (alias ? alias : zabbix.expandItemName(item)),
|
||||
target: (item.hosts ? item.hosts[0].name+': ' : '')
|
||||
+ (alias ? alias : self.zabbixAPI.expandItemName(item)),
|
||||
datapoints: _.map(history, function (p) {
|
||||
|
||||
// Value must be a number for properly work
|
||||
@@ -261,9 +265,9 @@ function (angular, _, kbn) {
|
||||
|
||||
// Get items
|
||||
if (parts.length === 4) {
|
||||
return zabbix.itemFindQuery(template.group, template.host, template.app).then(function (result) {
|
||||
return this.zabbixAPI.itemFindQuery(template.group, template.host, template.app).then(function (result) {
|
||||
return _.map(result, function (item) {
|
||||
var itemname = zabbix.expandItemName(item);
|
||||
var itemname = this.zabbixAPI.expandItemName(item);
|
||||
return {
|
||||
text: itemname,
|
||||
expandable: false
|
||||
@@ -273,7 +277,7 @@ function (angular, _, kbn) {
|
||||
}
|
||||
// Get applications
|
||||
else if (parts.length === 3) {
|
||||
return zabbix.appFindQuery(template.host, template.group).then(function (result) {
|
||||
return this.zabbixAPI.appFindQuery(template.host, template.group).then(function (result) {
|
||||
return _.map(result, function (app) {
|
||||
return {
|
||||
text: app.name,
|
||||
@@ -284,7 +288,7 @@ function (angular, _, kbn) {
|
||||
}
|
||||
// Get hosts
|
||||
else if (parts.length === 2) {
|
||||
return zabbix.hostFindQuery(template.group).then(function (result) {
|
||||
return this.zabbixAPI.hostFindQuery(template.group).then(function (result) {
|
||||
return _.map(result, function (host) {
|
||||
return {
|
||||
text: host.name,
|
||||
@@ -295,7 +299,7 @@ function (angular, _, kbn) {
|
||||
}
|
||||
// Get groups
|
||||
else if (parts.length === 1) {
|
||||
return zabbix.getGroupByName(template.group).then(function (result) {
|
||||
return this.zabbixAPI.getGroupByName(template.group).then(function (result) {
|
||||
return _.map(result, function (hostgroup) {
|
||||
return {
|
||||
text: hostgroup.name,
|
||||
@@ -330,7 +334,7 @@ function (angular, _, kbn) {
|
||||
expandDescription: true
|
||||
};
|
||||
|
||||
return this.performZabbixAPIRequest('trigger.get', params)
|
||||
return this.zabbixAPI.performZabbixAPIRequest('trigger.get', params)
|
||||
.then(function (result) {
|
||||
if(result) {
|
||||
var objects = _.indexBy(result, 'triggerid');
|
||||
@@ -347,7 +351,7 @@ function (angular, _, kbn) {
|
||||
params.value = 1;
|
||||
}
|
||||
|
||||
return self.performZabbixAPIRequest('event.get', params)
|
||||
return self.zabbixAPI.performZabbixAPIRequest('event.get', params)
|
||||
.then(function (result) {
|
||||
var events = [];
|
||||
_.each(result, function(e) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
define([
|
||||
'angular',
|
||||
'lodash',
|
||||
'./zabbixAPIWrapper'
|
||||
'lodash'
|
||||
],
|
||||
function (angular, _) {
|
||||
'use strict';
|
||||
@@ -9,7 +8,7 @@ function (angular, _) {
|
||||
var module = angular.module('grafana.controllers');
|
||||
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv, zabbix) {
|
||||
module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv) {
|
||||
|
||||
$scope.init = function() {
|
||||
$scope.targetLetters = targetLetters;
|
||||
@@ -123,7 +122,7 @@ function (angular, _) {
|
||||
$scope.metric.groupList = [{name: '*', visible_name: 'All'}];
|
||||
addTemplatedVariables($scope.metric.groupList);
|
||||
|
||||
zabbix.performHostGroupSuggestQuery().then(function (groups) {
|
||||
$scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) {
|
||||
$scope.metric.groupList = $scope.metric.groupList.concat(groups);
|
||||
});
|
||||
};
|
||||
@@ -136,7 +135,7 @@ function (angular, _) {
|
||||
addTemplatedVariables($scope.metric.hostList);
|
||||
|
||||
var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
|
||||
zabbix.hostFindQuery(groups).then(function (hosts) {
|
||||
$scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) {
|
||||
$scope.metric.hostList = $scope.metric.hostList.concat(hosts);
|
||||
});
|
||||
};
|
||||
@@ -150,7 +149,7 @@ function (angular, _) {
|
||||
|
||||
var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
|
||||
var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
|
||||
zabbix.appFindQuery(hosts, groups).then(function (apps) {
|
||||
$scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) {
|
||||
apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
|
||||
return {name: appname};
|
||||
});
|
||||
@@ -168,12 +167,12 @@ function (angular, _) {
|
||||
var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
|
||||
var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
|
||||
var apps = $scope.target.application ? splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined;
|
||||
zabbix.itemFindQuery(groups, hosts, apps).then(function (items) {
|
||||
$scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps).then(function (items) {
|
||||
// Show only unique item names
|
||||
var uniq_items = _.map(_.uniq(items, function (item) {
|
||||
return zabbix.expandItemName(item);
|
||||
return $scope.datasource.zabbixAPI.expandItemName(item);
|
||||
}), function (item) {
|
||||
return {name: zabbix.expandItemName(item)};
|
||||
return {name: $scope.datasource.zabbixAPI.expandItemName(item)};
|
||||
});
|
||||
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
|
||||
});
|
||||
|
||||
@@ -7,16 +7,16 @@ function (angular, _) {
|
||||
|
||||
var module = angular.module('grafana.services');
|
||||
|
||||
module.service('zabbix', function($q, backendSrv) {
|
||||
module.factory('ZabbixAPI', function($q, backendSrv) {
|
||||
|
||||
/**
|
||||
* Initialize API parameters.
|
||||
*/
|
||||
this.init = function(api_url, username, password) {
|
||||
function ZabbixAPI(api_url, username, password) {
|
||||
// Initialize API parameters.
|
||||
this.url = api_url;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
};
|
||||
}
|
||||
|
||||
var p = ZabbixAPI.prototype;
|
||||
|
||||
//////////////////
|
||||
// Core methods //
|
||||
@@ -29,7 +29,7 @@ function (angular, _) {
|
||||
* @param {object} params method params
|
||||
* @return {object} data.result field or []
|
||||
*/
|
||||
this.performZabbixAPIRequest = function(method, params) {
|
||||
p.performZabbixAPIRequest = function(method, params) {
|
||||
var options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -72,7 +72,7 @@ function (angular, _) {
|
||||
*
|
||||
* @return {string} auth token
|
||||
*/
|
||||
this.performZabbixAPILogin = function() {
|
||||
p.performZabbixAPILogin = function() {
|
||||
var options = {
|
||||
url : this.url,
|
||||
method : 'POST',
|
||||
@@ -108,7 +108,7 @@ function (angular, _) {
|
||||
* @param {Number} end Time in seconds
|
||||
* @return {Array} Array of Zabbix history objects
|
||||
*/
|
||||
this.getHistory = function(items, start, end) {
|
||||
p.getHistory = function(items, start, end) {
|
||||
// Group items by value type
|
||||
var grouped_items = _.groupBy(items, 'value_type');
|
||||
|
||||
@@ -144,7 +144,7 @@ function (angular, _) {
|
||||
* @param {Number} end Time in seconds
|
||||
* @return {Array} Array of Zabbix trend objects
|
||||
*/
|
||||
this.getTrends = function(items, start, end) {
|
||||
p.getTrends = function(items, start, end) {
|
||||
// Group items by value type
|
||||
var grouped_items = _.groupBy(items, 'value_type');
|
||||
|
||||
@@ -176,7 +176,7 @@ function (angular, _) {
|
||||
*
|
||||
* @return {array} array of Zabbix hostgroup objects
|
||||
*/
|
||||
this.performHostGroupSuggestQuery = function() {
|
||||
p.performHostGroupSuggestQuery = function() {
|
||||
var params = {
|
||||
output: ['name'],
|
||||
sortfield: 'name',
|
||||
@@ -195,7 +195,7 @@ function (angular, _) {
|
||||
* @param {array} groupids
|
||||
* @return {array} array of Zabbix host objects
|
||||
*/
|
||||
this.performHostSuggestQuery = function(groupids) {
|
||||
p.performHostSuggestQuery = function(groupids) {
|
||||
var params = {
|
||||
output: ['name', 'host'],
|
||||
sortfield: 'name',
|
||||
@@ -218,7 +218,7 @@ function (angular, _) {
|
||||
* @param {array} groupids
|
||||
* @return {array} array of Zabbix application objects
|
||||
*/
|
||||
this.performAppSuggestQuery = function(hostids, /* optional */ groupids) {
|
||||
p.performAppSuggestQuery = function(hostids, /* optional */ groupids) {
|
||||
var params = {
|
||||
output: ['name'],
|
||||
sortfield: 'name'
|
||||
@@ -241,7 +241,7 @@ function (angular, _) {
|
||||
* @param {string or Array} groupids ///////////////////////////
|
||||
* @return {string or Array} Array of Zabbix API item objects
|
||||
*/
|
||||
this.performItemSuggestQuery = function(hostids, applicationids, /* optional */ groupids) {
|
||||
p.performItemSuggestQuery = function(hostids, applicationids, /* optional */ groupids) {
|
||||
var params = {
|
||||
output: ['name', 'key_', 'value_type', 'delay'],
|
||||
sortfield: 'name',
|
||||
@@ -282,7 +282,7 @@ function (angular, _) {
|
||||
* @param {string or array} group group names
|
||||
* @return {array} array of Zabbix API hostgroup objects
|
||||
*/
|
||||
this.getGroupByName = function (group) {
|
||||
p.getGroupByName = function (group) {
|
||||
var params = {
|
||||
output: ['name']
|
||||
};
|
||||
@@ -300,7 +300,7 @@ function (angular, _) {
|
||||
* @param {string} group group name
|
||||
* @return {array} groups
|
||||
*/
|
||||
this.searchGroup = function (group) {
|
||||
p.searchGroup = function (group) {
|
||||
var params = {
|
||||
output: ['name'],
|
||||
search: {
|
||||
@@ -317,7 +317,7 @@ function (angular, _) {
|
||||
* @param {string or array} hostnames hosts names
|
||||
* @return {array} array of Zabbix API host objects
|
||||
*/
|
||||
this.getHostByName = function (hostnames) {
|
||||
p.getHostByName = function (hostnames) {
|
||||
var params = {
|
||||
output: ['host', 'name']
|
||||
};
|
||||
@@ -335,7 +335,7 @@ function (angular, _) {
|
||||
* @param {string or array} application applications names
|
||||
* @return {array} array of Zabbix API application objects
|
||||
*/
|
||||
this.getAppByName = function (application) {
|
||||
p.getAppByName = function (application) {
|
||||
var params = {
|
||||
output: ['name']
|
||||
};
|
||||
@@ -356,7 +356,7 @@ function (angular, _) {
|
||||
* @param {string or array} apps
|
||||
* @return {array} array of Zabbix API item objects
|
||||
*/
|
||||
this.itemFindQuery = function(groups, hosts, apps) {
|
||||
p.itemFindQuery = function(groups, hosts, apps) {
|
||||
var promises = [];
|
||||
|
||||
// Get hostids from names
|
||||
@@ -405,7 +405,7 @@ function (angular, _) {
|
||||
* @param {string or array} groups
|
||||
* @return {array} array of Zabbix API application objects
|
||||
*/
|
||||
this.appFindQuery = function(hosts, groups) {
|
||||
p.appFindQuery = function(hosts, groups) {
|
||||
var promises = [];
|
||||
|
||||
// Get hostids from names
|
||||
@@ -443,7 +443,7 @@ function (angular, _) {
|
||||
* @param {string or array} groups
|
||||
* @return {array} array of Zabbix API host objects
|
||||
*/
|
||||
this.hostFindQuery = function(groups) {
|
||||
p.hostFindQuery = function(groups) {
|
||||
var self = this;
|
||||
return this.getGroupByName(groups).then(function (results) {
|
||||
results = _.flatten(results);
|
||||
@@ -462,7 +462,7 @@ function (angular, _) {
|
||||
* @param item: zabbix api item object
|
||||
* @return: expanded item name (string)
|
||||
*/
|
||||
this.expandItemName = function(item) {
|
||||
p.expandItemName = function(item) {
|
||||
var name = item.name;
|
||||
var key = item.key_;
|
||||
|
||||
@@ -477,5 +477,8 @@ function (angular, _) {
|
||||
return name;
|
||||
};
|
||||
|
||||
return ZabbixAPI;
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user