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:
Alexander Zobnin
2017-03-05 13:12:11 +03:00
parent 5192e8648d
commit f805213b02
15 changed files with 349 additions and 22 deletions

View File

@@ -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;

View File

@@ -33,9 +33,13 @@ describe('ZabbixDatasource', function () {
};
ctx.templateSrv = {};
ctx.alertSrv = {};
ctx.dashboardSrv = {};
ctx.zabbix = function () {};
ctx.ds = new _module.Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.zabbix);
ctx.ds = new _module.Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.dashboardSrv, ctx.zabbix);
ctx.ds.alertQuery = function () {
return _q2.default.when([]);
};
});
describe('When querying data', function () {

View File

@@ -52,6 +52,7 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAlerts = this.zabbixAPI.getAlerts.bind(this.zabbixAPI);
this.getAcknowledges = this.zabbixAPI.getAcknowledges.bind(this.zabbixAPI);
this.getITService = this.zabbixAPI.getITService.bind(this.zabbixAPI);
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);

View File

@@ -445,6 +445,31 @@ function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
});
});
}
}, {
key: 'getAlerts',
value: function getAlerts(itemids, timeFrom, timeTo) {
var params = {
output: 'extend',
itemids: itemids,
expandDescription: true,
expandData: true,
expandComment: true,
monitored: true,
skipDependent: true,
//only_true: true,
// filter: {
// value: 1
// },
selectLastEvent: 'extend'
};
if (timeFrom || timeTo) {
params.lastChangeSince = timeFrom;
params.lastChangeTill = timeTo;
}
return this.request('trigger.get', params);
}
}]);
return ZabbixAPI;