Initial ES6 migration. module.js and datasource.js are migrated to ES6.

This commit is contained in:
Alexander Zobnin
2016-03-14 23:42:24 +03:00
parent 50c0764d01
commit 8b37478131
31 changed files with 567 additions and 455 deletions

View File

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

View File

@@ -0,0 +1,241 @@
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

@@ -0,0 +1,430 @@
//import angular from 'angular';
import _ from 'lodash';
import {parse as dateMathParse} from 'app/core/utils/datemath';
import Utils from './utils';
import metricFunctions from './metricFunctions';
import {zabbixHelperSrv} from './helperFunctions';
import {ZabbixAPI} from './zabbixAPI';
import {ZabbixCachingProxy} from './zabbixCache';
import {QueryProcessor} from './queryProcessor';
import {DataProcessingService} from './dataProcessingService';
export class ZabbixAPIDatasource {
constructor(instanceSettings, $q, templateSrv, alertSrv, ZabbixAPI, ZabbixCachingProxy, QueryProcessor, zabbixHelperSrv, 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);
// Dependencies
this.q = $q;
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
this.zabbixHelperSrv = zabbixHelperSrv;
this.DataProcessingService = DataProcessingService;
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(dateMathParse(options.range.from) / 1000);
var to = Math.ceil(dateMathParse(options.range.to) / 1000);
var useTrendsFrom = Math.ceil(dateMathParse('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', self.DataProcessingService);
// 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', self.DataProcessingService);
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', self.DataProcessingService);
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(_.bind(zabbixHelperSrv.handleSLAResponse, zabbixHelperSrv, target.itservice, target.slaProperty));
}
}
}, this);
var self = 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) {
var DPS = self.DataProcessingService;
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = DPS.groupBy(options.interval, DPS.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 parts = [];
_.each(query.split('.'), function (part) {
part = this.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(dateMathParse(options.rangeRaw.from) / 1000);
var to = Math.ceil(dateMathParse(options.rangeRaw.to) / 1000);
var annotation = options.annotation;
var self = this;
var showEvents = 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,
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
};
});
});
});
});
}
}
function bindFunctionDefs(functionDefs, category, DataProcessingService) {
'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(DataProcessingService.metricFunctions);
});
}
function formatMetric(metricObj) {
'use strict';
return {
text: metricObj.name,
expandable: false
};
}

View File

@@ -0,0 +1,21 @@
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

@@ -0,0 +1,311 @@
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

@@ -0,0 +1,247 @@
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

@@ -0,0 +1,214 @@
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

@@ -0,0 +1,19 @@
import {ZabbixAPIDatasource} from './datasource';
import {ZabbixQueryCtrl} from './queryCtrl';
class ZabbixConfigCtrl {}
ZabbixConfigCtrl.templateUrl = 'partials/config.html';
class ZabbixQueryOptionsCtrl {}
ZabbixQueryOptionsCtrl.templateUrl = 'partials/query.options.html';
class ZabbixAnnotationsQueryCtrl {}
ZabbixAnnotationsQueryCtrl.templateUrl = 'partials/annotations.editor.html';
export {
ZabbixAPIDatasource as Datasource,
ZabbixQueryCtrl as QueryCtrl,
ZabbixConfigCtrl as ConfigCtrl,
ZabbixQueryOptionsCtrl as QueryOptionsCtrl,
ZabbixAnnotationsQueryCtrl as AnnotationsQueryCtrl
};

View File

@@ -0,0 +1,61 @@
<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

@@ -0,0 +1,56 @@
<datasource-http-settings current="ctrl.current">
</datasource-http-settings>
<h5>Zabbix API details</h5>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
User
</li>
<li>
<input type="text" class="tight-form-input input-large"
ng-model='ctrl.current.jsonData.username'
placeholder="">
</li>
<li class="tight-form-item" style="width: 80px">
Password
</li>
<li>
<input type="password" class="tight-form-input input-large"
ng-model='ctrl.current.jsonData.password'
placeholder="">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 200px">
Cache update interval
</li>
<li>
<input type="text" class="tight-form-input input-small"
ng-model='ctrl.current.jsonData.cacheTTL'
placeholder="1h">
</li>
<li class="tight-form-item" style="width: 80px">
Trends&nbsp;&nbsp;
<input class="cr1" id="ctrl.current.jsonData.trends" type="checkbox"
ng-model="ctrl.current.jsonData.trends"
ng-checked="ctrl.current.jsonData.trends">
<label for="ctrl.current.jsonData.trends" class="cr1"></label>
</li>
<li ng-if="ctrl.current.jsonData.trends"
class="tight-form-item"
style="width: 105px">
Use trends from
</li>
<li ng-if="ctrl.current.jsonData.trends">
<input type="text" class="tight-form-input input-small"
ng-model='ctrl.current.jsonData.trendsFrom'
placeholder="7d">
</li>
</ul>
<div class="clearfix"></div>
</div>

