Merge branch 'ES6' into cache-refactor

This commit is contained in:
Alexander Zobnin
2016-03-25 18:11:21 +03:00
43 changed files with 3027 additions and 3227 deletions

15
.gitignore vendored
View File

@@ -12,3 +12,18 @@
# Builded docs
docs/site/
node_modules
npm-debug.log
coverage/
.aws-config.json
awsconfig
/emails/dist
/public_gen
/tmp
vendor/phantomjs/phantomjs
dist/
# locally required config files
public/css/*.min.css

View File

@@ -4,7 +4,8 @@
"bitwise":false,
"curly": true,
"eqnull": true,
"globalstrict": true,
"strict": true,
"module": true,
"devel": true,
"eqeqeq": true,
"forin": false,
@@ -12,7 +13,7 @@
"supernew": true,
"expr": true,
"indent": 2,
"latedef": true,
"latedef": false,
"newcap": true,
"noarg": true,
"noempty": true,
@@ -25,9 +26,11 @@
"unused": true,
"maxdepth": 6,
"maxlen": 140,
"esnext": true,
"globals": {
"System": true,
"Promise": true,
"define": true,
"require": true,
"Chromath": false,

78
Gruntfile.js Normal file
View File

@@ -0,0 +1,78 @@
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
grunt.loadNpmTasks('grunt-execute');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.initConfig({
clean: ["dist"],
copy: {
src_to_dist: {
cwd: 'src',
expand: true,
src: [
'**/*',
'!datasource-zabbix/*.js',
'!panel-triggers/*.js',
'!**/*.scss'
],
dest: 'dist/'
},
pluginDef: {
expand: true,
src: ['plugin.json', 'README.md'],
dest: 'dist/',
}
},
watch: {
rebuild_all: {
files: ['src/**/*', 'plugin.json'],
tasks: ['default'],
options: {spawn: false}
},
},
babel: {
options: {
sourceMap: true,
presets: ["es2015"],
plugins: ['transform-es2015-modules-systemjs', "transform-es2015-for-of"],
},
dist: {
files: [{
cwd: 'src',
expand: true,
src: [
'datasource-zabbix/*.js',
'panel-triggers/*.js',
],
dest: 'dist/'
}]
},
},
sass: {
options: {
sourceMap: true
},
dist: {
files: {
'dist/panel-triggers/css/panel_triggers.css' : 'src/panel-triggers/sass/panel_triggers.scss',
}
}
}
});
grunt.registerTask('default', [
'clean',
'copy:src_to_dist',
'copy:pluginDef',
'babel',
'sass'
]);
};

38
package.json Normal file
View File

@@ -0,0 +1,38 @@
{
"name": "grafana-zabbix",
"private": false,
"version": "3.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alexanderzobnin/grafana-zabbix.git"
},
"author": "Alexander Zobnin",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/alexanderzobnin/grafana-zabbix/issues"
},
"devDependencies": {
"grunt": "~0.4.5",
"babel": "~6.5.1",
"grunt-babel": "~6.0.0",
"grunt-sass": "^1.1.0",
"grunt-contrib-copy": "~0.8.2",
"grunt-contrib-watch": "^0.6.1",
"grunt-contrib-uglify": "~0.11.0",
"grunt-systemjs-builder": "^0.2.5",
"load-grunt-tasks": "~3.2.0",
"grunt-execute": "~0.2.2",
"grunt-contrib-clean": "~0.6.0"
},
"dependencies": {
"babel-plugin-transform-es2015-modules-systemjs": "^6.5.0",
"babel-plugin-transform-es2015-for-of": "^6.5.0",
"babel-preset-es2015": "^6.5.0",
"lodash": "~4.0.0"
},
"homepage": "http://grafana-zabbix.org"
}

View File

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

View File

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

View File

@@ -1,429 +0,0 @@
define([
'angular',
'lodash',
'app/core/utils/datemath',
'./utils',
'./metricFunctions',
'./queryProcessor',
'./directives',
'./zabbixAPI',
'./helperFunctions',
'./dataProcessingService',
'./zabbixCache',
'./queryCtrl',
'./addMetricFunction',
'./metricFunctionEditor'
],
function (angular, _, dateMath, utils, metricFunctions) {
'use strict';
/** @ngInject */
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv,
ZabbixAPI, ZabbixCachingProxy, QueryProcessor, DataProcessingService) {
// 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);
// Initialize Zabbix API
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
// Initialize cache service
this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI, this.cacheTTL);
// Initialize query builder
this.queryProcessor = new QueryProcessor(this.zabbixCache);
console.log(this.zabbixCache);
////////////////////////
// Datasource methods //
////////////////////////
/**
* Test connection to Zabbix API
* @return {object} Connection status and Zabbix API version
*/
this.testDatasource = function() {
var self = this;
return this.zabbixAPI.getVersion().then(function (version) {
return self.zabbixAPI.login().then(function (auth) {
if (auth) {
return {
status: "success",
title: "Success",
message: "Zabbix API version: " + version
};
} else {
return {
status: "error",
title: "Invalid user name or password",
message: "Zabbix API version: " + version
};
}
}, function(error) {
console.log(error);
return {
status: "error",
title: "Connection failed",
message: error
};
});
},
function(error) {
console.log(error);
return {
status: "error",
title: "Connection failed",
message: "Could not connect to given url"
};
});
};
/**
* Query panel data. Calls for each panel in dashboard.
* @param {Object} options Contains time range, targets and other info.
* @return {Object} Grafana metrics object with timeseries data for each target.
*/
this.query = function(options) {
var self = this;
// get from & to in seconds
var from = Math.ceil(dateMath.parse(options.range.from) / 1000);
var to = Math.ceil(dateMath.parse(options.range.to) / 1000);
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
// Create request for each target
var promises = _.map(options.targets, function(target) {
if (target.mode !== 1) {
// Don't request undefined and hidden targets
if (target.hide || !target.group ||
!target.host || !target.item) {
return [];
}
// Replace templated variables
var groupFilter = templateSrv.replace(target.group.filter, options.scopedVars);
var hostFilter = templateSrv.replace(target.host.filter, options.scopedVars);
var appFilter = templateSrv.replace(target.application.filter, options.scopedVars);
var itemFilter = templateSrv.replace(target.item.filter, options.scopedVars);
// Query numeric data
if (!target.mode || target.mode === 0) {
// Build query in asynchronous manner
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter)
.then(function(items) {
// Add hostname for items from multiple hosts
var addHostName = target.host.isRegex;
var getHistory;
// Use trends
if ((from < useTrendsFrom) && self.trends) {
// Find trendValue() function and get specified trend value
var trendFunctions = _.map(metricFunctions.getCategories()['Trends'], 'name');
var trendValueFunc = _.find(target.functions, function(func) {
return _.contains(trendFunctions, func.def.name);
});
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
getHistory = self.zabbixAPI.getTrend(items, from, to).then(function(history) {
return self.queryProcessor.handleTrends(history, addHostName, valueType);
});
} else {
// Use history
getHistory = self.zabbixCache.getHistory(items, from, to).then(function(history) {
return self.queryProcessor.handleHistory(history, addHostName);
});
}
return getHistory.then(function (timeseries_data) {
timeseries_data = _.map(timeseries_data, function (timeseries) {
// Filter only transform functions
var transformFunctions = bindFunctionDefs(target.functions, 'Transform');
// Metric data processing
var dp = timeseries.datapoints;
for (var i = 0; i < transformFunctions.length; i++) {
dp = transformFunctions[i](dp);
}
timeseries.datapoints = dp;
return timeseries;
});
// Aggregations
var aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate');
var dp = _.map(timeseries_data, 'datapoints');
if (aggregationFunctions.length) {
for (var i = 0; i < aggregationFunctions.length; i++) {
dp = aggregationFunctions[i](dp);
}
var lastAgg = _.findLast(target.functions, function(func) {
return _.contains(
_.map(metricFunctions.getCategories()['Aggregate'], 'name'), func.def.name);
});
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
}
// Apply alias functions
var aliasFunctions = bindFunctionDefs(target.functions, 'Alias');
for (var j = 0; j < aliasFunctions.length; j++) {
_.each(timeseries_data, aliasFunctions[j]);
}
return timeseries_data;
});
});
}
// Query text data
else if (target.mode === 2) {
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter)
.then(function(items) {
var deferred = $q.defer();
if (items.length) {
self.zabbixAPI.getLastValue(items[0].itemid).then(function(lastvalue) {
if (target.textFilter) {
var text_extract_pattern = new RegExp(templateSrv.replace(target.textFilter, options.scopedVars));
var result = text_extract_pattern.exec(lastvalue);
if (result) {
if (target.useCaptureGroups) {
result = result[1];
} else {
result = result[0];
}
}
deferred.resolve(result);
} else {
deferred.resolve(lastvalue);
}
});
} else {
deferred.resolve(null);
}
return deferred.promise.then(function(text) {
return {
target: target.item.name,
datapoints: [[text, to * 1000]]
};
});
});
}
}
// IT services mode
else if (target.mode === 1) {
// Don't show undefined and hidden targets
if (target.hide || !target.itservice || !target.slaProperty) {
return [];
} else {
return this.zabbixAPI.getSLA(target.itservice.serviceid, from, to)
.then(_.bind(zabbixHelperSrv.handleSLAResponse, zabbixHelperSrv, target.itservice, target.slaProperty));
}
}
}, this);
// Data for panel (all targets)
return $q.all(_.flatten(promises))
.then(_.flatten)
.then(function (timeseries_data) {
// Series downsampling
var data = _.map(timeseries_data, function(timeseries) {
var DPS = DataProcessingService;
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = DPS.groupBy(options.interval, DPS.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
return { data: data };
});
};
function bindFunctionDefs(functionDefs, category) {
var aggregationFunctions = _.map(metricFunctions.getCategories()[category], 'name');
var aggFuncDefs = _.filter(functionDefs, function(func) {
return _.contains(aggregationFunctions, func.def.name);
});
return _.map(aggFuncDefs, function(func) {
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
return funcInstance.bindFunction(DataProcessingService.metricFunctions);
});
}
////////////////
// Templating //
////////////////
/**
* 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.
*/
this.metricFindQuery = function (query) {
// Split query. Query structure:
// group.host.app.item
var parts = [];
_.each(query.split('.'), function (part) {
part = templateSrv.replace(part);
// Replace wildcard to regex
if (part === '*') {
part = '/.*/';
}
parts.push(part);
});
var template = _.object(['group', 'host', 'app', 'item'], parts);
// Get items
if (parts.length === 4) {
return this.queryProcessor.filterItems(template.group, template.host,
template.app, 'all', true)
.then(function(items) {
return _.map(items, formatMetric);
});
}
// Get applications
else if (parts.length === 3) {
return this.queryProcessor.filterApplications(template.group, template.host)
.then(function(apps) {
return _.map(apps, formatMetric);
});
}
// Get hosts
else if (parts.length === 2) {
return this.queryProcessor.filterHosts(template.group)
.then(function(hosts) {
return _.map(hosts, formatMetric);
});
}
// Get groups
else if (parts.length === 1) {
return this.zabbixCache.getGroups(template.group).then(function(groups) {
return _.map(groups, formatMetric);
});
}
// Return empty object for invalid request
else {
return $q.when([]);
}
};
function formatMetric(metricObj) {
return {
text: metricObj.name,
expandable: false
};
}
/////////////////
// Annotations //
/////////////////
this.annotationQuery = function(options) {
var from = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var to = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
var annotation = options.annotation;
var self = this;
var showEvents = annotation.showOkEvents ? [0, 1] : 1;
var buildQuery = self.queryProcessor.buildTriggerQuery(templateSrv.replace(annotation.group),
templateSrv.replace(annotation.host),
templateSrv.replace(annotation.application));
return buildQuery.then(function(query) {
return self.zabbixAPI.getTriggers(query.groupids,
query.hostids,
query.applicationids,
showEvents)
.then(function(triggers) {
// Filter triggers by description
if (utils.isRegex(annotation.trigger)) {
triggers = _.filter(triggers, function(trigger) {
return utils.buildRegex(annotation.trigger).test(trigger.description);
});
} else if (annotation.trigger) {
triggers = _.filter(triggers, function(trigger) {
return trigger.description === annotation.trigger;
});
}
// Remove events below the chose severity
triggers = _.filter(triggers, function(trigger) {
return Number(trigger.priority) >= Number(annotation.minseverity);
});
var objectids = _.map(triggers, 'triggerid');
var params = {
output: 'extend',
time_from: from,
time_till: to,
objectids: objectids,
select_acknowledges: 'extend',
selectHosts: 'extend',
value: showEvents
};
return self.zabbixAPI.request('event.get', params)
.then(function (events) {
var indexedTriggers = _.indexBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
if (annotation.hideAcknowledged) {
events = _.filter(events, function(event) {
return !event.acknowledges.length;
});
}
return _.map(events, function(e) {
var title ='';
if (annotation.showHostname) {
title += e.hosts[0].name + ': ';
}
// Show event type (OK or Problem)
title += Number(e.value) ? 'Problem' : 'OK';
var formatted_acknowledges = utils.formatAcknowledges(e.acknowledges);
return {
annotation: annotation,
time: e.clock * 1000,
title: title,
text: indexedTriggers[e.objectid].description + formatted_acknowledges
};
});
});
});
});
};
}
return ZabbixAPIDatasource;
});

