Merge branch 'query-editor' into grafana-3.0

Conflicts:
	plugins/datasource-zabbix/zabbixAPIWrapper.js
This commit is contained in:
Alexander Zobnin
2016-01-31 10:44:12 +03:00
15 changed files with 2183 additions and 1094 deletions

View File

@@ -0,0 +1,107 @@
define([
'angular',
'lodash',
'jquery',
'./metricFunctions'
],
function (angular, _, $, metricFunctions) {
'use strict';
angular
.module('grafana.directives')
.directive('addMetricFunction', function($compile) {
var inputTemplate = '<input type="text"'+
' class="tight-form-input input-medium tight-form-input"' +
' spellcheck="false" style="display:none"></input>';
var buttonTemplate = '<a class="tight-form-item tight-form-func dropdown-toggle"' +
' tabindex="1" gf-dropdown="functionMenu" data-toggle="dropdown">' +
'<i class="fa fa-plus"></i></a>';
return {
link: function($scope, elem) {
var categories = metricFunctions.getCategories();
var allFunctions = getAllFunctionNames(categories);
$scope.functionMenu = createFunctionDropDownMenu(categories);
var $input = $(inputTemplate);
var $button = $(buttonTemplate);
$input.appendTo(elem);
$button.appendTo(elem);
$input.attr('data-provide', 'typeahead');
$input.typeahead({
source: allFunctions,
minLength: 1,
items: 10,
updater: function (value) {
var funcDef = metricFunctions.getFuncDef(value);
if (!funcDef) {
// try find close match
value = value.toLowerCase();
funcDef = _.find(allFunctions, function(funcName) {
return funcName.toLowerCase().indexOf(value) === 0;
});
if (!funcDef) { return; }
}
$scope.$apply(function() {
$scope.addFunction(funcDef);
});
$input.trigger('blur');
return '';
}
});
$button.click(function() {
$button.hide();
$input.show();
$input.focus();
});
$input.keyup(function() {
elem.toggleClass('open', $input.val() === '');
});
$input.blur(function() {
// clicking the function dropdown menu wont
// work if you remove class at once
setTimeout(function() {
$input.val('');
$input.hide();
$button.show();
elem.removeClass('open');
}, 200);
});
$compile(elem.contents())($scope);
}
};
});
function getAllFunctionNames(categories) {
return _.reduce(categories, function(list, category) {
_.each(category, function(func) {
list.push(func.name);
});
return list;
}, []);
}
function createFunctionDropDownMenu(categories) {
return _.map(categories, function(list, category) {
return {
text: category,
submenu: _.map(list, function(value) {
return {
text: value.name,
click: "addFunction('" + value.name + "')",
};
})
};
});
}
});

View File

@@ -0,0 +1,247 @@
define([
'angular',
'lodash',
'moment',
'./utils'
],
function (angular, _, moment, utils) {
'use strict';
var module = angular.module('grafana.services');
module.service('DataProcessingService', function() {
var self = this;
/**
* Downsample datapoints series
*/
this.downsampleSeries = function(datapoints, time_to, ms_interval, func) {
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;
var frame = [];
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++;
frame.push(datapoints[i][0]);
}
else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_.max(frame), timeWindow.to]);
}
else if (func === "min") {
downsampledSeries.push([_.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
};
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
this.groupBy = function(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var min_timestamp = datapoints[0][1];
var frames = _.groupBy(datapoints, function(point) {
var group_time = Math.floor(point[1] / ms_interval) * ms_interval;
// Prevent points outside of time range
if (group_time < min_timestamp) {
group_time = min_timestamp;
}
return group_time;
});
// frame: { '<unixtime>': [[<value>, <unixtime>], ...] }
// return [{ '<unixtime>': <value> }, { '<unixtime>': <value> }, ...]
var grouped = _.mapValues(frames, function(frame) {
var points = _.map(frame, function(point) {
return point[0];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function(value, timestamp) {
return [Number(value), Number(timestamp)];
}));
};
this.sumSeries = function(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _.uniq(_.map(_.flatten(timeseries, true), function(point) {
return point[1];
}));
new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function(series) {
var timestamps = _.map(series, function(point) {
return point[1];
});
var new_points = _.map(_.difference(new_timestamps, timestamps), function(timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
};
function sortByTime(series) {
return _.sortBy(series, function(point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}
function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return (left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]));
}
}
function findNearestRight(series, point) {
var point_index = _.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0]) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0]) {
return series[i];
}
}
return nearestLeft;
}
this.AVERAGE = function(values) {
var sum = 0;
_.each(values, function(value) {
sum += value;
});
return sum / values.length;
};
this.MIN = function(values) {
return _.min(values);
};
this.MAX = function(values) {
return _.max(values);
};
this.MEDIAN = function(values) {
var sorted = _.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
};
this.setAlias = function(alias, timeseries) {
timeseries.target = alias;
return timeseries;
};
this.aggregationFunctions = {
avg: this.AVERAGE,
min: this.MIN,
max: this.MAX,
median: this.MEDIAN
};
this.groupByWrapper = function(interval, groupFunc, datapoints) {
var groupByCallback = self.aggregationFunctions[groupFunc];
return self.groupBy(interval, groupByCallback, datapoints);
};
this.aggregateWrapper = function(groupByCallback, interval, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
return self.groupBy(interval, groupByCallback, flattenedPoints);
};
this.metricFunctions = {
groupBy: this.groupByWrapper,
average: _.partial(this.aggregateWrapper, this.AVERAGE),
min: _.partial(this.aggregateWrapper, this.MIN),
max: _.partial(this.aggregateWrapper, this.MAX),
median: _.partial(this.aggregateWrapper, this.MEDIAN),
sumSeries: this.sumSeries,
setAlias: this.setAlias,
};
});
});

View File

