Merge branch 'cache-service' into grafana-3.0
This commit is contained in:
@@ -19,7 +19,7 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv,
|
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv,
|
||||||
ZabbixAPI, ZabbixCache, QueryProcessor, DataProcessingService) {
|
ZabbixAPI, ZabbixCachingProxy, QueryProcessor, DataProcessingService) {
|
||||||
|
|
||||||
// General data source settings
|
// General data source settings
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
@@ -39,7 +39,7 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
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
|
// Initialize cache service
|
||||||
this.zabbixCache = new ZabbixCache(this.zabbixAPI);
|
this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI);
|
||||||
|
|
||||||
// Initialize query builder
|
// Initialize query builder
|
||||||
this.queryProcessor = new QueryProcessor(this.zabbixCache);
|
this.queryProcessor = new QueryProcessor(this.zabbixCache);
|
||||||
@@ -140,7 +140,7 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Use history
|
// Use history
|
||||||
getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) {
|
getHistory = self.zabbixCache.getHistory(items, from, to).then(function(history) {
|
||||||
return self.queryProcessor.handleHistory(history, addHostName);
|
return self.queryProcessor.handleHistory(history, addHostName);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -277,8 +277,6 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
||||||
*/
|
*/
|
||||||
this.metricFindQuery = function (query) {
|
this.metricFindQuery = function (query) {
|
||||||
var metrics;
|
|
||||||
|
|
||||||
// Split query. Query structure:
|
// Split query. Query structure:
|
||||||
// group.host.app.item
|
// group.host.app.item
|
||||||
var parts = [];
|
var parts = [];
|
||||||
@@ -296,31 +294,36 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
|
|
||||||
// Get items
|
// Get items
|
||||||
if (parts.length === 4) {
|
if (parts.length === 4) {
|
||||||
var items = this.queryProcessor.filterItems(template.host, template.app, true);
|
//var items = this.queryProcessor.filterItems(template.host, template.app, true);
|
||||||
metrics = _.map(items, formatMetric);
|
return this.queryProcessor.filterItems(template.host, template.app, true)
|
||||||
|
.then(function(items) {
|
||||||
|
return _.map(items, formatMetric);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// Get applications
|
// Get applications
|
||||||
else if (parts.length === 3) {
|
else if (parts.length === 3) {
|
||||||
var apps = this.queryProcessor.filterApplications(template.host);
|
return this.queryProcessor.filterApplications(template.host)
|
||||||
metrics = _.map(apps, formatMetric);
|
.then(function(apps) {
|
||||||
|
return _.map(apps, formatMetric);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// Get hosts
|
// Get hosts
|
||||||
else if (parts.length === 2) {
|
else if (parts.length === 2) {
|
||||||
var hosts = this.queryProcessor.filterHosts(template.group);
|
return this.queryProcessor.filterHosts(template.group)
|
||||||
metrics = _.map(hosts, formatMetric);
|
.then(function(hosts) {
|
||||||
|
return _.map(hosts, formatMetric);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// Get groups
|
// Get groups
|
||||||
else if (parts.length === 1) {
|
else if (parts.length === 1) {
|
||||||
metrics = _.map(this.zabbixCache.getGroups(template.group), formatMetric);
|
return this.zabbixCache.getGroups(template.group).then(function(groups) {
|
||||||
|
return _.map(groups, formatMetric);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// Return empty object for invalid request
|
// Return empty object for invalid request
|
||||||
else {
|
else {
|
||||||
var d = $q.defer();
|
return $q.when([]);
|
||||||
d.resolve([]);
|
|
||||||
return d.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $q.when(metrics);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatMetric(metricObj) {
|
function formatMetric(metricObj) {
|
||||||
|
|||||||
@@ -10,13 +10,16 @@ define([
|
|||||||
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) {
|
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, $q, templateSrv) {
|
||||||
|
|
||||||
var zabbixCache = $scope.datasource.zabbixCache;
|
var zabbixCache = $scope.datasource.zabbixCache;
|
||||||
|
|
||||||
$scope.init = function () {
|
$scope.init = function () {
|
||||||
$scope.targetLetters = targetLetters;
|
$scope.targetLetters = targetLetters;
|
||||||
|
|
||||||
|
if (!$scope.metric) {
|
||||||
$scope.metric = {};
|
$scope.metric = {};
|
||||||
|
}
|
||||||
|
|
||||||
// Load default values
|
// Load default values
|
||||||
var targetDefaults = {
|
var targetDefaults = {
|
||||||
@@ -49,18 +52,10 @@ define([
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load metrics from cache
|
// Load metrics from cache
|
||||||
if (zabbixCache._initialized) {
|
$scope.getMetricsFromCache().then(function() {
|
||||||
$scope.getMetricsFromCache();
|
|
||||||
$scope.initFilters();
|
$scope.initFilters();
|
||||||
//console.log("Cached", $scope.metric);
|
|
||||||
} else {
|
|
||||||
zabbixCache.refresh().then(function () {
|
|
||||||
$scope.getMetricsFromCache();
|
|
||||||
$scope.initFilters();
|
|
||||||
//console.log("From server", $scope.metric);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if ($scope.target.mode === 1) {
|
else if ($scope.target.mode === 1) {
|
||||||
$scope.slaPropertyList = [
|
$scope.slaPropertyList = [
|
||||||
{name: "Status", property: "status"},
|
{name: "Status", property: "status"},
|
||||||
@@ -75,19 +70,27 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
$scope.initFilters = function () {
|
$scope.initFilters = function () {
|
||||||
$scope.metric.filteredHosts = $scope.filterHosts();
|
$scope.filterHosts();
|
||||||
$scope.metric.filteredApplications = $scope.filterApplications();
|
$scope.filterApplications();
|
||||||
$scope.metric.filteredItems = $scope.filterItems();
|
$scope.filterItems();
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.getMetricsFromCache = function() {
|
$scope.getMetricsFromCache = function() {
|
||||||
var item_type = $scope.editorModes[$scope.target.mode];
|
var item_type = $scope.editorModes[$scope.target.mode];
|
||||||
|
var promises = [
|
||||||
|
zabbixCache.getGroups(),
|
||||||
|
zabbixCache.getHosts(),
|
||||||
|
zabbixCache.getApplications(),
|
||||||
|
zabbixCache.getItems(item_type)
|
||||||
|
];
|
||||||
|
return $q.all(promises).then(function(results) {
|
||||||
$scope.metric = {
|
$scope.metric = {
|
||||||
groupList: zabbixCache.getGroups(),
|
groupList: results[0],
|
||||||
hostList: zabbixCache.getHosts(),
|
hostList: results[1],
|
||||||
applicationList: zabbixCache.getApplications(),
|
applicationList: results[2],
|
||||||
itemList: zabbixCache.getItems(item_type)
|
itemList: results[3]
|
||||||
};
|
};
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get list of metric names for bs-typeahead directive
|
// Get list of metric names for bs-typeahead directive
|
||||||
@@ -102,130 +105,26 @@ define([
|
|||||||
$scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems');
|
$scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems');
|
||||||
|
|
||||||
$scope.filterHosts = function () {
|
$scope.filterHosts = function () {
|
||||||
var group = $scope.target.group;
|
var groupFilter = templateSrv.replace($scope.target.group.filter);
|
||||||
var groups = [];
|
$scope.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) {
|
||||||
var hosts = [];
|
$scope.metric.filteredHosts = hosts;
|
||||||
|
|
||||||
// Filter groups by regex
|
|
||||||
if (group.isRegex) {
|
|
||||||
var filterPattern = Utils.buildRegex(group.filter);
|
|
||||||
groups = _.filter($scope.metric.groupList, function (groupObj) {
|
|
||||||
return filterPattern.test(groupObj.name);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
// Find hosts in selected group
|
|
||||||
else {
|
|
||||||
var finded = _.find($scope.metric.groupList, {'name': group.filter});
|
|
||||||
if (finded) {
|
|
||||||
groups.push(finded);
|
|
||||||
} else {
|
|
||||||
groups = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (groups) {
|
|
||||||
var groupids = _.map(groups, 'groupid');
|
|
||||||
hosts = _.filter($scope.metric.hostList, function (hostObj) {
|
|
||||||
return _.intersection(groupids, hostObj.groups).length;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return hosts;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.filterApplications = function () {
|
$scope.filterApplications = function () {
|
||||||
var host = $scope.target.host;
|
var hostFilter = templateSrv.replace($scope.target.host.filter);
|
||||||
var hosts = [];
|
$scope.datasource.queryProcessor.filterApplications(hostFilter).then(function(apps) {
|
||||||
var apps = [];
|
$scope.metric.filteredApplications = apps;
|
||||||
|
|
||||||
// Filter hosts by regex
|
|
||||||
if (host.isRegex) {
|
|
||||||
var filterPattern = Utils.buildRegex(host.filter);
|
|
||||||
hosts = _.filter($scope.metric.hostList, function (hostObj) {
|
|
||||||
return filterPattern.test(hostObj.name);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
// Find applications in selected host
|
|
||||||
else {
|
|
||||||
var finded = _.find($scope.metric.hostList, {'name': host.filter});
|
|
||||||
if (finded) {
|
|
||||||
hosts.push(finded);
|
|
||||||
} else {
|
|
||||||
hosts = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hosts) {
|
|
||||||
var hostsids = _.map(hosts, 'hostid');
|
|
||||||
apps = _.filter($scope.metric.applicationList, function (appObj) {
|
|
||||||
return _.intersection(hostsids, appObj.hosts).length;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return apps;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.filterItems = function () {
|
$scope.filterItems = function () {
|
||||||
var app = $scope.target.application;
|
var hostFilter = templateSrv.replace($scope.target.host.filter);
|
||||||
var host = $scope.target.host;
|
var appFilter = templateSrv.replace($scope.target.application.filter);
|
||||||
var hosts = [];
|
$scope.datasource.queryProcessor.filterItems(hostFilter, appFilter, $scope.target.showDisabledItems)
|
||||||
var apps = [];
|
.then(function(items) {
|
||||||
var items = [];
|
$scope.metric.filteredItems = items;
|
||||||
|
|
||||||
// Filter hosts by regex
|
|
||||||
if (host.isRegex) {
|
|
||||||
var hostFilterPattern = Utils.buildRegex(host.filter);
|
|
||||||
hosts = _.filter($scope.metric.hostList, function (hostObj) {
|
|
||||||
return hostFilterPattern.test(hostObj.name);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
else {
|
|
||||||
var findedHosts = _.find($scope.metric.hostList, {'name': host.filter});
|
|
||||||
if (findedHosts) {
|
|
||||||
hosts.push(findedHosts);
|
|
||||||
} else {
|
|
||||||
hosts = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter applications by regex
|
|
||||||
if (app.isRegex) {
|
|
||||||
var filterPattern = Utils.buildRegex(app.filter);
|
|
||||||
apps = _.filter($scope.metric.applicationList, function (appObj) {
|
|
||||||
return filterPattern.test(appObj.name);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Find items in selected application
|
|
||||||
else if (app.filter) {
|
|
||||||
var finded = _.find($scope.metric.applicationList, {'name': app.filter});
|
|
||||||
if (finded) {
|
|
||||||
apps.push(finded);
|
|
||||||
} else {
|
|
||||||
apps = undefined;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
apps = undefined;
|
|
||||||
if (hosts) {
|
|
||||||
items = _.filter($scope.metric.itemList, function (itemObj) {
|
|
||||||
return _.find(hosts, {'hostid': itemObj.hostid });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (apps) {
|
|
||||||
var appids = _.flatten(_.map(apps, 'applicationids'));
|
|
||||||
items = _.filter($scope.metric.itemList, function (itemObj) {
|
|
||||||
return _.intersection(appids, itemObj.applications).length;
|
|
||||||
});
|
|
||||||
items = _.filter(items, function (itemObj) {
|
|
||||||
return _.find(hosts, {'hostid': itemObj.hostid });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$scope.target.showDisabledItems) {
|
|
||||||
items = _.filter(items, {'status': '0'});
|
|
||||||
}
|
|
||||||
|
|
||||||
return items;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.onTargetPartChange = function (targetPart) {
|
$scope.onTargetPartChange = function (targetPart) {
|
||||||
@@ -236,21 +135,21 @@ define([
|
|||||||
|
|
||||||
// Handle group blur and filter hosts
|
// Handle group blur and filter hosts
|
||||||
$scope.onGroupBlur = function() {
|
$scope.onGroupBlur = function() {
|
||||||
$scope.metric.filteredHosts = $scope.filterHosts();
|
$scope.filterHosts();
|
||||||
$scope.parseTarget();
|
$scope.parseTarget();
|
||||||
$scope.get_data();
|
$scope.get_data();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle host blur and filter applications
|
// Handle host blur and filter applications
|
||||||
$scope.onHostBlur = function() {
|
$scope.onHostBlur = function() {
|
||||||
$scope.metric.filteredApplications = $scope.filterApplications();
|
$scope.filterApplications();
|
||||||
$scope.parseTarget();
|
$scope.parseTarget();
|
||||||
$scope.get_data();
|
$scope.get_data();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle application blur and filter items
|
// Handle application blur and filter items
|
||||||
$scope.onApplicationBlur = function() {
|
$scope.onApplicationBlur = function() {
|
||||||
$scope.metric.filteredItems = $scope.filterItems();
|
$scope.filterItems();
|
||||||
$scope.parseTarget();
|
$scope.parseTarget();
|
||||||
$scope.get_data();
|
$scope.get_data();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ function (angular, _, utils) {
|
|||||||
this.filterHosts = function(groupFilter) {
|
this.filterHosts = function(groupFilter) {
|
||||||
var groups = [];
|
var groups = [];
|
||||||
var hosts = [];
|
var hosts = [];
|
||||||
var groupList = self.cache.getGroups();
|
|
||||||
|
|
||||||
|
return self.cache.getGroups().then(function(groupList) {
|
||||||
// Filter groups by regex
|
// Filter groups by regex
|
||||||
if (utils.isRegex(groupFilter)) {
|
if (utils.isRegex(groupFilter)) {
|
||||||
var filterPattern = utils.buildRegex(groupFilter);
|
var filterPattern = utils.buildRegex(groupFilter);
|
||||||
@@ -52,17 +52,29 @@ function (angular, _, utils) {
|
|||||||
|
|
||||||
if (groups) {
|
if (groups) {
|
||||||
var groupids = _.map(groups, 'groupid');
|
var groupids = _.map(groups, 'groupid');
|
||||||
hosts = _.filter(self.cache.getHosts(), function (hostObj) {
|
return self.cache.getHosts().then(function(hosts) {
|
||||||
|
return _.filter(hosts, function (hostObj) {
|
||||||
return _.intersection(groupids, hostObj.groups).length;
|
return _.intersection(groupids, hostObj.groups).length;
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
} else {
|
||||||
return hosts;
|
return hosts;
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.filterApplications = function(hostFilter) {
|
this.filterApplications = function(hostFilter) {
|
||||||
var hosts = [];
|
var hosts = [];
|
||||||
var apps = [];
|
var apps = [];
|
||||||
var hostList = this.cache.getHosts();
|
|
||||||
|
var promises = [
|
||||||
|
this.cache.getHosts(),
|
||||||
|
this.cache.getApplications()
|
||||||
|
];
|
||||||
|
|
||||||
|
return $q.all(promises).then(function(results) {
|
||||||
|
var hostList = results[0];
|
||||||
|
var applicationList = results[1];
|
||||||
|
|
||||||
// Filter hosts by regex
|
// Filter hosts by regex
|
||||||
if (utils.isRegex(hostFilter)) {
|
if (utils.isRegex(hostFilter)) {
|
||||||
@@ -83,20 +95,29 @@ function (angular, _, utils) {
|
|||||||
|
|
||||||
if (hosts) {
|
if (hosts) {
|
||||||
var hostsids = _.map(hosts, 'hostid');
|
var hostsids = _.map(hosts, 'hostid');
|
||||||
apps = _.filter(this.cache.getApplications(), function (appObj) {
|
apps = _.filter(applicationList, function (appObj) {
|
||||||
return _.intersection(hostsids, appObj.hosts).length;
|
return _.intersection(hostsids, appObj.hosts).length;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return apps;
|
return apps;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.filterItems = function (hostFilter, appFilter, showDisabledItems) {
|
this.filterItems = function (hostFilter, appFilter, showDisabledItems) {
|
||||||
var hosts = [];
|
var hosts = [];
|
||||||
var apps = [];
|
var apps = [];
|
||||||
var items = [];
|
var items = [];
|
||||||
var hostList = this.cache.getHosts();
|
|
||||||
var applicationList = this.cache.getApplications();
|
var promises = [
|
||||||
|
this.cache.getHosts(),
|
||||||
|
this.cache.getApplications(),
|
||||||
|
this.cache.getItems()
|
||||||
|
];
|
||||||
|
|
||||||
|
return $q.all(promises).then(function(results) {
|
||||||
|
var hostList = results[0];
|
||||||
|
var applicationList = results[1];
|
||||||
|
var cachedItems = results[2];
|
||||||
|
|
||||||
// Filter hosts by regex
|
// Filter hosts by regex
|
||||||
if (utils.isRegex(hostFilter)) {
|
if (utils.isRegex(hostFilter)) {
|
||||||
@@ -132,7 +153,7 @@ function (angular, _, utils) {
|
|||||||
} else {
|
} else {
|
||||||
apps = undefined;
|
apps = undefined;
|
||||||
if (hosts) {
|
if (hosts) {
|
||||||
items = _.filter(this.cache.getItems(), function (itemObj) {
|
items = _.filter(cachedItems, function (itemObj) {
|
||||||
return _.find(hosts, {'hostid': itemObj.hostid });
|
return _.find(hosts, {'hostid': itemObj.hostid });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -140,7 +161,7 @@ function (angular, _, utils) {
|
|||||||
|
|
||||||
if (apps) {
|
if (apps) {
|
||||||
var appids = _.flatten(_.map(apps, 'applicationids'));
|
var appids = _.flatten(_.map(apps, 'applicationids'));
|
||||||
items = _.filter(this.cache.getItems(), function (itemObj) {
|
items = _.filter(cachedItems, function (itemObj) {
|
||||||
return _.intersection(appids, itemObj.applications).length;
|
return _.intersection(appids, itemObj.applications).length;
|
||||||
});
|
});
|
||||||
items = _.filter(items, function (itemObj) {
|
items = _.filter(items, function (itemObj) {
|
||||||
@@ -153,6 +174,7 @@ function (angular, _, utils) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -165,17 +187,29 @@ function (angular, _, utils) {
|
|||||||
var hosts = [];
|
var hosts = [];
|
||||||
var apps = [];
|
var apps = [];
|
||||||
var items = [];
|
var items = [];
|
||||||
|
var promises = [
|
||||||
|
this.cache.getGroups(),
|
||||||
|
this.cache.getHosts(),
|
||||||
|
this.cache.getApplications(),
|
||||||
|
this.cache.getItems()
|
||||||
|
];
|
||||||
|
|
||||||
|
return $q.all(promises).then(function(results) {
|
||||||
|
var cachedGroups = results[0];
|
||||||
|
var cachedHosts = results[1];
|
||||||
|
var cachedApps = results[2];
|
||||||
|
var cachedItems = results[3];
|
||||||
|
|
||||||
if (utils.isRegex(hostFilter)) {
|
if (utils.isRegex(hostFilter)) {
|
||||||
|
|
||||||
// Filter groups
|
// Filter groups
|
||||||
if (utils.isRegex(groupFilter)) {
|
if (utils.isRegex(groupFilter)) {
|
||||||
var groupPattern = utils.buildRegex(groupFilter);
|
var groupPattern = utils.buildRegex(groupFilter);
|
||||||
groups = _.filter(this.cache.getGroups(), function (groupObj) {
|
groups = _.filter(cachedGroups, function (groupObj) {
|
||||||
return groupPattern.test(groupObj.name);
|
return groupPattern.test(groupObj.name);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter});
|
var findedGroup = _.find(cachedGroups, {'name': groupFilter});
|
||||||
if (findedGroup) {
|
if (findedGroup) {
|
||||||
groups.push(findedGroup);
|
groups.push(findedGroup);
|
||||||
} else {
|
} else {
|
||||||
@@ -184,7 +218,7 @@ function (angular, _, utils) {
|
|||||||
}
|
}
|
||||||
if (groups) {
|
if (groups) {
|
||||||
var groupids = _.map(groups, 'groupid');
|
var groupids = _.map(groups, 'groupid');
|
||||||
hosts = _.filter(this.cache.getHosts(), function (hostObj) {
|
hosts = _.filter(cachedHosts, function (hostObj) {
|
||||||
return _.intersection(groupids, hostObj.groups).length;
|
return _.intersection(groupids, hostObj.groups).length;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -198,7 +232,7 @@ function (angular, _, utils) {
|
|||||||
return hostPattern.test(hostObj.name);
|
return hostPattern.test(hostObj.name);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter});
|
var findedHost = _.find(cachedHosts, {'name': hostFilter});
|
||||||
if (findedHost) {
|
if (findedHost) {
|
||||||
hosts.push(findedHost);
|
hosts.push(findedHost);
|
||||||
} else {
|
} else {
|
||||||
@@ -208,7 +242,7 @@ function (angular, _, utils) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find items belongs to selected hosts
|
// Find items belongs to selected hosts
|
||||||
items = _.filter(this.cache.getItems(), function (itemObj) {
|
items = _.filter(cachedItems, function (itemObj) {
|
||||||
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -217,7 +251,7 @@ function (angular, _, utils) {
|
|||||||
// Filter applications
|
// Filter applications
|
||||||
if (utils.isRegex(appFilter)) {
|
if (utils.isRegex(appFilter)) {
|
||||||
var appPattern = utils.buildRegex(appFilter);
|
var appPattern = utils.buildRegex(appFilter);
|
||||||
apps = _.filter(this.cache.getApplications(), function (appObj) {
|
apps = _.filter(cachedApps, function (appObj) {
|
||||||
return appPattern.test(appObj.name);
|
return appPattern.test(appObj.name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -226,7 +260,7 @@ function (angular, _, utils) {
|
|||||||
apps = undefined;
|
apps = undefined;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var findedApp = _.find(this.cache.getApplications(), {'name': appFilter});
|
var findedApp = _.find(cachedApps, {'name': appFilter});
|
||||||
if (findedApp) {
|
if (findedApp) {
|
||||||
apps.push(findedApp);
|
apps.push(findedApp);
|
||||||
} else {
|
} else {
|
||||||
@@ -266,6 +300,7 @@ function (angular, _, utils) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,6 +8,11 @@ function (angular, _) {
|
|||||||
|
|
||||||
var module = angular.module('grafana.services');
|
var module = angular.module('grafana.services');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zabbix API Wrapper.
|
||||||
|
* Creates Zabbix API instance with given parameters (url, credentials and other).
|
||||||
|
* Wraps API calls and provides high-level methods.
|
||||||
|
*/
|
||||||
module.factory('ZabbixAPI', function($q, backendSrv, ZabbixAPIService) {
|
module.factory('ZabbixAPI', function($q, backendSrv, ZabbixAPIService) {
|
||||||
|
|
||||||
// Initialize Zabbix API.
|
// Initialize Zabbix API.
|
||||||
@@ -15,12 +20,14 @@ function (angular, _) {
|
|||||||
this.url = api_url;
|
this.url = api_url;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.auth = null;
|
this.auth = "";
|
||||||
|
|
||||||
this.requestOptions = {
|
this.requestOptions = {
|
||||||
basicAuth: basicAuth,
|
basicAuth: basicAuth,
|
||||||
withCredentials: withCredentials
|
withCredentials: withCredentials
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.loginPromise = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = ZabbixAPI.prototype;
|
var p = ZabbixAPI.prototype;
|
||||||
@@ -31,31 +38,49 @@ function (angular, _) {
|
|||||||
|
|
||||||
p.request = function(method, params) {
|
p.request = function(method, params) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if (this.auth) {
|
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
|
||||||
return ZabbixAPIService._request(this.url, method, params, this.requestOptions, this.auth)
|
|
||||||
.then(function(result) {
|
.then(function(result) {
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
function(error) {
|
function(error) {
|
||||||
if (error.message === "Session terminated, re-login, please.") {
|
if (isAuthError(error.data)) {
|
||||||
return ZabbixAPIService.login(self.url, self.username, self.password, self.requestOptions)
|
return self.loginOnce().then(function() {
|
||||||
.then(function(auth) {
|
return self.request(method, params);
|
||||||
self.auth = auth;
|
|
||||||
return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
function isAuthError(message) {
|
||||||
|
return (
|
||||||
|
message === "Session terminated, re-login, please." ||
|
||||||
|
message === "Not authorised." ||
|
||||||
|
message === "Not authorized."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When API unauthenticated or auth token expired each request produce login()
|
||||||
|
* call. But auth token is common to all requests. This function wraps login() method
|
||||||
|
* and call it once. If login() already called just wait for it (return its promise).
|
||||||
|
* @return login promise
|
||||||
|
*/
|
||||||
|
p.loginOnce = function() {
|
||||||
|
var self = this;
|
||||||
|
var deferred = $q.defer();
|
||||||
|
if (!self.loginPromise) {
|
||||||
|
self.loginPromise = deferred.promise;
|
||||||
|
self.login().then(function(auth) {
|
||||||
|
self.loginPromise = null;
|
||||||
|
self.auth = auth;
|
||||||
|
deferred.resolve(auth);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
|
return self.loginPromise;
|
||||||
// Login first
|
|
||||||
return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions)
|
|
||||||
.then(function(auth) {
|
|
||||||
self.auth = auth;
|
|
||||||
return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ function (angular) {
|
|||||||
* Request data from Zabbix API
|
* Request data from Zabbix API
|
||||||
* @return {object} response.result
|
* @return {object} response.result
|
||||||
*/
|
*/
|
||||||
this._request = function(api_url, method, params, options, auth) {
|
this.request = function(api_url, method, params, options, auth) {
|
||||||
|
var deferred = $q.defer();
|
||||||
var requestData = {
|
var requestData = {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: method,
|
method: method,
|
||||||
@@ -24,8 +25,12 @@ function (angular) {
|
|||||||
id: 1
|
id: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (auth === "") {
|
||||||
|
// Reject immediately if not authenticated
|
||||||
|
deferred.reject({data: "Not authorised."});
|
||||||
|
return deferred.promise;
|
||||||
|
} else if (auth) {
|
||||||
// Set auth parameter only if it needed
|
// Set auth parameter only if it needed
|
||||||
if (auth) {
|
|
||||||
requestData.auth = auth;
|
requestData.auth = auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,19 +51,20 @@ function (angular) {
|
|||||||
requestOptions.headers.Authorization = options.basicAuth;
|
requestOptions.headers.Authorization = options.basicAuth;
|
||||||
}
|
}
|
||||||
|
|
||||||
return backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
||||||
// General connection issues
|
// General connection issues
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
return [];
|
deferred.reject(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Zabbix API errors
|
// Handle Zabbix API errors
|
||||||
else if (response.data.error) {
|
else if (response.data.error) {
|
||||||
throw new ZabbixException(response.data.error);
|
deferred.reject(response.data.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.data.result;
|
deferred.resolve(response.data.result);
|
||||||
});
|
});
|
||||||
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,7 +76,7 @@ function (angular) {
|
|||||||
user: username,
|
user: username,
|
||||||
password: password
|
password: password
|
||||||
};
|
};
|
||||||
return this._request(api_url, 'user.login', params, options, null);
|
return this.request(api_url, 'user.login', params, options, null);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,7 +84,7 @@ function (angular) {
|
|||||||
* Matches the version of Zabbix starting from Zabbix 2.0.4
|
* Matches the version of Zabbix starting from Zabbix 2.0.4
|
||||||
*/
|
*/
|
||||||
this.getVersion = function(api_url, options) {
|
this.getVersion = function(api_url, options) {
|
||||||
return this._request(api_url, 'apiinfo.version', [], options);
|
return this.request(api_url, 'apiinfo.version', [], options);
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ function (angular, _, utils) {
|
|||||||
|
|
||||||
// Use factory() instead service() for multiple datasources support.
|
// Use factory() instead service() for multiple datasources support.
|
||||||
// Each datasource instance must initialize its own cache.
|
// Each datasource instance must initialize its own cache.
|
||||||
module.factory('ZabbixCache', function($q) {
|
module.factory('ZabbixCachingProxy', function($q) {
|
||||||
|
|
||||||
function ZabbixCache(zabbixAPI, ttl) {
|
function ZabbixCachingProxy(zabbixAPI, ttl) {
|
||||||
this.zabbixAPI = zabbixAPI;
|
this.zabbixAPI = zabbixAPI;
|
||||||
this.ttl = ttl;
|
this.ttl = ttl;
|
||||||
|
|
||||||
@@ -21,14 +21,23 @@ function (angular, _, utils) {
|
|||||||
this._hosts = undefined;
|
this._hosts = undefined;
|
||||||
this._applications = undefined;
|
this._applications = undefined;
|
||||||
this._items = undefined;
|
this._items = undefined;
|
||||||
|
this.storage = {
|
||||||
|
history: {},
|
||||||
|
trends: {}
|
||||||
|
};
|
||||||
|
|
||||||
// Check is a service initialized or not
|
// Check is a service initialized or not
|
||||||
this._initialized = undefined;
|
this._initialized = undefined;
|
||||||
|
|
||||||
|
this.refreshPromise = false;
|
||||||
|
|
||||||
|
// Wrap _refresh() method to call it once.
|
||||||
|
this.refresh = callOnce(p._refresh, this.refreshPromise);
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = ZabbixCache.prototype;
|
var p = ZabbixCachingProxy.prototype;
|
||||||
|
|
||||||
p.refresh = function () {
|
p._refresh = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
var promises = [
|
var promises = [
|
||||||
this.zabbixAPI.getGroups(),
|
this.zabbixAPI.getGroups(),
|
||||||
@@ -49,33 +58,107 @@ function (angular, _, utils) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
p.getGroups = function() {
|
p.getGroups = function() {
|
||||||
return this._groups;
|
var self = this;
|
||||||
|
if (this._groups) {
|
||||||
|
return $q.when(self._groups);
|
||||||
|
} else {
|
||||||
|
return this.refresh().then(function() {
|
||||||
|
return self._groups;
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
p.getHosts = function() {
|
p.getHosts = function() {
|
||||||
return this._hosts;
|
var self = this;
|
||||||
|
if (this._hosts) {
|
||||||
|
return $q.when(self._hosts);
|
||||||
|
} else {
|
||||||
|
return this.refresh().then(function() {
|
||||||
|
return self._hosts;
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
p.getApplications = function() {
|
p.getApplications = function() {
|
||||||
return this._applications;
|
var self = this;
|
||||||
|
if (this._applications) {
|
||||||
|
return $q.when(self._applications);
|
||||||
|
} else {
|
||||||
|
return this.refresh().then(function() {
|
||||||
|
return self._applications;
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
p.getItems = function(type) {
|
p.getItems = function(type) {
|
||||||
|
var self = this;
|
||||||
|
if (this._items) {
|
||||||
|
return $q.when(filterItems(self._items, type));
|
||||||
|
} else {
|
||||||
|
return this.refresh().then(function() {
|
||||||
|
return filterItems(self._items, type);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function filterItems(items, type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'num':
|
case 'num':
|
||||||
return _.filter(this._items, function(item) {
|
return _.filter(items, function(item) {
|
||||||
return (item.value_type === '0' ||
|
return (item.value_type === '0' ||
|
||||||
item.value_type === '3');
|
item.value_type === '3');
|
||||||
});
|
});
|
||||||
case 'text':
|
case 'text':
|
||||||
return _.filter(this._items, function(item) {
|
return _.filter(items, function(item) {
|
||||||
return (item.value_type === '1' ||
|
return (item.value_type === '1' ||
|
||||||
item.value_type === '2' ||
|
item.value_type === '2' ||
|
||||||
item.value_type === '4');
|
item.value_type === '4');
|
||||||
});
|
});
|
||||||
default:
|
default:
|
||||||
return this._items;
|
return items;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p.getHistory = function(items, time_from, time_till) {
|
||||||
|
var itemids = _.map(arguments[0], 'itemid');
|
||||||
|
var stamp = itemids.join() + arguments[1] + arguments[2];
|
||||||
|
//console.log(arguments, stamp);
|
||||||
|
return this.zabbixAPI.getHistory(items, time_from, time_till);
|
||||||
|
};
|
||||||
|
|
||||||
|
p.getHistory_ = function(items, time_from, time_till) {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
var historyStorage = this.storage.history;
|
||||||
|
var full_history;
|
||||||
|
var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) {
|
||||||
|
return !historyStorage[itemid];
|
||||||
|
});
|
||||||
|
if (expired.length) {
|
||||||
|
this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
|
||||||
|
var grouped_history = _.groupBy(history, 'itemid');
|
||||||
|
_.forEach(expired, function(item) {
|
||||||
|
var itemid = item.itemid;
|
||||||
|
historyStorage[itemid] = item;
|
||||||
|
historyStorage[itemid].time_from = time_from;
|
||||||
|
historyStorage[itemid].time_till = time_till;
|
||||||
|
historyStorage[itemid].history = grouped_history[itemid];
|
||||||
|
});
|
||||||
|
full_history = _.map(items, function(item) {
|
||||||
|
return historyStorage[item.itemid].history;
|
||||||
|
});
|
||||||
|
deferred.resolve(_.flatten(full_history, true));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
full_history = _.map(items, function(item) {
|
||||||
|
return historyStorage[item.itemid].history;
|
||||||
|
});
|
||||||
|
deferred.resolve(_.flatten(full_history, true));
|
||||||
|
}
|
||||||
|
return deferred.promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
p.getHistoryFromAPI = function(items, time_from, time_till) {
|
||||||
|
return this.zabbixAPI.getHistory(items, time_from, time_till);
|
||||||
};
|
};
|
||||||
|
|
||||||
p.getHost = function(hostid) {
|
p.getHost = function(hostid) {
|
||||||
@@ -126,7 +209,23 @@ function (angular, _, utils) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return ZabbixCache;
|
function callOnce(func, promiseKeeper) {
|
||||||
|
return function() {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
if (!promiseKeeper) {
|
||||||
|
promiseKeeper = deferred.promise;
|
||||||
|
func.apply(this, arguments).then(function(result) {
|
||||||
|
deferred.resolve(result);
|
||||||
|
promiseKeeper = null;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return promiseKeeper;
|
||||||
|
}
|
||||||
|
return deferred.promise;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return ZabbixCachingProxy;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user