View File

@@ -1,21 +0,0 @@
define([
'angular'
],
function (angular) {
'use strict';
var module = angular.module('grafana.directives');
module.directive('metricQueryEditorZabbix', function() {
return {controller: 'ZabbixAPIQueryCtrl', templateUrl: 'public/plugins/zabbix/partials/query.editor.html'};
});
module.directive('metricQueryOptionsZabbix', function() {
return {templateUrl: 'public/plugins/zabbix/partials/query.options.html'};
});
module.directive('annotationsQueryEditorZabbix', function() {
return {templateUrl: 'public/plugins/zabbix/partials/annotations.editor.html'};
});
});

View File

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

View File

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

View File

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

View File

@@ -1,24 +0,0 @@
define([
'./datasource',
'./queryCtrl'
],
function (ZabbixAPIDatasource, ZabbixQueryCtrl) {
'use strict';
function ZabbixQueryOptionsCtrl() {}
ZabbixQueryOptionsCtrl.templateUrl = 'partials/query.options.html';
function ZabbixAnnotationsQueryCtrl() {}
ZabbixAnnotationsQueryCtrl.templateUrl = 'partials/annotations.editor.html';
function ZabbixConfigCtrl() {}
ZabbixConfigCtrl.templateUrl = 'partials/config.html';
return {
Datasource: ZabbixAPIDatasource,
QueryCtrl: ZabbixQueryCtrl,
ConfigCtrl: ZabbixConfigCtrl,
QueryOptionsCtrl: ZabbixQueryOptionsCtrl,
AnnotationsQueryCtrl: ZabbixAnnotationsQueryCtrl
};
});

View File

@@ -1,61 +0,0 @@
<div class="editor-row">
<div class="section tight-form-container" style="margin-bottom: 20px">
<h5>Filter Triggers</h5>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
Group
</li>
<li>
<input type="text"
ng-model="annotation.group"
class="input-large tight-form-input">
</li>
<li class="tight-form-item" style="width: 50px">
Host
</li>
<li>
<input type="text"
ng-model="annotation.host"
class="input-large tight-form-input last">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
Application
</li>
<li>
<input type="text"
ng-model="annotation.application"
class="input-large tight-form-input">
</li>
<li class="tight-form-item" style="width: 50px">
Trigger
</li>
<li>
<input type="text"
ng-model="annotation.trigger"
class="input-large tight-form-input last">
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="editor-row">
<div class="section">
<h5>Options</h5>
<div class="editor-option">
<label class="small">Minimum severity
</label>
<select class="small" style="width: 113px" ng-init='annotation.minseverity = annotation.minseverity || 0' ng-model='annotation.minseverity' ng-options="v as k for (k, v) in {'Not classified': 0, 'Information': 1, 'Warning': 2, 'Average': 3, 'High': 4, 'Disaster': 5}" ng-change="render()"></select>
</div>
<editor-opt-bool text="Show OK events" model="annotation.showOkEvents"></editor-opt-bool>
<editor-opt-bool text="Hide acknowledged events" model="annotation.hideAcknowledged"></editor-opt-bool>
<editor-opt-bool text="Show hostname" model="annotation.showHostname"></editor-opt-bool>
</div>
</div>

View File

@@ -1,242 +0,0 @@
define([
'app/plugins/sdk',
'angular',
'lodash',
'./metricFunctions',
'./utils'
],
function (sdk, angular, _, metricFunctions, utils) {
'use strict';
var ZabbixQueryCtrl = (function(_super) {
// ZabbixQueryCtrl constructor
function ZabbixQueryCtrl($scope, $injector, $sce, $q, templateSrv) {
// Call superclass constructor
_super.call(this, $scope, $injector);
this.editorModes = {
0: 'num',
1: 'itservice',
2: 'text'
};
// Map functions for bs-typeahead
this.getGroupNames = _.partial(getMetricNames, this, 'groupList');
this.getHostNames = _.partial(getMetricNames, this, 'filteredHosts');
this.getApplicationNames = _.partial(getMetricNames, this, 'filteredApplications');
this.getItemNames = _.partial(getMetricNames, this, 'filteredItems');
this.init = function() {
this.templateSrv = templateSrv;
var target = this.target;
var scopeDefaults = {
metric: {}
};
_.defaults(this, scopeDefaults);
// Load default values
var targetDefaults = {
mode: 0,
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
item: { filter: "" },
functions: [],
};
_.defaults(target, targetDefaults);
// Create function instances from saved JSON
target.functions = _.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"}
];
// Set avg by default
if (!target.downsampleFunction) {
target.downsampleFunction = this.downsampleFunctionList[0];
}
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();
}
ZabbixQueryCtrl.templateUrl = 'partials/query.editor.html';
ZabbixQueryCtrl.prototype = Object.create(_super.prototype);
ZabbixQueryCtrl.prototype.constructor = ZabbixQueryCtrl;
var p = ZabbixQueryCtrl.prototype;
p.initFilters = function () {
this.filterGroups();
this.filterHosts();
this.filterApplications();
this.filterItems();
};
p.filterHosts = function () {
var self = this;
var groupFilter = this.templateSrv.replace(this.target.group.filter);
this.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) {
self.metric.filteredHosts = hosts;
});
};
p.filterGroups = function() {
var self = this;
this.datasource.queryProcessor.filterGroups().then(function(groups) {
self.metric.groupList = groups;
});
};
p.filterApplications = function () {
var self = this;
var groupFilter = this.templateSrv.replace(this.target.group.filter);
var hostFilter = this.templateSrv.replace(this.target.host.filter);
this.datasource.queryProcessor.filterApplications(groupFilter, hostFilter)
.then(function(apps) {
self.metric.filteredApplications = apps;
});
};
p.filterItems = function () {
var self = this;
var item_type = this.editorModes[this.target.mode];
var groupFilter = this.templateSrv.replace(this.target.group.filter);
var hostFilter = this.templateSrv.replace(this.target.host.filter);
var appFilter = this.templateSrv.replace(this.target.application.filter);
this.datasource.queryProcessor.filterItems(groupFilter, hostFilter, appFilter,
item_type, this.target.showDisabledItems).then(function(items) {
self.metric.filteredItems = items;
});
};
p.onTargetPartChange = function (targetPart) {
var regexStyle = {'color': '#CCA300'};
targetPart.isRegex = utils.isRegex(targetPart.filter);
targetPart.style = targetPart.isRegex ? regexStyle : {};
};
p.onTargetBlur = function() {
this.initFilters();
this.parseTarget();
this.panelCtrl.refresh();
};
p.parseTarget = function() {
// Parse target
};
// Validate target and set validation info
p.validateTarget = function () {};
p.targetChanged = function() {
this.panelCtrl.refresh();
};
p.addFunction = function(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();
}
};
p.removeFunction = function(func) {
this.target.functions = _.without(this.target.functions, func);
this.targetChanged();
};
p.moveAliasFuncLast = function() {
var aliasFunc = _.find(this.target.functions, function(func) {
return func.def.name === 'alias' ||
func.def.name === 'aliasByNode' ||
func.def.name === 'aliasByMetric';
});
if (aliasFunc) {
this.target.functions = _.without(this.target.functions, aliasFunc);
this.target.functions.push(aliasFunc);
}
};
/**
* Switch query editor to specified mode.
* Modes:
* 0 - items
* 1 - IT services
* 2 - Text metrics
*/
p.switchEditorMode = function (mode) {
this.target.mode = mode;
this.init();
};
/////////////////
// IT Services //
/////////////////
/**
* Update list of IT services
*/
p.updateITServiceList = function () {
var self = this;
this.datasource.zabbixAPI.getITService().then(function (iteservices) {
self.itserviceList = [];
self.itserviceList = self.itserviceList.concat(iteservices);
});
};
/**
* Call when IT service is selected.
*/
p.selectITService = function () {
if (!_.isEqual(this.oldTarget, this.target) && _.isEmpty(this.target.errors)) {
this.oldTarget = angular.copy(this.target);
this.panelCtrl.refresh();
}
};
return ZabbixQueryCtrl;
})(sdk.QueryCtrl);
return ZabbixQueryCtrl;
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _.uniq(_.map(scope.metric[metricList], 'name'));
}
});

View File

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

View File

