Merge branch 'release-2.0.0' into develop

This commit is contained in:
Alexander Zobnin
2015-07-12 16:55:07 +03:00
6 changed files with 368 additions and 353 deletions

5
.gitignore vendored
View File

@@ -4,3 +4,8 @@
.idea/
*.bat
# Grafana linter config
.jshintrc
.jscs.json
.jsfmtrc

View File

@@ -1,16 +1,18 @@
'use strict';
define([
'angular',
'lodash',
'kbn',
'./zabbixAPIWrapper',
'./helperFunctions',
'./queryCtrl'
],
function (angular, _, kbn) {
'use strict';
//'use strict';
var module = angular.module('grafana.services');
module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv, zabbix) {
module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv, alertSrv, ZabbixAPI, zabbixHelperSrv) {
/**
* Datasource initialization. Calls when you refresh page, add
@@ -21,6 +23,8 @@ function (angular, _, kbn) {
function ZabbixAPIDatasource(datasource) {
this.name = datasource.name;
this.url = datasource.url;
this.basicAuth = datasource.basicAuth;
this.withCredentials = datasource.withCredentials;
// TODO: fix passing username and password from config.html
this.username = datasource.meta.username;
@@ -31,13 +35,12 @@ function (angular, _, kbn) {
this.trendsFrom = datasource.meta.trendsFrom || '7d';
// Limit metrics per panel for templated request
this.limitmetrics = datasource.meta.limitmetrics || 50;
this.limitmetrics = datasource.meta.limitmetrics || 100;
// Initialize Zabbix API
zabbix.init(this.url, this.username, this.password);
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
}
/**
* Calls for each panel in dashboard.
*
@@ -71,28 +74,27 @@ function (angular, _, kbn) {
// Extract zabbix groups, hosts and apps from string:
// "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN]
var groups = splitMetrics(groupname);
var hosts = splitMetrics(hostname);
var apps = splitMetrics(appname);
var groups = zabbixHelperSrv.splitMetrics(groupname);
var hosts = zabbixHelperSrv.splitMetrics(hostname);
var apps = zabbixHelperSrv.splitMetrics(appname);
// Remove hostnames from item names and then
// extract item names
// "hostname: itemname" --> "itemname"
var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g;
var itemnames = splitMetrics(itemname.replace(delete_hostname_pattern, ''));
var itemnames = zabbixHelperSrv.splitMetrics(itemname.replace(delete_hostname_pattern, ''));
// 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
if (target.host.visible_name == 'All') {
if (target.host.visible_name === 'All') {
if (target.hostFilter && _.every(items, _.identity.hosts)) {
// Use templated variables in filter
var pattern = templateSrv.replace(target.hostFilter);
var host_pattern = new RegExp(pattern);
var host_pattern = new RegExp(templateSrv.replace(target.hostFilter));
items = _.filter(items, function (item) {
return _.some(item.hosts, function (host) {
return host_pattern.test(host.name);
@@ -101,16 +103,15 @@ function (angular, _, kbn) {
}
}
if (itemnames == 'All') {
if (itemnames[0] === 'All') {
// Filter items by regex
if (target.itemFilter) {
// Use templated variables in filter
var pattern = templateSrv.replace(target.itemFilter);
var item_pattern = new RegExp(pattern);
var item_pattern = new RegExp(templateSrv.replace(target.itemFilter));
return _.filter(items, function (item) {
return item_pattern.test(zabbix.expandItemName(item));
return item_pattern.test(zabbixHelperSrv.expandItemName(item));
});
} else {
return items;
@@ -119,7 +120,7 @@ function (angular, _, kbn) {
// Filtering items
return _.filter(items, function (item) {
return _.contains(itemnames, zabbix.expandItemName(item));
return _.contains(itemnames, zabbixHelperSrv.expandItemName(item));
});
}
}).then(function (items) {
@@ -127,19 +128,22 @@ function (angular, _, kbn) {
// Don't perform query for high number of items
// to prevent Grafana slowdown
if (items.length > self.limitmetrics) {
var message = "Try to increase limitmetrics parameter in datasource config.<br>"
+ "Current limitmetrics value is " + self.limitmetrics;
alertSrv.set("Metrics limit exceeded", message, "warning", 10000);
return [];
} else {
items = _.flatten(items);
// Use alias only for single metric, otherwise use item names
var alias = items.length > 1 ? undefined : templateSrv.replace(target.alias);
var alias = target.item.name === 'All' || itemnames.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(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, 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(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale));
}
}
});
@@ -152,7 +156,7 @@ function (angular, _, kbn) {
// Series downsampling
if (timeseries.datapoints.length > options.maxDataPoints) {
var ms_interval = Math.floor((to - from) / options.maxDataPoints) * 1000;
timeseries.datapoints = downsampleSeries(timeseries.datapoints, to, ms_interval);
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval);
}
return timeseries;
});
@@ -160,92 +164,11 @@ function (angular, _, kbn) {
});
};
ZabbixAPIDatasource.prototype.handleTrendResponse = function(items, alias, scale, trends) {
// Group items and trends by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(trends, 'itemid');
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)),
datapoints: _.map(trends, function (p) {
// Value must be a number for properly work
var value = Number(p.value_avg);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
////////////////
// Templating //
////////////////
/**
* Convert Zabbix API data to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param {Array} history Array of Zabbix History
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
ZabbixAPIDatasource.prototype.handleHistoryResponse = function(items, alias, scale, history) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* ]
*/
// Group items and history by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(history, 'itemid');
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)),
datapoints: _.map(history, function (p) {
// Value must be a number for properly work
var value = Number(p.value);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/**
* For templated query.
* Find metrics from templated request.
*
* @param {string} query Query from Templating
@@ -261,18 +184,19 @@ function (angular, _, kbn) {
if (part[0] === '{') {
// Convert multiple mettrics to array
// "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
parts.push(splitMetrics(part));
parts.push(zabbixHelperSrv.splitMetrics(part));
} else {
parts.push(part);
}
});
var template = _.object(['group', 'host', 'app', 'item'], parts)
var template = _.object(['group', 'host', 'app', 'item'], parts);
// 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 = zabbixHelperSrv.expandItemName(item);
return {
text: itemname,
expandable: false
@@ -282,7 +206,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,
@@ -293,7 +217,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,
@@ -304,7 +228,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,
@@ -321,12 +245,10 @@ function (angular, _, kbn) {
}
};
/////////////////
// Annotations //
/////////////////
ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
var from = Math.ceil(kbn.parseDate(rangeUnparsed.from).getTime() / 1000);
var to = Math.ceil(kbn.parseDate(rangeUnparsed.to).getTime() / 1000);
@@ -341,7 +263,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');
@@ -358,11 +280,11 @@ 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) {
var formatted_acknowledges = formatAcknowledges(e.acknowledges);;
var formatted_acknowledges = zabbixHelperSrv.formatAcknowledges(e.acknowledges);
events.push({
annotation: annotation,
time: e.clock * 1000,
@@ -381,99 +303,3 @@ function (angular, _, kbn) {
return ZabbixAPIDatasource;
});
});
/**
* Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
*
* @param {string} metrics "{metric1,metcic2,...,metricN}"
* @return {Array} [metric1, metcic2,..., metricN]
*/
function splitMetrics(metrics) {
var remove_brackets_pattern = /^{|}$/g;
var metric_split_pattern = /,(?!\s)/g;
return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern)
}
/**
* Convert Date object to local time in format
* YYYY-MM-DD HH:mm:ss
*
* @param {Date} date Date object
* @return {string} formatted local time YYYY-MM-DD HH:mm:ss
*/
function getShortTime(date) {
var MM = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth();
var DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return date.getFullYear() + '-' + MM + '-' + DD + ' ' + HH + ':' + mm + ':' + ss;
}
/**
* Format acknowledges.
*
* @param {array} acknowledges array of Zabbix acknowledge objects
* @return {string} HTML-formatted table
*/
function formatAcknowledges(acknowledges) {
if (acknowledges.length) {
var formatted_acknowledges = '<br><br>Acknowledges:<br><table><tr><td><b>Time</b></td><td><b>User</b></td><td><b>Comments</b></td></tr>';
_.each(_.map(acknowledges, function (ack) {
var time = new Date(ack.clock * 1000);
return '<tr><td><i>' + getShortTime(time) + '</i></td><td>' + ack.alias + ' (' + ack.name+ ' ' + ack.surname + ')' + '</td><td>' + ack.message + '</td></tr>';
}), function (ack) {
formatted_acknowledges = formatted_acknowledges.concat(ack)
});
formatted_acknowledges = formatted_acknowledges.concat('</table>')
return formatted_acknowledges;
} else {
return '';
}
}
/**
* Downsample datapoints series
*
* @param {array} datapoints [[<value>, <unixtime>], ...]
* @param {integer} time_to Panel time to
* @param {integer} ms_interval Interval in milliseconds for grouping datapoints
* @return {array} [[<value>, <unixtime>], ...]
*/
function downsampleSeries(datapoints, time_to, ms_interval) {
var downsampledSeries = new Array();
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0;
var points_num = 0;
var value_avg = 0;
for (var i = datapoints.length - 1; i >= 0; i -= 1) {
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
}
else {
value_avg = points_num ? points_sum / points_num : 0;
downsampledSeries.push([value_avg, timeWindow.to]);
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
// Process point again
i++;
}
}
return downsampledSeries.reverse();
}

