Triggers Panel: allow to change font size, closes #351.

This commit is contained in:
Alexander Zobnin
2017-02-10 16:31:57 +03:00
parent 437699372c
commit 0e1069da27
30 changed files with 4678 additions and 8 deletions

View File

@@ -0,0 +1,116 @@
'use strict';
var _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _jquery = require('jquery');
var _jquery2 = _interopRequireDefault(_jquery);
var _metricFunctions = require('./metricFunctions');
var metricFunctions = _interopRequireWildcard(_metricFunctions);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/** @ngInject */
_angular2.default.module('grafana.directives').directive('addMetricFunction', function ($compile) {
var inputTemplate = '<input type="text"' + ' class="gf-form-input"' + ' spellcheck="false" style="display:none"></input>';
var buttonTemplate = '<a class="gf-form-label tight-form-func dropdown-toggle query-part"' + ' tabindex="1" gf-dropdown="functionMenu" data-toggle="dropdown">' + '<i class="fa fa-plus"></i></a>';
return {
link: function link($scope, elem) {
var categories = metricFunctions.getCategories();
var allFunctions = getAllFunctionNames(categories);
$scope.functionMenu = createFunctionDropDownMenu(categories);
var $input = (0, _jquery2.default)(inputTemplate);
var $button = (0, _jquery2.default)(buttonTemplate);
$input.appendTo(elem);
$button.appendTo(elem);
$input.attr('data-provide', 'typeahead');
$input.typeahead({
source: allFunctions,
minLength: 1,
items: 10,
updater: function updater(value) {
var funcDef = metricFunctions.getFuncDef(value);
if (!funcDef) {
// try find close match
value = value.toLowerCase();
funcDef = _lodash2.default.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 _lodash2.default.reduce(categories, function (list, category) {
_lodash2.default.each(category, function (func) {
list.push(func.name);
});
return list;
}, []);
}
function createFunctionDropDownMenu(categories) {
return _lodash2.default.map(categories, function (list, category) {
return {
text: category,
submenu: _lodash2.default.map(list, function (value) {
return {
text: value.name,
click: "ctrl.addFunction('" + value.name + "')"
};
})
};
});
}

View File

@@ -0,0 +1,330 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Downsample datapoints series
*/
function downsampleSeries(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([_lodash2.default.max(frame), timeWindow.to]);
} else if (func === "min") {
downsampledSeries.push([_lodash2.default.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>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _lodash2.default.groupBy(datapoints, function (point) {
// Calculate time for group of points
return Math.floor(point[1] / ms_interval) * ms_interval;
});
// frame: { '<unixtime>': [[<value>, <unixtime>], ...] }
// return [{ '<unixtime>': <value> }, { '<unixtime>': <value> }, ...]
var grouped = _lodash2.default.mapValues(frames, function (frame) {
var points = _lodash2.default.map(frame, function (point) {
return point[0];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_lodash2.default.map(grouped, function (value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _lodash2.default.uniq(_lodash2.default.map(_lodash2.default.flatten(timeseries, true), function (point) {
return point[1];
}));
new_timestamps = _lodash2.default.sortBy(new_timestamps);
var interpolated_timeseries = _lodash2.default.map(timeseries, function (series) {
var timestamps = _lodash2.default.map(series, function (point) {
return point[1];
});
var new_points = _lodash2.default.map(_lodash2.default.difference(new_timestamps, timestamps), function (timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_lodash2.default.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 limit(order, n, orderByFunc, timeseries) {
var orderByCallback = aggregationFunctions[orderByFunc];
var sortByIteratee = function sortByIteratee(ts) {
var values = _lodash2.default.map(ts.datapoints, function (point) {
return point[0];
});
return orderByCallback(values);
};
var sortedTimeseries = _lodash2.default.sortBy(timeseries, sortByIteratee);
if (order === 'bottom') {
return sortedTimeseries.slice(0, n);
} else {
return sortedTimeseries.slice(-n);
}
}
function AVERAGE(values) {
var sum = 0;
_lodash2.default.each(values, function (value) {
sum += value;
});
return sum / values.length;
}
function MIN(values) {
return _lodash2.default.min(values);
}
function MAX(values) {
return _lodash2.default.max(values);
}
function MEDIAN(values) {
var sorted = _lodash2.default.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
function setAlias(alias, timeseries) {
timeseries.target = alias;
return timeseries;
}
function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias);
return timeseries;
}
function extractText(str, pattern) {
var extractPattern = new RegExp(pattern);
var extractedValue = extractPattern.exec(str);
extractedValue = extractedValue[0];
return extractedValue;
}
function scale(factor, datapoints) {
return _lodash2.default.map(datapoints, function (point) {
return [point[0] * factor, point[1]];
});
}
function delta(datapoints) {
var newSeries = [];
var deltaValue = void 0;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}
function groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = aggregationFunctions[groupFunc];
return groupBy(interval, groupByCallback, datapoints);
}
function aggregateByWrapper(interval, aggregateFunc, datapoints) {
// Flatten all points in frame and then just use groupBy()
var flattenedPoints = _lodash2.default.flatten(datapoints, true);
var groupByCallback = aggregationFunctions[aggregateFunc];
return groupBy(interval, groupByCallback, flattenedPoints);
}
function aggregateWrapper(groupByCallback, interval, datapoints) {
var flattenedPoints = _lodash2.default.flatten(datapoints, true);
return groupBy(interval, groupByCallback, flattenedPoints);
}
function sortByTime(series) {
return _lodash2.default.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 = _lodash2.default.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _lodash2.default.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}
function timeShift(interval, range) {
var shift = utils.parseTimeShiftInterval(interval) / 1000;
return range.map(function (time) {
return time - shift;
});
}
function unShiftTimeSeries(interval, datapoints) {
var unshift = utils.parseTimeShiftInterval(interval);
return datapoints.map(function (dp) {
return [dp[0], dp[1] + unshift];
});
}
var metricFunctions = {
groupBy: groupByWrapper,
scale: scale,
delta: delta,
aggregateBy: aggregateByWrapper,
average: _lodash2.default.partial(aggregateWrapper, AVERAGE),
min: _lodash2.default.partial(aggregateWrapper, MIN),
max: _lodash2.default.partial(aggregateWrapper, MAX),
median: _lodash2.default.partial(aggregateWrapper, MEDIAN),
sumSeries: sumSeries,
top: _lodash2.default.partial(limit, 'top'),
bottom: _lodash2.default.partial(limit, 'bottom'),
timeShift: timeShift,
setAlias: setAlias,
setAliasByRegex: setAliasByRegex
};
var aggregationFunctions = {
avg: AVERAGE,
min: MIN,
max: MAX,
median: MEDIAN
};
exports.default = {
downsampleSeries: downsampleSeries,
groupBy: groupBy,
AVERAGE: AVERAGE,
MIN: MIN,
MAX: MAX,
MEDIAN: MEDIAN,
unShiftTimeSeries: unShiftTimeSeries,
get aggregationFunctions() {
return aggregationFunctions;
},
get metricFunctions() {
return metricFunctions;
}
};

View File

@@ -0,0 +1,582 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.zabbixTemplateFormat = exports.ZabbixAPIDatasource = undefined;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); //import angular from 'angular';
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _datemath = require('app/core/utils/datemath');
var dateMath = _interopRequireWildcard(_datemath);
var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils);
var _migrations = require('./migrations');
var migrations = _interopRequireWildcard(_migrations);
var _metricFunctions = require('./metricFunctions');
var metricFunctions = _interopRequireWildcard(_metricFunctions);
var _dataProcessor = require('./dataProcessor');
var _dataProcessor2 = _interopRequireDefault(_dataProcessor);
var _responseHandler = require('./responseHandler');
var _responseHandler2 = _interopRequireDefault(_responseHandler);
require('./zabbix.js');
var _zabbixAPICoreService = require('./zabbixAPICore.service.js');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ZabbixAPIDatasource = function () {
/** @ngInject */
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, Zabbix) {
_classCallCheck(this, ZabbixAPIDatasource);
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
// General data source settings
this.name = instanceSettings.name;
this.url = instanceSettings.url;
this.basicAuth = instanceSettings.basicAuth;
this.withCredentials = instanceSettings.withCredentials;
// Zabbix API credentials
this.username = instanceSettings.jsonData.username;
this.password = instanceSettings.jsonData.password;
// Use trends instead history since specified time
this.trends = instanceSettings.jsonData.trends;
this.trendsFrom = instanceSettings.jsonData.trendsFrom || '7d';
// Set cache update interval
var ttl = instanceSettings.jsonData.cacheTTL || '1h';
this.cacheTTL = utils.parseInterval(ttl);
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Use custom format for template variables
this.replaceTemplateVars = _lodash2.default.partial(replaceTemplateVars, this.templateSrv);
}
////////////////////////
// Datasource methods //
////////////////////////
/**
* 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.
*/
_createClass(ZabbixAPIDatasource, [{
key: 'query',
value: function query(options) {
var _this = this;
var timeFrom = Math.ceil(dateMath.parse(options.range.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.range.to) / 1000);
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
var useTrends = timeFrom <= useTrendsFrom && this.trends;
// Create request for each target
var promises = _lodash2.default.map(options.targets, function (target) {
// Prevent changes of original object
target = _lodash2.default.cloneDeep(target);
_this.replaceTargetVariables(target, options);
// Apply Time-related functions (timeShift(), etc)
var timeFunctions = bindFunctionDefs(target.functions, 'Time');
if (timeFunctions.length) {
var _sequence = sequence(timeFunctions)([timeFrom, timeTo]),
_sequence2 = _slicedToArray(_sequence, 2),
time_from = _sequence2[0],
time_to = _sequence2[1];
timeFrom = time_from;
timeTo = time_to;
}
// Metrics or Text query mode
if (target.mode !== 1) {
// Migrate old targets
target = migrations.migrate(target);
// Don't request undefined and hidden targets
if (target.hide || !target.group || !target.host || !target.item) {
return [];
}
if (!target.mode || target.mode === 0) {
return _this.queryNumericData(target, timeFrom, timeTo, useTrends);
} else if (target.mode === 2) {
return _this.queryTextData(target, timeFrom, timeTo);
}
}
// IT services mode
else if (target.mode === 1) {
// Don't show undefined and hidden targets
if (target.hide || !target.itservice || !target.slaProperty) {
return [];
}
return _this.zabbix.getSLA(target.itservice.serviceid, timeFrom, timeTo).then(function (slaObject) {
return _responseHandler2.default.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
}
});
// Data for panel (all targets)
return Promise.all(_lodash2.default.flatten(promises)).then(_lodash2.default.flatten).then(function (timeseries_data) {
return downsampleSeries(timeseries_data, options);
}).then(function (data) {
return { data: data };
});
}
}, {
key: 'queryNumericData',
value: function queryNumericData(target, timeFrom, timeTo, useTrends) {
var _this2 = this;
var options = {
itemtype: 'num'
};
return this.zabbix.getItemsFromTarget(target, options).then(function (items) {
var getHistoryPromise = void 0;
if (useTrends) {
(function () {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
});
})();
} else {
// Use history
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleHistory(history, items);
});
}
return getHistoryPromise.then(function (timeseries_data) {
return _this2.applyDataProcessingFunctions(timeseries_data, target);
});
});
}
}, {
key: 'getTrendValueType',
value: function getTrendValueType(target) {
// Find trendValue() function and get specified trend value
var trendFunctions = _lodash2.default.map(metricFunctions.getCategories()['Trends'], 'name');
var trendValueFunc = _lodash2.default.find(target.functions, function (func) {
return _lodash2.default.includes(trendFunctions, func.def.name);
});
return trendValueFunc ? trendValueFunc.params[0] : "avg";
}
}, {
key: 'applyDataProcessingFunctions',
value: function applyDataProcessingFunctions(timeseries_data, target) {
var transformFunctions = bindFunctionDefs(target.functions, 'Transform');
var aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate');
var filterFunctions = bindFunctionDefs(target.functions, 'Filter');
var aliasFunctions = bindFunctionDefs(target.functions, 'Alias');
// Apply transformation functions
timeseries_data = _lodash2.default.map(timeseries_data, function (timeseries) {
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
return timeseries;
});
// Apply filter functions
if (filterFunctions.length) {
timeseries_data = sequence(filterFunctions)(timeseries_data);
}
// Apply aggregations
if (aggregationFunctions.length) {
(function () {
var dp = _lodash2.default.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
var aggFuncNames = _lodash2.default.map(metricFunctions.getCategories()['Aggregate'], 'name');
var lastAgg = _lodash2.default.findLast(target.functions, function (func) {
return _lodash2.default.includes(aggFuncNames, func.def.name);
});
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
})();
}
// Apply alias functions
_lodash2.default.forEach(timeseries_data, sequence(aliasFunctions));
// Apply Time-related functions (timeShift(), etc)
// Find timeShift() function and get specified trend value
this.applyTimeShiftFunction(timeseries_data, target);
return timeseries_data;
}
}, {
key: 'applyTimeShiftFunction',
value: function applyTimeShiftFunction(timeseries_data, target) {
// Find timeShift() function and get specified interval
var timeShiftFunc = _lodash2.default.find(target.functions, function (func) {
return func.def.name === 'timeShift';
});
if (timeShiftFunc) {
(function () {
var shift = timeShiftFunc.params[0];
_lodash2.default.forEach(timeseries_data, function (series) {
series.datapoints = _dataProcessor2.default.unShiftTimeSeries(shift, series.datapoints);
});
})();
}
}
}, {
key: 'queryTextData',
value: function queryTextData(target, timeFrom, timeTo) {
var _this3 = this;
var options = {
itemtype: 'text'
};
return this.zabbix.getItemsFromTarget(target, options).then(function (items) {
if (items.length) {
return _this3.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.convertHistory(history, items, false, function (point) {
var value = point.value;
// Regex-based extractor
if (target.textFilter) {
value = extractText(point.value, target.textFilter, target.useCaptureGroups);
}
return [value, point.clock * 1000];
});
});
} else {
return Promise.resolve([]);
}
});
}
/**
* Test connection to Zabbix API
* @return {object} Connection status and Zabbix API version
*/
}, {
key: 'testDatasource',
value: function testDatasource() {
var _this4 = this;
var zabbixVersion = void 0;
return this.zabbix.getVersion().then(function (version) {
zabbixVersion = version;
return _this4.zabbix.login();
}).then(function () {
return {
status: "success",
title: "Success",
message: "Zabbix API version: " + zabbixVersion
};
}).catch(function (error) {
if (error instanceof _zabbixAPICoreService.ZabbixAPIError) {
return {
status: "error",
title: error.message,
message: error.data
};
} else {
return {
status: "error",
title: "Connection failed",
message: "Could not connect to given url"
};
}
});
}
////////////////
// Templating //
////////////////
/**
* Find metrics from templated request.
*
* @param {string} query Query from Templating
* @return {string} Metric name - group, host, app or item or list
* of metrics in "{metric1,metcic2,...,metricN}" format.
*/
}, {
key: 'metricFindQuery',
value: function metricFindQuery(query) {
var _this5 = this;
var result = void 0;
var parts = [];
// Split query. Query structure: group.host.app.item
_lodash2.default.each(query.split('.'), function (part) {
part = _this5.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
part = '/.*/';
}
parts.push(part);
});
var template = _lodash2.default.zipObject(['group', 'host', 'app', 'item'], parts);
// Get items
if (parts.length === 4) {
// Search for all items, even it's not belong to any application
if (template.app === '/.*/') {
template.app = '';
}
result = this.zabbix.getItems(template.group, template.host, template.app, template.item);
} else if (parts.length === 3) {
// Get applications
result = this.zabbix.getApps(template.group, template.host, template.app);
} else if (parts.length === 2) {
// Get hosts
result = this.zabbix.getHosts(template.group, template.host);
} else if (parts.length === 1) {
// Get groups
result = this.zabbix.getGroups(template.group);
} else {
result = Promise.resolve([]);
}
return result.then(function (metrics) {
return metrics.map(formatMetric);
});
}
/////////////////
// Annotations //
/////////////////
}, {
key: 'annotationQuery',
value: function annotationQuery(options) {
var _this6 = this;
var timeFrom = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
var annotation = options.annotation;
var showOkEvents = annotation.showOkEvents ? [0, 1] : 1;
// Show all triggers
var showTriggers = [0, 1];
var getTriggers = this.zabbix.getTriggers(this.replaceTemplateVars(annotation.group, {}), this.replaceTemplateVars(annotation.host, {}), this.replaceTemplateVars(annotation.application, {}), showTriggers);
return getTriggers.then(function (triggers) {
// Filter triggers by description
if (utils.isRegex(annotation.trigger)) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return utils.buildRegex(annotation.trigger).test(trigger.description);
});
} else if (annotation.trigger) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return trigger.description === annotation.trigger;
});
}
// Remove events below the chose severity
triggers = _lodash2.default.filter(triggers, function (trigger) {
return Number(trigger.priority) >= Number(annotation.minseverity);
});
var objectids = _lodash2.default.map(triggers, 'triggerid');
return _this6.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
var indexedTriggers = _lodash2.default.keyBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
if (annotation.hideAcknowledged) {
events = _lodash2.default.filter(events, function (event) {
return !event.acknowledges.length;
});
}
return _lodash2.default.map(events, function (event) {
var tags = void 0;
if (annotation.showHostname) {
tags = _lodash2.default.map(event.hosts, 'name');
}
// Show event type (OK or Problem)
var title = Number(event.value) ? 'Problem' : 'OK';
var formatted_acknowledges = utils.formatAcknowledges(event.acknowledges);
return {
annotation: annotation,
time: event.clock * 1000,
title: title,
tags: tags,
text: indexedTriggers[event.objectid].description + formatted_acknowledges
};
});
});
});
}
// Replace template variables
}, {
key: 'replaceTargetVariables',
value: function replaceTargetVariables(target, options) {
var _this7 = this;
var parts = ['group', 'host', 'application', 'item'];
parts.forEach(function (p) {
if (target[p] && target[p].filter) {
target[p].filter = _this7.replaceTemplateVars(target[p].filter, options.scopedVars);
}
});
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
_lodash2.default.forEach(target.functions, function (func) {
func.params = func.params.map(function (param) {
if (typeof param === 'number') {
return +_this7.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return _this7.templateSrv.replace(param, options.scopedVars);
}
});
});
}
}]);
return ZabbixAPIDatasource;
}();
function bindFunctionDefs(functionDefs, category) {
var aggregationFunctions = _lodash2.default.map(metricFunctions.getCategories()[category], 'name');
var aggFuncDefs = _lodash2.default.filter(functionDefs, function (func) {
return _lodash2.default.includes(aggregationFunctions, func.def.name);
});
return _lodash2.default.map(aggFuncDefs, function (func) {
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
return funcInstance.bindFunction(_dataProcessor2.default.metricFunctions);
});
}
function downsampleSeries(timeseries_data, options) {
return _lodash2.default.map(timeseries_data, function (timeseries) {
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = _dataProcessor2.default.groupBy(options.interval, _dataProcessor2.default.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
}
function formatMetric(metricObj) {
return {
text: metricObj.name,
expandable: false
};
}
/**
* Custom formatter for template variables.
* Default Grafana "regex" formatter returns
* value1|value2
* This formatter returns
* (value1|value2)
* This format needed for using in complex regex with
* template variables, for example
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
*/
function zabbixTemplateFormat(value) {
if (typeof value === 'string') {
return utils.escapeRegex(value);
}
var escapedValues = _lodash2.default.map(value, utils.escapeRegex);
return '(' + escapedValues.join('|') + ')';
}
/**
* If template variables are used in request, replace it using regex format
* and wrap with '/' for proper multi-value work. Example:
* $variable selected as a, b, c
* We use filter $variable
* $variable -> a|b|c -> /a|b|c/
* /$variable/ -> /a|b|c/ -> /a|b|c/
*/
function replaceTemplateVars(templateSrv, target, scopedVars) {
var replacedTarget = templateSrv.replace(target, scopedVars, zabbixTemplateFormat);
if (target !== replacedTarget && !utils.isRegex(replacedTarget)) {
replacedTarget = '/^' + replacedTarget + '$/';
}
return replacedTarget;
}
function extractText(str, pattern, useCaptureGroups) {
var extractPattern = new RegExp(pattern);
var extractedValue = extractPattern.exec(str);
if (extractedValue) {
if (useCaptureGroups) {
extractedValue = extractedValue[1];
} else {
extractedValue = extractedValue[0];
}
}
return extractedValue;
}
// Apply function one by one:
// sequence([a(), b(), c()]) = c(b(a()));
function sequence(funcsArray) {
return function (result) {
for (var i = 0; i < funcsArray.length; i++) {
result = funcsArray[i].call(this, result);
}
return result;
};
}
exports.ZabbixAPIDatasource = ZabbixAPIDatasource;
exports.zabbixTemplateFormat = zabbixTemplateFormat;
// Fix for backward compatibility with lodash 2.4
if (!_lodash2.default.includes) {
_lodash2.default.includes = _lodash2.default.contains;
}
if (!_lodash2.default.keyBy) {
_lodash2.default.keyBy = _lodash2.default.indexBy;
}

View File

@@ -0,0 +1,246 @@
'use strict';
var _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _jquery = require('jquery');
var _jquery2 = _interopRequireDefault(_jquery);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/** @ngInject */
_angular2.default.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 = (0, _jquery2.default)(funcSpanTemplate);
var $funcControls = (0, _jquery2.default)(funcControlsTemplate);
var ctrl = $scope.ctrl;
var func = $scope.func;
var funcDef = func.def;
var scheduledRelink = false;
var paramCountAtLink = 0;
function clickFuncParam(paramIndex) {
/*jshint validthis:true */
var $link = (0, _jquery2.default)(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 = (0, _jquery2.default)(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(function () {
ctrl.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' || funcDef.params[paramIndex].type === 'float') {
options = _lodash2.default.map(options, function (val) {
return val.toString();
});
}
$input.typeahead({
source: options,
minLength: 0,
items: 20,
updater: function updater(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);
_lodash2.default.each(funcDef.params, function (param, index) {
if (param.optional && func.params.length <= index) {
return;
}
if (index > 0) {
(0, _jquery2.default)('<span>, </span>').appendTo(elem);
}
var paramValue = templateSrv.highlightVariablesAsHtml(func.params[index]);
var $paramLink = (0, _jquery2.default)('<a ng-click="" class="graphite-func-param-link">' + paramValue + '</a>');
var $input = (0, _jquery2.default)(paramTemplate);
paramCountAtLink++;
$paramLink.appendTo(elem);
$input.appendTo(elem);
$input.blur(_lodash2.default.partial(inputBlur, index));
$input.keyup(inputKeyDown);
$input.keypress(_lodash2.default.partial(inputKeyPress, index));
$paramLink.click(_lodash2.default.partial(clickFuncParam, index));
if (funcDef.params[index].options) {
addTypeahead($input, index);
}
});
(0, _jquery2.default)('<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 = (0, _jquery2.default)(e.target);
if ($target.hasClass('fa-remove')) {
toggleFuncControls();
$scope.$apply(function () {
ctrl.removeFunction($scope.func);
});
return;
}
if ($target.hasClass('fa-arrow-left')) {
$scope.$apply(function () {
_lodash2.default.move($scope.target.functions, $scope.$index, $scope.$index - 1);
ctrl.targetChanged();
});
return;
}
if ($target.hasClass('fa-arrow-right')) {
$scope.$apply(function () {
_lodash2.default.move($scope.target.functions, $scope.$index, $scope.$index + 1);
ctrl.targetChanged();
});
return;
}
if ($target.hasClass('fa-question-circle')) {
var docSite = "http://docs.grafana-zabbix.org/reference/functions/";
window.open(docSite + '#' + funcDef.name.toLowerCase(), '_blank');
return;
}
});
}
function relink() {
elem.children().remove();
addElementsAndCompile();
ifJustAddedFocusFistParam();
registerFuncControlsToggle();
registerFuncControlsActions();
}
relink();
}
};
});

View File

@@ -0,0 +1,292 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.createFuncInstance = createFuncInstance;
exports.getFuncDef = getFuncDef;
exports.getCategories = getCategories;
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _jquery = require('jquery');
var _jquery2 = _interopRequireDefault(_jquery);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var index = [];
var categories = {
Transform: [],
Aggregate: [],
Filter: [],
Trends: [],
Time: [],
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;
}
// Transform
addFuncDef({
name: 'groupBy',
category: 'Transform',
params: [{ name: 'interval', type: 'string' }, { name: 'function', type: 'string', options: ['avg', 'min', 'max', 'median'] }],
defaultParams: ['1m', 'avg']
});
addFuncDef({
name: 'scale',
category: 'Transform',
params: [{ name: 'factor', type: 'float', options: [100, 0.01, 10, -1] }],
defaultParams: [100]
});
addFuncDef({
name: 'delta',
category: 'Transform',
params: [],
defaultParams: []
});
// Aggregate
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: 'aggregateBy',
category: 'Aggregate',
params: [{ name: 'interval', type: 'string' }, { name: 'function', type: 'string', options: ['avg', 'min', 'max', 'median'] }],
defaultParams: ['1m', 'avg']
});
// Filter
addFuncDef({
name: 'top',
category: 'Filter',
params: [{ name: 'number', type: 'int' }, { name: 'value', type: 'string', options: ['avg', 'min', 'max', 'median'] }],
defaultParams: [5, 'avg']
});
addFuncDef({
name: 'bottom',
category: 'Filter',
params: [{ name: 'number', type: 'int' }, { name: 'value', type: 'string', options: ['avg', 'min', 'max', 'median'] }],
defaultParams: [5, 'avg']
});
// Trends
addFuncDef({
name: 'trendValue',
category: 'Trends',
params: [{ name: 'type', type: 'string', options: ['avg', 'min', 'max'] }],
defaultParams: ['avg']
});
// Time
addFuncDef({
name: 'timeShift',
category: 'Time',
params: [{ name: 'interval', type: 'string', options: ['24h', '7d', '1M', '+24h', '-24h'] }],
defaultParams: ['24h']
});
//Alias
addFuncDef({
name: 'setAlias',
category: 'Alias',
params: [{ name: 'alias', type: 'string' }],
defaultParams: []
});
addFuncDef({
name: 'setAliasByRegex',
category: 'Alias',
params: [{ name: 'aliasByRegex', type: 'string' }],
defaultParams: []
});
_lodash2.default.each(categories, function (funcList, catName) {
categories[catName] = _lodash2.default.sortBy(funcList, 'name');
});
var FuncInstance = function () {
function FuncInstance(funcDef, params) {
_classCallCheck(this, FuncInstance);
this.def = funcDef;
if (params) {
this.params = params;
} else {
// Create with default params
this.params = [];
this.params = funcDef.defaultParams.slice(0);
}
this.updateText();
}
_createClass(FuncInstance, [{
key: 'bindFunction',
value: function bindFunction(metricFunctions) {
var func = metricFunctions[this.def.name];
if (func) {
// Bind function arguments
var bindedFunc = func;
var param;
for (var i = 0; i < this.params.length; i++) {
param = this.params[i];
// Convert numeric params
if (this.def.params[i].type === 'int' || this.def.params[i].type === 'float') {
param = Number(param);
}
bindedFunc = _lodash2.default.partial(bindedFunc, param);
}
return bindedFunc;
} else {
throw { message: 'Method not found ' + this.def.name };
}
}
}, {
key: 'render',
value: function render(metricExp) {
var str = this.def.name + '(';
var parameters = _lodash2.default.map(this.params, function (value, index) {
var paramType = this.def.params[index].type;
if (paramType === 'int' || paramType === 'float' || paramType === 'value_or_series' || paramType === 'boolean') {
return value;
} else if (paramType === 'int_or_interval' && _jquery2.default.isNumeric(value)) {
return value;
}
return "'" + value + "'";
}, this);
if (metricExp) {
parameters.unshift(metricExp);
}
return str + parameters.join(', ') + ')';
}
}, {
key: '_hasMultipleParamsInString',
value: function _hasMultipleParamsInString(strValue, index) {
if (strValue.indexOf(',') === -1) {
return false;
}
return this.def.params[index + 1] && this.def.params[index + 1].optional;
}
}, {
key: 'updateParam',
value: function updateParam(strValue, index) {
// handle optional parameters
// if string contains ',' and next param is optional, split and update both
if (this._hasMultipleParamsInString(strValue, index)) {
_lodash2.default.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();
}
}, {
key: 'updateText',
value: function updateText() {
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 FuncInstance;
}();
function createFuncInstance(funcDef, params) {
if (_lodash2.default.isString(funcDef)) {
if (!index[funcDef]) {
throw { message: 'Method not found ' + name };
}
funcDef = index[funcDef];
}
return new FuncInstance(funcDef, params);
}
function getFuncDef(name) {
return index[name];
}
function getCategories() {
return categories;
}

View File

@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isGrafana2target = isGrafana2target;
exports.migrateFrom2To3version = migrateFrom2To3version;
exports.migrate = migrate;
/**
* Query format migration.
* This module can detect query format version and make migration.
*/
function isGrafana2target(target) {
if (!target.mode || target.mode === 0 || target.mode === 2) {
if ((target.hostFilter || target.itemFilter || target.downsampleFunction || target.host && target.host.host) && target.item.filter === undefined && target.host.filter === undefined) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function migrateFrom2To3version(target) {
target.group.filter = target.group.name === "*" ? "/.*/" : target.group.name;
target.host.filter = target.host.name === "*" ? convertToRegex(target.hostFilter) : target.host.name;
target.application.filter = target.application.name === "*" ? "" : target.application.name;
target.item.filter = target.item.name === "All" ? convertToRegex(target.itemFilter) : target.item.name;
return target;
}
function migrate(target) {
if (isGrafana2target(target)) {
return migrateFrom2To3version(target);
} else {
return target;
}
}
function convertToRegex(str) {
if (str) {
return '/' + str + '/';
} else {
return '/.*/';
}
}

36
dist/test/datasource-zabbix/module.js vendored Normal file
View File

@@ -0,0 +1,36 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AnnotationsQueryCtrl = exports.QueryOptionsCtrl = exports.QueryCtrl = exports.ConfigCtrl = exports.Datasource = undefined;
var _datasource = require('./datasource');
var _query = require('./query.controller');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ZabbixConfigController = function ZabbixConfigController() {
_classCallCheck(this, ZabbixConfigController);
};
ZabbixConfigController.templateUrl = 'datasource-zabbix/partials/config.html';
var ZabbixQueryOptionsController = function ZabbixQueryOptionsController() {
_classCallCheck(this, ZabbixQueryOptionsController);
};
ZabbixQueryOptionsController.templateUrl = 'datasource-zabbix/partials/query.options.html';
var ZabbixAnnotationsQueryController = function ZabbixAnnotationsQueryController() {
_classCallCheck(this, ZabbixAnnotationsQueryController);
};
ZabbixAnnotationsQueryController.templateUrl = 'datasource-zabbix/partials/annotations.editor.html';
exports.Datasource = _datasource.ZabbixAPIDatasource;
exports.ConfigCtrl = ZabbixConfigController;
exports.QueryCtrl = _query.ZabbixQueryController;
exports.QueryOptionsCtrl = ZabbixQueryOptionsController;
exports.AnnotationsQueryCtrl = ZabbixAnnotationsQueryController;

View File

@@ -0,0 +1,378 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ZabbixQueryController = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _sdk = require('app/plugins/sdk');
var _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils);
var _metricFunctions = require('./metricFunctions');
var metricFunctions = _interopRequireWildcard(_metricFunctions);
var _migrations = require('./migrations');
var migrations = _interopRequireWildcard(_migrations);
require('./add-metric-function.directive');
require('./metric-function-editor.directive');
require('./css/query-editor.css!');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl) {
_inherits(ZabbixQueryController, _QueryCtrl);
// ZabbixQueryCtrl constructor
function ZabbixQueryController($scope, $injector, $rootScope, $sce, templateSrv) {
_classCallCheck(this, ZabbixQueryController);
var _this = _possibleConstructorReturn(this, (ZabbixQueryController.__proto__ || Object.getPrototypeOf(ZabbixQueryController)).call(this, $scope, $injector));
_this.zabbix = _this.datasource.zabbix;
// Use custom format for template variables
_this.replaceTemplateVars = _this.datasource.replaceTemplateVars;
_this.templateSrv = templateSrv;
_this.editorModes = {
0: { value: 'num', text: 'Metrics', mode: 0 },
1: { value: 'itservice', text: 'IT Services', mode: 1 },
2: { value: 'text', text: 'Text', mode: 2 }
};
// Map functions for bs-typeahead
_this.getGroupNames = _lodash2.default.partial(getMetricNames, _this, 'groupList');
_this.getHostNames = _lodash2.default.partial(getMetricNames, _this, 'hostList');
_this.getApplicationNames = _lodash2.default.partial(getMetricNames, _this, 'appList');
_this.getItemNames = _lodash2.default.partial(getMetricNames, _this, 'itemList');
// Update metric suggestion when template variable was changed
$rootScope.$on('template-variable-value-updated', function () {
return _this.onVariableChange();
});
// Update metrics when item selected from dropdown
$scope.$on('typeahead-updated', function () {
_this.onTargetBlur();
});
_this.init = function () {
var target = this.target;
// Migrate old targets
target = migrations.migrate(target);
var scopeDefaults = {
metric: {},
oldTarget: _lodash2.default.cloneDeep(this.target),
queryOptionsText: this.renderQueryOptionsText()
};
_lodash2.default.defaults(this, scopeDefaults);
// Load default values
var targetDefaults = {
mode: 0,
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
item: { filter: "" },
functions: [],
options: {
showDisabledItems: false
}
};
_lodash2.default.defaults(target, targetDefaults);
// Create function instances from saved JSON
target.functions = _lodash2.default.map(target.functions, function (func) {
return metricFunctions.createFuncInstance(func.def, func.params);
});
if (target.mode === 0 || target.mode === 2) {
this.downsampleFunctionList = [{ name: "avg", value: "avg" }, { name: "min", value: "min" }, { name: "max", value: "max" }];
this.initFilters();
} else if (target.mode === 1) {
this.slaPropertyList = [{ name: "Status", property: "status" }, { name: "SLA", property: "sla" }, { name: "OK time", property: "okTime" }, { name: "Problem time", property: "problemTime" }, { name: "Down time", property: "downtimeTime" }];
this.itserviceList = [{ name: "test" }];
this.updateITServiceList();
}
};
_this.init();
return _this;
}
_createClass(ZabbixQueryController, [{
key: 'initFilters',
value: function initFilters() {
var itemtype = this.editorModes[this.target.mode].value;
return Promise.all([this.suggestGroups(), this.suggestHosts(), this.suggestApps(), this.suggestItems(itemtype)]);
}
}, {
key: 'suggestGroups',
value: function suggestGroups() {
var _this2 = this;
return this.zabbix.getAllGroups().then(function (groups) {
_this2.metric.groupList = groups;
return groups;
});
}
}, {
key: 'suggestHosts',
value: function suggestHosts() {
var _this3 = this;
var groupFilter = this.replaceTemplateVars(this.target.group.filter);
return this.zabbix.getAllHosts(groupFilter).then(function (hosts) {
_this3.metric.hostList = hosts;
return hosts;
});
}
}, {
key: 'suggestApps',
value: function suggestApps() {
var _this4 = this;
var groupFilter = this.replaceTemplateVars(this.target.group.filter);
var hostFilter = this.replaceTemplateVars(this.target.host.filter);
return this.zabbix.getAllApps(groupFilter, hostFilter).then(function (apps) {
_this4.metric.appList = apps;
return apps;
});
}
}, {
key: 'suggestItems',
value: function suggestItems() {
var _this5 = this;
var itemtype = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'num';
var groupFilter = this.replaceTemplateVars(this.target.group.filter);
var hostFilter = this.replaceTemplateVars(this.target.host.filter);
var appFilter = this.replaceTemplateVars(this.target.application.filter);
var options = {
itemtype: itemtype,
showDisabledItems: this.target.options.showDisabledItems
};
return this.zabbix.getAllItems(groupFilter, hostFilter, appFilter, options).then(function (items) {
_this5.metric.itemList = items;
return items;
});
}
}, {
key: 'isRegex',
value: function isRegex(str) {
return utils.isRegex(str);
}
}, {
key: 'isVariable',
value: function isVariable(str) {
return utils.isTemplateVariable(str, this.templateSrv.variables);
}
}, {
key: 'onTargetBlur',
value: function onTargetBlur() {
var newTarget = _lodash2.default.cloneDeep(this.target);
if (!_lodash2.default.isEqual(this.oldTarget, this.target)) {
this.oldTarget = newTarget;
this.targetChanged();
}
}
}, {
key: 'onVariableChange',
value: function onVariableChange() {
if (this.isContainsVariables()) {
this.targetChanged();
}
}
/**
* Check query for template variables
*/
}, {
key: 'isContainsVariables',
value: function isContainsVariables() {
var _this6 = this;
return _lodash2.default.some(['group', 'host', 'application'], function (field) {
if (_this6.target[field] && _this6.target[field].filter) {
return utils.isTemplateVariable(_this6.target[field].filter, _this6.templateSrv.variables);
} else {
return false;
}
});
}
}, {
key: 'parseTarget',
value: function parseTarget() {}
// Parse target
// Validate target and set validation info
}, {
key: 'validateTarget',
value: function validateTarget() {
// validate
}
}, {
key: 'targetChanged',
value: function targetChanged() {
this.initFilters();
this.parseTarget();
this.panelCtrl.refresh();
}
}, {
key: 'addFunction',
value: function addFunction(funcDef) {
var newFunc = metricFunctions.createFuncInstance(funcDef);
newFunc.added = true;
this.target.functions.push(newFunc);
this.moveAliasFuncLast();
if (newFunc.params.length && newFunc.added || newFunc.def.params.length === 0) {
this.targetChanged();
}
}
}, {
key: 'removeFunction',
value: function removeFunction(func) {
this.target.functions = _lodash2.default.without(this.target.functions, func);
this.targetChanged();
}
}, {
key: 'moveAliasFuncLast',
value: function moveAliasFuncLast() {
var aliasFunc = _lodash2.default.find(this.target.functions, function (func) {
return func.def.name === 'alias' || func.def.name === 'aliasByNode' || func.def.name === 'aliasByMetric';
});
if (aliasFunc) {
this.target.functions = _lodash2.default.without(this.target.functions, aliasFunc);
this.target.functions.push(aliasFunc);
}
}
}, {
key: 'toggleQueryOptions',
value: function toggleQueryOptions() {
this.showQueryOptions = !this.showQueryOptions;
}
}, {
key: 'onQueryOptionChange',
value: function onQueryOptionChange() {
this.queryOptionsText = this.renderQueryOptionsText();
this.onTargetBlur();
}
}, {
key: 'renderQueryOptionsText',
value: function renderQueryOptionsText() {
var optionsMap = {
showDisabledItems: "Show disabled items"
};
var options = [];
_lodash2.default.forOwn(this.target.options, function (value, key) {
if (value) {
if (value === true) {
// Show only option name (if enabled) for boolean options
options.push(optionsMap[key]);
} else {
// Show "option = value" for another options
options.push(optionsMap[key] + " = " + value);
}
}
});
return "Options: " + options.join(', ');
}
/**
* Switch query editor to specified mode.
* Modes:
* 0 - items
* 1 - IT services
* 2 - Text metrics
*/
}, {
key: 'switchEditorMode',
value: function switchEditorMode(mode) {
this.target.mode = mode;
this.init();
}
/////////////////
// IT Services //
/////////////////
/**
* Update list of IT services
*/
}, {
key: 'updateITServiceList',
value: function updateITServiceList() {
var _this7 = this;
this.zabbix.getITService().then(function (iteservices) {
_this7.itserviceList = [];
_this7.itserviceList = _this7.itserviceList.concat(iteservices);
});
}
/**
* Call when IT service is selected.
*/
}, {
key: 'selectITService',
value: function selectITService() {
if (!_lodash2.default.isEqual(this.oldTarget, this.target) && _lodash2.default.isEmpty(this.target.errors)) {
this.oldTarget = _angular2.default.copy(this.target);
this.panelCtrl.refresh();
}
}
}]);
return ZabbixQueryController;
}(_sdk.QueryCtrl);
// Set templateUrl as static property
ZabbixQueryController.templateUrl = 'datasource-zabbix/partials/query.editor.html';
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _lodash2.default.uniq(_lodash2.default.map(scope.metric[metricList], 'name'));
}

View File

@@ -0,0 +1,116 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Convert Zabbix API history.get response to Grafana format
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
function convertHistory(history, items, addHostName, convertPointCallback) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }, ...
* ]
*/
// Group history by itemid
var grouped_history = _lodash2.default.groupBy(history, 'itemid');
var hosts = _lodash2.default.uniqBy(_lodash2.default.flatten(_lodash2.default.map(items, 'hosts')), 'hostid'); //uniqBy is needed to deduplicate
return _lodash2.default.map(grouped_history, function (hist, itemid) {
var item = _lodash2.default.find(items, { 'itemid': itemid });
var alias = item.name;
if (_lodash2.default.keys(hosts).length > 1 && addHostName) {
//only when actual multi hosts selected
var host = _lodash2.default.find(hosts, { 'hostid': item.hostid });
alias = host.name + ": " + alias;
}
return {
target: alias,
datapoints: _lodash2.default.map(hist, convertPointCallback)
};
});
}
function handleHistory(history, items) {
var addHostName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
return convertHistory(history, items, addHostName, convertHistoryPoint);
}
function handleTrends(history, items, valueType) {
var addHostName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
var convertPointCallback = _lodash2.default.partial(convertTrendPoint, valueType);
return convertHistory(history, items, addHostName, convertPointCallback);
}
function handleSLAResponse(itservice, slaProperty, slaObject) {
var targetSLA = slaObject[itservice.serviceid].sla[0];
if (slaProperty.property === 'status') {
var targetStatus = parseInt(slaObject[itservice.serviceid].status);
return {
target: itservice.name + ' ' + slaProperty.name,
datapoints: [[targetStatus, targetSLA.to * 1000]]
};
} else {
return {
target: itservice.name + ' ' + slaProperty.name,
datapoints: [[targetSLA[slaProperty.property], targetSLA.from * 1000], [targetSLA[slaProperty.property], targetSLA.to * 1000]]
};
}
}
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];
}
exports.default = {
handleHistory: handleHistory,
convertHistory: convertHistory,
handleTrends: handleTrends,
handleSLAResponse: handleSLAResponse
};
// Fix for backward compatibility with lodash 2.4
if (!_lodash2.default.uniqBy) {
_lodash2.default.uniqBy = _lodash2.default.uniq;
}

View File

@@ -0,0 +1,317 @@
"use strict";
var _module = require("../module");
var _datasource = require("../datasource");
var _q = require("q");
var _q2 = _interopRequireDefault(_q);
var _sinon = require("sinon");
var _sinon2 = _interopRequireDefault(_sinon);
var _lodash = require("lodash");
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('ZabbixDatasource', function () {
var ctx = {};
var defined = _sinon2.default.match.defined;
beforeEach(function () {
ctx.instanceSettings = {
jsonData: {
username: 'zabbix',
password: 'zabbix',
trends: true,
trendsFrom: '7d'
}
};
ctx.templateSrv = {};
ctx.alertSrv = {};
ctx.zabbix = function () {};
ctx.ds = new _module.Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.zabbix);
});
describe('When querying data', function () {
beforeEach(function () {
ctx.ds.replaceTemplateVars = function (str) {
return str;
};
});
ctx.options = {
targets: [{
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
item: { filter: "" }
}],
range: { from: 'now-7d', to: 'now' }
};
it('should return an empty array when no targets are set', function (done) {
var options = {
targets: [],
range: { from: 'now-6h', to: 'now' }
};
ctx.ds.query(options).then(function (result) {
expect(result.data).to.have.length(0);
done();
});
});
it('should use trends if it enabled and time more than trendsFrom', function (done) {
var ranges = ['now-7d', 'now-168h', 'now-1M', 'now-1y'];
_lodash2.default.forEach(ranges, function (range) {
ctx.options.range.from = range;
ctx.ds.queryNumericData = _sinon2.default.spy();
ctx.ds.query(ctx.options);
// Check that useTrends options is true
expect(ctx.ds.queryNumericData).to.have.been.calledWith(defined, defined, defined, true);
});
done();
});
it('shouldnt use trends if it enabled and time less than trendsFrom', function (done) {
var ranges = ['now-6d', 'now-167h', 'now-1h', 'now-30m', 'now-30s'];
_lodash2.default.forEach(ranges, function (range) {
ctx.options.range.from = range;
ctx.ds.queryNumericData = _sinon2.default.spy();
ctx.ds.query(ctx.options);
// Check that useTrends options is false
expect(ctx.ds.queryNumericData).to.have.been.calledWith(defined, defined, defined, false);
});
done();
});
});
describe('When replacing template variables', function () {
function testReplacingVariable(target, varValue, expectedResult, done) {
ctx.ds.templateSrv.replace = function () {
return (0, _datasource.zabbixTemplateFormat)(varValue);
};
var result = ctx.ds.replaceTemplateVars(target);
expect(result).to.equal(expectedResult);
done();
}
/*
* Alphanumerics, spaces, dots, dashes and underscores
* are allowed in Zabbix host name.
* 'AaBbCc0123 .-_'
*/
it('should return properly escaped regex', function (done) {
var target = '$host';
var template_var_value = 'AaBbCc0123 .-_';
var expected_result = '/^AaBbCc0123 \\.-_$/';
testReplacingVariable(target, template_var_value, expected_result, done);
});
/*
* Single-value variable
* $host = backend01
* $host => /^backend01|backend01$/
*/
it('should return proper regex for single value', function (done) {
var target = '$host';
var template_var_value = 'backend01';
var expected_result = '/^backend01$/';
testReplacingVariable(target, template_var_value, expected_result, done);
});
/*
* Multi-value variable
* $host = [backend01, backend02]
* $host => /^(backend01|backend01)$/
*/
it('should return proper regex for multi-value', function (done) {
var target = '$host';
var template_var_value = ['backend01', 'backend02'];
var expected_result = '/^(backend01|backend02)$/';
testReplacingVariable(target, template_var_value, expected_result, done);
});
});
describe('When invoking metricFindQuery()', function () {
beforeEach(function () {
ctx.ds.replaceTemplateVars = function (str) {
return str;
};
ctx.ds.zabbix = {
getGroups: function getGroups() {
return _q2.default.when([]);
},
getHosts: function getHosts() {
return _q2.default.when([]);
},
getApps: function getApps() {
return _q2.default.when([]);
},
getItems: function getItems() {
return _q2.default.when([]);
}
};
});
it('should return groups', function (done) {
var tests = [{ query: '*', expect: '/.*/' }, { query: '', expect: '' }, { query: 'Backend', expect: 'Backend' }, { query: 'Back*', expect: 'Back*' }];
var getGroups = _sinon2.default.spy(ctx.ds.zabbix, 'getGroups');
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = tests[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var test = _step.value;
ctx.ds.metricFindQuery(test.query);
expect(getGroups).to.have.been.calledWith(test.expect);
getGroups.reset();
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
done();
});
it('should return hosts', function (done) {
var tests = [{ query: '*.*', expect: '/.*/' }, { query: '.', expect: '' }, { query: 'Backend.*', expect: 'Backend' }, { query: 'Back*.', expect: 'Back*' }];
var getHosts = _sinon2.default.spy(ctx.ds.zabbix, 'getHosts');
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = tests[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var test = _step2.value;
ctx.ds.metricFindQuery(test.query);
expect(getHosts).to.have.been.calledWith(test.expect);
getHosts.reset();
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
done();
});
it('should return applications', function (done) {
var tests = [{ query: '*.*.*', expect: ['/.*/', '/.*/'] }, { query: '.*.', expect: ['', '/.*/'] }, { query: 'Backend.backend01.*', expect: ['Backend', 'backend01'] }, { query: 'Back*.*.', expect: ['Back*', '/.*/'] }];
var getApps = _sinon2.default.spy(ctx.ds.zabbix, 'getApps');
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = tests[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var test = _step3.value;
ctx.ds.metricFindQuery(test.query);
expect(getApps).to.have.been.calledWith(test.expect[0], test.expect[1]);
getApps.reset();
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
done();
});
it('should return items', function (done) {
var tests = [{ query: '*.*.*.*', expect: ['/.*/', '/.*/', ''] }, { query: '.*.*.*', expect: ['', '/.*/', ''] }, { query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', ''] }, { query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu'] }];
var getItems = _sinon2.default.spy(ctx.ds.zabbix, 'getItems');
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = tests[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var test = _step4.value;
ctx.ds.metricFindQuery(test.query);
expect(getItems).to.have.been.calledWith(test.expect[0], test.expect[1], test.expect[2]);
getItems.reset();
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
done();
});
it('should invoke method with proper arguments', function (done) {
var query = '*.*';
var getHosts = _sinon2.default.spy(ctx.ds.zabbix, 'getHosts');
ctx.ds.metricFindQuery(query);
expect(getHosts).to.have.been.calledWith('/.*/');
done();
});
});
});

View File

@@ -0,0 +1,135 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parse = parse;
exports.isValid = isValid;
exports.parseDateMath = parseDateMath;
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var units = ['y', 'M', 'w', 'd', 'h', 'm', 's'];
function parse(text, roundUp) {
if (!text) {
return undefined;
}
if (_moment2.default.isMoment(text)) {
return text;
}
if (_lodash2.default.isDate(text)) {
return (0, _moment2.default)(text);
}
var time;
var mathString = '';
var index;
var parseString;
if (text.substring(0, 3) === 'now') {
time = (0, _moment2.default)();
mathString = text.substring('now'.length);
} else {
index = text.indexOf('||');
if (index === -1) {
parseString = text;
mathString = ''; // nothing else
} else {
parseString = text.substring(0, index);
mathString = text.substring(index + 2);
}
// We're going to just require ISO8601 timestamps, k?
time = (0, _moment2.default)(parseString, _moment2.default.ISO_8601);
}
if (!mathString.length) {
return time;
}
return parseDateMath(mathString, time, roundUp);
}
function isValid(text) {
var date = parse(text);
if (!date) {
return false;
}
if (_moment2.default.isMoment(date)) {
return date.isValid();
}
return false;
}
function parseDateMath(mathString, time, roundUp) {
var dateTime = time;
var i = 0;
var len = mathString.length;
while (i < len) {
var c = mathString.charAt(i++);
var type;
var num;
var unit;
if (c === '/') {
type = 0;
} else if (c === '+') {
type = 1;
} else if (c === '-') {
type = 2;
} else {
return undefined;
}
if (isNaN(mathString.charAt(i))) {
num = 1;
} else if (mathString.length === 2) {
num = mathString.charAt(i);
} else {
var numFrom = i;
while (!isNaN(mathString.charAt(i))) {
i++;
if (i > 10) {
return undefined;
}
}
num = parseInt(mathString.substring(numFrom, i), 10);
}
if (type === 0) {
// rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
if (num !== 1) {
return undefined;
}
}
unit = mathString.charAt(i++);
if (!_lodash2.default.includes(units, unit)) {
return undefined;
} else {
if (type === 0) {
if (roundUp) {
dateTime.endOf(unit);
} else {
dateTime.startOf(unit);
}
} else if (type === 1) {
dateTime.add(num, unit);
} else if (type === 2) {
dateTime.subtract(num, unit);
}
}
}
return dateTime;
}

View File

@@ -0,0 +1,66 @@
'use strict';
var _prunk = require('prunk');
var _prunk2 = _interopRequireDefault(_prunk);
var _jsdom = require('jsdom');
var _chai = require('chai');
var _chai2 = _interopRequireDefault(_chai);
var _sinonChai = require('sinon-chai');
var _sinonChai2 = _interopRequireDefault(_sinonChai);
var _datemath = require('./modules/datemath');
var dateMath = _interopRequireWildcard(_datemath);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Mock angular module
// import sinon from 'sinon';
var angularMocks = {
module: function module() {
return {
directive: function directive() {},
service: function service() {},
factory: function factory() {}
};
}
}; // JSHint options
/* globals global: false */
var datemathMock = {
parse: dateMath.parse,
parseDateMath: dateMath.parseDateMath,
isValid: dateMath.isValid
};
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
_prunk2.default.mock('./css/query-editor.css!', 'no css, dude.');
_prunk2.default.mock('app/plugins/sdk', {
QueryCtrl: null
});
_prunk2.default.mock('app/core/utils/datemath', datemathMock);
_prunk2.default.mock('angular', angularMocks);
_prunk2.default.mock('jquery', 'module not found');
// Setup jsdom
// Required for loading angularjs
global.document = (0, _jsdom.jsdom)('<html><head><script></script></head><body></body></html>');
global.window = global.document.parentWindow;
global.navigator = window.navigator = {};
global.Node = window.Node;
// Setup Chai
_chai2.default.should();
_chai2.default.use(_sinonChai2.default);
global.assert = _chai2.default.assert;
global.expect = _chai2.default.expect;

151
dist/test/datasource-zabbix/utils.js vendored Normal file
View File

@@ -0,0 +1,151 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.regexPattern = undefined;
exports.expandItemName = expandItemName;
exports.isRegex = isRegex;
exports.isTemplateVariable = isTemplateVariable;
exports.buildRegex = buildRegex;
exports.escapeRegex = escapeRegex;
exports.parseInterval = parseInterval;
exports.parseTimeShiftInterval = parseTimeShiftInterval;
exports.formatAcknowledges = formatAcknowledges;
exports.convertToZabbixAPIUrl = convertToZabbixAPIUrl;
exports.callOnce = callOnce;
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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"
*/
function expandItemName(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 = exports.regexPattern = /^\/(.*)\/([gmi]*)$/m;
function isRegex(str) {
return regexPattern.test(str);
}
function isTemplateVariable(str, templateVariables) {
var variablePattern = /^\$\w+/;
if (variablePattern.test(str)) {
var variables = _lodash2.default.map(templateVariables, function (variable) {
return '$' + variable.name;
});
return _lodash2.default.includes(variables, str);
} else {
return false;
}
}
function buildRegex(str) {
var matches = str.match(regexPattern);
var pattern = matches[1];
var flags = matches[2] !== "" ? matches[2] : undefined;
return new RegExp(pattern, flags);
}
// Need for template variables replace
// From Grafana's templateSrv.js
function escapeRegex(value) {
return value.replace(/[\\^$*+?.()|[\]{}\/]/g, '\\$&');
}
function parseInterval(interval) {
var intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g;
var momentInterval = intervalPattern.exec(interval);
return _moment2.default.duration(Number(momentInterval[1]), momentInterval[2]).valueOf();
}
function parseTimeShiftInterval(interval) {
var intervalPattern = /^([\+\-]*)([\d]+)(y|M|w|d|h|m|s)/g;
var momentInterval = intervalPattern.exec(interval);
var duration = 0;
if (momentInterval[1] === '+') {
duration = 0 - _moment2.default.duration(Number(momentInterval[2]), momentInterval[3]).valueOf();
} else {
duration = _moment2.default.duration(Number(momentInterval[2]), momentInterval[3]).valueOf();
}
return duration;
}
/**
* 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>';
_lodash2.default.each(_lodash2.default.map(acknowledges, function (ack) {
var timestamp = _moment2.default.unix(ack.clock);
return '<tr><td><i>' + timestamp.format("DD MMM YYYY HH:mm:ss") + '</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 '';
}
}
function convertToZabbixAPIUrl(url) {
var zabbixAPIUrlPattern = /.*api_jsonrpc.php$/;
var trimSlashPattern = /(.*?)[\/]*$/;
if (url.match(zabbixAPIUrlPattern)) {
return url;
} else {
return url.replace(trimSlashPattern, "$1");
}
}
/**
* Wrap function to prevent multiple calls
* when waiting for result.
*/
function callOnce(func, promiseKeeper) {
return function () {
if (!promiseKeeper) {
promiseKeeper = Promise.resolve(func.apply(this, arguments).then(function (result) {
promiseKeeper = null;
return result;
}));
}
return promiseKeeper;
};
}
// Fix for backward compatibility with lodash 2.4
if (!_lodash2.default.includes) {
_lodash2.default.includes = _lodash2.default.contains;
}

266
dist/test/datasource-zabbix/zabbix.js vendored Normal file
View File

@@ -0,0 +1,266 @@
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils);
require('./zabbixAPI.service.js');
require('./zabbixCachingProxy.service.js');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// Use factory() instead service() for multiple data sources support.
// Each Zabbix data source instance should initialize its own API instance.
/** @ngInject */
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
var Zabbix = function () {
function Zabbix(url, username, password, basicAuth, withCredentials, cacheTTL) {
_classCallCheck(this, Zabbix);
// Initialize Zabbix API
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
// Initialize caching proxy for requests
var cacheOptions = {
enabled: true,
ttl: cacheTTL
};
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions);
// Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAcknowledges = this.zabbixAPI.getAcknowledges.bind(this.zabbixAPI);
this.getITService = this.zabbixAPI.getITService.bind(this.zabbixAPI);
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
}
_createClass(Zabbix, [{
key: 'getItemsFromTarget',
value: function getItemsFromTarget(target, options) {
var parts = ['group', 'host', 'application', 'item'];
var filters = _lodash2.default.map(parts, function (p) {
return target[p].filter;
});
return this.getItems.apply(this, _toConsumableArray(filters).concat([options]));
}
}, {
key: 'getAllGroups',
value: function getAllGroups() {
return this.cachingProxy.getGroups();
}
}, {
key: 'getGroups',
value: function getGroups(groupFilter) {
return this.getAllGroups().then(function (groups) {
return findByFilter(groups, groupFilter);
});
}
/**
* Get list of host belonging to given groups.
*/
}, {
key: 'getAllHosts',
value: function getAllHosts(groupFilter) {
var _this = this;
return this.getGroups(groupFilter).then(function (groups) {
var groupids = _lodash2.default.map(groups, 'groupid');
return _this.cachingProxy.getHosts(groupids);
});
}
}, {
key: 'getHosts',
value: function getHosts(groupFilter, hostFilter) {
return this.getAllHosts(groupFilter).then(function (hosts) {
return findByFilter(hosts, hostFilter);
});
}
/**
* Get list of applications belonging to given groups and hosts.
*/
}, {
key: 'getAllApps',
value: function getAllApps(groupFilter, hostFilter) {
var _this2 = this;
return this.getHosts(groupFilter, hostFilter).then(function (hosts) {
var hostids = _lodash2.default.map(hosts, 'hostid');
return _this2.cachingProxy.getApps(hostids);
});
}
}, {
key: 'getApps',
value: function getApps(groupFilter, hostFilter, appFilter) {
var _this3 = this;
return this.getHosts(groupFilter, hostFilter).then(function (hosts) {
var hostids = _lodash2.default.map(hosts, 'hostid');
if (appFilter) {
return _this3.cachingProxy.getApps(hostids).then(function (apps) {
return filterByQuery(apps, appFilter);
});
} else {
return {
appFilterEmpty: true,
hostids: hostids
};
}
});
}
}, {
key: 'getAllItems',
value: function getAllItems(groupFilter, hostFilter, appFilter) {
var _this4 = this;
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
return this.getApps(groupFilter, hostFilter, appFilter).then(function (apps) {
if (apps.appFilterEmpty) {
return _this4.cachingProxy.getItems(apps.hostids, undefined, options.itemtype);
} else {
var appids = _lodash2.default.map(apps, 'applicationid');
return _this4.cachingProxy.getItems(undefined, appids, options.itemtype);
}
}).then(function (items) {
if (!options.showDisabledItems) {
items = _lodash2.default.filter(items, { 'status': '0' });
}
return items;
});
}
}, {
key: 'getItems',
value: function getItems(groupFilter, hostFilter, appFilter, itemFilter) {
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
return this.getAllItems(groupFilter, hostFilter, appFilter, options).then(function (items) {
return filterByQuery(items, itemFilter);
});
}
/**
* Build query - convert target filters to array of Zabbix items
*/
}, {
key: 'getTriggers',
value: function getTriggers(groupFilter, hostFilter, appFilter, showTriggers) {
var _this5 = this;
var promises = [this.getGroups(groupFilter), this.getHosts(groupFilter, hostFilter), this.getApps(groupFilter, hostFilter, appFilter)];
return Promise.all(promises).then(function (results) {
var filteredGroups = results[0];
var filteredHosts = results[1];
var filteredApps = results[2];
var query = {};
if (appFilter) {
query.applicationids = _lodash2.default.flatten(_lodash2.default.map(filteredApps, 'applicationid'));
}
if (hostFilter) {
query.hostids = _lodash2.default.map(filteredHosts, 'hostid');
}
if (groupFilter) {
query.groupids = _lodash2.default.map(filteredGroups, 'groupid');
}
return query;
}).then(function (query) {
return _this5.zabbixAPI.getTriggers(query.groupids, query.hostids, query.applicationids, showTriggers);
});
}
}]);
return Zabbix;
}();
return Zabbix;
}
_angular2.default.module('grafana.services').factory('Zabbix', ZabbixFactory);
///////////////////////////////////////////////////////////////////////////////
/**
* Find group, host, app or item by given name.
* @param list list of groups, apps or other
* @param name visible name
* @return array with finded element or undefined
*/
function findByName(list, name) {
var finded = _lodash2.default.find(list, { 'name': name });
if (finded) {
return [finded];
} else {
return undefined;
}
}
/**
* Different hosts can contains applications and items with same name.
* For this reason use _.filter, which return all elements instead _.find,
* which return only first finded.
* @param {[type]} list list of elements
* @param {[type]} name app name
* @return {[type]} array with finded element or undefined
*/
function filterByName(list, name) {
var finded = _lodash2.default.filter(list, { 'name': name });
if (finded) {
return finded;
} else {
return undefined;
}
}
function filterByRegex(list, regex) {
var filterPattern = utils.buildRegex(regex);
return _lodash2.default.filter(list, function (zbx_obj) {
return filterPattern.test(zbx_obj.name);
});
}
function findByFilter(list, filter) {
if (utils.isRegex(filter)) {
return filterByRegex(list, filter);
} else {
return findByName(list, filter);
}
}
function filterByQuery(list, filter) {
if (utils.isRegex(filter)) {
return filterByRegex(list, filter);
} else {
return filterByName(list, filter);
}
}

View File

@@ -0,0 +1,436 @@
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils);
require('./zabbixAPICore.service');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/** @ngInject */
function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
/**
* Zabbix API Wrapper.
* Creates Zabbix API instance with given parameters (url, credentials and other).
* Wraps API calls and provides high-level methods.
*/
var ZabbixAPI = function () {
function ZabbixAPI(api_url, username, password, basicAuth, withCredentials) {
_classCallCheck(this, ZabbixAPI);
this.url = api_url;
this.username = username;
this.password = password;
this.auth = "";
this.requestOptions = {
basicAuth: basicAuth,
withCredentials: withCredentials
};
this.loginPromise = null;
this.loginErrorCount = 0;
this.maxLoginAttempts = 3;
this.alertSrv = alertSrv;
this.zabbixAPICore = zabbixAPICoreService;
this.getTrend = this.getTrend_ZBXNEXT1193;
//getTrend = getTrend_30;
}
//////////////////////////
// Core method wrappers //
//////////////////////////
_createClass(ZabbixAPI, [{
key: 'request',
value: function request(method, params) {
var _this = this;
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth).catch(function (error) {
if (isNotAuthorized(error.data)) {
// Handle auth errors
_this.loginErrorCount++;
if (_this.loginErrorCount > _this.maxLoginAttempts) {
_this.loginErrorCount = 0;
return null;
} else {
return _this.loginOnce().then(function () {
return _this.request(method, params);
});
}
} else {
// Handle API errors
var message = error.data ? error.data : error.statusText;
_this.alertAPIError(message);
}
});
}
}, {
key: 'alertAPIError',
value: function alertAPIError(message) {
var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000;
this.alertSrv.set("Zabbix API Error", message, 'error', timeout);
}
/**
* When API unauthenticated or auth token expired each request produce login()
* call. But auth token is common to all requests. This function wraps login() method
* and call it once. If login() already called just wait for it (return its promise).
* @return login promise
*/
}, {
key: 'loginOnce',
value: function loginOnce() {
var _this2 = this;
if (!this.loginPromise) {
this.loginPromise = Promise.resolve(this.login().then(function (auth) {
_this2.auth = auth;
_this2.loginPromise = null;
return auth;
}));
}
return this.loginPromise;
}
/**
* Get authentication token.
*/
}, {
key: 'login',
value: function login() {
return this.zabbixAPICore.login(this.url, this.username, this.password, this.requestOptions);
}
/**
* Get Zabbix API version
*/
}, {
key: 'getVersion',
value: function getVersion() {
return this.zabbixAPICore.getVersion(this.url, this.requestOptions);
}
////////////////////////////////
// Zabbix API method wrappers //
////////////////////////////////
}, {
key: 'acknowledgeEvent',
value: function acknowledgeEvent(eventid, message) {
var params = {
eventids: eventid,
message: message
};
return this.request('event.acknowledge', params);
}
}, {
key: 'getGroups',
value: function getGroups() {
var params = {
output: ['name'],
sortfield: 'name',
real_hosts: true
};
return this.request('hostgroup.get', params);
}
}, {
key: 'getHosts',
value: function getHosts(groupids) {
var params = {
output: ['name', 'host'],
sortfield: 'name'
};
if (groupids) {
params.groupids = groupids;
}
return this.request('host.get', params);
}
}, {
key: 'getApps',
value: function getApps(hostids) {
var params = {
output: ['applicationid', 'name'],
hostids: hostids
};
return this.request('application.get', params);
}
/**
* Get Zabbix items
* @param {[type]} hostids host ids
* @param {[type]} appids application ids
* @param {String} itemtype 'num' or 'text'
* @return {[type]} array of items
*/
}, {
key: 'getItems',
value: function getItems(hostids, appids, itemtype) {
var params = {
output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'],
sortfield: 'name',
webitems: true,
filter: {},
selectHosts: ['hostid', 'name']
};
if (hostids) {
params.hostids = hostids;
}
if (appids) {
params.applicationids = appids;
}
if (itemtype === 'num') {
// Return only numeric metrics
params.filter.value_type = [0, 3];
}
if (itemtype === 'text') {
// Return only text metrics
params.filter.value_type = [1, 2, 4];
}
return this.request('item.get', params).then(expandItems);
function expandItems(items) {
items.forEach(function (item) {
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
return items;
}
}
}, {
key: 'getLastValue',
value: function getLastValue(itemid) {
var params = {
output: ['lastvalue'],
itemids: itemid
};
return this.request('item.get', params).then(function (items) {
return items.length ? items[0].lastvalue : null;
});
}
/**
* Perform history query from Zabbix API
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} timeFrom Time in seconds
* @param {Number} timeTill Time in seconds
* @return {Array} Array of Zabbix history objects
*/
}, {
key: 'getHistory',
value: function getHistory(items, timeFrom, timeTill) {
var _this3 = this;
// Group items by value type and perform request for each value type
var grouped_items = _lodash2.default.groupBy(items, 'value_type');
var promises = _lodash2.default.map(grouped_items, function (items, value_type) {
var itemids = _lodash2.default.map(items, 'itemid');
var params = {
output: 'extend',
history: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: timeFrom
};
// Relative queries (e.g. last hour) don't include an end time
if (timeTill) {
params.time_till = timeTill;
}
return _this3.request('history.get', params);
});
return Promise.all(promises).then(_lodash2.default.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
*/
}, {
key: 'getTrend_ZBXNEXT1193',
value: function getTrend_ZBXNEXT1193(items, timeFrom, timeTill) {
var _this4 = this;
// Group items by value type and perform request for each value type
var grouped_items = _lodash2.default.groupBy(items, 'value_type');
var promises = _lodash2.default.map(grouped_items, function (items, value_type) {
var itemids = _lodash2.default.map(items, 'itemid');
var params = {
output: 'extend',
trend: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: timeFrom
};
// Relative queries (e.g. last hour) don't include an end time
if (timeTill) {
params.time_till = timeTill;
}
return _this4.request('trend.get', params);
});
return Promise.all(promises).then(_lodash2.default.flatten);
}
}, {
key: 'getTrend_30',
value: function getTrend_30(items, time_from, time_till, value_type) {
var self = this;
var itemids = _lodash2.default.map(items, 'itemid');
var params = {
output: ["itemid", "clock", value_type],
itemids: itemids,
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);
}
}, {
key: 'getITService',
value: function getITService(serviceids) {
var params = {
output: 'extend',
serviceids: serviceids
};
return this.request('service.get', params);
}
}, {
key: 'getSLA',
value: function getSLA(serviceids, timeFrom, timeTo) {
var params = {
serviceids: serviceids,
intervals: [{
from: timeFrom,
to: timeTo
}]
};
return this.request('service.getsla', params);
}
}, {
key: 'getTriggers',
value: function getTriggers(groupids, hostids, applicationids, showTriggers, timeFrom, timeTo) {
var params = {
output: 'extend',
groupids: groupids,
hostids: hostids,
applicationids: applicationids,
expandDescription: true,
expandData: true,
expandComment: true,
monitored: true,
skipDependent: true,
//only_true: true,
filter: {
value: 1
},
selectGroups: ['name'],
selectHosts: ['name', 'host'],
selectItems: ['name', 'key_', 'lastvalue'],
selectLastEvent: 'extend'
};
if (showTriggers) {
params.filter.value = showTriggers;
}
if (timeFrom || timeTo) {
params.lastChangeSince = timeFrom;
params.lastChangeTill = timeTo;
}
return this.request('trigger.get', params);
}
}, {
key: 'getEvents',
value: function getEvents(objectids, timeFrom, timeTo, showEvents) {
var params = {
output: 'extend',
time_from: timeFrom,
time_till: timeTo,
objectids: objectids,
select_acknowledges: 'extend',
selectHosts: 'extend',
value: showEvents
};
return this.request('event.get', params);
}
}, {
key: 'getAcknowledges',
value: function getAcknowledges(eventids) {
var params = {
output: 'extend',
eventids: eventids,
preservekeys: true,
select_acknowledges: 'extend',
sortfield: 'clock',
sortorder: 'DESC'
};
return this.request('event.get', params).then(function (events) {
return _lodash2.default.filter(events, function (event) {
return event.acknowledges.length;
});
});
}
}]);
return ZabbixAPI;
}();
return ZabbixAPI;
}
function isNotAuthorized(message) {
return message === "Session terminated, re-login, please." || message === "Not authorised." || message === "Not authorized.";
}
_angular2.default.module('grafana.services').factory('zabbixAPIService', ZabbixAPIServiceFactory);

View File

@@ -0,0 +1,142 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ZabbixAPIError = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
* General Zabbix API methods
*/
var _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ZabbixAPICoreService = function () {
/** @ngInject */
function ZabbixAPICoreService(backendSrv) {
_classCallCheck(this, ZabbixAPICoreService);
this.backendSrv = backendSrv;
}
/**
* Request data from Zabbix API
* @return {object} response.result
*/
_createClass(ZabbixAPICoreService, [{
key: 'request',
value: function request(api_url, method, params, options, auth) {
var requestData = {
jsonrpc: '2.0',
method: method,
params: params,
id: 1
};
if (auth === "") {
// Reject immediately if not authenticated
return Promise.reject(new ZabbixAPIError({ data: "Not authorised." }));
} else if (auth) {
// Set auth parameter only if it needed
requestData.auth = auth;
}
var requestOptions = {
method: 'POST',
url: api_url,
data: requestData,
headers: {
'Content-Type': 'application/json'
}
};
// Set request options for basic auth
if (options.basicAuth || options.withCredentials) {
requestOptions.withCredentials = true;
}
if (options.basicAuth) {
requestOptions.headers.Authorization = options.basicAuth;
}
return this.datasourceRequest(requestOptions);
}
}, {
key: 'datasourceRequest',
value: function datasourceRequest(requestOptions) {
return this.backendSrv.datasourceRequest(requestOptions).then(function (response) {
if (!response.data) {
return Promise.reject(new ZabbixAPIError({ data: "General Error, no data" }));
} else if (response.data.error) {
// Handle Zabbix API errors
return Promise.reject(new ZabbixAPIError(response.data.error));
}
// Success
return response.data.result;
});
}
/**
* Get authentication token.
* @return {string} auth token
*/
}, {
key: 'login',
value: function login(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
*/
}, {
key: 'getVersion',
value: function getVersion(api_url, options) {
return this.request(api_url, 'apiinfo.version', [], options);
}
}]);
return ZabbixAPICoreService;
}();
// Define zabbix API exception type
var ZabbixAPIError = exports.ZabbixAPIError = function () {
function ZabbixAPIError(error) {
_classCallCheck(this, ZabbixAPIError);
this.code = error.code;
this.name = error.data;
this.message = error.data;
this.data = error.data;
}
_createClass(ZabbixAPIError, [{
key: 'toString',
value: function toString() {
return this.name + ": " + this.message;
}
}]);
return ZabbixAPIError;
}();
_angular2.default.module('grafana.services').service('zabbixAPICoreService', ZabbixAPICoreService);

View File

@@ -0,0 +1,215 @@
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// Use factory() instead service() for multiple datasources support.
// Each datasource instance must initialize its own cache.
/** @ngInject */
function ZabbixCachingProxyFactory() {
var ZabbixCachingProxy = function () {
function ZabbixCachingProxy(zabbixAPI, cacheOptions) {
_classCallCheck(this, ZabbixCachingProxy);
this.zabbixAPI = zabbixAPI;
this.cacheEnabled = cacheOptions.enabled;
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
// Internal objects for data storing
this.cache = {
groups: {},
hosts: {},
applications: {},
items: {},
history: {},
trends: {}
};
this.historyPromises = {};
// Don't run duplicated history requests
this.getHistory = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getHistory, this.zabbixAPI), this.historyPromises, getHistoryRequestHash);
// Don't run duplicated requests
this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getGroups, this.zabbixAPI), this.groupPromises, getRequestHash);
this.hostPromises = {};
this.getHostsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getHosts, this.zabbixAPI), this.hostPromises, getRequestHash);
this.appPromises = {};
this.getAppsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getApps, this.zabbixAPI), this.appPromises, getRequestHash);
this.itemPromises = {};
this.getItemsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getItems, this.zabbixAPI), this.itemPromises, getRequestHash);
}
_createClass(ZabbixCachingProxy, [{
key: 'isExpired',
value: function isExpired(cacheObject) {
if (cacheObject) {
var object_age = Date.now() - cacheObject.timestamp;
return !(cacheObject.timestamp && object_age < this.ttl);
} else {
return true;
}
}
/**
* Check that result is present in cache and up to date
* or send request to API.
*/
}, {
key: 'proxyRequest',
value: function proxyRequest(request, params, cacheObject) {
var hash = getRequestHash(params);
if (this.cacheEnabled && !this.isExpired(cacheObject[hash])) {
return Promise.resolve(cacheObject[hash].value);
} else {
return request.apply(undefined, _toConsumableArray(params)).then(function (result) {
cacheObject[hash] = {
value: result,
timestamp: Date.now()
};
return result;
});
}
}
}, {
key: 'getGroups',
value: function getGroups() {
return this.proxyRequest(this.getGroupsOnce, [], this.cache.groups);
}
}, {
key: 'getHosts',
value: function getHosts(groupids) {
return this.proxyRequest(this.getHostsOnce, [groupids], this.cache.hosts);
}
}, {
key: 'getApps',
value: function getApps(hostids) {
return this.proxyRequest(this.getAppsOnce, [hostids], this.cache.applications);
}
}, {
key: 'getItems',
value: function getItems(hostids, appids, itemtype) {
var params = [hostids, appids, itemtype];
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
}
}, {
key: 'getHistoryFromCache',
value: function getHistoryFromCache(items, time_from, time_till) {
var historyStorage = this.cache.history;
var full_history;
var expired = _lodash2.default.filter(_lodash2.default.keyBy(items, 'itemid'), function (item, itemid) {
return !historyStorage[itemid];
});
if (expired.length) {
return this.zabbixAPI.getHistory(expired, time_from, time_till).then(function (history) {
var grouped_history = _lodash2.default.groupBy(history, 'itemid');
_lodash2.default.forEach(expired, function (item) {
var itemid = item.itemid;
historyStorage[itemid] = item;
historyStorage[itemid].time_from = time_from;
historyStorage[itemid].time_till = time_till;
historyStorage[itemid].history = grouped_history[itemid];
});
full_history = _lodash2.default.map(items, function (item) {
return historyStorage[item.itemid].history;
});
return _lodash2.default.flatten(full_history, true);
});
} else {
full_history = _lodash2.default.map(items, function (item) {
return historyStorage[item.itemid].history;
});
return Promise.resolve(_lodash2.default.flatten(full_history, true));
}
}
}, {
key: 'getHistoryFromAPI',
value: function getHistoryFromAPI(items, time_from, time_till) {
return this.zabbixAPI.getHistory(items, time_from, time_till);
}
}]);
return ZabbixCachingProxy;
}();
return ZabbixCachingProxy;
}
_angular2.default.module('grafana.services').factory('ZabbixCachingProxy', ZabbixCachingProxyFactory);
/**
* Wrap zabbix API request to prevent multiple calls
* with same params when waiting for result.
*/
function callAPIRequestOnce(func, promiseKeeper, argsHashFunc) {
return function () {
var hash = argsHashFunc(arguments);
if (!promiseKeeper[hash]) {
promiseKeeper[hash] = Promise.resolve(func.apply(this, arguments).then(function (result) {
promiseKeeper[hash] = null;
return result;
}));
}
return promiseKeeper[hash];
};
}
function getRequestHash(args) {
var requestStamp = _lodash2.default.map(args, function (arg) {
if (arg === undefined) {
return 'undefined';
} else {
if (_lodash2.default.isArray(arg)) {
return arg.sort().toString();
} else {
return arg.toString();
}
}
}).join();
return requestStamp.getHash();
}
function getHistoryRequestHash(args) {
var itemids = _lodash2.default.map(args[0], 'itemid');
var stamp = itemids.join() + args[1] + args[2];
return stamp.getHash();
}
String.prototype.getHash = function () {
var hash = 0,
i,
chr,
len;
if (this.length !== 0) {
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
}
return hash;
};
// Fix for backward compatibility with lodash 2.4
if (!_lodash2.default.keyBy) {
_lodash2.default.keyBy = _lodash2.default.indexBy;
}