@@ -1,77 +0,0 @@
define([
'lodash',
'moment'
],
function (_, moment) {
'use strict';
function Utils() {
/**
* Expand Zabbix item name
*
* @param {string} name item name, ie "CPU $2 time"
* @param {string} key item key, ie system.cpu.util[,system,avg1]
* @return {string} expanded name, ie "CPU system time"
*/
this.expandItemName = function(name, key) {
// extract params from key:
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
// replace item parameters
for (var i = key_params.length; i >= 1; i--) {
name = name.replace('$' + i, key_params[i - 1]);
}
return name;
};
// Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
this.isRegex = function (str) {
return regexPattern.test(str);
};
this.buildRegex = function (str) {
var matches = str.match(regexPattern);
var pattern = matches[1];
var flags = matches[2] !== "" ? matches[2] : undefined;
return new RegExp(pattern, flags);
};
this.parseInterval = function(interval) {
var intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g;
var momentInterval = intervalPattern.exec(interval);
return moment.duration(Number(momentInterval[1]), momentInterval[2]).valueOf();
};
/**
* Format acknowledges.
*
* @param {array} acknowledges array of Zabbix acknowledge objects
* @return {string} HTML-formatted table
*/
this.formatAcknowledges = function(acknowledges) {
if (acknowledges.length) {
var formatted_acknowledges = '<br><br>Acknowledges:<br><table><tr><td><b>Time</b></td>'
+ '<td><b>User</b></td><td><b>Comments</b></td></tr>';
_.each(_.map(acknowledges, function (ack) {
var timestamp = moment.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 '';
}
};
}
return new Utils();
});

View File

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

View File

@@ -1,307 +0,0 @@
define([
'angular',
'lodash',
'./utils'
],
function (angular, _, utils) {
'use strict';
var module = angular.module('grafana.services');
// Use factory() instead service() for multiple datasources support.
// Each datasource instance must initialize its own cache.
module.factory('ZabbixCachingProxy', function($q, $interval) {
function ZabbixCachingProxy(zabbixAPI, ttl) {
this.zabbixAPI = zabbixAPI;
this.ttl = ttl;
// Internal objects for data storing
this._groups = undefined;
this._hosts = undefined;
this._applications = undefined;
this._items = undefined;
this._hostsExtend = undefined;
this.storage = {
history: {},
trends: {}
};
// Check is a service initialized or not
this._initialized = undefined;
this.refreshPromise = false;
this.historyPromises = {};
// Wrap _refresh() method to call it once.
this.refresh = callOnce(p._refresh, this.refreshPromise);
// Update cache periodically
$interval(_.bind(this.refresh, this), this.ttl);
// Don't run duplicated history requests
this.getHistory = callHistoryOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
this.historyPromises);
}
var p = ZabbixCachingProxy.prototype;
p._refresh = function() {
var self = this;
var promises = [
this.zabbixAPI.getGroups(),
this.zabbixAPI.getHosts(),
this.zabbixAPI.getApplications(),
this.zabbixAPI.getItems(),
this.zabbixAPI.getHostsExtend()
];
return $q.all(promises).then(function(results) {
if (results.length) {
self._groups = convertGroups(results[0]);
self._hosts = convertHosts(results[1]);
self._applications = convertApplications(results[2]);
self._items = convertItems(results[3]);
self._hostsExtend = convertHostsExtend(results[4]);
}
self._initialized = true;
});
};
p.getGroups = function() {
var self = this;
if (this._groups) {
return $q.when(self._groups);
} else {
return this.refresh().then(function() {
return self._groups;
});
}
};
p.getHosts = function() {
var self = this;
if (this._hosts) {
return $q.when(self._hosts);
} else {
return this.refresh().then(function() {
return self._hosts;
});
}
};
p.getHostsExtend = function() {
var self = this;
if (this._hostsExtend) {
return $q.when(self._hostsExtend);
} else {
return this.refresh().then(function() {
return self._hostsExtend;
});
}
};
p.getApplications = function() {
var self = this;
if (this._applications) {
return $q.when(self._applications);
} else {
return this.refresh().then(function() {
return self._applications;
});
}
};
p.getItems = function(type) {
var self = this;
if (this._items) {
return $q.when(filterItems(self._items, type));
} else {
return this.refresh().then(function() {
return filterItems(self._items, type);
});
}
};
function filterItems(items, type) {
switch (type) {
case 'num':
return _.filter(items, function(item) {
return (item.value_type === '0' ||
item.value_type === '3');
});
case 'text':
return _.filter(items, function(item) {
return (item.value_type === '1' ||
item.value_type === '2' ||
item.value_type === '4');
});
default:
return items;
}
}
p.getHistoryFromCache = function(items, time_from, time_till) {
var deferred = $q.defer();
var historyStorage = this.storage.history;
var full_history;
var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) {
return !historyStorage[itemid];
});
if (expired.length) {
this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
var grouped_history = _.groupBy(history, 'itemid');
_.forEach(expired, function(item) {
var itemid = item.itemid;
historyStorage[itemid] = item;
historyStorage[itemid].time_from = time_from;
historyStorage[itemid].time_till = time_till;
historyStorage[itemid].history = grouped_history[itemid];
});
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
});
} else {
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
}
return deferred.promise;
};
p.getHistoryFromAPI = function(items, time_from, time_till) {
return this.zabbixAPI.getHistory(items, time_from, time_till);
};
p.getHost = function(hostid) {
return _.find(this._hosts, {'hostid': hostid});
};
p.getItem = function(itemid) {
return _.find(this._items, {'itemid': itemid});
};
/**
* Convert host.get response to cache format
* host.groups - array of group ids
*/
function convertHosts(hosts) {
return _.forEach(hosts, function(host) {
host.groups = _.map(host.groups, 'groupid');
return host;
});
}
function convertGroups(groups) {
return _.forEach(groups, function(group) {
group.hosts = _.map(group.hosts, 'hostid');
return group;
});
}
function convertHostsExtend(hosts) {
return _.indexBy(_.map(hosts, function(host) {
host.items = _.forEach(host.items, function(item) {
item.applications = _.map(item.applications, 'applicationid');
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
return host;
}), 'hostid');
}
/**
* Group Zabbix applications by name
* host.hosts - array of host ids
*/
function convertApplications(applications) {
return _.map(_.groupBy(applications, 'name'), function(value, key) {
// Hack for supporting different apis (2.2 vs 2.4 vs 3.0)
var hostField = 'host';
if (value[0] && value[0]['hosts']) {
// For Zabbix 2.2
hostField = 'hosts';
}
return {
name: key,
applicationids: _.map(value, 'applicationid'),
hosts: _.uniq(_.map(_.flatten(value, hostField), 'hostid'))
};
});
}
/**
* Convert item.get response to cache format
* item.applications - array of application ids
* item.item - original item name returned by api (ie "CPU $2 time")
* item.name - expanded name (ie "CPU system time")
*/
function convertItems(items) {
return _.forEach(items, function(item) {
item.applications = _.map(item.applications, 'applicationid');
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
}
String.prototype.getHash = function() {
var hash = 0, i, chr, len;
if (this.length === 0) {
return hash;
}
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;
};
function callHistoryOnce(func, promiseKeeper) {
return function() {
var itemids = _.map(arguments[0], 'itemid');
var stamp = itemids.join() + arguments[1] + arguments[2];
var hash = stamp.getHash();
var deferred = $q.defer();
if (!promiseKeeper[hash]) {
promiseKeeper[hash] = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper[hash] = null;
});
} else {
return promiseKeeper[hash];
}
return deferred.promise;
};
}
function callOnce(func, promiseKeeper) {
return function() {
var deferred = $q.defer();
if (!promiseKeeper) {
promiseKeeper = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper = null;
});
} else {
return promiseKeeper;
}
return deferred.promise;
};
}
return ZabbixCachingProxy;
});
});

View File

@@ -1,230 +0,0 @@
/**
* Grafana-Zabbix
* Zabbix plugin for Grafana.
* http://github.com/alexanderzobnin/grafana-zabbix
*
* Trigger panel.
* This feature sponsored by CORE IT
* http://www.coreit.fr
*
* Copyright 2015 Alexander Zobnin alexanderzobnin@gmail.com
* Licensed under the Apache License, Version 2.0
*/
define([
'app/plugins/sdk',
'angular',
'lodash',
'jquery',
'moment',
'./editor'
],
function (sdk, angular, _, $, moment, triggerPanelEditor) {
'use strict';
var defaultSeverity = [
{ priority: 0, severity: 'Not classified', color: '#B7DBAB', show: true },
{ priority: 1, severity: 'Information', color: '#82B5D8', show: true },
{ priority: 2, severity: 'Warning', color: '#E5AC0E', show: true },
{ priority: 3, severity: 'Average', color: '#C15C17', show: true },
{ priority: 4, severity: 'High', color: '#BF1B00', show: true },
{ priority: 5, severity: 'Disaster', color: '#890F02', show: true }
];
var panelDefaults = {
datasource: null,
triggers: {
group: {filter: ""},
host: {filter: ""},
application: {filter: ""},
trigger: {filter: ""}
},
hostField: true,
statusField: false,
severityField: false,
lastChangeField: true,
ageField: true,
infoField: true,
limit: 10,
showTriggers: 'all triggers',
sortTriggersBy: { text: 'last change', value: 'lastchange' },
showEvents: { text: 'Problem events', value: '1' },
triggerSeverity: defaultSeverity,
okEventColor: '#890F02',
};
var triggerStatusMap = {
'0': 'OK',
'1': 'Problem'
};
var TriggerPanelCtrl = (function(_super) {
/** @ngInject */
function TriggerPanelCtrl($scope, $injector, $q, $element, datasourceSrv) {
_super.call(this, $scope, $injector);
this.datasourceSrv = datasourceSrv;
this.triggerStatusMap = triggerStatusMap;
// Load panel defaults
_.defaults(this.panel, panelDefaults);
this.triggerList = [];
this.refreshData();
}
TriggerPanelCtrl.templateUrl = 'module.html';
TriggerPanelCtrl.prototype = Object.create(_super.prototype);
TriggerPanelCtrl.prototype.constructor = TriggerPanelCtrl;
// Add panel editor
TriggerPanelCtrl.prototype.initEditMode = function() {
_super.prototype.initEditMode();
this.icon = "fa fa-lightbulb-o";
this.addEditorTab('Options', triggerPanelEditor, 2);
};
TriggerPanelCtrl.prototype.refreshData = function() {
var self = this;
// Load datasource
return this.datasourceSrv.get(this.panel.datasource).then(function (datasource) {
var zabbix = datasource.zabbixAPI;
var queryProcessor = datasource.queryProcessor;
var triggerFilter = self.panel.triggers;
var showEvents = self.panel.showEvents.value;
var buildQuery = queryProcessor.buildTriggerQuery(triggerFilter.group.filter,
triggerFilter.host.filter,
triggerFilter.application.filter);
return buildQuery.then(function(query) {
return zabbix.getTriggers(query.groupids,
query.hostids,
query.applicationids,
showEvents)
.then(function(triggers) {
return _.map(triggers, function (trigger) {
var triggerObj = trigger;
// Format last change and age
trigger.lastchangeUnix = Number(trigger.lastchange);
var timestamp = moment.unix(trigger.lastchangeUnix);
if (self.panel.customLastChangeFormat) {
// User defined format
triggerObj.lastchange = timestamp.format(self.panel.lastChangeFormat);
} else {
triggerObj.lastchange = timestamp.format(self.defaultTimeFormat);
}
triggerObj.age = timestamp.fromNow(true);
// Set color
if (trigger.value === '1') {
triggerObj.color = self.panel.triggerSeverity[trigger.priority].color;
} else {
triggerObj.color = self.panel.okEventColor;
}
triggerObj.severity = self.panel.triggerSeverity[trigger.priority].severity;
return triggerObj;
});
})
.then(function (triggerList) {
// Request acknowledges for trigger
var eventids = _.map(triggerList, function(trigger) {
return trigger.lastEvent.eventid;
});
return zabbix.getAcknowledges(eventids)
.then(function (events) {
// Map events to triggers
_.each(triggerList, function(trigger) {
var event = _.find(events, function(event) {
return event.eventid === trigger.lastEvent.eventid;
});
if (event) {
trigger.acknowledges = _.map(event.acknowledges, function (ack) {
var time = new Date(+ack.clock * 1000);
ack.time = time.toLocaleString();
ack.user = ack.alias + ' (' + ack.name + ' ' + ack.surname + ')';
return ack;
});
}
});
// Filter triggers by description
var triggerFilter = self.panel.triggers.trigger.filter;
if (triggerFilter) {
triggerList = filterTriggers(triggerList, triggerFilter);
}
// Filter acknowledged triggers
if (self.panel.showTriggers === 'unacknowledged') {
triggerList = _.filter(triggerList, function (trigger) {
return !trigger.acknowledges;
});
} else if (self.panel.showTriggers === 'acknowledged') {
triggerList = _.filter(triggerList, 'acknowledges');
} else {
triggerList = triggerList;
}
// Filter triggers by severity
triggerList = _.filter(triggerList, function (trigger) {
return self.panel.triggerSeverity[trigger.priority].show;
});
// Sort triggers
if (self.panel.sortTriggersBy.value === 'priority') {
triggerList = _.sortBy(triggerList, 'priority').reverse();
} else {
triggerList = _.sortBy(triggerList, 'lastchangeUnix').reverse();
}
// Limit triggers number
self.triggerList = _.first(triggerList, self.panel.limit);
self.renderingCompleted();
});
});
});
});
};
function filterTriggers(triggers, triggerFilter) {
if (isRegex(triggerFilter)) {
return _.filter(triggers, function(trigger) {
return buildRegex(triggerFilter).test(trigger.description);
});
} else {
return _.filter(triggers, function(trigger) {
return trigger.description === triggerFilter;
});
}
}
function isRegex(str) {
// Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
return regexPattern.test(str);
}
function buildRegex(str) {
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
var matches = str.match(regexPattern);
var pattern = matches[1];
var flags = matches[2] !== "" ? matches[2] : undefined;
return new RegExp(pattern, flags);
}
return TriggerPanelCtrl;
})(sdk.PanelCtrl);
return {
PanelCtrl: TriggerPanelCtrl
};
});