View File

@@ -0,0 +1,221 @@
<div class="tight-form">
<ul class="tight-form-list pull-right">
<li class="tight-form-item small" ng-show="ctrl.target.datasource">
<em>{{ctrl.target.datasource}}</em>
</li>
<li class="tight-form-item">
<div class="dropdown">
<a class="pointer dropdown-toggle"
data-toggle="dropdown"
tabindex="1">
<i class="fa fa-bars"></i>
</a>
<ul class="dropdown-menu pull-right" role="menu">
<!-- Switch editor mode -->
<li role="menuitem" ng-show="ctrl.target.mode">
<a class="pointer" tabindex="1"
ng-click="ctrl.switchEditorMode(0)">Numeric metrics</a>
</li>
<li role="menuitem" ng-show="ctrl.target.mode != 1">
<a class="pointer" tabindex="1"
ng-click="ctrl.switchEditorMode(1)">IT services</a>
</li>
<li role="menuitem" ng-show="ctrl.target.mode != 2">
<a class="pointer" tabindex="1"
ng-click="ctrl.switchEditorMode(2)">Text metrics</a>
</li>
<li class="divider" role="menuitem"></li>
<!-- From app/features/panel/partials/query_editor_row.html -->
<li role="menuitem">
<a tabindex="1" ng-click="ctrl.duplicateQuery()">Duplicate</a>
</li>
<li role="menuitem">
<a tabindex="1" ng-click="ctrl.moveQuery(-1)">Move up</a>
</li>
<li role="menuitem">
<a tabindex="1" ng-click="ctrl.moveQuery(1)">Move down</a>
</li>
</ul>
</div>
</li>
<li class="tight-form-item last">
<a class="pointer" tabindex="1" ng-click="ctrl.removeQuery(target)">
<i class="fa fa-trash"></i>
</a>
</li>
</ul>
<ul class="tight-form-list">
<li class="tight-form-item" style="min-width: 15px; text-align: center">
{{ctrl.target.refId}}
</li>
<li>
<a class="tight-form-item" ng-click="ctrl.toggleHideQuery()" role="menuitem">
<i class="fa fa-eye"></i>
</a>
</li>
</ul>
<!-- IT Service editor -->
<ul class="tight-form-list" role="menu" ng-show="ctrl.target.mode == 1">
<li class="tight-form-item input-small">IT Service</li>
<li>
<select class="tight-form-input input-large"
ng-change="ctrl.selectITService()"
ng-model="ctrl.target.itservice"
bs-tooltip="ctrl.target.itservice.name.length > 25 ? ctrl.target.itservice.name : ''"
ng-options="itservice.name for itservice in ctrl.itserviceList track by itservice.name">
<option value="">-- Select IT service --</option>
</select>
</li>
<li class="tight-form-item input-medium">IT service property</li>
<li>
<select class="tight-form-input input-medium"
ng-change="ctrl.selectITService()"
ng-model="ctrl.target.slaProperty"
ng-options="slaProperty.name for slaProperty in ctrl.slaPropertyList track by slaProperty.name">
<option value="">-- Property --</option>
</select>
</li>
</ul>
<ul class="tight-form-list" role="menu" ng-hide="ctrl.target.mode == 1">
<!-- Select Host Group -->
<li class="tight-form-item input-small" style="width: 5em">Group</li>
<li>
<input type="text"
ng-model="ctrl.target.group.filter"
bs-typeahead="ctrl.getGroupNames"
ng-change="ctrl.onTargetPartChange(ctrl.target.group)"
ng-blur="ctrl.onTargetBlur()"
data-min-length=0
data-items=100
class="input-medium tight-form-input"
ng-style="ctrl.target.group.style">
</li>
<!-- Select Host -->
<li class="tight-form-item input-small" style="width: 3em">Host</li>
<li>
<input type="text"
ng-model="ctrl.target.host.filter"
bs-typeahead="ctrl.getHostNames"
ng-change="ctrl.onTargetPartChange(ctrl.target.host)"
ng-blur="ctrl.onTargetBlur()"
data-min-length=0
data-items=100
class="input-large tight-form-input"
ng-style="ctrl.target.host.style">
</li>
<li class="tight-form-item" ng-hide="ctrl.target.mode == 2">
Show disabled items&nbsp;
<editor-checkbox text=""
model="ctrl.target.showDisabledItems"
change="ctrl.onTargetBlur()">
</editor-checkbox>
</li>
<!-- Downsampling function -->
<!-- <li class="tight-form-item input-medium" ng-hide="ctrl.target.mode == 2">
Downsampling
</li>
<li ng-hide="ctrl.target.mode == 2">
<select class="tight-form-input input-small"
ng-change="ctrl.targetBlur()"
ng-model="ctrl.target.downsampleFunction"
bs-tooltip="'Downsampling function'"
ng-options="func.name for func in downsampleFunctionList track by func.name">
</select>
<a bs-tooltip="ctrl.target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="ctrl.target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li> -->
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form" ng-hide="ctrl.target.mode == 1">
<ul class="tight-form-list" role="menu">
<li class="tight-form-item" style="width: 44px">&nbsp</li>
<!-- Select Application -->
<li class="tight-form-item" style="width: 5em">Application</li>
<li>
<input type="text"
ng-model="ctrl.target.application.filter"
bs-typeahead="ctrl.getApplicationNames"
ng-change="ctrl.onTargetPartChange(ctrl.target.application)"
ng-blur="ctrl.onTargetBlur()"
data-min-length=0
data-items=100
class="input-medium tight-form-input"
ng-style="ctrl.target.application.style">
</li>
<!-- Select Item -->
<li class="tight-form-item input-small" style="width: 3em">Item</li>
<li>
<input type="text"
ng-model="ctrl.target.item.filter"
bs-typeahead="ctrl.getItemNames"
ng-change="ctrl.onTargetPartChange(ctrl.target.item)"
ng-blur="ctrl.onTargetBlur()"
data-min-length=0
data-items=100
class="input-large tight-form-input"
ng-style="ctrl.target.item.style">
</li>
<li class="tight-form-item query-keyword">Options</li>
<li ng-repeat="func in ctrl.target.functions">
<span metric-function-editor class="tight-form-item tight-form-func">
</span>
</li>
<li class="dropdown" add-metric-function>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form" ng-hide="ctrl.target.mode === 1" ng-if="ctrl.target.options">
<ul class="tight-form-list" role="menu">
<li class="tight-form-item" style="width: 44px">&nbsp</li>
<li class="tight-form-item query-keyword" style="width: 5em">Options</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form" ng-show="ctrl.target.mode == 2">
<ul class="tight-form-list" role="menu">
<li class="tight-form-item" style="width: 44px">&nbsp</li>
<!-- Text metric regex -->
<li class="tight-form-item" style="width: 5em" ng-show="ctrl.target.mode == 2">
Text filter
</li>
<li ng-show="ctrl.target.mode == 2">
<input type="text"
class="tight-form-input" style="width: 417px"
ng-model="ctrl.target.textFilter"
spellcheck='false'
placeholder="Text filter (regex)"
ng-blur="ctrl.targetBlur()">
</li>
<li class="tight-form-item" ng-show="ctrl.target.mode == 2">
Use capture groups&nbsp;
<input class="cr1" id="ctrl.target.useCaptureGroups" type="checkbox"
ng-model="ctrl.target.useCaptureGroups"
ng-checked="ctrl.target.useCaptureGroups"
ng-change="ctrl.targetBlur()">
<label for="ctrl.target.useCaptureGroups" class="cr1"></label>
</li>
</ul>
<div class="clearfix"></div>
</div>