226
zabbix/helperFunctions.js Normal file
View File

@@ -0,0 +1,226 @@
define([
'angular',
'lodash'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
module.service('zabbixHelperSrv', function($q) {
var self = this;
/**
* Convert Zabbix API history.get response to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param {Array} history Array of Zabbix History
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.handleHistoryResponse = function(items, alias, scale, history) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* ]
*/
// Group items and history by itemid
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 : self.expandItemName(item)),
datapoints: _.map(history, function (p) {
// Value must be a number for properly work
var value = Number(p.value);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/**
* Convert Zabbix API trends.get response to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param {Array} trends Array of Zabbix Trends
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.handleTrendResponse = function (items, alias, scale, trends) {
// Group items and trends by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_trends = _.groupBy(trends, 'itemid');
var self = this;
return $q.when(_.map(grouped_trends, function (trends, itemid) {
var item = indexed_items[itemid];
var series = {
target: (item.hosts ? item.hosts[0].name+': ' : '')
+ (alias ? alias : self.expandItemName(item)),
datapoints: _.map(trends, function (p) {
// Value must be a number for properly work
var value = Number(p.value_avg);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/**
* Expand item parameters, for example:
* CPU $2 time ($3) --> CPU system time (avg1)
*
* @param item: zabbix api item object
* @return: expanded item name (string)
*/
this.expandItemName = function(item) {
var name = item.name;
var key = item.key_;
// extract params from key:
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
// replace item parameters
for (var i = key_params.length; i >= 1; i--) {
name = name.replace('$' + i, key_params[i - 1]);
}
return name;
};
/**
* Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
*
* @param {string} metrics "{metric1,metcic2,...,metricN}"
* @return {Array} [metric1, metcic2,..., metricN]
*/
this.splitMetrics = function(metrics) {
var remove_brackets_pattern = /^{|}$/g;
var metric_split_pattern = /,(?!\s)/g;
return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern);
};
/**
* Convert Date object to local time in format
* YYYY-MM-DD HH:mm:ss
*
* @param {Date} date Date object
* @return {string} formatted local time YYYY-MM-DD HH:mm:ss
*/
this.getShortTime = function(date) {
var MM = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth();
var DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return date.getFullYear() + '-' + MM + '-' + DD + ' ' + HH + ':' + mm + ':' + ss;
};
/**
* Format acknowledges.
*
* @param {array} acknowledges array of Zabbix acknowledge objects
* @return {string} HTML-formatted table
*/
this.formatAcknowledges = function(acknowledges) {
if (acknowledges.length) {
var formatted_acknowledges = '<br><br>Acknowledges:<br><table><tr><td><b>Time</b></td>'
+ '<td><b>User</b></td><td><b>Comments</b></td></tr>';
_.each(_.map(acknowledges, function (ack) {
var time = new Date(ack.clock * 1000);
return '<tr><td><i>' + self.getShortTime(time) + '</i></td><td>' + ack.alias
+ ' (' + ack.name + ' ' + ack.surname + ')' + '</td><td>' + ack.message + '</td></tr>';
}), function (ack) {
formatted_acknowledges = formatted_acknowledges.concat(ack);
});
formatted_acknowledges = formatted_acknowledges.concat('</table>');
return formatted_acknowledges;
} else {
return '';
}
};
/**
* Downsample datapoints series
*
* @param {array} datapoints [[<value>, <unixtime>], ...]
* @param {integer} time_to Panel time to
* @param {integer} ms_interval Interval in milliseconds for grouping datapoints
* @return {array} [[<value>, <unixtime>], ...]
*/
this.downsampleSeries = function(datapoints, time_to, ms_interval) {
var downsampledSeries = [];
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0;
var points_num = 0;
var value_avg = 0;
for (var i = datapoints.length - 1; i >= 0; i -= 1) {
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
}
else {
value_avg = points_num ? points_sum / points_num : 0;
downsampledSeries.push([value_avg, timeWindow.to]);
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
// Process point again
i++;
}
}
return downsampledSeries.reverse();
};
});
});