View File

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

View File

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

View File

@@ -0,0 +1,420 @@
//import angular from 'angular';
import _ from 'lodash';
import * as dateMath from 'app/core/utils/datemath';
import * as utils from './utils';
import * as metricFunctions from './metricFunctions';
import DataProcessor from './DataProcessor';
import './zabbixAPI.service.js';
import './zabbixCache.service.js';
import './queryProcessor.service.js';
export class ZabbixAPIDatasource {
/** @ngInject */
constructor(instanceSettings, $q, templateSrv, alertSrv, zabbixAPIService, ZabbixCachingProxy, QueryProcessor) {
// 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);
// Initialize Zabbix API
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
// Initialize cache service
this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI, this.cacheTTL);
// Initialize query builder
this.queryProcessor = new QueryProcessor(this.zabbixCache);
// Dependencies
this.q = $q;
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
console.log(this.zabbixCache);
}
////////////////////////
// Datasource methods //
////////////////////////
/**
* Test connection to Zabbix API
* @return {object} Connection status and Zabbix API version
*/
testDatasource() {
var self = this;
return this.zabbixAPI.getVersion().then(function (version) {
return self.zabbixAPI.login().then(function (auth) {
if (auth) {
return {
status: "success",
title: "Success",
message: "Zabbix API version: " + version
};
} else {
return {
status: "error",
title: "Invalid user name or password",
message: "Zabbix API version: " + version
};
}
}, function(error) {
console.log(error);
return {
status: "error",
title: "Connection failed",
message: error
};
});
},
function(error) {
console.log(error);
return {
status: "error",
title: "Connection failed",
message: "Could not connect to given url"
};
});
}
/**
* 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.
*/
query(options) {
var self = this;
// get from & to in seconds
var from = Math.ceil(dateMath.parse(options.range.from) / 1000);
var to = Math.ceil(dateMath.parse(options.range.to) / 1000);
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
// Create request for each target
var promises = _.map(options.targets, function(target) {
if (target.mode !== 1) {
// Don't request undefined and hidden targets
if (target.hide || !target.group ||
!target.host || !target.item) {
return [];
}
// Replace templated variables
var groupFilter = this.templateSrv.replace(target.group.filter, options.scopedVars);
var hostFilter = this.templateSrv.replace(target.host.filter, options.scopedVars);
var appFilter = this.templateSrv.replace(target.application.filter, options.scopedVars);
var itemFilter = this.templateSrv.replace(target.item.filter, options.scopedVars);
// Query numeric data
if (!target.mode || target.mode === 0) {
// Build query in asynchronous manner
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter)
.then(function(items) {
// Add hostname for items from multiple hosts
var addHostName = target.host.isRegex;
var getHistory;
// Use trends
if ((from < useTrendsFrom) && self.trends) {
// Find trendValue() function and get specified trend value
var trendFunctions = _.map(metricFunctions.getCategories()['Trends'], 'name');
var trendValueFunc = _.find(target.functions, function(func) {
return _.contains(trendFunctions, func.def.name);
});
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
getHistory = self.zabbixAPI.getTrend(items, from, to).then(function(history) {
return self.queryProcessor.handleTrends(history, addHostName, valueType);
});
} else {
// Use history
getHistory = self.zabbixCache.getHistory(items, from, to).then(function(history) {
return self.queryProcessor.handleHistory(history, addHostName);
});
}
return getHistory.then(function (timeseries_data) {
timeseries_data = _.map(timeseries_data, function (timeseries) {
// Filter only transform functions
var transformFunctions = bindFunctionDefs(target.functions, 'Transform', DataProcessor);
// Metric data processing
var dp = timeseries.datapoints;
for (var i = 0; i < transformFunctions.length; i++) {
dp = transformFunctions[i](dp);
}
timeseries.datapoints = dp;
return timeseries;
});
// Aggregations
var aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate', DataProcessor);
var dp = _.map(timeseries_data, 'datapoints');
if (aggregationFunctions.length) {
for (var i = 0; i < aggregationFunctions.length; i++) {
dp = aggregationFunctions[i](dp);
}
var lastAgg = _.findLast(target.functions, function(func) {
return _.contains(
_.map(metricFunctions.getCategories()['Aggregate'], 'name'), func.def.name);
});
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
}
// Apply alias functions
var aliasFunctions = bindFunctionDefs(target.functions, 'Alias', DataProcessor);
for (var j = 0; j < aliasFunctions.length; j++) {
_.each(timeseries_data, aliasFunctions[j]);
}
return timeseries_data;
});
});
}
// Query text data
else if (target.mode === 2) {
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter)
.then(function(items) {
var deferred = self.q.defer();
if (items.length) {
self.zabbixAPI.getLastValue(items[0].itemid).then(function(lastvalue) {
if (target.textFilter) {
var text_extract_pattern = new RegExp(self.templateSrv.replace(target.textFilter, options.scopedVars));
var result = text_extract_pattern.exec(lastvalue);
if (result) {
if (target.useCaptureGroups) {
result = result[1];
} else {
result = result[0];
}
}
deferred.resolve(result);
} else {
deferred.resolve(lastvalue);
}
});
} else {
deferred.resolve(null);
}
return deferred.promise.then(function(text) {
return {
target: target.item.name,
datapoints: [[text, to * 1000]]
};
});
});
}
}
// IT services mode
else if (target.mode === 1) {
// Don't show undefined and hidden targets
if (target.hide || !target.itservice || !target.slaProperty) {
return [];
} else {
return this.zabbixAPI.getSLA(target.itservice.serviceid, from, to)
.then(slaObject => {
return self.queryProcessor.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
}
}
}, this);
// Data for panel (all targets)
return this.q.all(_.flatten(promises))
.then(_.flatten)
.then(function (timeseries_data) {
// Series downsampling
var data = _.map(timeseries_data, function(timeseries) {
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints =
DataProcessor.groupBy(options.interval, DataProcessor.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
return { data: data };
});
}
////////////////
// 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.
*/
metricFindQuery(query) {
// Split query. Query structure:
// group.host.app.item
var self = this;
var parts = [];
_.each(query.split('.'), function (part) {
part = self.templateSrv.replace(part);
// Replace wildcard to regex
if (part === '*') {
part = '/.*/';
}
parts.push(part);
});
var template = _.object(['group', 'host', 'app', 'item'], parts);
// Get items
if (parts.length === 4) {
return this.queryProcessor.filterItems(template.group, template.host,
template.app, 'all', true)
.then(function(items) {
return _.map(items, formatMetric);
});
}
// Get applications
else if (parts.length === 3) {
return this.queryProcessor.filterApplications(template.group, template.host)
.then(function(apps) {
return _.map(apps, formatMetric);
});
}
// Get hosts
else if (parts.length === 2) {
return this.queryProcessor.filterHosts(template.group)
.then(function(hosts) {
return _.map(hosts, formatMetric);
});
}
// Get groups
else if (parts.length === 1) {
return this.zabbixCache.getGroups(template.group).then(function(groups) {
return _.map(groups, formatMetric);
});
}
// Return empty object for invalid request
else {
return this.q.when([]);
}
}
/////////////////
// Annotations //
/////////////////
annotationQuery(options) {
var from = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var to = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
var annotation = options.annotation;
var self = this;
var showOkEvents = annotation.showOkEvents ? [0, 1] : 1;
var buildQuery = self.queryProcessor.buildTriggerQuery(this.templateSrv.replace(annotation.group),
this.templateSrv.replace(annotation.host),
this.templateSrv.replace(annotation.application));
return buildQuery.then(function(query) {
return self.zabbixAPI.getTriggers(query.groupids,
query.hostids,
query.applicationids,
true)
.then(function(triggers) {
// Filter triggers by description
if (utils.isRegex(annotation.trigger)) {
triggers = _.filter(triggers, function(trigger) {
return utils.buildRegex(annotation.trigger).test(trigger.description);
});
} else if (annotation.trigger) {
triggers = _.filter(triggers, function(trigger) {
return trigger.description === annotation.trigger;
});
}
// Remove events below the chose severity
triggers = _.filter(triggers, function(trigger) {
return Number(trigger.priority) >= Number(annotation.minseverity);
});
var objectids = _.map(triggers, 'triggerid');
return self.zabbixAPI.getEvents(objectids, from, to, showOkEvents)
.then(function (events) {
var indexedTriggers = _.indexBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
if (annotation.hideAcknowledged) {
events = _.filter(events, function(event) {
return !event.acknowledges.length;
});
}
return _.map(events, function(e) {
var title ='';
if (annotation.showHostname) {
title += e.hosts[0].name + ': ';
}
// Show event type (OK or Problem)
title += Number(e.value) ? 'Problem' : 'OK';
var formatted_acknowledges = utils.formatAcknowledges(e.acknowledges);
return {
annotation: annotation,
time: e.clock * 1000,
title: title,
text: indexedTriggers[e.objectid].description + formatted_acknowledges
};
});
});
});
});
}
}
function bindFunctionDefs(functionDefs, category, DataProcessor) {
'use strict';
var aggregationFunctions = _.map(metricFunctions.getCategories()[category], 'name');
var aggFuncDefs = _.filter(functionDefs, function(func) {
return _.contains(aggregationFunctions, func.def.name);
});
return _.map(aggFuncDefs, function(func) {
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
return funcInstance.bindFunction(DataProcessor.metricFunctions);
});
}
function formatMetric(metricObj) {
'use strict';
return {
text: metricObj.name,
expandable: false
};
}

View File

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

View File

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

View File

@@ -0,0 +1,19 @@
import {ZabbixAPIDatasource} from './datasource';
import {ZabbixQueryController} from './query.controller';
class ZabbixConfigController {}
ZabbixConfigController.templateUrl = 'partials/config.html';
class ZabbixQueryOptionsController {}
ZabbixQueryOptionsController.templateUrl = 'partials/query.options.html';
class ZabbixAnnotationsQueryController {}
ZabbixAnnotationsQueryController.templateUrl = 'partials/annotations.editor.html';
export {
ZabbixAPIDatasource as Datasource,
ZabbixConfigController as ConfigCtrl,
ZabbixQueryController as QueryCtrl,
ZabbixQueryOptionsController as QueryOptionsCtrl,
ZabbixAnnotationsQueryController as AnnotationsQueryCtrl
};

View File

@@ -0,0 +1,65 @@
<div class="gf-form-group">
<h6>Filter Triggers</h6>
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label width-10">Group</span>
<input type="text"
class="gf-form-input max-width-16"
ng-model="ctrl.annotation.group">
</input>
</div>
<div class="gf-form">
<span class="gf-form-label width-10">Host</span>
<input type="text"
class="gf-form-input max-width-16"
ng-model="ctrl.annotation.host">
</input>
</div>
</div>
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label width-10">Application</span>
<input type="text"
class="gf-form-input max-width-16"
ng-model="ctrl.annotation.application">
</input>
</div>
<div class="gf-form">
<span class="gf-form-label width-10">Trigger</span>
<input type="text"
class="gf-form-input max-width-16"
ng-model="ctrl.annotation.trigger">
</input>
</div>
</div>
</div>
<div class="gf-form-group">
<div class="gf-form">
<span class="gf-form-label width-10">Minimum severity</span>
<div class="gf-form-select-wrapper">
<select class="gf-form-input gf-size-auto"
ng-init='ctrl.annotation.minseverity = ctrl.annotation.minseverity || 0'
ng-model='ctrl.annotation.minseverity'
ng-options="v as k for (k, v) in {
'Not classified': 0,
'Information': 1,
'Warning': 2,
'Average': 3,
'High': 4,
'Disaster': 5
}"
ng-change="render()">
</select>
</div>
</div>
</div>
<div class="gf-form-group">
<h6>Options</h6>
<div class="gf-form">
<editor-checkbox text="Show OK events" model="ctrl.annotation.showOkEvents"></editor-checkbox>
<editor-checkbox text="Hide acknowledged events" model="ctrl.annotation.hideAcknowledged"></editor-checkbox>
<editor-checkbox text="Show hostname" model="ctrl.annotation.showHostname"></editor-checkbox>
</div>
</div>

View File

@@ -0,0 +1,232 @@
import {QueryCtrl} from 'app/plugins/sdk';
import _ from 'lodash';
import * as utils from './utils';
import * as metricFunctions from './metricFunctions';
import './add-metric-function.directive';
import './metric-function-editor.directive';
export class ZabbixQueryController extends QueryCtrl {
// ZabbixQueryCtrl constructor
constructor($scope, $injector, $sce, $q, templateSrv) {
// Call superclass constructor
super($scope, $injector);
this.editorModes = {
0: 'num',
1: 'itservice',
2: 'text'
};
// Map functions for bs-typeahead
this.getGroupNames = _.partial(getMetricNames, this, 'groupList');
this.getHostNames = _.partial(getMetricNames, this, 'filteredHosts');
this.getApplicationNames = _.partial(getMetricNames, this, 'filteredApplications');
this.getItemNames = _.partial(getMetricNames, this, 'filteredItems');
this.init = function() {
this.templateSrv = templateSrv;
var target = this.target;
var scopeDefaults = {
metric: {}
};
_.defaults(this, scopeDefaults);
// Load default values
var targetDefaults = {
mode: 0,
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
item: { filter: "" },
functions: [],
};
_.defaults(target, targetDefaults);
// Create function instances from saved JSON
target.functions = _.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"}
];
// Set avg by default
if (!target.downsampleFunction) {
target.downsampleFunction = this.downsampleFunctionList[0];
}
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();
}
initFilters() {
this.filterGroups();
this.filterHosts();
this.filterApplications();
this.filterItems();
}
filterHosts() {
var self = this;
var groupFilter = this.templateSrv.replace(this.target.group.filter);
this.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) {
self.metric.filteredHosts = hosts;
});
}
filterGroups() {
var self = this;
this.datasource.queryProcessor.filterGroups().then(function(groups) {
self.metric.groupList = groups;
});
}
filterApplications() {
var self = this;
var groupFilter = this.templateSrv.replace(this.target.group.filter);
var hostFilter = this.templateSrv.replace(this.target.host.filter);
this.datasource.queryProcessor.filterApplications(groupFilter, hostFilter)
.then(function(apps) {
self.metric.filteredApplications = apps;
});
}
filterItems() {
var self = this;
var item_type = this.editorModes[this.target.mode];
var groupFilter = this.templateSrv.replace(this.target.group.filter);
var hostFilter = this.templateSrv.replace(this.target.host.filter);
var appFilter = this.templateSrv.replace(this.target.application.filter);
this.datasource.queryProcessor.filterItems(groupFilter, hostFilter, appFilter,
item_type, this.target.showDisabledItems).then(function(items) {
self.metric.filteredItems = items;
});
}
onTargetPartChange(targetPart) {
var regexStyle = {'color': '#CCA300'};
targetPart.isRegex = utils.isRegex(targetPart.filter);
targetPart.style = targetPart.isRegex ? regexStyle : {};
}
onTargetBlur() {
this.initFilters();
this.parseTarget();
this.panelCtrl.refresh();
}
parseTarget() {
// Parse target
}
// Validate target and set validation info
validateTarget() {
// validate
}
targetChanged() {
this.panelCtrl.refresh();
}
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();
}
}
removeFunction(func) {
this.target.functions = _.without(this.target.functions, func);
this.targetChanged();
}
moveAliasFuncLast() {
var aliasFunc = _.find(this.target.functions, function(func) {
return func.def.name === 'alias' ||
func.def.name === 'aliasByNode' ||
func.def.name === 'aliasByMetric';
});
if (aliasFunc) {
this.target.functions = _.without(this.target.functions, aliasFunc);
this.target.functions.push(aliasFunc);
}
}
/**
* Switch query editor to specified mode.
* Modes:
* 0 - items
* 1 - IT services
* 2 - Text metrics
*/
switchEditorMode(mode) {
this.target.mode = mode;
this.init();
}
/////////////////
// IT Services //
/////////////////
/**
* Update list of IT services
*/
updateITServiceList() {
var self = this;
this.datasource.zabbixAPI.getITService().then(function (iteservices) {
self.itserviceList = [];
self.itserviceList = self.itserviceList.concat(iteservices);
});
}
/**
* Call when IT service is selected.
*/
selectITService() {
if (!_.isEqual(this.oldTarget, this.target) && _.isEmpty(this.target.errors)) {
this.oldTarget = angular.copy(this.target);
this.panelCtrl.refresh();
}
}
}
// Set templateUrl as static property
ZabbixQueryController.templateUrl = 'partials/query.editor.html';
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _.uniq(_.map(scope.metric[metricList], 'name'));
}

