Initial alerting feature. This implementation doesn't use Grafana alerting,
instead it get triggers for particular metrics and sets panel's alert state.
This commit is contained in:
97
dist/test/datasource-zabbix/datasource.js
vendored
97
dist/test/datasource-zabbix/datasource.js
vendored
@@ -7,13 +7,16 @@ exports.zabbixTemplateFormat = exports.ZabbixAPIDatasource = undefined;
|
||||
|
||||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); //import angular from 'angular';
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
var _lodash = require('lodash');
|
||||
|
||||
var _lodash2 = _interopRequireDefault(_lodash);
|
||||
|
||||
var _jquery = require('jquery');
|
||||
|
||||
var _jquery2 = _interopRequireDefault(_jquery);
|
||||
|
||||
var _datemath = require('app/core/utils/datemath');
|
||||
|
||||
var dateMath = _interopRequireWildcard(_datemath);
|
||||
@@ -51,11 +54,12 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
||||
var ZabbixAPIDatasource = function () {
|
||||
|
||||
/** @ngInject */
|
||||
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, Zabbix) {
|
||||
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, dashboardSrv, Zabbix) {
|
||||
_classCallCheck(this, ZabbixAPIDatasource);
|
||||
|
||||
this.templateSrv = templateSrv;
|
||||
this.alertSrv = alertSrv;
|
||||
this.dashboardSrv = dashboardSrv;
|
||||
|
||||
// General data source settings
|
||||
this.name = instanceSettings.name;
|
||||
@@ -103,6 +107,11 @@ var ZabbixAPIDatasource = function () {
|
||||
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
|
||||
var useTrends = timeFrom <= useTrendsFrom && this.trends;
|
||||
|
||||
// Get alerts for current panel
|
||||
this.alertQuery(options).then(function (alert) {
|
||||
_this.setPanelAlertState(options.panelId, alert.state);
|
||||
});
|
||||
|
||||
// Create request for each target
|
||||
var promises = _lodash2.default.map(options.targets, function (target) {
|
||||
// Prevent changes of original object
|
||||
@@ -454,18 +463,86 @@ var ZabbixAPIDatasource = function () {
|
||||
});
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'alertQuery',
|
||||
value: function alertQuery(options) {
|
||||
var _this7 = this;
|
||||
|
||||
var enabled_targets = filterEnabledTargets(options.targets);
|
||||
var getPanelItems = _lodash2.default.map(enabled_targets, function (target) {
|
||||
return _this7.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
|
||||
});
|
||||
|
||||
return Promise.all(getPanelItems).then(function (results) {
|
||||
var items = _lodash2.default.flatten(results);
|
||||
var itemids = _lodash2.default.map(items, 'itemid');
|
||||
|
||||
return _this7.zabbix.getAlerts(itemids);
|
||||
}).then(function (triggers) {
|
||||
if (!triggers || triggers.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
var state = 'ok';
|
||||
|
||||
var firedTriggers = _lodash2.default.filter(triggers, { value: '1' });
|
||||
if (firedTriggers.length) {
|
||||
state = 'alerting';
|
||||
}
|
||||
|
||||
return {
|
||||
panelId: options.panelId,
|
||||
state: state
|
||||
};
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'setPanelAlertState',
|
||||
value: function setPanelAlertState(panelId, alertState) {
|
||||
var panelContainers = _lodash2.default.filter((0, _jquery2.default)('.panel-container'), function (elem) {
|
||||
return elem.clientHeight && elem.clientWidth;
|
||||
});
|
||||
|
||||
var panelModels = _lodash2.default.flatten(_lodash2.default.map(this.dashboardSrv.dash.rows, function (row) {
|
||||
if (row.collapse) {
|
||||
return [];
|
||||
} else {
|
||||
return row.panels;
|
||||
}
|
||||
}));
|
||||
var panelIndex = _lodash2.default.findIndex(panelModels, function (panel) {
|
||||
return panel.id === panelId;
|
||||
});
|
||||
|
||||
if (panelIndex >= 0) {
|
||||
if (alertState) {
|
||||
if (alertState === 'alerting') {
|
||||
var alertClass = "panel-has-alert panel-alert-state--" + alertState;
|
||||
(0, _jquery2.default)(panelContainers[panelIndex]).addClass(alertClass);
|
||||
}
|
||||
if (alertState === 'ok') {
|
||||
var _alertClass = "panel-alert-state--" + alertState;
|
||||
(0, _jquery2.default)(panelContainers[panelIndex]).addClass(_alertClass);
|
||||
(0, _jquery2.default)(panelContainers[panelIndex]).removeClass("panel-has-alert");
|
||||
}
|
||||
} else {
|
||||
var _alertClass2 = "panel-has-alert panel-alert-state--ok panel-alert-state--alerting";
|
||||
(0, _jquery2.default)(panelContainers[panelIndex]).removeClass(_alertClass2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replace template variables
|
||||
|
||||
}, {
|
||||
key: 'replaceTargetVariables',
|
||||
value: function replaceTargetVariables(target, options) {
|
||||
var _this7 = this;
|
||||
var _this8 = this;
|
||||
|
||||
var parts = ['group', 'host', 'application', 'item'];
|
||||
_lodash2.default.forEach(parts, function (p) {
|
||||
if (target[p] && target[p].filter) {
|
||||
target[p].filter = _this7.replaceTemplateVars(target[p].filter, options.scopedVars);
|
||||
target[p].filter = _this8.replaceTemplateVars(target[p].filter, options.scopedVars);
|
||||
}
|
||||
});
|
||||
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
|
||||
@@ -473,9 +550,9 @@ var ZabbixAPIDatasource = function () {
|
||||
_lodash2.default.forEach(target.functions, function (func) {
|
||||
func.params = _lodash2.default.map(func.params, function (param) {
|
||||
if (typeof param === 'number') {
|
||||
return +_this7.templateSrv.replace(param.toString(), options.scopedVars);
|
||||
return +_this8.templateSrv.replace(param.toString(), options.scopedVars);
|
||||
} else {
|
||||
return _this7.templateSrv.replace(param, options.scopedVars);
|
||||
return _this8.templateSrv.replace(param, options.scopedVars);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -572,6 +649,12 @@ function sequence(funcsArray) {
|
||||
};
|
||||
}
|
||||
|
||||
function filterEnabledTargets(targets) {
|
||||
return _lodash2.default.filter(targets, function (target) {
|
||||
return !(target.hide || !target.group || !target.host || !target.item);
|
||||
});
|
||||
}
|
||||
|
||||
exports.ZabbixAPIDatasource = ZabbixAPIDatasource;
|
||||
exports.zabbixTemplateFormat = zabbixTemplateFormat;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user