View File

@@ -0,0 +1,96 @@
<section class="grafana-metric-options">
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item tight-form-item-icon">
<i class="fa fa-wrench"></i>
</li>
<li class="tight-form-item">
Max data points
</li>
<li>
<input type="text"
class="input-mini tight-form-input"
ng-model="ctrl.panelCtrl.panel.maxDataPoints"
bs-tooltip="'Override max data points, automatically set to graph width in pixels.'"
data-placement="right"
ng-model-onblur ng-change="ctrl.panelCtrl.refresh()"
spellcheck='false'
placeholder="auto">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item tight-form-item-icon">
<i class="fa fa-info-circle"></i>
</li>
<li class="tight-form-item">
<a ng-click="ctrl.panelCtrl.toggleEditorHelp(1)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
Max data points
</a>
</li>
<li class="tight-form-item">
<a ng-click="ctrl.panelCtrl.toggleEditorHelp(2)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
IT services
</a>
</li>
<li class="tight-form-item">
<a ng-click="ctrl.panelCtrl.toggleEditorHelp(3)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
IT service property
</a>
</li>
<li class="tight-form-item">
<a ng-click="ctrl.panelCtrl.toggleEditorHelp(4)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
Text filter
</a>
</li>
</ul>
<div class="clearfix"></div>
</div>
</section>
<div class="editor-row">
<div class="pull-left" style="margin-top: 30px;">
<div class="grafana-info-box span8" ng-if="ctrl.panelCtrl.editorHelpIndex === 1">
<h5>Max data points</h5>
<ul>
<li>Grafana-Zabbix plugin uses maxDataPoints parameter to consolidate the real number of values down to this
number
</li>
<li>If there are more real values, then by default they will be consolidated using averages</li>
<li>This could hide real peaks and max values in your series</li>
<li>Point consolidation will effect series legend values (min,max,total,current)</li>
<li>If you override maxDataPoint and set a high value performance can be severely effected</li>
</ul>
</div>
<div class="grafana-info-box span8" ng-if="ctrl.panelCtrl.editorHelpIndex === 2">
<h5>IT services</h5>
<ul>
<li>Select "IT services" in targets menu to activate IT services mode.</li>
</ul>
</div>
<div class="grafana-info-box span8" ng-if="ctrl.panelCtrl.editorHelpIndex === 3">
<h5>IT service property</h5>
<ul>
<li>Zabbix returns the following availability information about IT service</li>
<li>Status - current status of the IT service</li>
<li>SLA - SLA for the given time interval</li>
<li>OK time - time the service was in OK state, in seconds</li>
<li>Problem time - time the service was in problem state, in seconds</li>
<li>Down time - time the service was in scheduled downtime, in seconds</li>
</ul>
</div>
<div class="grafana-info-box span8" ng-if="ctrl.panelCtrl.editorHelpIndex === 4">
<h5>Text filter</h5>
<ul>
<li>Use regex to extract a part of the returned value.</li>
</ul>
</div>
</div>
</div>