View File

@@ -0,0 +1,361 @@
import angular from 'angular';
import _ from 'lodash';
import * as utils from './utils';
/** @ngInject */
angular.module('grafana.services').factory('QueryProcessor', function($q) {
class QueryProcessor {
constructor(zabbixCacheInstance) {
this.cache = zabbixCacheInstance;
this.$q = $q;
}
/**
* Build query in asynchronous manner
*/
build(groupFilter, hostFilter, appFilter, itemFilter) {
var self = this;
if (this.cache._initialized) {
return this.$q.when(self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter));
} else {
return this.cache.refresh().then(function() {
return self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter);
});
}
}
/**
* Build trigger query in asynchronous manner
*/
buildTriggerQuery(groupFilter, hostFilter, appFilter) {
var self = this;
if (this.cache._initialized) {
return this.$q.when(self.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter));
} else {
return this.cache.refresh().then(function() {
return self.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter);
});
}
}
filterGroups(groupFilter) {
return this.cache.getGroups().then(function(groupList) {
return groupList;
});
}
/**
* Get list of host belonging to given groups.
* @return list of hosts
*/
filterHosts(groupFilter) {
var self = this;
return this.cache.getGroups().then(function(groups) {
groups = findByFilter(groups, groupFilter);
var hostids = _.flatten(_.map(groups, 'hosts'));
if (hostids.length) {
return self.cache.getIndexedHosts().then(function(hosts) {
return _.map(hostids, function(hostid) {
return hosts[hostid];
});
});
} else {
return [];
}
});
}
/**
* Get list of applications belonging to given groups and hosts.
* @return list of applications belonging to given hosts
*/
filterApplications(groupFilter, hostFilter) {
var promises = [
this.filterHosts(groupFilter),
this.cache.getApplications()
];
return this.$q.all(promises).then(function(results) {
var hostList = results[0];
var applicationList = results[1];
var hosts = findByFilter(hostList, hostFilter);
if (hosts) {
var hostsids = _.map(hosts, 'hostid');
return _.filter(applicationList, function (appObj) {
return _.intersection(hostsids, appObj.hosts).length;
});
} else {
return [];
}
});
}
filterItems(groupFilter, hostFilter, appFilter, itemType, showDisabledItems) {
var hosts;
var apps;
var items;
var promises = [
this.filterHosts(groupFilter),
this.filterApplications(groupFilter, hostFilter),
this.cache.getIndexedHosts(),
this.cache.getIndexedApplications()
];
return this.$q.all(promises).then(function(results) {
var hostList = results[0];
var applicationList = results[1];
var idx_hosts = results[2];
var idx_apps = results[3];
// Filter hosts
hosts = findByFilter(hostList, hostFilter);
idx_hosts = getFromIndex(idx_hosts, _.map(hosts, 'hostid'));
// Filter applications
if (appFilter === "") {
// Get all items
apps = undefined;
if (hosts) {
// Get all items in given hosts
items = _.flatten(_.map(idx_hosts, function(host) {
return _.values(host.idx_items);
}), true);
}
} else {
apps = findByFilter(applicationList, appFilter);
}
if (apps) {
// Get ids for finded applications
var appids = _.flatten(_.map(apps, 'applicationids'));
appids = _.flatten(_.map(_.map(hosts, 'applications'), function(apps) {
return _.intersection(apps, appids);
}));
// For each finded host get list of items in finded applications
items = _.flatten(_.map(idx_hosts, function(host) {
var host_apps = _.intersection(appids, host.applications);
var host_itemids = _.flatten(_.map(getFromIndex(idx_apps, host_apps), 'itemids'));
return _.values(getFromIndex(host.idx_items, host_itemids));
}), true);
}
if (!showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
return items;
});
}
/**
* Build query - convert target filters to array of Zabbix items
*/
buildFromCache(groupFilter, hostFilter, appFilter, itemFilter) {
return this.filterItems(groupFilter, hostFilter, appFilter).then(function(items) {
if (items.length) {
if (utils.isRegex(itemFilter)) {
return findByFilter(items, itemFilter);
} else {
return _.filter(items, {'name': itemFilter});
}
} else {
return [];
}
});
}
/**
* Build query - convert target filters to array of Zabbix items
*/
buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter) {
var promises = [
this.cache.getGroups().then(function(groups) {
return _.filter(groups, function(group) {
if (utils.isRegex(groupFilter)) {
return utils.buildRegex(groupFilter).test(group.name);
} else {
return group.name === groupFilter;
}
});
}),
this.filterHosts(groupFilter).then(function(hosts) {
return _.filter(hosts, function(host) {
if (utils.isRegex(hostFilter)) {
return utils.buildRegex(hostFilter).test(host.name);
} else {
return host.name === hostFilter;
}
});
}),
this.filterApplications(groupFilter, hostFilter).then(function(apps) {
return _.filter(apps, function(app) {
if (utils.isRegex(appFilter)) {
return utils.buildRegex(appFilter).test(app.name);
} else {
return app.name === appFilter;
}
});
})
];
return this.$q.all(promises).then(function(results) {
var filteredGroups = results[0];
var filteredHosts = results[1];
var filteredApps = results[2];
var query = {};
if (appFilter) {
query.applicationids = _.flatten(_.map(filteredApps, 'applicationids'));
}
if (hostFilter) {
query.hostids = _.map(filteredHosts, 'hostid');
}
if (groupFilter) {
query.groupids = _.map(filteredGroups, 'groupid');
}
return query;
});
}
/**
* Convert Zabbix API history.get response to Grafana format
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
convertHistory(history, addHostName, convertPointCallback) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }, ...
* ]
*/
var self = this;
// Group history by itemid
var grouped_history = _.groupBy(history, 'itemid');
return _.map(grouped_history, function(hist, itemid) {
var item = self.cache.getItem(itemid);
var alias = item.name;
if (addHostName) {
var host = self.cache.getHost(item.hostid);
alias = host.name + ": " + alias;
}
return {
target: alias,
datapoints: _.map(hist, convertPointCallback)
};
});
}
handleHistory(history, addHostName) {
return this.convertHistory(history, addHostName, convertHistoryPoint);
}
handleTrends(history, addHostName, valueType) {
var convertPointCallback = _.partial(convertTrendPoint, valueType);
return this.convertHistory(history, addHostName, convertPointCallback);
}
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]
]
};
}
}
}
return QueryProcessor;
});
/**
* 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 = _.find(list, {'name': name});
if (finded) {
return [finded];
} else {
return undefined;
}
}
function findByRegex(list, regex) {
var filterPattern = utils.buildRegex(regex);
return _.filter(list, function (zbx_obj) {
return filterPattern.test(zbx_obj.name);
});
}
function findByFilter(list, filter) {
if (utils.isRegex(filter)) {
return findByRegex(list, filter);
} else {
return findByName(list, filter);
}
}
function getFromIndex(index, objids) {
return _.map(objids, function(id) {
return index[id];
});
}
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
];
}

View File

@@ -0,0 +1,67 @@
import _ from 'lodash';
import moment from 'moment';
/**
* 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"
*/
export 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 = /^\/(.*)\/([gmi]*)$/m;
export function isRegex(str) {
return regexPattern.test(str);
}
export function buildRegex(str) {
var matches = str.match(regexPattern);
var pattern = matches[1];
var flags = matches[2] !== "" ? matches[2] : undefined;
return new RegExp(pattern, flags);
}
export function parseInterval(interval) {
var intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g;
var momentInterval = intervalPattern.exec(interval);
return moment.duration(Number(momentInterval[1]), momentInterval[2]).valueOf();
}
/**
* Format acknowledges.
*
* @param {array} acknowledges array of Zabbix acknowledge objects
* @return {string} HTML-formatted table
*/
export function formatAcknowledges(acknowledges) {
if (acknowledges.length) {
var formatted_acknowledges = '<br><br>Acknowledges:<br><table><tr><td><b>Time</b></td>'
+ '<td><b>User</b></td><td><b>Comments</b></td></tr>';
_.each(_.map(acknowledges, function (ack) {
var timestamp = moment.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 '';
}
}

View File

@@ -1,22 +1,18 @@
define([
'angular',
'lodash',
'./zabbixAPIService'
],
function (angular, _) {
'use strict';
import angular from 'angular';
import _ from 'lodash';
import './zabbixAPICore.service';
var module = angular.module('grafana.services');
/** @ngInject */
function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
/**
* Zabbix API Wrapper.
* Creates Zabbix API instance with given parameters (url, credentials and other).
* Wraps API calls and provides high-level methods.
*/
module.factory('ZabbixAPI', function($q, backendSrv, alertSrv, ZabbixAPIService) {
class ZabbixAPI {
// Initialize Zabbix API.
function ZabbixAPI(api_url, username, password, basicAuth, withCredentials) {
constructor(api_url, username, password, basicAuth, withCredentials) {
this.url = api_url;
this.username = username;
this.password = password;
@@ -28,18 +24,23 @@ function (angular, _) {
};
this.loginPromise = null;
this.$q = $q;
this.alertSrv = alertSrv;
this.zabbixAPICore = zabbixAPICoreService;
this.getTrend = this.getTrend_ZBXNEXT1193;
//getTrend = getTrend_30;
}
var p = ZabbixAPI.prototype;
//////////////////////////
// Core method wrappers //
//////////////////////////
//////////////////
// Core methods //
//////////////////
p.request = function(method, params) {
request(method, params) {
var self = this;
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
.then(function(result) {
return result;
},
@@ -56,22 +57,14 @@ function (angular, _) {
});
}
});
};
}
p.alertAPIError = function(message) {
alertSrv.set(
alertAPIError(message) {
this.alertSrv.set(
"Zabbix API Error",
message,
'error'
);
};
function isNotAuthorized(message) {
return (
message === "Session terminated, re-login, please." ||
message === "Not authorised." ||
message === "Not authorized."
);
}
/**
@@ -80,9 +73,9 @@ function (angular, _) {
* and call it once. If login() already called just wait for it (return its promise).
* @return login promise
*/
p.loginOnce = function() {
loginOnce() {
var self = this;
var deferred = $q.defer();
var deferred = this.$q.defer();
if (!self.loginPromise) {
self.loginPromise = deferred.promise;
self.login().then(
@@ -100,27 +93,27 @@ function (angular, _) {
return self.loginPromise;
}
return deferred.promise;
};
}
/**
* Get authentication token.
*/
p.login = function() {
return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions);
};
login() {
return this.zabbixAPICore.login(this.url, this.username, this.password, this.requestOptions);
}
/**
* Get Zabbix API version
*/
p.getVersion = function() {
return ZabbixAPIService.getVersion(this.url, this.requestOptions);
};
getVersion() {
return this.zabbixAPICore.getVersion(this.url, this.requestOptions);
}
/////////////////
// API methods //
/////////////////
////////////////////////////////
// Zabbix API method wrappers //
////////////////////////////////
p.getGroups = function() {
getGroups() {
var params = {
output: ['name'],
sortfield: 'name',
@@ -128,32 +121,33 @@ function (angular, _) {
};
return this.request('hostgroup.get', params);
};
}
p.getHosts = function() {
getHosts() {
var params = {
output: ['name', 'host'],
sortfield: 'name',
selectGroups: []
selectGroups: [],
selectApplications: ['applicationid']
};
return this.request('host.get', params);
};
}
p.getApplications = function() {
getApplications() {
var params = {
output: ['name'],
sortfield: 'name',
output: ['applicationid', 'name'],
// Hack for supporting different apis (2.2 vs 2.4 vs 3.0)
selectHost: [],
selectHosts: []
selectHost: ['hostid'],
selectHosts: ['hostid'],
selectItems: ['itemid']
};
return this.request('application.get', params);
};
}
p.getItems = function() {
getItems() {
var params = {
output: [
'name', 'key_',
@@ -167,19 +161,22 @@ function (angular, _) {
};
return this.request('item.get', params);
};
}
/**
* Get Hosts list with host's items.
* @return {[type]} [description]
*/
p.getHostsExtend = function() {
getHostsExtend() {
var params = {
output: ['name', 'host'],
sortfield: 'name',
selectGroups: [],
selectGroups: ['groupid'],
selectApplications: ['applicationid'],
selectItems: [
'name', 'key_',
'itemid',
'name',
'key_',
'value_type',
'hostid',
'status',
@@ -188,9 +185,9 @@ function (angular, _) {
};
return this.request('host.get', params);
};
}
p.getLastValue = function(itemid) {
getLastValue(itemid) {
var params = {
output: ['lastvalue'],
itemids: itemid
@@ -202,7 +199,7 @@ function (angular, _) {
return null;
}
});
};
}
/**
* Perform history query from Zabbix API
@@ -212,14 +209,14 @@ function (angular, _) {
* @param {Number} time_till Time in seconds
* @return {Array} Array of Zabbix history objects
*/
p.getHistory = function(items, time_from, time_till) {
getHistory(items, time_from, time_till) {
var self = this;
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
return this.$q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
@@ -237,7 +234,7 @@ function (angular, _) {
return self.request('history.get', params);
})).then(_.flatten);
};
}
/**
* Perform trends query from Zabbix API
@@ -248,14 +245,14 @@ function (angular, _) {
* @param {Number} time_till Time in seconds
* @return {Array} Array of Zabbix trend objects
*/
p.getTrend_ZBXNEXT1193 = function(items, time_from, time_till) {
getTrend_ZBXNEXT1193(items, time_from, time_till) {
var self = this;
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
return this.$q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
@@ -273,9 +270,9 @@ function (angular, _) {
return self.request('trend.get', params);
})).then(_.flatten);
};
}
p.getTrend_30 = function(items, time_from, time_till, value_type) {
getTrend_30(items, time_from, time_till, value_type) {
var self = this;
var itemids = _.map(items, 'itemid');
@@ -294,20 +291,17 @@ function (angular, _) {
}
return self.request('trend.get', params);
};
}
p.getTrend = p.getTrend_ZBXNEXT1193;
//p.getTrend = p.getTrend_30;
p.getITService = function(/* optional */ serviceids) {
getITService(/* optional */ serviceids) {
var params = {
output: 'extend',
serviceids: serviceids
};
return this.request('service.get', params);
};
}
p.getSLA = function(serviceids, from, to) {
getSLA(serviceids, from, to) {
var params = {
serviceids: serviceids,
intervals: [{
@@ -316,9 +310,9 @@ function (angular, _) {
}]
};
return this.request('service.getsla', params);
};
}
p.getTriggers = function(groupids, hostids, applicationids, showEvents) {
getTriggers(groupids, hostids, applicationids, showAll) {
var params = {
output: 'extend',
groupids: groupids,
@@ -338,14 +332,28 @@ function (angular, _) {
selectLastEvent: 'extend'
};
if (showEvents) {
params.filter.value = showEvents;
if (showAll) {
params.filter = {};
}
return this.request('trigger.get', params);
};
}
p.getAcknowledges = function(eventids) {
getEvents(objectids, from, to, showOkEvents) {
var params = {
output: 'extend',
time_from: from,
time_till: to,
objectids: objectids,
select_acknowledges: 'extend',
selectHosts: 'extend',
value: showOkEvents
};
return this.request('event.get', params);
}
getAcknowledges(eventids) {
var params = {
output: 'extend',
eventids: eventids,
@@ -361,10 +369,21 @@ function (angular, _) {
return event.acknowledges.length;
});
});
};
}
return ZabbixAPI;
}
});
return ZabbixAPI;
}
});
function isNotAuthorized(message) {
return (
message === "Session terminated, re-login, please." ||
message === "Not authorised." ||
message === "Not authorized."
);
}
angular
.module('grafana.services')
.factory('zabbixAPIService', ZabbixAPIService);

View File

@@ -0,0 +1,104 @@
/**
* General Zabbix API methods
*/
import angular from 'angular';
class ZabbixAPICoreService {
/** @ngInject */
constructor($q, backendSrv) {
this.$q = $q;
this.backendSrv = backendSrv;
}
/**
* Request data from Zabbix API
* @return {object} response.result
*/
request(api_url, method, params, options, auth) {
var deferred = this.$q.defer();
var requestData = {
jsonrpc: '2.0',
method: method,
params: params,
id: 1
};
if (auth === "") {
// Reject immediately if not authenticated
deferred.reject({data: "Not authorised."});
return deferred.promise;
} else if (auth) {
// Set auth parameter only if it needed
requestData.auth = auth;
}
var requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: api_url,
data: requestData
};
// Set request options for basic auth
if (options.basicAuth || options.withCredentials) {
requestOptions.withCredentials = true;
}
if (options.basicAuth) {
requestOptions.headers.Authorization = options.basicAuth;
}
this.backendSrv.datasourceRequest(requestOptions).then(function (response) {
// General connection issues
if (!response.data) {
deferred.reject(response);
}
// Handle Zabbix API errors
else if (response.data.error) {
deferred.reject(response.data.error);
}
deferred.resolve(response.data.result);
});
return deferred.promise;
}
/**
* Get authentication token.
* @return {string} auth token
*/
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
*/
getVersion(api_url, options) {
return this.request(api_url, 'apiinfo.version', [], options);
}
}
// Define zabbix API exception type
function ZabbixException(error) {
this.code = error.code;
this.errorType = error.message;
this.message = error.data;
}
ZabbixException.prototype.toString = function() {
return this.errorType + ": " + this.message;
};
angular
.module('grafana.services')
.service('zabbixAPICoreService', ZabbixAPICoreService);

View File

@@ -0,0 +1,336 @@
import angular from 'angular';
import _ from 'lodash';
import * as utils from './utils';
// Use factory() instead service() for multiple datasources support.
// Each datasource instance must initialize its own cache.
/** @ngInject */
angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $interval) {
class ZabbixCachingProxy {
constructor(zabbixAPI, ttl) {
this.zabbixAPI = zabbixAPI;
this.ttl = ttl;
this.$q = $q;
// Internal objects for data storing
this._groups = undefined;
this._hosts = undefined;
this._applications = undefined;
this._items = undefined;
this.storage = {
history: {},
trends: {}
};
// Check is a service initialized or not
this._initialized = undefined;
this.refreshPromise = false;
this.historyPromises = {};
// Wrap _refresh() method to call it once.
this.refresh = callOnce(this._refresh, this.refreshPromise);
// Update cache periodically
$interval(_.bind(this.refresh, this), this.ttl);
// Don't run duplicated history requests
this.getHistory = callHistoryOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
this.historyPromises);
}
_refresh() {
var self = this;
var promises = [
this.zabbixAPI.getGroups(),
this.zabbixAPI.getHosts(),
this.zabbixAPI.getApplications(),
this.zabbixAPI.getItems(),
this.zabbixAPI.getHostsExtend()
];
return this.$q.all(promises).then(function(results) {
if (results.length) {
self._groups = convertGroups(results[0]);
self._hosts = convertHosts(results[1]);
self._applications = convertApplications(results[2]);
self._items = convertItems(results[3]);
self._idx_apps = indexApps(results[2]);
self._idx_hosts = indexHosts(results[4]);
}
self._initialized = true;
});
}
getGroups() {
var self = this;
if (this._groups) {
return this.$q.when(self._groups);
} else {
return this.refresh().then(function() {
return self._groups;
});
}
}
getHosts() {
var self = this;
if (this._hosts) {
return this.$q.when(self._hosts);
} else {
return this.refresh().then(function() {
return self._hosts;
});
}
}
getIndexedHosts() {
var self = this;
if (this._idx_hosts) {
return this.$q.when(self._idx_hosts);
} else {
return this.refresh().then(function() {
return self._idx_hosts;
});
}
}
getIndexedApplications() {
var self = this;
if (this._idx_apps) {
return this.$q.when(self._idx_apps);
} else {
return this.refresh().then(function() {
return self._idx_apps;
});
}
}
getApplications() {
var self = this;
if (this._applications) {
return this.$q.when(self._applications);
} else {
return this.refresh().then(function() {
return self._applications;
});
}
}
getItems(type) {
var self = this;
if (this._items) {
return this.$q.when(filterItems(self._items, type));
} else {
return this.refresh().then(function() {
return filterItems(self._items, type);
});
}
}
getHistoryFromCache(items, time_from, time_till) {
var deferred = this.$q.defer();
var historyStorage = this.storage.history;
var full_history;
var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) {
return !historyStorage[itemid];
});
if (expired.length) {
this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
var grouped_history = _.groupBy(history, 'itemid');
_.forEach(expired, function(item) {
var itemid = item.itemid;
historyStorage[itemid] = item;
historyStorage[itemid].time_from = time_from;
historyStorage[itemid].time_till = time_till;
historyStorage[itemid].history = grouped_history[itemid];
});
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
});
} else {
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
}
return deferred.promise;
}
getHistoryFromAPI(items, time_from, time_till) {
return this.zabbixAPI.getHistory(items, time_from, time_till);
}
getHost(hostid) {
return _.find(this._hosts, {'hostid': hostid});
}
getItem(itemid) {
return _.find(this._items, {'itemid': itemid});
}
}
function callHistoryOnce(func, promiseKeeper) {
return function() {
var itemids = _.map(arguments[0], 'itemid');
var stamp = itemids.join() + arguments[1] + arguments[2];
var hash = stamp.getHash();
var deferred = $q.defer();
if (!promiseKeeper[hash]) {
promiseKeeper[hash] = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper[hash] = null;
});
} else {
return promiseKeeper[hash];
}
return deferred.promise;
};
}
function callOnce(func, promiseKeeper) {
return function() {
var deferred = $q.defer();
if (!promiseKeeper) {
promiseKeeper = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper = null;
});
} else {
return promiseKeeper;
}
return deferred.promise;
};
}
return ZabbixCachingProxy;
});
/**
* Convert host.get response to cache format
* host.groups - array of group ids
*/
function convertHosts(hosts) {
return _.forEach(hosts, function(host) {
host.groups = _.map(host.groups, 'groupid');
return host;
});
}
function convertGroups(groups) {
return _.forEach(groups, function(group) {
group.hosts = _.map(group.hosts, 'hostid');
return group;
});
}
/**
* Group Zabbix applications by name
* host.hosts - array of host ids
*/
function convertApplications(applications) {
return _.map(_.groupBy(applications, 'name'), function(value, key) {
//console.log(value);
// Hack for supporting different apis (2.2 vs 2.4 vs 3.0)
var hostField = 'host';
if (value[0] && value[0]['hosts']) {
// For Zabbix 2.2
hostField = 'hosts';
}
return {
name: key,
applicationids: _.map(value, 'applicationid'),
itemids: _.uniq(_.map(_.flatten(value, 'items'), 'itemid')),
hosts: _.uniq(_.map(_.flatten(value, hostField), 'hostid'))
};
});
}
function indexHosts(hosts) {
return _.indexBy(_.map(hosts, function(host) {
// Expand item names
host.items = _.forEach(host.items, function(item) {
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
host.applications = _.map(host.applications, 'applicationid');
host.idx_items = indexItems(host.items);
host.items = _.map(host.items, 'itemid');
return host;
}), 'hostid');
}
function indexApps(applications) {
return _.indexBy(_.map(applications, function(app) {
return {
name: app.name,
applicationid: app.applicationid,
host: _.first(_.map(app.hosts, 'hostid')),
itemids: _.map(app.items, 'itemid')
};
}), 'applicationid');
}
function indexItems(items) {
return _.indexBy(_.map(items, function(item) {
return item;
}), 'itemid');
}
/**
* Convert item.get response to cache format
* item.applications - array of application ids
* item.item - original item name returned by api (ie "CPU $2 time")
* item.name - expanded name (ie "CPU system time")
*/
function convertItems(items) {
return _.forEach(items, function(item) {
item.applications = _.map(item.applications, 'applicationid');
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
}
function filterItems(items, type) {
switch (type) {
case 'num':
return _.filter(items, function(item) {
return (item.value_type === '0' ||
item.value_type === '3');
});
case 'text':
return _.filter(items, function(item) {
return (item.value_type === '1' ||
item.value_type === '2' ||
item.value_type === '4');
});
default:
return items;
}
}
String.prototype.getHash = function() {
var hash = 0, i, chr, len;
if (this.length === 0) {
return hash;
}
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;
};

View File

@@ -11,15 +11,13 @@
* Licensed under the Apache License, Version 2.0
*/
define([
'angular',
'lodash',
'jquery'
],
function (angular, _, $) {
'use strict';
import _ from 'lodash';
import $ from 'jquery';
function TriggerPanelEditorCtrl($scope, $q, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
class TriggerPanelEditorCtrl{
/** @ngInject */
constructor($scope, $q, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
@@ -55,8 +53,7 @@ function (angular, _, $) {
var scopeDefaults = {
metric: {},
inputStyles: {},
oldTarget: _.cloneDeep(this.panel.triggers),
defaultTimeFormat: "DD MMM YYYY HH:mm:ss"
oldTarget: _.cloneDeep(this.panel.triggers)
};
_.defaults(this, scopeDefaults);
@@ -80,35 +77,28 @@ function (angular, _, $) {
});
}
var p = TriggerPanelEditorCtrl.prototype;
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _.uniq(_.map(scope.metric[metricList], 'name'));
}
p.initFilters = function () {
initFilters() {
this.filterGroups();
this.filterHosts();
this.filterApplications();
};
}
p.filterGroups = function() {
filterGroups() {
var self = this;
this.datasource.queryProcessor.filterGroups().then(function(groups) {
self.metric.groupList = groups;
});
};
}
p.filterHosts = function() {
filterHosts() {
var self = this;
var groupFilter = this.templateSrv.replace(this.panel.triggers.group.filter);
this.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) {
self.metric.filteredHosts = hosts;
});
};
}
p.filterApplications = function() {
filterApplications() {
var self = this;
var groupFilter = this.templateSrv.replace(this.panel.triggers.group.filter);
var hostFilter = this.templateSrv.replace(this.panel.triggers.host.filter);
@@ -116,51 +106,41 @@ function (angular, _, $) {
.then(function(apps) {
self.metric.filteredApplications = apps;
});
};
}
p.onTargetPartChange = function(targetPart) {
onTargetPartChange(targetPart) {
var regexStyle = {'color': '#CCA300'};
targetPart.isRegex = isRegex(targetPart.filter);
targetPart.style = targetPart.isRegex ? regexStyle : {};
};
function isRegex(str) {
// Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
return regexPattern.test(str);
}
p.parseTarget = function() {
parseTarget() {
this.initFilters();
var newTarget = _.cloneDeep(this.panel.triggers);
if (!_.isEqual(this.oldTarget, this.panel.triggers)) {
this.oldTarget = newTarget;
this.panelCtrl.refreshData();
}
};
}
p.refreshTriggerSeverity = function() {
refreshTriggerSeverity() {
_.each(this.triggerList, function(trigger) {
trigger.color = this.panel.triggerSeverity[trigger.priority].color;
trigger.severity = this.panel.triggerSeverity[trigger.priority].severity;
});
this.panelCtrl.refreshData();
};
p.datasourceChanged = function() {
this.panelCtrl.refreshData();
};
p.changeTriggerSeverityColor = function(trigger, color) {
this.panel.triggerSeverity[trigger.priority].color = color;
this.refreshTriggerSeverity();
};
function getTriggerIndexForElement(el) {
return el.parents('[data-trigger-index]').data('trigger-index');
}
p.openTriggerColorSelector = function(event) {
datasourceChanged() {
this.panelCtrl.refreshData();
}
changeTriggerSeverityColor(trigger, color) {
this.panel.triggerSeverity[trigger.priority].color = color;
this.refreshTriggerSeverity();
}
openTriggerColorSelector(event) {
var el = $(event.currentTarget);
var index = getTriggerIndexForElement(el);
var popoverScope = this.$new();
@@ -173,9 +153,9 @@ function (angular, _, $) {
templateUrl: 'public/plugins/triggers/trigger.colorpicker.html',
scope: popoverScope
});
};
}
p.openOkEventColorSelector = function(event) {
openOkEventColorSelector(event) {
var el = $(event.currentTarget);
var popoverScope = this.$new();
popoverScope.trigger = {color: this.panel.okEventColor};
@@ -190,16 +170,29 @@ function (angular, _, $) {
templateUrl: 'public/plugins/triggers/trigger.colorpicker.html',
scope: popoverScope
});
};
}
}
var triggerPanelEditor = function() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/triggers/editor.html',
controller: TriggerPanelEditorCtrl,
};
};
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _.uniq(_.map(scope.metric[metricList], 'name'));
}
return triggerPanelEditor;
});
function getTriggerIndexForElement(el) {
return el.parents('[data-trigger-index]').data('trigger-index');
}
function isRegex(str) {
// Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
return regexPattern.test(str);
}
export function triggerPanelEditor() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/triggers/editor.html',
controller: TriggerPanelEditorCtrl,
};
}