@@ -2,18 +2,24 @@ define([
'angular',
'lodash',
'app/core/utils/datemath',
'./utils',
'./metricFunctions',
'./queryProcessor',
'./directives',
'./zabbixAPIWrapper',
'./zabbixAPI',
'./helperFunctions',
'./zabbixCacheSrv',
'./queryCtrl'
'./dataProcessingService',
'./zabbixCache',
'./queryCtrl',
'./addMetricFunction',
'./metricFunctionEditor'
],
function (angular, _, dateMath) {
function (angular, _, dateMath, utils, metricFunctions) {
'use strict';
/** @ngInject */
function ZabbixAPIDatasource(instanceSettings, $q, backendSrv, templateSrv, alertSrv,
ZabbixAPI, zabbixHelperSrv, ZabbixCache) {
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv,
ZabbixAPI, ZabbixCache, QueryProcessor, DataProcessingService) {
// General data source settings
this.name = instanceSettings.name;
@@ -35,48 +41,62 @@ function (angular, _, dateMath) {
// Initialize cache service
this.zabbixCache = new ZabbixCache(this.zabbixAPI);
// Initialize query builder
this.queryProcessor = new QueryProcessor(this.zabbixCache);
console.log(this.zabbixCache);
////////////////////////
// Datasource methods //
////////////////////////
/**
* Test connection to Zabbix API
*
* @return {object} Connection status and Zabbix API version
*/
this.testDatasource = function() {
var self = this;
return this.zabbixAPI.getZabbixAPIVersion().then(function (apiVersion) {
return self.zabbixAPI.performZabbixAPILogin().then(function (auth) {
return this.zabbixAPI.getVersion().then(function (version) {
return self.zabbixAPI.login().then(function (auth) {
if (auth) {
return {
status: "success",
title: "Success",
message: "Zabbix API version: " + apiVersion
message: "Zabbix API version: " + version
};
} else {
return {
status: "error",
title: "Invalid user name or password",
message: "Zabbix API version: " + apiVersion
message: "Zabbix API version: " + version
};
}
});
}, function(error) {
console.log(error);
return {
status: "error",
title: "Connection failed",
message: "Could not connect to " + error.config.url
message: error
};
});
},
function(error) {
console.log(error);
return {
status: "error",
title: "Connection failed",
message: "Could not connect to given url"
};
});
};
/**
* Calls for each panel in dashboard.
*
* @param {Object} options Query options. Contains time range, targets
* and other info.
*
* @return {Object} Grafana metrics object with timeseries data
* for each target.
* Query panel data. Calls for each panel in dashboard.
* @param {Object} options Contains time range, targets and other info.
* @return {Object} Grafana metrics object with timeseries data for each target.
*/
this.query = function(options) {
var self = this;
// get from & to in seconds
var from = Math.ceil(dateMath.parse(options.range.from) / 1000);
@@ -88,102 +108,83 @@ function (angular, _, dateMath) {
if (target.mode !== 1) {
// Don't show undefined and hidden targets
if (target.hide || !target.group || !target.host ||
!target.application || !target.item) {
// Don't request undefined and hidden targets
if (target.hide || !target.group ||
!target.host || !target.item) {
return [];
}
// Replace templated variables
var groupname = templateSrv.replace(target.group.name, options.scopedVars);
var hostname = templateSrv.replace(target.host.name, options.scopedVars);
var appname = templateSrv.replace(target.application.name, options.scopedVars);
var itemname = templateSrv.replace(target.item.name, options.scopedVars);
// Extract zabbix groups, hosts and apps from string:
// "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN]
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 = zabbixHelperSrv.splitMetrics(itemname.replace(delete_hostname_pattern, ''));
var self = this;
var groupFilter = templateSrv.replace(target.group.filter, options.scopedVars);
var hostFilter = templateSrv.replace(target.host.filter, options.scopedVars);
var appFilter = templateSrv.replace(target.application.filter, options.scopedVars);
var itemFilter = templateSrv.replace(target.item.filter, options.scopedVars);
// Query numeric data
if (!target.mode) {
if (!target.mode || target.mode === 0) {
// Find items by item names and perform queries
return this.zabbixAPI.itemFindQuery(groups, hosts, apps)
// Build query in asynchronous manner
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter)
.then(function(items) {
// Add hostname for items from multiple hosts
var addHostName = target.host.isRegex;
// Filter hosts by regex
if (target.host.visible_name === 'All') {
if (target.hostFilter && _.every(items, _.identity.hosts)) {
// Use templated variables in filter
var host_pattern = new RegExp(templateSrv.replace(target.hostFilter, options.scopedVars));
items = _.filter(items, function (item) {
return _.some(item.hosts, function (host) {
return host_pattern.test(host.name);
});
});
}
}
if (itemnames[0] === 'All') {
// Filter items by regex
if (target.itemFilter) {
// Use templated variables in filter
var item_pattern = new RegExp(templateSrv.replace(target.itemFilter, options.scopedVars));
return _.filter(items, function (item) {
return item_pattern.test(zabbixHelperSrv.expandItemName(item));
});
} else {
return items;
}
} else {
// Filtering items
return _.filter(items, function (item) {
return _.contains(itemnames, zabbixHelperSrv.expandItemName(item));
});
}
}).then(function (items) {
items = _.flatten(items);
// Use alias only for single metric, otherwise use item names
var alias = target.item.name === 'All' || itemnames.length > 1 ?
undefined : templateSrv.replace(target.alias, options.scopedVars);
var history;
var getHistory;
if ((from < useTrendsFrom) && self.trends) {
var points = target.downsampleFunction ? target.downsampleFunction.value : "avg";
history = self.zabbixAPI.getTrends(items, from, to)
.then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale, points));
// Use trends
var valueType = target.downsampleFunction ? target.downsampleFunction.value : "avg";
getHistory = self.zabbixAPI.getTrends(items, from, to).then(function(history) {
return self.queryProcessor.handleTrends(history, addHostName, valueType);
});
} else {
history = self.zabbixAPI.getHistory(items, from, to)
.then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale));
// Use history
getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) {
return self.queryProcessor.handleHistory(history, addHostName);
});
}
return history.then(function (timeseries) {
var timeseries_data = _.flatten(timeseries);
return _.map(timeseries_data, function (timeseries) {
return getHistory.then(function (timeseries_data) {
timeseries_data = _.map(timeseries_data, function (timeseries) {
// Series downsampling
if (timeseries.datapoints.length > options.maxDataPoints) {
var ms_interval = Math.floor((to - from) / options.maxDataPoints) * 1000;
var downsampleFunc = target.downsampleFunction ? target.downsampleFunction.value : "avg";
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval, downsampleFunc);
// Filter only transform functions
var transformFunctions = bindFunctionDefs(target.functions, 'Transform');
// Metric data processing
var dp = timeseries.datapoints;
for (var i = 0; i < transformFunctions.length; i++) {
dp = transformFunctions[i](dp);
}
timeseries.datapoints = dp;
return timeseries;
});
// Aggregations
var aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate');
var dp = _.map(timeseries_data, 'datapoints');
if (aggregationFunctions.length) {
for (var i = 0; i < aggregationFunctions.length; i++) {
dp = aggregationFunctions[i](dp);
}
var lastAgg = _.findLast(target.functions, function(func) {
return _.contains(
_.map(metricFunctions.getCategories()['Aggregate'], 'name'), func.def.name);
});
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
}
// Apply alias functions
var aliasFunctions = bindFunctionDefs(target.functions, 'Alias');
for (var j = 0; j < aliasFunctions.length; j++) {
_.each(timeseries_data, aliasFunctions[j]);
}
return timeseries_data;
});
});
}
@@ -236,12 +237,34 @@ function (angular, _, dateMath) {
}
}, this);
return $q.all(_.flatten(promises)).then(function (results) {
var timeseries_data = _.flatten(results);
return { data: timeseries_data };
// Data for panel (all targets)
return $q.all(_.flatten(promises))
.then(_.flatten)
.then(function (timeseries_data) {
// Series downsampling
var data = _.map(timeseries_data, function(timeseries) {
var DPS = DataProcessingService;
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = DPS.groupBy(options.interval, DPS.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
return { data: data };
});
};
function bindFunctionDefs(functionDefs, category) {
var aggregationFunctions = _.map(metricFunctions.getCategories()[category], 'name');
var aggFuncDefs = _.filter(functionDefs, function(func) {
return _.contains(aggregationFunctions, func.def.name);
});
return _.map(aggFuncDefs, function(func) {
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
return funcInstance.bindFunction(DataProcessingService.metricFunctions);
});
}
////////////////
// Templating //
////////////////
@@ -254,6 +277,8 @@ function (angular, _, dateMath) {
* of metrics in "{metric1,metcic2,...,metricN}" format.
*/
this.metricFindQuery = function (query) {
var metrics;
// Split query. Query structure:
// group.host.app.item
var parts = [];
@@ -271,49 +296,22 @@ function (angular, _, dateMath) {
// Get items
if (parts.length === 4) {
return this.zabbixAPI.itemFindQuery(template.group, template.host, template.app)
.then(function (result) {
return _.map(result, function (item) {
var itemname = zabbixHelperSrv.expandItemName(item);
return {
text: itemname,
expandable: false
};
});
});
var items = this.queryProcessor.filterItems(template.host, template.app, true);
metrics = _.map(items, formatMetric);
}
// Get applications
else if (parts.length === 3) {
return this.zabbixAPI.appFindQuery(template.host, template.group).then(function (result) {
return _.map(result, function (app) {
return {
text: app.name,
expandable: false
};
});
});
var apps = this.queryProcessor.filterApplications(template.host);
metrics = _.map(apps, formatMetric);
}
// Get hosts
else if (parts.length === 2) {
return this.zabbixAPI.hostFindQuery(template.group).then(function (result) {
return _.map(result, function (host) {
return {
text: host.name,
expandable: false
};
});
});
var hosts = this.queryProcessor.filterHosts(template.group);
metrics = _.map(hosts, formatMetric);
}
// Get groups
else if (parts.length === 1) {
return this.zabbixAPI.getGroupByName(template.group).then(function (result) {
return _.map(result, function (hostgroup) {
return {
text: hostgroup.name,
expandable: false
};
});
});
metrics = _.map(this.zabbixCache.getGroups(template.group), formatMetric);
}
// Return empty object for invalid request
else {
@@ -321,8 +319,17 @@ function (angular, _, dateMath) {
d.resolve([]);
return d.promise;
}
return $q.when(metrics);
};
function formatMetric(metricObj) {
return {
text: metricObj.name,
expandable: false
};
}
/////////////////
// Annotations //
/////////////////

View File

@@ -47,7 +47,7 @@ function (angular, _) {
return $q.when(_.map(grouped_history, function (history, itemid) {
var item = indexed_items[itemid];
return {
target: (item.hosts ? item.hosts[0].name+': ' : '')
target: (item.host ? item.host + ': ' : '')
+ (alias ? alias : self.expandItemName(item)),
datapoints: _.map(history, function (p) {

View File

@@ -0,0 +1,244 @@
define([
'angular',
'lodash',
'jquery',
],
function (angular, _, $) {
'use strict';
angular
.module('grafana.directives')
.directive('metricFunctionEditor', function($compile, templateSrv) {
var funcSpanTemplate = '<a ng-click="">{{func.def.name}}</a><span>(</span>';
var paramTemplate = '<input type="text" style="display:none"' +
' class="input-mini tight-form-func-param"></input>';
var funcControlsTemplate =
'<div class="tight-form-func-controls">' +
'<span class="pointer fa fa-arrow-left"></span>' +
'<span class="pointer fa fa-question-circle"></span>' +
'<span class="pointer fa fa-remove" ></span>' +
'<span class="pointer fa fa-arrow-right"></span>' +
'</div>';
return {
restrict: 'A',
link: function postLink($scope, elem) {
var $funcLink = $(funcSpanTemplate);
var $funcControls = $(funcControlsTemplate);
var func = $scope.func;
var funcDef = func.def;
var scheduledRelink = false;
var paramCountAtLink = 0;
function clickFuncParam(paramIndex) {
/*jshint validthis:true */
var $link = $(this);
var $input = $link.next();
$input.val(func.params[paramIndex]);
$input.css('width', ($link.width() + 16) + 'px');
$link.hide();
$input.show();
$input.focus();
$input.select();
var typeahead = $input.data('typeahead');
if (typeahead) {
$input.val('');
typeahead.lookup();
}
}
function scheduledRelinkIfNeeded() {
if (paramCountAtLink === func.params.length) {
return;
}
if (!scheduledRelink) {
scheduledRelink = true;
setTimeout(function() {
relink();
scheduledRelink = false;
}, 200);
}
}
function inputBlur(paramIndex) {
/*jshint validthis:true */
var $input = $(this);
var $link = $input.prev();
var newValue = $input.val();
if (newValue !== '' || func.def.params[paramIndex].optional) {
$link.html(templateSrv.highlightVariablesAsHtml(newValue));
func.updateParam($input.val(), paramIndex);
scheduledRelinkIfNeeded();
$scope.$apply($scope.targetChanged);
}
$input.hide();
$link.show();
}
function inputKeyPress(paramIndex, e) {
/*jshint validthis:true */
if(e.which === 13) {
inputBlur.call(this, paramIndex);
}
}
function inputKeyDown() {
/*jshint validthis:true */
this.style.width = (3 + this.value.length) * 8 + 'px';
}
function addTypeahead($input, paramIndex) {
$input.attr('data-provide', 'typeahead');
var options = funcDef.params[paramIndex].options;
if (funcDef.params[paramIndex].type === 'int') {
options = _.map(options, function(val) { return val.toString(); });
}
$input.typeahead({
source: options,
minLength: 0,
items: 20,
updater: function (value) {
setTimeout(function() {
inputBlur.call($input[0], paramIndex);
}, 0);
return value;
}
});
var typeahead = $input.data('typeahead');
typeahead.lookup = function () {
this.query = this.$element.val() || '';
return this.process(this.source);
};
}
function toggleFuncControls() {
var targetDiv = elem.closest('.tight-form');
if (elem.hasClass('show-function-controls')) {
elem.removeClass('show-function-controls');
targetDiv.removeClass('has-open-function');
$funcControls.hide();
return;
}
elem.addClass('show-function-controls');
targetDiv.addClass('has-open-function');
$funcControls.show();
}
function addElementsAndCompile() {
$funcControls.appendTo(elem);
$funcLink.appendTo(elem);
_.each(funcDef.params, function(param, index) {
if (param.optional && func.params.length <= index) {
return;
}
if (index > 0) {
$('<span>, </span>').appendTo(elem);
}
var paramValue = templateSrv.highlightVariablesAsHtml(func.params[index]);
var $paramLink = $('<a ng-click="" class="graphite-func-param-link">' + paramValue + '</a>');
var $input = $(paramTemplate);
paramCountAtLink++;
$paramLink.appendTo(elem);
$input.appendTo(elem);
$input.blur(_.partial(inputBlur, index));
$input.keyup(inputKeyDown);
$input.keypress(_.partial(inputKeyPress, index));
$paramLink.click(_.partial(clickFuncParam, index));
if (funcDef.params[index].options) {
addTypeahead($input, index);
}
});
$('<span>)</span>').appendTo(elem);
$compile(elem.contents())($scope);
}
function ifJustAddedFocusFistParam() {
if ($scope.func.added) {
$scope.func.added = false;
setTimeout(function() {
elem.find('.graphite-func-param-link').first().click();
}, 10);
}
}
function registerFuncControlsToggle() {
$funcLink.click(toggleFuncControls);
}
function registerFuncControlsActions() {
$funcControls.click(function(e) {
var $target = $(e.target);
if ($target.hasClass('fa-remove')) {
toggleFuncControls();
$scope.$apply(function() {
$scope.removeFunction($scope.func);
});
return;
}
if ($target.hasClass('fa-arrow-left')) {
$scope.$apply(function() {
_.move($scope.target.functions, $scope.$index, $scope.$index - 1);
$scope.targetChanged();
});
return;
}
if ($target.hasClass('fa-arrow-right')) {
$scope.$apply(function() {
_.move($scope.target.functions, $scope.$index, $scope.$index + 1);
$scope.targetChanged();
});
return;
}
if ($target.hasClass('fa-question-circle')) {
window.open("http://graphite.readthedocs.org/en/latest/functions.html#graphite.render.functions." + funcDef.name,'_blank');
return;
}
});
}
function relink() {
elem.children().remove();
addElementsAndCompile();
ifJustAddedFocusFistParam();
registerFuncControlsToggle();
registerFuncControlsActions();
}
relink();
}
};
});
});

View File

@@ -0,0 +1,204 @@
define([
'lodash',
'jquery'
],
function (_, $) {
'use strict';
var index = [];
var categories = {
Transform: [],
Aggregate: [],
Alias: []
};
function addFuncDef(funcDef) {
funcDef.params = funcDef.params || [];
funcDef.defaultParams = funcDef.defaultParams || [];
if (funcDef.category) {
categories[funcDef.category].push(funcDef);
}
index[funcDef.name] = funcDef;
index[funcDef.shortName || funcDef.name] = funcDef;
}
addFuncDef({
name: 'groupBy',
category: 'Transform',
params: [
{ name: 'interval', type: 'string'},
{ name: 'function', type: 'string', options: ['avg', 'min', 'max', 'median'] }
],
defaultParams: ['1m', 'avg'],
});
addFuncDef({
name: 'sumSeries',
category: 'Aggregate',
params: [],
defaultParams: [],
});
addFuncDef({
name: 'median',
category: 'Aggregate',
params: [
{ name: 'interval', type: 'string'}
],
defaultParams: ['1m'],
});
addFuncDef({
name: 'average',
category: 'Aggregate',
params: [
{ name: 'interval', type: 'string' }
],
defaultParams: ['1m'],
});
addFuncDef({
name: 'min',
category: 'Aggregate',
params: [
{ name: 'interval', type: 'string' }
],
defaultParams: ['1m'],
});
addFuncDef({
name: 'max',
category: 'Aggregate',
params: [
{ name: 'interval', type: 'string' }
],
defaultParams: ['1m'],
});
addFuncDef({
name: 'setAlias',
category: 'Alias',
params: [
{ name: 'alias', type: 'string'}
],
defaultParams: [],
});
_.each(categories, function(funcList, catName) {
categories[catName] = _.sortBy(funcList, 'name');
});
function FuncInstance(funcDef, params) {
this.def = funcDef;
if (params) {
this.params = params;
} else {
// Create with default params
this.params = [];
this.params = funcDef.defaultParams.slice(0);
}
this.updateText();
}
FuncInstance.prototype.bindFunction = function(metricFunctions) {
var func = metricFunctions[this.def.name];
if (func) {
// Bind function arguments
var bindedFunc = func;
for (var i = 0; i < this.params.length; i++) {
bindedFunc = _.partial(bindedFunc, this.params[i]);
}
return bindedFunc;
} else {
throw { message: 'Method not found ' + this.def.name };
}
};
FuncInstance.prototype.render = function(metricExp) {
var str = this.def.name + '(';
var parameters = _.map(this.params, function(value, index) {
var paramType = this.def.params[index].type;
if (paramType === 'int' || paramType === 'value_or_series' || paramType === 'boolean') {
return value;
}
else if (paramType === 'int_or_interval' && $.isNumeric(value)) {
return value;
}
return "'" + value + "'";
}, this);
if (metricExp) {
parameters.unshift(metricExp);
}
return str + parameters.join(', ') + ')';
};
FuncInstance.prototype._hasMultipleParamsInString = function(strValue, index) {
if (strValue.indexOf(',') === -1) {
return false;
}
return this.def.params[index + 1] && this.def.params[index + 1].optional;
};
FuncInstance.prototype.updateParam = function(strValue, index) {
// handle optional parameters
// if string contains ',' and next param is optional, split and update both
if (this._hasMultipleParamsInString(strValue, index)) {
_.each(strValue.split(','), function(partVal, idx) {
this.updateParam(partVal.trim(), idx);
}, this);
return;
}
if (strValue === '' && this.def.params[index].optional) {
this.params.splice(index, 1);
}
else {
this.params[index] = strValue;
}
this.updateText();
};
FuncInstance.prototype.updateText = function () {
if (this.params.length === 0) {
this.text = this.def.name + '()';
return;
}
var text = this.def.name + '(';
text += this.params.join(', ');
text += ')';
this.text = text;
};
return {
createFuncInstance: function(funcDef, params) {
if (_.isString(funcDef)) {
if (!index[funcDef]) {
throw { message: 'Method not found ' + name };
}
funcDef = index[funcDef];
}
return new FuncInstance(funcDef, params);
},
getFuncDef: function(name) {
return index[name];
},
getCategories: function() {
return categories;
}
};
});

View File

@@ -76,62 +76,52 @@
<ul class="tight-form-list" role="menu" ng-hide="target.mode == 1">
<!-- Alias -->
<li ng-hide="target.mode == 2">
<!-- <li>
<input type="text"
class="tight-form-input input-medium"
ng-model="target.alias"
spellcheck='false'
placeholder="Alias"
ng-blur="targetBlur()">
</li>
</li> -->
<!-- Select Host Group -->
<li class="tight-form-item input-small" style="width: 5em">Group</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectHostGroup()"
ng-model="target.group"
bs-tooltip="target.group.name.length > 25 ? target.group.name : ''"
ng-options="group.visible_name ? group.visible_name : group.name for group in metric.groupList track by group.name">
<option value="">-- Select host group --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="fa fa-warning"></i>
</a>
<input type="text"
ng-model="target.group.filter"
bs-typeahead="getGroupNames"
ng-change="onTargetPartChange(target.group)"
ng-blur="onGroupBlur()"
data-min-length=0
data-items=100
class="input-medium tight-form-input"
ng-style="target.group.style">
</li>
<!-- Select Host -->
<li class="tight-form-item input-small" style="width: 3em">Host</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectHost()"
ng-model="target.host"
bs-tooltip="target.host.name.length > 25 ? target.host.name : ''"
ng-options="host.visible_name ? host.visible_name : host.name for host in metric.hostList track by host.name">
<option value="">-- Select host --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
<!-- Host filter -->
<li class="tight-form-item" ng-hide="target.mode == 2">
Filter
<i class="fa fa-question-circle"
bs-tooltip="'Filtering hosts by regex. Select All in items and specify regex for host names.'"></i>
</li>
<li ng-hide="target.mode == 2">
<input type="text"
class="tight-form-input input-large"
ng-model="target.hostFilter"
spellcheck='false'
placeholder="Host filter (regex)"
ng-blur="targetBlur()">
ng-model="target.host.filter"
bs-typeahead="getHostNames"
ng-change="onTargetPartChange(target.host)"
ng-blur="onHostBlur()"
data-min-length=0
data-items=100
class="input-large tight-form-input"
ng-style="target.host.style">
</li>
<li class="tight-form-item" ng-hide="target.mode == 2">
Show disabled items&nbsp;
<editor-checkbox text=""
model="target.showDisabledItems"
change="onApplicationBlur()">
</editor-checkbox>
</li>
<!-- Downsampling function -->
<li class="tight-form-item input-medium" ng-hide="target.mode == 2">
<!-- <li class="tight-form-item input-medium" ng-hide="target.mode == 2">
Downsampling
</li>
<li ng-hide="target.mode == 2">
@@ -146,7 +136,7 @@
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
</li> -->
</ul>
<div class="clearfix"></div>
@@ -159,51 +149,40 @@
<!-- Select Application -->
<li class="tight-form-item input-small" style="width: 5em">Application</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectApplication()"
ng-model="target.application"
bs-tooltip="target.application.name.length > 15 ? target.application.name : ''"
ng-options="app.visible_name ? app.visible_name : app.name for app in metric.applicationList track by app.name">
<option value="">-- Select application --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
<input type="text"
ng-model="target.application.filter"
bs-typeahead="getApplicationNames"
ng-change="onTargetPartChange(target.application)"
ng-blur="onApplicationBlur()"
data-min-length=0
data-items=100
class="input-medium tight-form-input"
ng-style="target.application.style">
</li>
<!-- Select Item -->
<li class="tight-form-item input-small" style="width: 3em">Item</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectItem()"
ng-model="target.item"
bs-tooltip="target.item.name.length > 25 ? target.item.name : ''"
ng-options="item.name for item in metric.itemList track by item.name">
<option value="">-- Select item --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
<!-- Item filter -->
<li class="tight-form-item" ng-hide="target.mode == 2">
Filter
<i class="fa fa-question-circle"
bs-tooltip="'Filtering items by regex. Select All in items and specify regex for item names.'"></i>
</li>
<li ng-hide="target.mode == 2">
<input type="text"
class="tight-form-input input-large"
ng-model="target.itemFilter"
spellcheck='false'
placeholder="Item filter (regex)"
ng-blur="targetBlur()">
ng-model="target.item.filter"
bs-typeahead="getItemNames"
ng-change="onTargetPartChange(target.item)"
ng-blur="onItemBlur()"
data-min-length=0
data-items=100
class="input-large tight-form-input"
ng-style="target.item.style">
</li>
<li class="tight-form-item query-keyword">Options</li>
<li ng-repeat="func in target.functions">
<span metric-function-editor class="tight-form-item tight-form-func">
</span>
</li>
<li class="dropdown" add-metric-function>
</li>
<!-- Scale -->
<li class="tight-form-item" ng-hide="target.mode == 2">
<!-- <li class="tight-form-item" ng-hide="target.mode == 2">
Scale
<i class="fa fa-question-circle"
bs-tooltip="'Set a custom multiplier for series values, for example -1 to invert series'"></i>
@@ -215,12 +194,22 @@
spellcheck='false'
placeholder="1"
ng-blur="targetBlur()">
</li>
</li> -->
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form" ng-hide="target.mode == 1">
<div class="tight-form" ng-hide="target.mode == 1" ng-if="target.options">
<ul class="tight-form-list" role="menu">
<li class="tight-form-item" style="width: 44px">&nbsp</li>
<!-- Select Application -->
<li class="tight-form-item query-keyword" style="width: 5em">Options</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form" ng-show="target.mode == 2">
<ul class="tight-form-list" role="menu">
<li class="tight-form-item" style="width: 44px">&nbsp</li>
<!-- Text metric regex -->

View File

@@ -1,19 +1,42 @@
define([
'angular',
'lodash',
'./helperFunctions'
'./metricFunctions',
'./utils'
],
function (angular, _) {
function (angular, _, metricFunctions, Utils) {
'use strict';
var module = angular.module('grafana.controllers');
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, zabbixHelperSrv) {
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) {
var zabbixCache = $scope.datasource.zabbixCache;
$scope.init = function () {
$scope.targetLetters = targetLetters;
if (!$scope.target.mode || $scope.target.mode !== 1) {
$scope.metric = {};
// Load default values
var targetDefaults = {
mode: 0,
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
item: { filter: "" },
functions: [],
};
_.defaults($scope.target, targetDefaults);
// Create function instances from saved JSON
$scope.target.functions = _.map($scope.target.functions, function(func) {
return metricFunctions.createFuncInstance(func.def, func.params);
});
if ($scope.target.mode === 0 ||
$scope.target.mode === 2) {
$scope.downsampleFunctionList = [
{name: "avg", value: "avg"},
{name: "min", value: "min"},
@@ -24,22 +47,19 @@ define([
if (!$scope.target.downsampleFunction) {
$scope.target.downsampleFunction = $scope.downsampleFunctionList[0];
}
if (!$scope.metric) {
$scope.metric = {
hostGroupList: [],
hostList: [{name: '*', visible_name: 'All'}],
applicationList: [{name: '*', visible_name: 'All'}],
itemList: [{name: 'All'}]
};
// Load metrics from cache
if (zabbixCache._initialized) {
$scope.getMetricsFromCache();
$scope.initFilters();
//console.log("Cached", $scope.metric);
} else {
zabbixCache.refresh().then(function () {
$scope.getMetricsFromCache();
$scope.initFilters();
//console.log("From server", $scope.metric);
});
}
// Update host group, host, application and item lists
$scope.updateGroupList();
$scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList();
setItemAlias();
}
else if ($scope.target.mode === 1) {
$scope.slaPropertyList = [
@@ -52,8 +72,243 @@ define([
$scope.itserviceList = [{name: "test"}];
$scope.updateITServiceList();
}
};
$scope.target.errors = validateTarget($scope.target);
$scope.initFilters = function () {
$scope.metric.filteredHosts = $scope.filterHosts();
$scope.metric.filteredApplications = $scope.filterApplications();
$scope.metric.filteredItems = $scope.filterItems();
};
$scope.getMetricsFromCache = function () {
var item_type = $scope.editorModes[$scope.target.mode];
$scope.metric = {
groupList: zabbixCache.getGroups(),
hostList: zabbixCache.getHosts(),
applicationList: zabbixCache.getApplications(),
itemList: zabbixCache.getItems(item_type)
};
};
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _.uniq(_.map(scope.metric[metricList], 'name'));
}
// Map functions for bs-typeahead
$scope.getGroupNames = _.partial(getMetricNames, $scope, 'groupList');
$scope.getHostNames = _.partial(getMetricNames, $scope, 'filteredHosts');
$scope.getApplicationNames = _.partial(getMetricNames, $scope, 'filteredApplications');
$scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems');
$scope.filterHosts = function () {
var group = $scope.target.group;
var groups = [];
var 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 () {
var host = $scope.target.host;
var hosts = [];
var 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 () {
var app = $scope.target.application;
var host = $scope.target.host;
var hosts = [];
var apps = [];
var 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) {
var regexStyle = {'color': '#CCA300'};
targetPart.isRegex = Utils.isRegex(targetPart.filter);
targetPart.style = targetPart.isRegex ? regexStyle : {};
};
// Handle group blur and filter hosts
$scope.onGroupBlur = function() {
$scope.metric.filteredHosts = $scope.filterHosts();
$scope.parseTarget();
$scope.get_data();
};
// Handle host blur and filter applications
$scope.onHostBlur = function() {
$scope.metric.filteredApplications = $scope.filterApplications();
$scope.parseTarget();
$scope.get_data();
};
// Handle application blur and filter items
$scope.onApplicationBlur = function() {
$scope.metric.filteredItems = $scope.filterItems();
$scope.parseTarget();
$scope.get_data();
};
$scope.onItemBlur = function () {
$scope.parseTarget();
$scope.get_data();
};
$scope.parseTarget = function() {
// Parse target
};
$scope.targetChanged = function() {
//console.log($scope.target);
$scope.get_data();
};
// Validate target and set validation info
$scope.validateTarget = function () {};
$scope.addFunction = function(funcDef) {
var newFunc = metricFunctions.createFuncInstance(funcDef);
newFunc.added = true;
$scope.target.functions.push(newFunc);
$scope.moveAliasFuncLast();
if (newFunc.params.length && newFunc.added ||
newFunc.def.params.length === 0) {
$scope.targetChanged();
}
};
$scope.removeFunction = function(func) {
$scope.target.functions = _.without($scope.target.functions, func);
$scope.targetChanged();
};
$scope.moveAliasFuncLast = function() {
var aliasFunc = _.find($scope.target.functions, function(func) {
return func.def.name === 'alias' ||
func.def.name === 'aliasByNode' ||
func.def.name === 'aliasByMetric';
});
if (aliasFunc) {
$scope.target.functions = _.without($scope.target.functions, aliasFunc);
$scope.target.functions.push(aliasFunc);
}
};
$scope.functionChanged = function() {};
$scope.editorModes = {
0: 'num',
1: 'itservice',
2: 'text'
};
/**
@@ -61,6 +316,7 @@ define([
* Modes:
* 0 - items
* 1 - IT services
* 2 - Text metrics
*/
$scope.switchEditorMode = function (mode) {
$scope.target.mode = mode;
@@ -78,7 +334,6 @@ define([
$scope.targetBlur = function () {
setItemAlias();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
@@ -89,61 +344,6 @@ define([
* Call when IT service is selected.
*/
$scope.selectITService = function () {
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host group selected
*/
$scope.selectHostGroup = function () {
$scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host selected
*/
$scope.selectHost = function () {
$scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when application selected
*/
$scope.selectApplication = function () {
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when item selected
*/
$scope.selectItem = function () {
setItemAlias();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
@@ -159,10 +359,6 @@ define([
_.move($scope.panel.targets, fromIndex, toIndex);
};
//////////////////////////////
// SUGGESTION QUERIES
//////////////////////////////
/**
* Update list of IT services
*/
@@ -173,73 +369,6 @@ define([
});
};
/**
* Update list of host groups
*/
$scope.updateGroupList = function () {
$scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) {
$scope.metric.groupList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.groupList);
$scope.metric.groupList = $scope.metric.groupList.concat(groups);
});
};
/**
* Update list of hosts
*/
$scope.updateHostList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
if (groups) {
$scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) {
$scope.metric.hostList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.hostList);
$scope.metric.hostList = $scope.metric.hostList.concat(hosts);
});
}
};
/**
* Update list of host applications
*/
$scope.updateAppList = function () {
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;
if (groups && hosts) {
$scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) {
apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
return {name: appname};
});
$scope.metric.applicationList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.applicationList);
$scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
});
}
};
/**
* Update list of items
*/
$scope.updateItemList = function () {
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;
var itemtype = $scope.target.mode === 2 ? "text" : "numeric";
if (groups && hosts && apps) {
$scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps, itemtype).then(function (items) {
// Show only unique item names
var uniq_items = _.map(_.uniq(items, function (item) {
return zabbixHelperSrv.expandItemName(item);
}), function (item) {
return {name: zabbixHelperSrv.expandItemName(item)};
});
$scope.metric.itemList = [{name: 'All'}];
addTemplatedVariables($scope.metric.itemList);
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
});
}
};
/**
* Add templated variables to list of available metrics
*
@@ -254,18 +383,6 @@ define([
});
}
//////////////////////////////
// VALIDATION
//////////////////////////////
function validateTarget(target) {
var errs = {};
if (!target) {
errs = 'Not defined';
}
return errs;
}
$scope.init();
});

View File

@@ -0,0 +1,351 @@
define([
'angular',
'lodash',
'./utils'
],
function (angular, _, utils) {
'use strict';
var module = angular.module('grafana.services');
module.factory('QueryProcessor', function($q) {
function QueryProcessor(zabbixCacheInstance) {
var self = this;
this.cache = zabbixCacheInstance;
/**
* Build query in asynchronous manner
*/
this.build = function (groupFilter, hostFilter, appFilter, itemFilter) {
if (this.cache._initialized) {
return $q.when(self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter));
} else {
return this.cache.refresh().then(function() {
return self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter);
});
}
};
this.filterHosts = function(groupFilter) {
var groups = [];
var hosts = [];
var groupList = self.cache.getGroups();
// Filter groups by regex
if (utils.isRegex(groupFilter)) {
var filterPattern = utils.buildRegex(groupFilter);
groups = _.filter(groupList, function (groupObj) {
return filterPattern.test(groupObj.name);
});
}
// Find hosts in selected group
else {
var finded = _.find(groupList, {'name': groupFilter});
if (finded) {
groups.push(finded);
} else {
groups = undefined;
}
}
if (groups) {
var groupids = _.map(groups, 'groupid');
hosts = _.filter(self.cache.getHosts(), function (hostObj) {
return _.intersection(groupids, hostObj.groups).length;
});
}
return hosts;
};
this.filterApplications = function(hostFilter) {
var hosts = [];
var apps = [];
var hostList = this.cache.getHosts();
// Filter hosts by regex
if (utils.isRegex(hostFilter)) {
var filterPattern = utils.buildRegex(hostFilter);
hosts = _.filter(hostList, function (hostObj) {
return filterPattern.test(hostObj.name);
});
}
// Find applications in selected host
else {
var finded = _.find(hostList, {'name': hostFilter});
if (finded) {
hosts.push(finded);
} else {
hosts = undefined;
}
}
if (hosts) {
var hostsids = _.map(hosts, 'hostid');
apps = _.filter(this.cache.getApplications(), function (appObj) {
return _.intersection(hostsids, appObj.hosts).length;
});
}
return apps;
};
this.filterItems = function (hostFilter, appFilter, showDisabledItems) {
var hosts = [];
var apps = [];
var items = [];
var hostList = this.cache.getHosts();
var applicationList = this.cache.getApplications();
// Filter hosts by regex
if (utils.isRegex(hostFilter)) {
var hostFilterPattern = utils.buildRegex(hostFilter);
hosts = _.filter(hostList, function (hostObj) {
return hostFilterPattern.test(hostObj.name);
});
}
else {
var findedHosts = _.find(hostList, {'name': hostFilter});
if (findedHosts) {
hosts.push(findedHosts);
} else {
hosts = undefined;
}
}
// Filter applications by regex
if (utils.isRegex(appFilter)) {
var filterPattern = utils.buildRegex(appFilter);
apps = _.filter(applicationList, function (appObj) {
return filterPattern.test(appObj.name);
});
}
// Find items in selected application
else if (appFilter) {
var finded = _.find(applicationList, {'name': appFilter});
if (finded) {
apps.push(finded);
} else {
apps = undefined;
}
} else {
apps = undefined;
if (hosts) {
items = _.filter(this.cache.getItems(), function (itemObj) {
return _.find(hosts, {'hostid': itemObj.hostid });
});
}
}
if (apps) {
var appids = _.flatten(_.map(apps, 'applicationids'));
items = _.filter(this.cache.getItems(), function (itemObj) {
return _.intersection(appids, itemObj.applications).length;
});
items = _.filter(items, function (itemObj) {
return _.find(hosts, {'hostid': itemObj.hostid });
});
}
if (!showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
return items;
};
/**
* Build query - convert target filters to array of Zabbix items
*/
this.buildFromCache = function (groupFilter, hostFilter, appFilter, itemFilter) {
// Find items by item names and perform queries
var groups = [];
var hosts = [];
var apps = [];
var items = [];
if (utils.isRegex(hostFilter)) {
// Filter groups
if (utils.isRegex(groupFilter)) {
var groupPattern = utils.buildRegex(groupFilter);
groups = _.filter(this.cache.getGroups(), function (groupObj) {
return groupPattern.test(groupObj.name);
});
} else {
var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter});
if (findedGroup) {
groups.push(findedGroup);
} else {
groups = undefined;
}
}
if (groups) {
var groupids = _.map(groups, 'groupid');
hosts = _.filter(this.cache.getHosts(), function (hostObj) {
return _.intersection(groupids, hostObj.groups).length;
});
} else {
// No groups finded
return [];
}
// Filter hosts
var hostPattern = utils.buildRegex(hostFilter);
hosts = _.filter(hosts, function (hostObj) {
return hostPattern.test(hostObj.name);
});
} else {
var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter});
if (findedHost) {
hosts.push(findedHost);
} else {
// No hosts finded
return [];
}
}
// Find items belongs to selected hosts
items = _.filter(this.cache.getItems(), function (itemObj) {
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
});
if (utils.isRegex(itemFilter)) {
// Filter applications
if (utils.isRegex(appFilter)) {
var appPattern = utils.buildRegex(appFilter);
apps = _.filter(this.cache.getApplications(), function (appObj) {
return appPattern.test(appObj.name);
});
}
// Don't use application filter if it empty
else if (appFilter === "") {
apps = undefined;
}
else {
var findedApp = _.find(this.cache.getApplications(), {'name': appFilter});
if (findedApp) {
apps.push(findedApp);
} else {
// No applications finded
return [];
}
}
// Find items belongs to selected applications
if (apps) {
var appids = _.flatten(_.map(apps, 'applicationids'));
items = _.filter(items, function (itemObj) {
return _.intersection(appids, itemObj.applications).length;
});
}
if (items) {
var itemPattern = utils.buildRegex(itemFilter);
items = _.filter(items, function (itemObj) {
return itemPattern.test(itemObj.name);
});
} else {
// No items finded
return [];
}
} else {
items = _.filter(items, {'name': itemFilter});
if (!items.length) {
// No items finded
return [];
}
}
// Set host as host name for each item
items = _.each(items, function (itemObj) {
itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name;
});
return items;
};
/**
* Convert Zabbix API history.get response to Grafana format
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.convertHistory = function(history, addHostName, convertPointCallback) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }, ...
* ]
*/
// Group history by itemid
var grouped_history = _.groupBy(history, 'itemid');
return _.map(grouped_history, function(hist, itemid) {
var item = self.cache.getItem(itemid);
var alias = item.name;
if (addHostName) {
var host = self.cache.getHost(item.hostid);
alias = host.name + ": " + alias;
}
return {
target: alias,
datapoints: _.map(hist, convertPointCallback)
};
});
};
this.handleHistory = function(history, addHostName) {
return this.convertHistory(history, addHostName, convertHistoryPoint);
};
this.handleTrends = function(history, addHostName, valueType) {
var convertPointCallback = _.partial(convertTrendPoint, valueType);
return this.convertHistory(history, addHostName, convertPointCallback);
};
function convertHistoryPoint(point) {
// Value must be a number for properly work
return [
Number(point.value),
point.clock * 1000
];
}
function convertTrendPoint(valueType, point) {
var value;
switch (valueType) {
case "min":
value = point.value_min;
break;
case "max":
value = point.value_max;
break;
case "avg":
value = point.value_avg;
break;
default:
value = point.value_avg;
}
return [
Number(value),
point.clock * 1000
];
}
}
return QueryProcessor;
});
});

View File

@@ -0,0 +1,53 @@
define([
'lodash',
'moment'
],
function (_, moment) {
'use strict';
function Utils() {
/**
* Expand Zabbix item name
*
* @param {string} name item name, ie "CPU $2 time"
* @param {string} key item key, ie system.cpu.util[,system,avg1]
* @return {string} expanded name, ie "CPU system time"
*/
this.expandItemName = function(name, 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;
};
// Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
this.isRegex = function (str) {
return regexPattern.test(str);
};
this.buildRegex = function (str) {
var matches = str.match(regexPattern);
var pattern = matches[1];
var flags = matches[2] !== "" ? matches[2] : undefined;
return new RegExp(pattern, flags);
};
this.parseInterval = function(interval) {
var intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g;
var momentInterval = intervalPattern.exec(interval);
return moment.duration(Number(momentInterval[1]), momentInterval[2]).valueOf();
};
}
return new Utils();
});

View File

@@ -0,0 +1,258 @@
define([
'angular',
'lodash',
'./zabbixAPIService'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
module.factory('ZabbixAPI', function($q, backendSrv, ZabbixAPIService) {
// Initialize Zabbix API.
function ZabbixAPI(api_url, username, password, basicAuth, withCredentials) {
this.url = api_url;
this.username = username;
this.password = password;
this.auth = null;
this.requestOptions = {
basicAuth: basicAuth,
withCredentials: withCredentials
};
}
var p = ZabbixAPI.prototype;
//////////////////
// Core methods //
//////////////////
p.request = function(method, params) {
var self = this;
if (this.auth) {
return ZabbixAPIService._request(this.url, method, params, this.requestOptions, this.auth)
.then(function(result) {
return result;
},
// Handle errors
function(error) {
if (error.message === "Session terminated, re-login, please.") {
return ZabbixAPIService.login(self.url, self.username, self.password, self.requestOptions)
.then(function(auth) {
self.auth = auth;
return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth);
});
}
});
} else {
// 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);
});
}
};
/**
* Get authentication token.
*/
p.login = function() {
return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions);
};
/**
* Get Zabbix API version
*/
p.getVersion = function() {
return ZabbixAPIService.getVersion(this.url, this.requestOptions);
};
/////////////////
// API methods //
/////////////////
p.getGroups = function() {
var params = {
output: ['name'],
sortfield: 'name'
};
return this.request('hostgroup.get', params);
};
p.getHosts = function() {
var params = {
output: ['name', 'host'],
sortfield: 'name',
selectGroups: []
};
return this.request('host.get', params);
};
p.getApplications = function() {
var params = {
output: ['name'],
sortfield: 'name',
selectHosts: []
};
return this.request('application.get', params);
};
p.getItems = function() {
var params = {
output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'],
sortfield: 'name',
selectApplications: []
};
return this.request('item.get', params);
};
/**
* Perform history query from Zabbix API
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} time_from Time in seconds
* @param {Number} time_till Time in seconds
* @return {Array} Array of Zabbix history objects
*/
p.getHistory = function(items, time_from, time_till) {
var self = this;
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
history: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: time_from
};
// Relative queries (e.g. last hour) don't include an end time
if (time_till) {
params.time_till = time_till;
}
return self.request('history.get', params);
})).then(_.flatten);
};
/**
* Perform trends query from Zabbix API
* Use trends api extension from ZBXNEXT-1193 patch.
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} time_from Time in seconds
* @param {Number} time_till Time in seconds
* @return {Array} Array of Zabbix trend objects
*/
p.getTrends = function(items, time_from, time_till) {
var self = this;
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
trend: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: time_from
};
// Relative queries (e.g. last hour) don't include an end time
if (time_till) {
params.time_till = time_till;
}
return self.request('trend.get', params);
})).then(_.flatten);
};
p.getITService = function(/* optional */ serviceids) {
var params = {
output: 'extend',
serviceids: serviceids
};
return this.request('service.get', params);
};
p.getSLA = function(serviceids, from, to) {
var params = {
serviceids: serviceids,
intervals: [{
from: from,
to: to
}]
};
return this.request('service.getsla', params);
};
p.getTriggers = function(limit, sortfield, groupids, hostids, applicationids, name) {
var params = {
output: 'extend',
expandDescription: true,
expandData: true,
monitored: true,
//only_true: true,
filter: {
value: 1
},
search : {
description: name
},
searchWildcardsEnabled: false,
groupids: groupids,
hostids: hostids,
applicationids: applicationids,
limit: limit,
sortfield: 'lastchange',
sortorder: 'DESC'
};
if (sortfield) {
params.sortfield = sortfield;
}
return this.request('trigger.get', params);
};
p.getAcknowledges = function(triggerids, from) {
var params = {
output: 'extend',
objectids: triggerids,
acknowledged: true,
select_acknowledges: 'extend',
sortfield: 'clock',
sortorder: 'DESC',
time_from: from
};
return this.request('event.get', params)
.then(function (events) {
return _.flatten(_.map(events, 'acknowledges'));
});
};
return ZabbixAPI;
});
});

View File

@@ -0,0 +1,97 @@
/**
* General Zabbix API methods
*/
define([
'angular',
],
function (angular) {
'use strict';
var module = angular.module('grafana.services');
module.service('ZabbixAPIService', function($q, backendSrv) {
/**
* Request data from Zabbix API
* @return {object} response.result
*/
this._request = function(api_url, method, params, options, auth) {
var requestData = {
jsonrpc: '2.0',
method: method,
params: params,
id: 1
};
// Set auth parameter only if it needed
if (auth) {
requestData.auth = auth;
}
var requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: api_url,
data: requestData
};
// Set request options for basic auth
if (options.basicAuth || options.withCredentials) {
requestOptions.withCredentials = true;
}
if (options.basicAuth) {
requestOptions.headers.Authorization = options.basicAuth;
}
return backendSrv.datasourceRequest(requestOptions).then(function (response) {
// General connection issues
if (!response.data) {
return [];
}
// Handle Zabbix API errors
else if (response.data.error) {
throw new ZabbixException(response.data.error);
}
return response.data.result;
});
};
/**
* Get authentication token.
* @return {string} auth token
*/
this.login = function(api_url, username, password, options) {
var params = {
user: username,
password: password
};
return this._request(api_url, 'user.login', params, options, null);
};
/**
* Get Zabbix API version
* Matches the version of Zabbix starting from Zabbix 2.0.4
*/
this.getVersion = function(api_url, options) {
return this._request(api_url, 'apiinfo.version', [], options);
};
});
// Define zabbix API exception type
function ZabbixException(error) {
this.code = error.code;
this.errorType = error.message;
this.message = error.data;
}
ZabbixException.prototype.toString = function() {
return this.errorType + ": " + this.message;
};
});

View File

@@ -1,629 +0,0 @@
define([
'angular',
'lodash'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
module.factory('ZabbixAPI', function($q, backendSrv) {
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
*
* @param {string} method Zabbix API method name
* @param {object} params method params
* @return {object} data.result field or []
*/
p.performZabbixAPIRequest = function(method, params) {
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: this.url,
data: {
jsonrpc: '2.0',
method: method,
params: params,
auth: this.auth,
id: 1
}
};
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) {
return [];
}
// Handle Zabbix API errors
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") {
return self.performZabbixAPILogin().then(function (response) {
self.auth = response;
return self.performZabbixAPIRequest(method, params);
});
}
}
return response.data.result;
});
};
/**
* Get authentication token.
*
* @return {string} auth token
*/
p.performZabbixAPILogin = function() {
var options = {
url : this.url,
method : 'POST',
data: {
jsonrpc: '2.0',
method: 'user.login',
params: {
user: this.username,
password: this.password
},
auth: null,
id: 1
}
};
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;
}
return result.data.result;
});
};
//////////////////////////
// High-level functions //
//////////////////////////
p.getGroups = function() {
var params = {
output: ['name'],
sortfield: 'name'
};
return this.performZabbixAPIRequest('hostgroup.get', params);
};
p.getHosts = function() {
var params = {
output: ['name', 'host'],
sortfield: 'name',
selectGroups: []
};
return this.performZabbixAPIRequest('host.get', params);
};
p.getApplications = function() {
var params = {
output: ['name'],
sortfield: 'name',
selectHosts: []
};
return this.performZabbixAPIRequest('application.get', params);
};
p.getItems = function() {
var params = {
output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'],
sortfield: 'name',
selectApplications: []
};
return this.performZabbixAPIRequest('item.get', params);
};
/////////////////////////
// API method wrappers //
/////////////////////////
/**
* Request version of the Zabbix API.
*
* @return {string} Zabbix API version
*/
p.getZabbixAPIVersion = function() {
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: this.url,
data: {
jsonrpc: '2.0',
method: 'apiinfo.version',
params: [],
id: 1
}
};
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;
}
return result.data.result;
});
};
/**
* Perform history query from Zabbix API
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} start Time in seconds
* @param {Number} end Time in seconds
* @return {Array} Array of Zabbix history objects
*/
p.getHistory = function(items, start, end) {
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
history: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: start
};
// Relative queries (e.g. last hour) don't include an end time
if (end) {
params.time_till = end;
}
return this.performZabbixAPIRequest('history.get', params);
}, this)).then(function (results) {
return _.flatten(results);
});
};
/**
* Perform trends query from Zabbix API
* Use trends api extension from ZBXNEXT-1193 patch.
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} start Time in seconds
* @param {Number} end Time in seconds
* @return {Array} Array of Zabbix trend objects
*/
p.getTrends = function(items, start, end) {
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
trend: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: start
};
// Relative queries (e.g. last hour) don't include an end time
if (end) {
params.time_till = end;
}
return this.performZabbixAPIRequest('trend.get', params);
}, this)).then(function (results) {
return _.flatten(results);
});
};
/**
* Get the list of host groups
*
* @return {array} array of Zabbix hostgroup objects
*/
p.performHostGroupSuggestQuery = function() {
var params = {
output: ['name'],
sortfield: 'name',
// Return only host groups that contain hosts
real_hosts: true,
// Return only host groups that contain monitored hosts.
monitored_hosts: true
};
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Get the list of hosts
*
* @param {string|string[]} groupids
* @return {Object} array of Zabbix host objects
*/
p.performHostSuggestQuery = function(groupids) {
var params = {
output: ['name', 'host'],
sortfield: 'name',
// Return only hosts that have items with numeric type of information.
with_simple_graph_items: true,
// Return only monitored hosts.
monitored_hosts: true
};
// Return only hosts in given group
if (groupids) {
params.groupids = groupids;
}
return this.performZabbixAPIRequest('host.get', params);
};
/**
* Get the list of applications
*
* @param {array} hostids
* @param {array} groupids
* @return {Object} array of Zabbix application objects
*/
p.performAppSuggestQuery = function(hostids, /* optional */ groupids) {
var params = {
output: ['name'],
sortfield: 'name'
};
if (hostids) {
params.hostids = hostids;
}
else if (groupids) {
params.groupids = groupids;
}
return this.performZabbixAPIRequest('application.get', params);
};
/**
* Items request
*
* @param {string|string[]} hostids ///////////////////////////
* @param {string|string[]} applicationids // Zabbix API parameters //
* @param {string|string[]} groupids ///////////////////////////
* @return {string|string[]} Array of Zabbix API item objects
*/
p.performItemSuggestQuery = function(hostids, applicationids, groupids, itemtype) {
var params = {
output: ['name', 'key_', 'value_type', 'delay'],
sortfield: 'name',
//Include web items in the result
webitems: true,
// Return only numeric items
filter: {
value_type: [0, 3]
},
// Return only enabled items
monitored: true,
searchByAny: true
};
if (itemtype === "text") {
params.filter.value_type = [1, 2, 4];
}
// Filter by hosts or by groups
if (hostids) {
params.hostids = hostids;
} else if (groupids) {
params.groupids = groupids;
}
// If application selected return only relative items
if (applicationids) {
params.applicationids = applicationids;
}
// Return host property for multiple hosts
if (!hostids || (_.isArray(hostids) && hostids.length > 1)) {
params.selectHosts = ['name'];
}
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
*/
p.getGroupByName = function (group) {
var params = {
output: ['name']
};
if (group && group[0] !== '*') {
params.filter = {
name: group
};
}
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Search group by name.
*
* @param {string} group group name
* @return {array} groups
*/
p.searchGroup = function (group) {
var params = {
output: ['name'],
search: {
name: group
},
searchWildcardsEnabled: true
};
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
*/
p.getHostByName = function (hostnames) {
var params = {
output: ['host', 'name']
};
if (hostnames && hostnames[0] !== '*') {
params.filter = {
name: hostnames
};
}
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
*/
p.getAppByName = function (application) {
var params = {
output: ['name']
};
if (application && application[0] !== '*') {
params.filter = {
name: application
};
}
return this.performZabbixAPIRequest('application.get', params);
};
/**
* Get items belongs to passed groups, hosts and
* applications
*
* @param {string or array} groups
* @param {string or array} hosts
* @param {string or array} apps
* @return {array} array of Zabbix API item objects
*/
p.itemFindQuery = function(groups, hosts, apps, itemtype) {
var promises = [];
// Get hostids from names
if (hosts && hosts[0] !== '*') {
promises.push(this.getHostByName(hosts));
}
// Get groupids from names
else if (groups) {
promises.push(this.getGroupByName(groups));
}
// Get applicationids from names
if (apps && apps[0] !== '*') {
promises.push(this.getAppByName(apps));
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids;
var hostids;
var applicationids;
if (groups) {
groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts[0] !== '*') {
hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
if (apps && apps[0] !== '*') {
applicationids = _.map(_.filter(results, function (object) {
return object.applicationid;
}), 'applicationid');
}
return self.performItemSuggestQuery(hostids, applicationids, groupids, itemtype);
});
};
/**
* Find applications belongs to passed groups and hosts
*
* @param {string or array} hosts
* @param {string or array} groups
* @return {array} array of Zabbix API application objects
*/
p.appFindQuery = function(hosts, groups) {
var promises = [];
// Get hostids from names
if (hosts && hosts[0] !== '*') {
promises.push(this.getHostByName(hosts));
}
// Get groupids from names
else if (groups) {
promises.push(this.getGroupByName(groups));
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids;
var hostids;
if (groups) {
groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts[0] !== '*') {
hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
return self.performAppSuggestQuery(hostids, groupids);
});
};
/**
* Find hosts belongs to passed groups
*
* @param {string or array} groups
* @return {array} array of Zabbix API host objects
*/
p.hostFindQuery = function(groups) {
var self = this;
return this.getGroupByName(groups).then(function (results) {
results = _.flatten(results);
var groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
return self.performHostSuggestQuery(groupids);
});
};
p.getITService = function(/* optional */ serviceids) {
var params = {
output: 'extend',
serviceids: serviceids
};
return this.performZabbixAPIRequest('service.get', params);
};
p.getSLA = function(serviceids, from, to) {
var params = {
serviceids: serviceids,
intervals: [{
from: from,
to: to
}]
};
return this.performZabbixAPIRequest('service.getsla', params);
};
p.getTriggers = function(limit, sortfield, groupids, hostids, applicationids, name, showevents) {
var params = {
output: 'extend',
expandDescription: true,
expandData: true,
monitored: true,
//only_true: true,
filter: {
value: [0,1]
},
search : {
description: name
},
searchWildcardsEnabled: false,
groupids: groupids,
hostids: hostids,
applicationids: applicationids,
limit: limit,
sortfield: 'lastchange',
sortorder: 'DESC'
};
if (sortfield) {
params.sortfield = sortfield;
}
params.filter.value = showevents;
return this.performZabbixAPIRequest('trigger.get', params);
};
p.getAcknowledges = function(triggerids, from) {
var params = {
output: 'extend',
objectids: triggerids,
acknowledged: true,
select_acknowledges: 'extend',
sortfield: 'clock',
sortorder: 'DESC',
time_from: from
};
return this.performZabbixAPIRequest('event.get', params)
.then(function (events) {
return _.flatten(_.map(events, 'acknowledges'));
});
};
return ZabbixAPI;
});
});

View File

@@ -0,0 +1,133 @@
define([
'angular',
'lodash',
'./utils'
],
function (angular, _, utils) {
'use strict';
var module = angular.module('grafana.services');
// Use factory() instead service() for multiple datasources support.
// Each datasource instance must initialize its own cache.
module.factory('ZabbixCache', function($q) {
function ZabbixCache(zabbixAPI, ttl) {
this.zabbixAPI = zabbixAPI;
this.ttl = ttl;
// Internal objects for data storing
this._groups = undefined;
this._hosts = undefined;
this._applications = undefined;
this._items = undefined;
// Check is a service initialized or not
this._initialized = undefined;
}
var p = ZabbixCache.prototype;
p.refresh = function () {
var self = this;
var promises = [
this.zabbixAPI.getGroups(),
this.zabbixAPI.getHosts(),
this.zabbixAPI.getApplications(),
this.zabbixAPI.getItems()
];
return $q.all(promises).then(function (results) {
if (results.length) {
self._groups = results[0];
self._hosts = convertHosts(results[1]);
self._applications = convertApplications(results[2]);
self._items = convertItems(results[3]);
}
self._initialized = true;
});
};
p.getGroups = function() {
return this._groups;
};
p.getHosts = function() {
return this._hosts;
};
p.getApplications = function() {
return this._applications;
};
p.getItems = function(type) {
switch (type) {
case 'num':
return _.filter(this._items, function(item) {
return (item.value_type === '0' ||
item.value_type === '3');
});
case 'text':
return _.filter(this._items, function(item) {
return (item.value_type === '1' ||
item.value_type === '2' ||
item.value_type === '4');
});
default:
return this._items;
}
};
p.getHost = function(hostid) {
return _.find(this._hosts, {'hostid': hostid});
};
p.getItem = function(itemid) {
return _.find(this._items, {'itemid': itemid});
};
/**
* Convert host.get response to cache format
* host.groups - array of group ids
*/
function convertHosts(hosts) {
return _.forEach(hosts, function(host) {
host.groups = _.map(host.groups, 'groupid');
return host;
});
}
/**
* Group Zabbix applications by name
* host.hosts - array of host ids
*/
function convertApplications(applications) {
return _.map(_.groupBy(applications, 'name'), function (value, key) {
return {
name: key,
applicationids: _.map(value, 'applicationid'),
hosts: _.uniq(_.map(_.flatten(value, 'hosts'), 'hostid'))
};
});
}
/**
* Convert item.get response to cache format
* item.applications - array of application ids
* item.item - original item name returned by api (ie "CPU $2 time")
* item.name - expanded name (ie "CPU system time")
*/
function convertItems(items) {
return _.forEach(items, function(item) {
item.applications = _.map(item.applications, 'applicationid');
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
}
return ZabbixCache;
});
});

View File

@@ -1,89 +0,0 @@
define([
'angular',
'lodash'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
module.factory('ZabbixCache', function($q, backendSrv) {
function ZabbixCache(zabbixAPI, lifetime) {
var self = this;
this.zabbixAPI = zabbixAPI;
this.lifetime = lifetime;
this._groups = [];
this._hosts = [];
this._applications = [];
this._items = [];
this.refresh();
}
var p = ZabbixCache.prototype;
p.refresh = function () {
var self = this;
var promises = [
this.zabbixAPI.getGroups(),
this.zabbixAPI.getHosts(),
this.zabbixAPI.getApplications(),
this.zabbixAPI.getItems()
];
$q.all(promises).then(function (results) {
if (results.length) {
self._groups = results[0];
self._hosts = _.forEach(results[1], function(host) {
host.groups = _.map(host.groups, 'groupid');
return host;
});
self._applications = groupApplications(results[2]);
self._items = _.forEach(results[3], function(item) {
item.applications = _.map(item.applications, 'applicationid');
return item;
});
}
});
};
p.getGroups = function() {
return this._groups;
};
p.getHosts = function() {
return this._hosts;
};
p.getApplications = function() {
return this._applications;
};
p.getItems = function() {
return this._items;
};
/**
* Group Zabbix applications by name
*/
function groupApplications(applications) {
return _.map(_.groupBy(applications, 'name'), function (value, key) {
return {
name: key,
applicationids: _.map(value, 'applicationid'),
hostids: _.uniq(_.map(_.flatten(value, 'hosts'), 'hostid'))
};
});
}
return ZabbixCache;
});
});