Fixed #42 - multiple Zabbix datasources at one dashboard. Zabbix API implement as factory.

This commit is contained in:
Alexander Zobnin
2015-07-09 23:02:18 +03:00
parent a8ae9f1fa7
commit ea880c4cba
3 changed files with 57 additions and 51 deletions

View File

@@ -11,7 +11,7 @@ function (angular, _, kbn) {
var module = angular.module('grafana.services'); 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 * Datasource initialization. Calls when you refresh page, add
@@ -35,7 +35,7 @@ function (angular, _, kbn) {
this.limitmetrics = datasource.meta.limitmetrics || 50; this.limitmetrics = datasource.meta.limitmetrics || 50;
// Initialize Zabbix API // 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 // Find items by item names and perform queries
var self = this; var self = this;
return zabbix.itemFindQuery(groups, hosts, apps) return this.zabbixAPI.itemFindQuery(groups, hosts, apps)
.then(function (items) { .then(function (items) {
// Filter hosts by regex // Filter hosts by regex
@@ -98,13 +98,13 @@ function (angular, _, kbn) {
} }
} }
if (itemnames === 'All') { if (itemnames == 'All') {
// Filter items by regex // Filter items by regex
if (target.itemFilter) { if (target.itemFilter) {
var item_pattern = new RegExp(target.itemFilter); var item_pattern = new RegExp(target.itemFilter);
return _.filter(items, function (item) { return _.filter(items, function (item) {
return item_pattern.test(zabbix.expandItemName(item)); return item_pattern.test(self.zabbixAPI.expandItemName(item));
}); });
} else { } else {
return items; return items;
@@ -113,7 +113,7 @@ function (angular, _, kbn) {
// Filtering items // Filtering items
return _.filter(items, function (item) { return _.filter(items, function (item) {
return _.contains(itemnames, zabbix.expandItemName(item)); return _.contains(itemnames, self.zabbixAPI.expandItemName(item));
}); });
} }
}).then(function (items) { }).then(function (items) {
@@ -129,11 +129,11 @@ function (angular, _, kbn) {
var alias = items.length > 1 ? undefined : templateSrv.replace(target.alias); var alias = items.length > 1 ? undefined : templateSrv.replace(target.alias);
if ((from < useTrendsFrom) && self.trends) { if ((from < useTrendsFrom) && self.trends) {
return zabbix.getTrends(items, from, to) return self.zabbixAPI.getTrends(items, from, to)
.then(_.partial(self.handleTrendResponse, items, alias, target.scale)); .then(_.bind(self.handleTrendResponse, self, items, alias, target.scale));
} else { } else {
return zabbix.getHistory(items, from, to) return self.zabbixAPI.getHistory(items, from, to)
.then(_.partial(self.handleHistoryResponse, items, alias, target.scale)); .then(_.bind(self.handleHistoryResponse, self, items, alias, target.scale));
} }
} }
}); });
@@ -154,16 +154,18 @@ function (angular, _, kbn) {
}); });
}; };
ZabbixAPIDatasource.prototype.handleTrendResponse = function(items, alias, scale, trends) { ZabbixAPIDatasource.prototype.handleTrendResponse = function (items, alias, scale, trends) {
// Group items and trends by itemid // Group items and trends by itemid
var indexed_items = _.indexBy(items, 'itemid'); var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(trends, 'itemid'); var grouped_history = _.groupBy(trends, 'itemid');
var self = this;
return $q.when(_.map(grouped_history, function (trends, itemid) { return $q.when(_.map(grouped_history, function (trends, itemid) {
var item = indexed_items[itemid]; var item = indexed_items[itemid];
var series = { 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) { datapoints: _.map(trends, function (p) {
// Value must be a number for properly work // Value must be a number for properly work
@@ -213,10 +215,12 @@ function (angular, _, kbn) {
var indexed_items = _.indexBy(items, 'itemid'); var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(history, 'itemid'); var grouped_history = _.groupBy(history, 'itemid');
var self = this;
return $q.when(_.map(grouped_history, function (history, itemid) { return $q.when(_.map(grouped_history, function (history, itemid) {
var item = indexed_items[itemid]; var item = indexed_items[itemid];
var series = { 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) { datapoints: _.map(history, function (p) {
// Value must be a number for properly work // Value must be a number for properly work
@@ -261,9 +265,9 @@ function (angular, _, kbn) {
// Get items // Get items
if (parts.length === 4) { 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) { return _.map(result, function (item) {
var itemname = zabbix.expandItemName(item); var itemname = this.zabbixAPI.expandItemName(item);
return { return {
text: itemname, text: itemname,
expandable: false expandable: false
@@ -273,7 +277,7 @@ function (angular, _, kbn) {
} }
// Get applications // Get applications
else if (parts.length === 3) { 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 _.map(result, function (app) {
return { return {
text: app.name, text: app.name,
@@ -284,7 +288,7 @@ function (angular, _, kbn) {
} }
// Get hosts // Get hosts
else if (parts.length === 2) { 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 _.map(result, function (host) {
return { return {
text: host.name, text: host.name,
@@ -295,7 +299,7 @@ function (angular, _, kbn) {
} }
// Get groups // Get groups
else if (parts.length === 1) { 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 _.map(result, function (hostgroup) {
return { return {
text: hostgroup.name, text: hostgroup.name,
@@ -330,7 +334,7 @@ function (angular, _, kbn) {
expandDescription: true expandDescription: true
}; };
return this.performZabbixAPIRequest('trigger.get', params) return this.zabbixAPI.performZabbixAPIRequest('trigger.get', params)
.then(function (result) { .then(function (result) {
if(result) { if(result) {
var objects = _.indexBy(result, 'triggerid'); var objects = _.indexBy(result, 'triggerid');
@@ -347,7 +351,7 @@ function (angular, _, kbn) {
params.value = 1; params.value = 1;
} }
return self.performZabbixAPIRequest('event.get', params) return self.zabbixAPI.performZabbixAPIRequest('event.get', params)
.then(function (result) { .then(function (result) {
var events = []; var events = [];
_.each(result, function(e) { _.each(result, function(e) {

View File

@@ -1,7 +1,6 @@
define([ define([
'angular', 'angular',
'lodash', 'lodash'
'./zabbixAPIWrapper'
], ],
function (angular, _) { function (angular, _) {
'use strict'; 'use strict';
@@ -9,7 +8,7 @@ function (angular, _) {
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv, zabbix) { module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv) {
$scope.init = function() { $scope.init = function() {
$scope.targetLetters = targetLetters; $scope.targetLetters = targetLetters;
@@ -123,7 +122,7 @@ function (angular, _) {
$scope.metric.groupList = [{name: '*', visible_name: 'All'}]; $scope.metric.groupList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.groupList); addTemplatedVariables($scope.metric.groupList);
zabbix.performHostGroupSuggestQuery().then(function (groups) { $scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) {
$scope.metric.groupList = $scope.metric.groupList.concat(groups); $scope.metric.groupList = $scope.metric.groupList.concat(groups);
}); });
}; };
@@ -136,7 +135,7 @@ function (angular, _) {
addTemplatedVariables($scope.metric.hostList); addTemplatedVariables($scope.metric.hostList);
var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; 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); $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 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 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) { apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
return {name: appname}; return {name: appname};
}); });
@@ -168,12 +167,12 @@ function (angular, _) {
var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; 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 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; 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 // Show only unique item names
var uniq_items = _.map(_.uniq(items, function (item) { var uniq_items = _.map(_.uniq(items, function (item) {
return zabbix.expandItemName(item); return $scope.datasource.zabbixAPI.expandItemName(item);
}), function (item) { }), function (item) {
return {name: zabbix.expandItemName(item)}; return {name: $scope.datasource.zabbixAPI.expandItemName(item)};
}); });
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items); $scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
}); });

View File

@@ -7,16 +7,16 @@ function (angular, _) {
var module = angular.module('grafana.services'); var module = angular.module('grafana.services');
module.service('zabbix', function($q, backendSrv) { module.factory('ZabbixAPI', function($q, backendSrv) {
/** function ZabbixAPI(api_url, username, password) {
* Initialize API parameters. // Initialize API parameters.
*/
this.init = function(api_url, username, password) {
this.url = api_url; this.url = api_url;
this.username = username; this.username = username;
this.password = password; this.password = password;
}; }
var p = ZabbixAPI.prototype;
////////////////// //////////////////
// Core methods // // Core methods //
@@ -29,7 +29,7 @@ function (angular, _) {
* @param {object} params method params * @param {object} params method params
* @return {object} data.result field or [] * @return {object} data.result field or []
*/ */
this.performZabbixAPIRequest = function(method, params) { p.performZabbixAPIRequest = function(method, params) {
var options = { var options = {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -72,7 +72,7 @@ function (angular, _) {
* *
* @return {string} auth token * @return {string} auth token
*/ */
this.performZabbixAPILogin = function() { p.performZabbixAPILogin = function() {
var options = { var options = {
url : this.url, url : this.url,
method : 'POST', method : 'POST',
@@ -108,7 +108,7 @@ function (angular, _) {
* @param {Number} end Time in seconds * @param {Number} end Time in seconds
* @return {Array} Array of Zabbix history objects * @return {Array} Array of Zabbix history objects
*/ */
this.getHistory = function(items, start, end) { p.getHistory = function(items, start, end) {
// Group items by value type // Group items by value type
var grouped_items = _.groupBy(items, 'value_type'); var grouped_items = _.groupBy(items, 'value_type');
@@ -144,7 +144,7 @@ function (angular, _) {
* @param {Number} end Time in seconds * @param {Number} end Time in seconds
* @return {Array} Array of Zabbix trend objects * @return {Array} Array of Zabbix trend objects
*/ */
this.getTrends = function(items, start, end) { p.getTrends = function(items, start, end) {
// Group items by value type // Group items by value type
var grouped_items = _.groupBy(items, 'value_type'); var grouped_items = _.groupBy(items, 'value_type');
@@ -176,7 +176,7 @@ function (angular, _) {
* *
* @return {array} array of Zabbix hostgroup objects * @return {array} array of Zabbix hostgroup objects
*/ */
this.performHostGroupSuggestQuery = function() { p.performHostGroupSuggestQuery = function() {
var params = { var params = {
output: ['name'], output: ['name'],
sortfield: 'name', sortfield: 'name',
@@ -195,7 +195,7 @@ function (angular, _) {
* @param {array} groupids * @param {array} groupids
* @return {array} array of Zabbix host objects * @return {array} array of Zabbix host objects
*/ */
this.performHostSuggestQuery = function(groupids) { p.performHostSuggestQuery = function(groupids) {
var params = { var params = {
output: ['name', 'host'], output: ['name', 'host'],
sortfield: 'name', sortfield: 'name',
@@ -218,7 +218,7 @@ function (angular, _) {
* @param {array} groupids * @param {array} groupids
* @return {array} array of Zabbix application objects * @return {array} array of Zabbix application objects
*/ */
this.performAppSuggestQuery = function(hostids, /* optional */ groupids) { p.performAppSuggestQuery = function(hostids, /* optional */ groupids) {
var params = { var params = {
output: ['name'], output: ['name'],
sortfield: 'name' sortfield: 'name'
@@ -241,7 +241,7 @@ function (angular, _) {
* @param {string or Array} groupids /////////////////////////// * @param {string or Array} groupids ///////////////////////////
* @return {string or Array} Array of Zabbix API item objects * @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 = { var params = {
output: ['name', 'key_', 'value_type', 'delay'], output: ['name', 'key_', 'value_type', 'delay'],
sortfield: 'name', sortfield: 'name',
@@ -282,7 +282,7 @@ function (angular, _) {
* @param {string or array} group group names * @param {string or array} group group names
* @return {array} array of Zabbix API hostgroup objects * @return {array} array of Zabbix API hostgroup objects
*/ */
this.getGroupByName = function (group) { p.getGroupByName = function (group) {
var params = { var params = {
output: ['name'] output: ['name']
}; };
@@ -300,7 +300,7 @@ function (angular, _) {
* @param {string} group group name * @param {string} group group name
* @return {array} groups * @return {array} groups
*/ */
this.searchGroup = function (group) { p.searchGroup = function (group) {
var params = { var params = {
output: ['name'], output: ['name'],
search: { search: {
@@ -317,7 +317,7 @@ function (angular, _) {
* @param {string or array} hostnames hosts names * @param {string or array} hostnames hosts names
* @return {array} array of Zabbix API host objects * @return {array} array of Zabbix API host objects
*/ */
this.getHostByName = function (hostnames) { p.getHostByName = function (hostnames) {
var params = { var params = {
output: ['host', 'name'] output: ['host', 'name']
}; };
@@ -335,7 +335,7 @@ function (angular, _) {
* @param {string or array} application applications names * @param {string or array} application applications names
* @return {array} array of Zabbix API application objects * @return {array} array of Zabbix API application objects
*/ */
this.getAppByName = function (application) { p.getAppByName = function (application) {
var params = { var params = {
output: ['name'] output: ['name']
}; };
@@ -356,7 +356,7 @@ function (angular, _) {
* @param {string or array} apps * @param {string or array} apps
* @return {array} array of Zabbix API item objects * @return {array} array of Zabbix API item objects
*/ */
this.itemFindQuery = function(groups, hosts, apps) { p.itemFindQuery = function(groups, hosts, apps) {
var promises = []; var promises = [];
// Get hostids from names // Get hostids from names
@@ -405,7 +405,7 @@ function (angular, _) {
* @param {string or array} groups * @param {string or array} groups
* @return {array} array of Zabbix API application objects * @return {array} array of Zabbix API application objects
*/ */
this.appFindQuery = function(hosts, groups) { p.appFindQuery = function(hosts, groups) {
var promises = []; var promises = [];
// Get hostids from names // Get hostids from names
@@ -443,7 +443,7 @@ function (angular, _) {
* @param {string or array} groups * @param {string or array} groups
* @return {array} array of Zabbix API host objects * @return {array} array of Zabbix API host objects
*/ */
this.hostFindQuery = function(groups) { p.hostFindQuery = function(groups) {
var self = this; var self = this;
return this.getGroupByName(groups).then(function (results) { return this.getGroupByName(groups).then(function (results) {
results = _.flatten(results); results = _.flatten(results);
@@ -462,7 +462,7 @@ function (angular, _) {
* @param item: zabbix api item object * @param item: zabbix api item object
* @return: expanded item name (string) * @return: expanded item name (string)
*/ */
this.expandItemName = function(item) { p.expandItemName = function(item) {
var name = item.name; var name = item.name;
var key = item.key_; var key = item.key_;
@@ -477,5 +477,8 @@ function (angular, _) {
return name; return name;
}; };
return ZabbixAPI;
}); });
}); });