View File

@@ -1,31 +1,31 @@
<div class="table-panel-container">
<div class="table-panel-header-bg"></div>
<div class="table-panel-scroll">
<table class="table-panel-table">
<div class="triggers-panel-container">
<div class="triggers-panel-header-bg"></div>
<div class="triggers-panel-scroll">
<table class="triggers-panel-table">
<thead>
<tr>
<th ng-if="ctrl.panel.hostField" style="width: 15%">
<div class="table-panel-table-header-inner pointer">
<div class="triggers-panel-table-header-inner pointer">
Host
</div>
</th>
<th ng-if="ctrl.panel.statusField" style="width: 85px">
<div class="table-panel-table-header-inner pointer">Status</div>
<div class="triggers-panel-table-header-inner pointer">Status</div>
</th>
<th ng-if="ctrl.panel.severityField" style="width: 120px">
<div class="table-panel-table-header-inner pointer">Severity</div>
<div class="triggers-panel-table-header-inner pointer">Severity</div>
</th>
<th>
<div class="table-panel-table-header-inner pointer">Issue</div>
<div class="triggers-panel-table-header-inner pointer">Issue</div>
</th>
<th ng-if="ctrl.panel.lastChangeField" style="width: 220px">
<div class="table-panel-table-header-inner pointer">Last change</div>
<div class="triggers-panel-table-header-inner pointer">Last change</div>
</th>
<th ng-if="ctrl.panel.ageField" style="width: 180px">
<div class="table-panel-table-header-inner pointer">Age</div>
<div class="triggers-panel-table-header-inner pointer">Age</div>
</th>
<th ng-if="ctrl.panel.infoField" style="width: 100px">
<div class="table-panel-table-header-inner pointer">Info</div>
<div class="triggers-panel-table-header-inner pointer">Info</div>
</th>
</tr>
</thead>
@@ -133,4 +133,4 @@
</table>
</div>
</div>
<div class="table-panel-footer"></div>
<div class="triggers-panel-footer"></div>