View File

@@ -19,7 +19,7 @@
"trends": false,
"trendsFrom": "7d",
"limitmetrics": 50,
"limitmetrics": 100,
"metrics": true,
"annotations": true

View File

@@ -1,7 +1,7 @@
define([
'angular',
'lodash',
'./zabbixAPIWrapper'
'./helperFunctions'
],
function (angular, _) {
'use strict';
@@ -9,7 +9,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, zabbixHelperSrv) {
$scope.init = function() {
$scope.targetLetters = targetLetters;
@@ -31,7 +31,6 @@ function (angular, _) {
$scope.target.errors = validateTarget($scope.target);
};
/**
* Take alias from item name by default
*/
@@ -39,8 +38,7 @@ function (angular, _) {
if (!$scope.target.alias && $scope.target.item) {
$scope.target.alias = $scope.target.item.name;
}
};
}
$scope.targetBlur = function() {
setItemAlias();
@@ -51,12 +49,11 @@ function (angular, _) {
}
};
/**
* Call when host group selected
*/
$scope.selectHostGroup = function() {
$scope.updateHostList()
$scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList();
@@ -67,7 +64,6 @@ function (angular, _) {
}
};
/**
* Call when host selected
*/
@@ -82,7 +78,6 @@ function (angular, _) {
}
};
/**
* Call when application selected
*/
@@ -96,7 +91,6 @@ function (angular, _) {
}
};
/**
* Call when item selected
*/
@@ -109,13 +103,11 @@ function (angular, _) {
}
};
$scope.duplicate = function() {
var clone = angular.copy($scope.target);
$scope.panel.targets.push(clone);
};
$scope.moveMetricQuery = function(fromIndex, toIndex) {
_.move($scope.panel.targets, fromIndex, toIndex);
};
@@ -124,7 +116,6 @@ function (angular, _) {
// SUGGESTION QUERIES
//////////////////////////////
/**
* Update list of host groups
*/
@@ -132,12 +123,11 @@ 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);
});
};
/**
* Update list of hosts
*/
@@ -145,13 +135,12 @@ function (angular, _) {
$scope.metric.hostList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.hostList);
var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
zabbix.hostFindQuery(groups).then(function (hosts) {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
$scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) {
$scope.metric.hostList = $scope.metric.hostList.concat(hosts);
});
};
/**
* Update list of host applications
*/
@@ -159,39 +148,37 @@ function (angular, _) {
$scope.metric.applicationList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.applicationList);
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) {
var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
$scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) {
apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
return {name: appname};
});
$scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
});
};
/**
* Update list of items
*/
$scope.updateItemList = function() {
$scope.metric.itemList = [{name: 'All'}];;
$scope.metric.itemList = [{name: 'All'}];
addTemplatedVariables($scope.metric.itemList);
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) {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
var apps = $scope.target.application ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined;
$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 zabbixHelperSrv.expandItemName(item);
}), function (item) {
return {name: zabbix.expandItemName(item)}
return {name: zabbixHelperSrv.expandItemName(item)};
});
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
});
};
/**
* Add templated variables to list of available metrics
*
@@ -202,10 +189,9 @@ function (angular, _) {
metricList.push({
name: '$' + variable.name,
templated: true
})
});
};
});
}
//////////////////////////////
// VALIDATION
@@ -213,24 +199,12 @@ function (angular, _) {
function validateTarget(target) {
var errs = {};
if (!target) {
errs = 'Not defined';
}
return errs;
}
});
});
/**
* Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
*
* @param {string} metrics "{metric1,metcic2,...,metricN}"
* @return {Array} [metric1, metcic2,..., metricN]
*/
function splitMetrics(metrics) {
var remove_brackets_pattern = /^{|}$/g;
var metric_split_pattern = /,(?!\s)/g;
return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern)
}