View File

@@ -0,0 +1,12 @@
{
"type": "datasource",
"name": "Zabbix",
"id": "zabbix",
"module": "plugins/zabbix/module",
"staticRoot": ".",
"metrics": true,
"annotations": true
}

View File

@@ -0,0 +1,242 @@
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

@@ -0,0 +1,453 @@
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

@@ -0,0 +1,77 @@
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

@@ -0,0 +1,370 @@
define([
'angular',
'lodash',
'./zabbixAPIService'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
/**
* Zabbix API Wrapper.
* Creates Zabbix API instance with given parameters (url, credentials and other).
* Wraps API calls and provides high-level methods.
*/
module.factory('ZabbixAPI', function($q, backendSrv, alertSrv, ZabbixAPIService) {
// Initialize Zabbix API.
function ZabbixAPI(api_url, username, password, basicAuth, withCredentials) {
this.url = api_url;
this.username = username;
this.password = password;
this.auth = "";
this.requestOptions = {
basicAuth: basicAuth,
withCredentials: withCredentials
};
this.loginPromise = null;
}
var p = ZabbixAPI.prototype;
//////////////////
// Core methods //
//////////////////
p.request = function(method, params) {
var self = this;
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
.then(function(result) {
return result;
},
// Handle API errors
function(error) {
if (isNotAuthorized(error.data)) {
return self.loginOnce().then(
function() {
return self.request(method, params);
},
// Handle user.login method errors
function(error) {
self.alertAPIError(error.data);
});
}
});
};
p.alertAPIError = function(message) {
alertSrv.set(
"Zabbix API Error",
message,
'error'
);
};
function isNotAuthorized(message) {
return (
message === "Session terminated, re-login, please." ||
message === "Not authorised." ||
message === "Not authorized."
);
}
/**
* When API unauthenticated or auth token expired each request produce login()
* call. But auth token is common to all requests. This function wraps login() method
* and call it once. If login() already called just wait for it (return its promise).
* @return login promise
*/
p.loginOnce = function() {
var self = this;
var deferred = $q.defer();
if (!self.loginPromise) {
self.loginPromise = deferred.promise;
self.login().then(
function(auth) {
self.loginPromise = null;
self.auth = auth;
deferred.resolve(auth);
},
function(error) {
self.loginPromise = null;
deferred.reject(error);
}
);
} else {
return self.loginPromise;
}
return deferred.promise;
};
/**
* Get authentication token.
*/
p.login = function() {
return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions);
};
/**
* Get Zabbix API version
*/
p.getVersion = function() {
return ZabbixAPIService.getVersion(this.url, this.requestOptions);
};
/////////////////
// API methods //
/////////////////
p.getGroups = function() {
var params = {
output: ['name'],
sortfield: 'name',
selectHosts: []
};
return this.request('hostgroup.get', params);
};
p.getHosts = function() {
var params = {
output: ['name', 'host'],
sortfield: 'name',
selectGroups: []
};
return this.request('host.get', params);
};
p.getApplications = function() {
var params = {
output: ['name'],
sortfield: 'name',
// Hack for supporting different apis (2.2 vs 2.4 vs 3.0)
selectHost: [],
selectHosts: []
};
return this.request('application.get', params);
};
p.getItems = function() {
var params = {
output: [
'name', 'key_',
'value_type',
'hostid',
'status',
'state'
],
sortfield: 'name',
selectApplications: []
};
return this.request('item.get', params);
};
/**
* Get Hosts list with host's items.
* @return {[type]} [description]
*/
p.getHostsExtend = function() {
var params = {
output: ['name', 'host'],
sortfield: 'name',
selectGroups: [],
selectItems: [
'name', 'key_',
'value_type',
'hostid',
'status',
'state'
]
};
return this.request('host.get', params);
};
p.getLastValue = function(itemid) {
var params = {
output: ['lastvalue'],
itemids: itemid
};
return this.request('item.get', params).then(function(items) {
if (items.length) {
return items[0].lastvalue;
} else {
return null;
}
});
};
/**
* Perform history query from Zabbix API
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} time_from Time in seconds
* @param {Number} time_till Time in seconds
* @return {Array} Array of Zabbix history objects
*/
p.getHistory = function(items, time_from, time_till) {
var self = this;
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
history: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: time_from
};
// Relative queries (e.g. last hour) don't include an end time
if (time_till) {
params.time_till = time_till;
}
return self.request('history.get', params);
})).then(_.flatten);
};
/**
* Perform trends query from Zabbix API
* Use trends api extension from ZBXNEXT-1193 patch.
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} time_from Time in seconds
* @param {Number} time_till Time in seconds
* @return {Array} Array of Zabbix trend objects
*/
p.getTrend_ZBXNEXT1193 = function(items, time_from, time_till) {
var self = this;
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
trend: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: time_from
};
// Relative queries (e.g. last hour) don't include an end time
if (time_till) {
params.time_till = time_till;
}
return self.request('trend.get', params);
})).then(_.flatten);
};
p.getTrend_30 = function(items, time_from, time_till, value_type) {
var self = this;
var itemids = _.map(items, 'itemid');
var params = {
output: ["itemid",
"clock",
value_type
],
itemids: itemids,
time_from: time_from
};
// Relative queries (e.g. last hour) don't include an end time
if (time_till) {
params.time_till = time_till;
}
return self.request('trend.get', params);
};
p.getTrend = p.getTrend_ZBXNEXT1193;
//p.getTrend = p.getTrend_30;
p.getITService = function(/* optional */ serviceids) {
var params = {
output: 'extend',
serviceids: serviceids
};
return this.request('service.get', params);
};
p.getSLA = function(serviceids, from, to) {
var params = {
serviceids: serviceids,
intervals: [{
from: from,
to: to
}]
};
return this.request('service.getsla', params);
};
p.getTriggers = function(groupids, hostids, applicationids, showEvents) {
var params = {
output: 'extend',
groupids: groupids,
hostids: hostids,
applicationids: applicationids,
expandDescription: true,
expandData: true,
monitored: true,
skipDependent: true,
//only_true: true,
filter: {
value: 1
},
selectGroups: ['name'],
selectHosts: ['name'],
selectItems: ['name', 'key_', 'lastvalue'],
selectLastEvent: 'extend'
};
if (showEvents) {
params.filter.value = showEvents;
}
return this.request('trigger.get', params);
};
p.getAcknowledges = function(eventids) {
var params = {
output: 'extend',
eventids: eventids,
preservekeys: true,
select_acknowledges: 'extend',
sortfield: 'clock',
sortorder: 'DESC'
};
return this.request('event.get', params)
.then(function (events) {
return _.filter(events, function(event) {
return event.acknowledges.length;
});
});
};
return ZabbixAPI;
});
});