View File

@@ -0,0 +1,221 @@
/**
* Grafana-Zabbix
* Zabbix plugin for Grafana.
* http://github.com/alexanderzobnin/grafana-zabbix
*
* Trigger panel.
* This feature sponsored by CORE IT
* http://www.coreit.fr
*
* Copyright 2015 Alexander Zobnin alexanderzobnin@gmail.com
* Licensed under the Apache License, Version 2.0
*/
import _ from 'lodash';
import moment from 'moment';
import {PanelCtrl} from 'app/plugins/sdk';
import {triggerPanelEditor} from './editor';
import './css/panel_triggers.css!';
var defaultSeverity = [
{ priority: 0, severity: 'Not classified', color: '#B7DBAB', show: true },
{ priority: 1, severity: 'Information', color: '#82B5D8', show: true },
{ priority: 2, severity: 'Warning', color: '#E5AC0E', show: true },
{ priority: 3, severity: 'Average', color: '#C15C17', show: true },
{ priority: 4, severity: 'High', color: '#BF1B00', show: true },
{ priority: 5, severity: 'Disaster', color: '#890F02', show: true }
];
var panelDefaults = {
datasource: null,
triggers: {
group: {filter: ""},
host: {filter: ""},
application: {filter: ""},
trigger: {filter: ""}
},
hostField: true,
statusField: false,
severityField: false,
lastChangeField: true,
ageField: true,
infoField: true,
limit: 10,
showTriggers: 'all triggers',
sortTriggersBy: { text: 'last change', value: 'lastchange' },
showEvents: { text: 'Problem events', value: '1' },
triggerSeverity: defaultSeverity,
okEventColor: '#890F02',
};
var triggerStatusMap = {
'0': 'OK',
'1': 'Problem'
};
var defaultTimeFormat = "DD MMM YYYY HH:mm:ss";
class TriggerPanelCtrl extends PanelCtrl {
/** @ngInject */
constructor($scope, $injector, $q, $element, datasourceSrv) {
super($scope, $injector);
this.datasourceSrv = datasourceSrv;
this.triggerStatusMap = triggerStatusMap;
this.defaultTimeFormat = defaultTimeFormat;
// Load panel defaults
_.defaults(this.panel, panelDefaults);
this.triggerList = [];
this.refreshData();
}
// Add panel editor
initEditMode() {
super.initEditMode();
this.icon = "fa fa-lightbulb-o";
this.addEditorTab('Options', triggerPanelEditor, 2);
}
refreshData() {
var self = this;
// Load datasource
return this.datasourceSrv.get(this.panel.datasource).then(datasource => {
var zabbix = datasource.zabbixAPI;
var queryProcessor = datasource.queryProcessor;
var triggerFilter = self.panel.triggers;
var showEvents = self.panel.showEvents.value;
var buildQuery = queryProcessor.buildTriggerQuery(triggerFilter.group.filter,
triggerFilter.host.filter,
triggerFilter.application.filter);
return buildQuery.then(query => {
return zabbix.getTriggers(query.groupids,
query.hostids,
query.applicationids,
showEvents)
.then(triggers => {
return _.map(triggers, trigger => {
var triggerObj = trigger;
// Format last change and age
trigger.lastchangeUnix = Number(trigger.lastchange);
var timestamp = moment.unix(trigger.lastchangeUnix);
if (self.panel.customLastChangeFormat) {
// User defined format
triggerObj.lastchange = timestamp.format(self.panel.lastChangeFormat);
} else {
triggerObj.lastchange = timestamp.format(self.defaultTimeFormat);
}
triggerObj.age = timestamp.fromNow(true);
// Set color
if (trigger.value === '1') {
triggerObj.color = self.panel.triggerSeverity[trigger.priority].color;
} else {
triggerObj.color = self.panel.okEventColor;
}
triggerObj.severity = self.panel.triggerSeverity[trigger.priority].severity;
return triggerObj;
});
})
.then(triggerList => {
// Request acknowledges for trigger
var eventids = _.map(triggerList, trigger => {
return trigger.lastEvent.eventid;
});
return zabbix.getAcknowledges(eventids)
.then(events => {
// Map events to triggers
_.each(triggerList, trigger => {
var event = _.find(events, event => {
return event.eventid === trigger.lastEvent.eventid;
});
if (event) {
trigger.acknowledges = _.map(event.acknowledges, ack => {
var time = new Date(+ack.clock * 1000);
ack.time = time.toLocaleString();
ack.user = ack.alias + ' (' + ack.name + ' ' + ack.surname + ')';
return ack;
});
}
});
// Filter triggers by description
var triggerFilter = self.panel.triggers.trigger.filter;
if (triggerFilter) {
triggerList = filterTriggers(triggerList, triggerFilter);
}
// Filter acknowledged triggers
if (self.panel.showTriggers === 'unacknowledged') {
triggerList = _.filter(triggerList, trigger => {
return !trigger.acknowledges;
});
} else if (self.panel.showTriggers === 'acknowledged') {
triggerList = _.filter(triggerList, 'acknowledges');
} else {
triggerList = triggerList;
}
// Filter triggers by severity
triggerList = _.filter(triggerList, trigger => {
return self.panel.triggerSeverity[trigger.priority].show;
});
// Sort triggers
if (self.panel.sortTriggersBy.value === 'priority') {
triggerList = _.sortBy(triggerList, 'priority').reverse();
} else {
triggerList = _.sortBy(triggerList, 'lastchangeUnix').reverse();
}
// Limit triggers number
self.triggerList = _.first(triggerList, self.panel.limit);
self.renderingCompleted();
});
});
});
});
}
}
TriggerPanelCtrl.templateUrl = 'module.html';
function filterTriggers(triggers, triggerFilter) {
if (isRegex(triggerFilter)) {
return _.filter(triggers, function(trigger) {
return buildRegex(triggerFilter).test(trigger.description);
});
} else {
return _.filter(triggers, function(trigger) {
return trigger.description === triggerFilter;
});
}
}
function isRegex(str) {
// Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
return regexPattern.test(str);
}
function buildRegex(str) {
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
var matches = str.match(regexPattern);
var pattern = matches[1];
var flags = matches[2] !== "" ? matches[2] : undefined;
return new RegExp(pattern, flags);
}
export {
TriggerPanelCtrl,
TriggerPanelCtrl as PanelCtrl
};