View File

@@ -7,23 +7,23 @@ 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, basicAuth, withCredentials) {
// Initialize API parameters.
this.url = api_url;
this.username = username;
this.password = password;
this.basicAuth = basicAuth;
this.withCredentials = withCredentials;
}
var p = ZabbixAPI.prototype;
//////////////////
// Core methods //
//////////////////
/**
* Request data from Zabbix API
*
@@ -31,7 +31,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: {
@@ -47,6 +47,13 @@ function (angular, _) {
}
};
if (this.basicAuth || this.withCredentials) {
options.withCredentials = true;
}
if (this.basicAuth) {
options.headers.Authorization = this.basicAuth;
}
var self = this;
return backendSrv.datasourceRequest(options).then(function (response) {
if (!response.data) {
@@ -56,9 +63,9 @@ function (angular, _) {
else if (response.data.error) {
// Handle auth errors
if (response.data.error.data == "Session terminated, re-login, please." ||
response.data.error.data == "Not authorised." ||
response.data.error.data == "Not authorized") {
if (response.data.error.data === "Session terminated, re-login, please." ||
response.data.error.data === "Not authorised." ||
response.data.error.data === "Not authorized") {
return self.performZabbixAPILogin().then(function (response) {
self.auth = response;
return self.performZabbixAPIRequest(method, params);
@@ -69,13 +76,12 @@ function (angular, _) {
});
};
/**
* Get authentication token.
*
* @return {string} auth token
*/
this.performZabbixAPILogin = function() {
p.performZabbixAPILogin = function() {
var options = {
url : this.url,
method : 'POST',
@@ -91,6 +97,14 @@ function (angular, _) {
},
};
if (this.basicAuth || this.withCredentials) {
options.withCredentials = true;
}
if (this.basicAuth) {
options.headers = options.headers || {};
options.headers.Authorization = this.basicAuth;
}
return backendSrv.datasourceRequest(options).then(function (result) {
if (!result.data) {
return null;
@@ -99,13 +113,10 @@ function (angular, _) {
});
};
/////////////////////////
// API method wrappers //
/////////////////////////
/**
* Perform history query from Zabbix API
*
@@ -114,7 +125,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');
@@ -141,7 +152,6 @@ function (angular, _) {
});
};
/**
* Perform trends query from Zabbix API
* Use trends api extension from ZBXNEXT-1193 patch.
@@ -151,7 +161,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');
@@ -178,13 +188,12 @@ function (angular, _) {
});
};
/**
* Get the list of host groups
*
* @return {array} array of Zabbix hostgroup objects
*/
this.performHostGroupSuggestQuery = function() {
p.performHostGroupSuggestQuery = function() {
var params = {
output: ['name'],
sortfield: 'name',
@@ -197,14 +206,13 @@ function (angular, _) {
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Get the list of hosts
*
* @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',
@@ -220,7 +228,6 @@ function (angular, _) {
return this.performZabbixAPIRequest('host.get', params);
};
/**
* Get the list of applications
*
@@ -228,7 +235,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'
@@ -243,7 +250,6 @@ function (angular, _) {
return this.performZabbixAPIRequest('application.get', params);
};
/**
* Items request
*
@@ -252,7 +258,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',
@@ -287,18 +293,17 @@ function (angular, _) {
return this.performZabbixAPIRequest('item.get', params);
};
/**
* Get groups by names
*
* @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']
};
if (group != '*') {
if (group[0] !== '*') {
params.filter = {
name: group
};
@@ -306,14 +311,13 @@ function (angular, _) {
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Search group by name.
*
* @param {string} group group name
* @return {array} groups
*/
this.searchGroup = function (group) {
p.searchGroup = function (group) {
var params = {
output: ['name'],
search: {
@@ -324,18 +328,17 @@ function (angular, _) {
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Get hosts by names
*
* @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']
};
if (hostnames != '*') {
if (hostnames[0] !== '*') {
params.filter = {
name: hostnames
};
@@ -343,26 +346,24 @@ function (angular, _) {
return this.performZabbixAPIRequest('host.get', params);
};
/**
* Get applications by names
*
* @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']
}
if (application != '*') {
};
if (application[0] !== '*') {
params.filter = {
name: application
};
};
}
return this.performZabbixAPIRequest('application.get', params);
};
/**
* Get items belongs to passed groups, hosts and
* applications
@@ -372,11 +373,11 @@ 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
if (hosts && hosts != '*') {
if (hosts && hosts[0] !== '*') {
promises.push(this.getHostByName(hosts));
}
// Get groupids from names
@@ -391,18 +392,21 @@ function (angular, _) {
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids;
var hostids;
var applicationids;
if (groups) {
var groupids = _.map(_.filter(results, function (object) {
groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts != '*') {
var hostids = _.map(_.filter(results, function (object) {
if (hosts && hosts[0] !== '*') {
hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
if (apps) {
var applicationids = _.map(_.filter(results, function (object) {
applicationids = _.map(_.filter(results, function (object) {
return object.applicationid;
}), 'applicationid');
}
@@ -411,7 +415,6 @@ function (angular, _) {
});
};
/**
* Find applications belongs to passed groups and hosts
*
@@ -419,11 +422,11 @@ 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
if (hosts && hosts != '*') {
if (hosts && hosts[0] !== '*') {
promises.push(this.getHostByName(hosts));
}
// Get groupids from names
@@ -434,13 +437,15 @@ function (angular, _) {
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids;
var hostids;
if (groups) {
var groupids = _.map(_.filter(results, function (object) {
groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts != '*') {
var hostids = _.map(_.filter(results, function (object) {
if (hosts && hosts[0] !== '*') {
hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
@@ -449,14 +454,13 @@ function (angular, _) {
});
};
/**
* Find hosts belongs to passed groups
*
* @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);
@@ -468,28 +472,8 @@ function (angular, _) {
});
};
/**
* Expand item parameters, for example:
* CPU $2 time ($3) --> CPU system time (avg1)
*
* @param item: zabbix api item object
* @return: expanded item name (string)
*/
this.expandItemName = function(item) {
var name = item.name;
var key = item.key_;
// extract params from key:
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
// replace item parameters
for (var i = key_params.length; i >= 1; i--) {
name = name.replace('$' + i, key_params[i - 1]);
};
return name;
}
return ZabbixAPI;
});
});