View File

@@ -0,0 +1,103 @@
/**
* 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

@@ -0,0 +1,307 @@
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

@@ -0,0 +1,288 @@
<div class="editor-row">
<div class="section tight-form-container" style="margin-bottom: 20px">
<h5>Select 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="editor.panel.triggers.group.filter"
bs-typeahead="editor.getGroupNames"
ng-change="editor.onTargetPartChange(editor.panel.triggers.group)"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="input-large tight-form-input"
ng-style="editor.panel.triggers.group.style">
</li>
<li class="tight-form-item" style="width: 50px">
Host
</li>
<li>
<input type="text"
ng-model="editor.panel.triggers.host.filter"
bs-typeahead="editor.getHostNames"
ng-change="editor.onTargetPartChange(editor.panel.triggers.host)"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="input-large tight-form-input last"
ng-style="editor.panel.triggers.host.style">
</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="editor.panel.triggers.application.filter"
bs-typeahead="editor.getApplicationNames"
ng-change="editor.onTargetPartChange(editor.panel.triggers.application)"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="input-large tight-form-input"
ng-style="editor.panel.triggers.application.style">
</li>
<li class="tight-form-item" style="width: 50px">
Trigger
</li>
<li>
<input type="text"
ng-model="editor.panel.triggers.trigger.filter"
ng-change="editor.onTargetPartChange(editor.panel.triggers.trigger)"
ng-blur="editor.parseTarget()"
placeholder="trigger name"
class="input-large tight-form-input last"
ng-style="editor.panel.triggers.trigger.style"
empty-to-null>
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
<div class="section">
<h5>Data source</h5>
<div class="section tight-form-container" style="margin-bottom: 20px">
<div class="tight-form">
<ul class="tight-form-list">
<li>
<select class="tight-form-input input-large last"
ng-model="editor.panel.datasource"
ng-options="ds for ds in editor.datasources"
ng-change="editor.datasourceChanged()">
</select>
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="editor-row">
<div class="section">
<h5>Options</h5>
<div class="tight-form-container" style="margin-bottom: 20px">
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 100px">
<strong>Acknowledged</strong>
</li>
<li>
<select class="input-medium tight-form-input"
ng-model="editor.panel.showTriggers"
ng-options="f for f in editor.ackFilters"
ng-change="editor.panelCtrl.refreshData()">
</select>
</li>
<li class="tight-form-item" style="width: 13em">
<strong>Limit triggers number to</strong>
</li>
<li>
<input class="input-small tight-form-input"
type="number"
ng-model="editor.panel.limit"
ng-model-onblur
ng-change="editor.panelCtrl.refreshData()">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 100px">
<strong>Sort by</strong>
</li>
<li>
<select class="input-medium tight-form-input"
ng-model="editor.panel.sortTriggersBy"
ng-options="f.text for f in editor.sortByFields track by f.value"
ng-change="editor.panelCtrl.refreshData()">
</select>
</li>
<li class="tight-form-item" style="width: 13em">
<strong>Show events</strong>
</li>
<li>
<select class="tight-form-input input-medium"
ng-model="editor.panel.showEvents"
ng-options="f.text for f in editor.showEventsFields track by f.value"
ng-change="editor.panelCtrl.refreshData()">
</select>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 100px">
<strong>Show fields</strong>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="hostField">Host</label>
<input class="cr1"
id="hostField"
type="checkbox"
ng-model="editor.panel.hostField"
ng-checked="editor.panel.hostField">
<label for="hostField" class="cr1"></label>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="statusField">Status</label>
<input class="cr1"
id="statusField"
type="checkbox"
ng-model="editor.panel.statusField"
ng-checked="editor.panel.statusField">
<label for="statusField" class="cr1"></label>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="severityField">Severity</label>
<input class="cr1"
id="severityField"
type="checkbox"
ng-model="editor.panel.severityField"
ng-checked="editor.panel.severityField">
<label for="severityField" class="cr1"></label>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="lastChangeField">Last change</label>
<input class="cr1"
id="lastChangeField"
type="checkbox"
ng-model="editor.panel.lastChangeField"
ng-checked="editor.panel.lastChangeField">
<label for="lastChangeField" class="cr1"></label>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="ageField">Age</label>
<input class="cr1"
id="ageField"
type="checkbox"
ng-model="editor.panel.ageField"
ng-checked="editor.panel.ageField">
<label for="ageField" class="cr1"></label>
</li>
<li class="tight-form-item last">
<label class="checkbox-label" for="infoField">Info</label>
<input class="cr1"
id="infoField"
type="checkbox"
ng-model="editor.panel.infoField"
ng-checked="editor.panel.infoField">
<label for="infoField" class="cr1"></label>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item">
<strong>Custom Last change format</strong>
<label class="checkbox-label" for="customLastChangeFormat">&nbsp;</label>
<input class="cr1"
id="customLastChangeFormat"
type="checkbox"
ng-change="editor.panelCtrl.refreshData()"
ng-model="editor.panel.customLastChangeFormat"
ng-checked="editor.panel.customLastChangeFormat">
<label for="customLastChangeFormat" class="cr1"></label>
</li>
<li ng-if="editor.panel.customLastChangeFormat">
<input type="text"
ng-model="editor.panel.lastChangeFormat"
ng-blur="editor.panelCtrl.refreshData()"
placeholder="dddd, MMMM Do YYYY, h:mm:ss a"
class="tight-form-input"
style="width: 300px"
empty-to-null>
</li>
<li class="tight-form-item last" ng-if="editor.panel.customLastChangeFormat">
<a href="http://momentjs.com/docs/#/displaying/format/" target="_blank">
<i class="fa fa-question-circle"
bs-tooltip="'See moment.js dosc for time format.'">
</i>
</a>
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="section">
<h5>Customize triggers severity and colors</h5>
<div class="tight-form" ng-repeat="trigger in editor.panel.triggerSeverity">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 10px">
{{ trigger.priority }}
</li>
<li>
<input class="tight-form-input input-medium"
type="text"
empty-to-null
ng-model="trigger.severity"
style="color: white"
ng-style="{background: trigger.color}"
ng-model-onblur
ng-change="editor.panelCtrl.refreshData()">
</li>
<li class="tight-form-item">
<spectrum-picker ng-model="trigger.color" ng-change="editor.panelCtrl.refreshData()"></spectrum-picker>
</li>
<li class="tight-form-item last" style="width: 28px">
<label class="checkbox-label" for="{{ 'trigger-show-' + $index }}"></label>
<input class="cr1"
ng-attr-id="{{ 'trigger-show-' + $index }}"
type="checkbox"
ng-model="trigger.show"
ng-checked="trigger.show"
ng-change="editor.panelCtrl.refreshData()">
<label for="{{ 'trigger-show-' + $index }}" class="cr1"></label>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item"
ng-style="{background:editor.panel.okEventColor}"
style="width: 160px; color: white">
<span style="padding-left: 25px"> OK event color </span>
</li>
<li class="tight-form-item">
<spectrum-picker
ng-model="editor.panel.okEventColor"
ng-change="editor.panelCtrl.refreshData()">
</spectrum-picker>
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
</div>

View File

@@ -0,0 +1,205 @@
/**
* 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([
'angular',
'lodash',
'jquery'
],
function (angular, _, $) {
'use strict';
function TriggerPanelEditorCtrl($scope, $q, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
this.popoverSrv = popoverSrv;
// 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.ackFilters = [
'all triggers',
'unacknowledged',
'acknowledged'
];
this.sortByFields = [
{ text: 'last change', value: 'lastchange' },
{ text: 'severity', value: 'priority' }
];
this.showEventsFields = [
{ text: 'all events', value: [0,1] },
{ text: 'Ok events', value: 0 },
{ text: 'Problem events', value: 1 }
];
// Load scope defaults
var scopeDefaults = {
metric: {},
inputStyles: {},
oldTarget: _.cloneDeep(this.panel.triggers),
defaultTimeFormat: "DD MMM YYYY HH:mm:ss"
};
_.defaults(this, scopeDefaults);
var self = this;
// Get zabbix data sources
var datasources = _.filter(this.datasourceSrv.getMetricSources(), function(datasource) {
return datasource.meta.id === 'zabbix';
});
this.datasources = _.map(datasources, 'name');
// Set default datasource
if (!this.panel.datasource) {
this.panel.datasource = this.datasources[0];
}
// Load datasource
this.datasourceSrv.get(this.panel.datasource).then(function (datasource) {
self.datasource = datasource;
self.initFilters();
self.panelCtrl.refreshData();
});
}
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 () {
this.filterGroups();
this.filterHosts();
this.filterApplications();
};
p.filterGroups = function() {
var self = this;
this.datasource.queryProcessor.filterGroups().then(function(groups) {
self.metric.groupList = groups;
});
};
p.filterHosts = function() {
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() {
var self = this;
var groupFilter = this.templateSrv.replace(this.panel.triggers.group.filter);
var hostFilter = this.templateSrv.replace(this.panel.triggers.host.filter);
this.datasource.queryProcessor.filterApplications(groupFilter, hostFilter)
.then(function(apps) {
self.metric.filteredApplications = apps;
});
};
p.onTargetPartChange = function(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() {
this.initFilters();
var newTarget = _.cloneDeep(this.panel.triggers);
if (!_.isEqual(this.oldTarget, this.panel.triggers)) {
this.oldTarget = newTarget;
this.panelCtrl.refreshData();
}
};
p.refreshTriggerSeverity = function() {
_.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) {
var el = $(event.currentTarget);
var index = getTriggerIndexForElement(el);
var popoverScope = this.$new();
popoverScope.trigger = this.panel.triggerSeverity[index];
popoverScope.changeTriggerSeverityColor = this.changeTriggerSeverityColor;
this.popoverSrv.show({
element: el,
placement: 'top',
templateUrl: 'public/plugins/triggers/trigger.colorpicker.html',
scope: popoverScope
});
};
p.openOkEventColorSelector = function(event) {
var el = $(event.currentTarget);
var popoverScope = this.$new();
popoverScope.trigger = {color: this.panel.okEventColor};
popoverScope.changeTriggerSeverityColor = function(trigger, color) {
this.panel.okEventColor = color;
this.refreshTriggerSeverity();
};
this.popoverSrv.show({
element: el,
placement: 'top',
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,
};
};
return triggerPanelEditor;
});

View File

@@ -0,0 +1,136 @@
<div class="table-panel-container">
<div class="table-panel-header-bg"></div>
<div class="table-panel-scroll">
<table class="table-panel-table">
<thead>
<tr>
<th ng-if="ctrl.panel.hostField" style="width: 15%">
<div class="table-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>
</th>
<th ng-if="ctrl.panel.severityField" style="width: 120px">
<div class="table-panel-table-header-inner pointer">Severity</div>
</th>
<th>
<div class="table-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>
</th>
<th ng-if="ctrl.panel.ageField" style="width: 180px">
<div class="table-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>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="trigger in ctrl.triggerList">
<td ng-if="ctrl.panel.hostField">
<div>
<span><strong>{{trigger.host}}</strong></span>
</div>
</td>
<td ng-if="ctrl.panel.statusField" style="background-color: {{trigger.color}}; color: white">
<div>
{{ctrl.triggerStatusMap[trigger.value]}}
</div>
</td>
<td ng-if="ctrl.panel.severityField" style="background-color: {{trigger.color}}; color: white">
<div>
{{trigger.severity}}
</div>
</td>
<td style="background-color: {{trigger.color}}; color: white">
<div>
{{trigger.description}}
<a ng-if="trigger.comments"
class="pointer"
style="float: right; padding-right: 8px"
data-toggle="collapse"
data-target="#comments-{{trigger.triggerid}}"
bs-tooltip="'Show additional trigger description'"
data-placement="top">
<i class="fa fa-file-text-o"></i>
</a>
</div>
<!-- Trigger comments -->
<div class="collapse"
id="comments-{{trigger.triggerid}}"
ng-if="trigger.comments">
<div>
<small>{{trigger.comments}}</small>
</div>
</div>
<!-- Trigger acknowledges -->
<div class="collapse"
id="acknowledges-{{trigger.triggerid}}"
ng-if="trigger.acknowledges">
<div style="padding-top: 12px;">
<table class="table table-condensed">
<thead>
<tr>
<th><small>Time</small></th>
<th><small>User</small></th>
<th><small>Comments</small></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="ack in trigger.acknowledges">
<td>
<small>{{ack.time}}</small>
</td>
<td>
<small>{{ack.user}}</small>
</td>
<td>
<small>{{ack.message}}</small>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
<td ng-if="ctrl.panel.lastChangeField">
{{trigger.lastchange}}
</td>
<td ng-if="ctrl.panel.ageField">
{{trigger.age}}
</td>
<td ng-if="ctrl.panel.infoField">
<!-- Trigger Url -->
<a ng-if="trigger.url"
href="{{trigger.url}}"
target="_blank">
<i class="fa fa-external-link"></i>
</a>
<!-- Trigger state -->
<span ng-if="trigger.state === '1'"
bs-tooltip="'{{trigger.error}}'">
<i class="fa fa-question-circle"></i>
</span>
<!-- Trigger acknowledges -->
<a ng-if="trigger.acknowledges"
bs-tooltip="'Acknowledges ({{trigger.acknowledges.length}})'"
data-toggle="collapse"
data-target="#acknowledges-{{trigger.triggerid}}">
<i class="fa fa-comments"></i>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="table-panel-footer"></div>

View File

@@ -0,0 +1,230 @@
/**
* 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,8 @@
{
"type": "panel",
"name": "Zabbix triggers",
"id": "triggers",
"module": "plugins/triggers/module",
"staticRoot": "."
}

View File

@@ -0,0 +1,13 @@
<div class="graph-legend-popover">
<a class="close"
href=""
ng-click="dismiss();">×</a>
<div class="editor-row">
<i ng-repeat="color in colors" class="pointer"
ng-class="{'fa fa-circle-o': color === trigger.color,'fa fa-circle': color !== trigger.color}"
ng-style="{color:color}"
ng-click="changeTriggerSeverityColor(trigger, color);dismiss();">&nbsp;</i>
</div>
</div>