View File

@@ -0,0 +1,108 @@
$tight-form-func-bg: #333;
$blue: #33B5E5;
$dark-2: #1f1d1d;
$body-bg: rgb(20,20,20);
$grafanaListAccent: lighten($dark-2, 2%);
.triggers-panel-wrapper {
.panel-content {
padding: 0;
}
.panel-title-container {
padding-bottom: 4px;
}
}
.triggers-panel-scroll {
overflow: auto;
}
.triggers-panel-container {
padding-top: 2.2em;
position: relative;
}
.triggers-panel-footer {
text-align: center;
font-size: 90%;
line-height: 2px;
ul {
position: relative;
display: inline-block;
margin-left: 0;
margin-bottom: 0;
}
ul > li {
display: inline; // Remove list-style and block-level defaults
}
ul > li > a {
float: left; // Collapse white-space
padding: 4px 12px;
text-decoration: none;
border-left-width: 0;
&:hover {
background-color: $tight-form-func-bg;
}
&.active {
font-weight: bold;
color: $blue;
}
}
}
.triggers-panel-table {
width: 100%;
border-collapse: collapse;
th {
padding: 0;
&:first-child {
.triggers-panel-table-header-inner {
padding-left: 15px;
}
}
}
td {
padding: 0.45em 0 0.45em 1.1em;
border-bottom: 2px solid $body-bg;
border-right: 2px solid $body-bg;
&:first-child {
padding-left: 15px;
}
&:last-child {
border-right: none;
}
}
}
.triggers-panel-header-bg {
background: $grafanaListAccent;
border-top: 2px solid $body-bg;
border-bottom: 2px solid $body-bg;
height: 2.0em;
position: absolute;
top: 0;
right: 0;
left: 0;
}
.triggers-panel-table-header-inner {
padding: 0.45em 0 0.45em 1.1em;
text-align: left;
color: $blue;
position: absolute;
top: 0;
}
.triggers-panel-width-hack {
visibility: hidden;
height: 0px;
line-height: 0px;
}