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:
96
dist/datasource-zabbix/datasource.js
vendored
96
dist/datasource-zabbix/datasource.js
vendored
@@ -1,9 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations', './metricFunctions', './dataProcessor', './responseHandler', './zabbix.js', './zabbixAPICore.service.js'], function (_export, _context) {
|
||||
System.register(['lodash', 'jquery', 'app/core/utils/datemath', './utils', './migrations', './metricFunctions', './dataProcessor', './responseHandler', './zabbix.js', './zabbixAPICore.service.js'], function (_export, _context) {
|
||||
"use strict";
|
||||
|
||||
var _, dateMath, utils, migrations, metricFunctions, dataProcessor, responseHandler, ZabbixAPIError, _slicedToArray, _createClass, ZabbixAPIDatasource;
|
||||
var _, $, dateMath, utils, migrations, metricFunctions, dataProcessor, responseHandler, ZabbixAPIError, _slicedToArray, _createClass, ZabbixAPIDatasource;
|
||||
|
||||
function _classCallCheck(instance, Constructor) {
|
||||
if (!(instance instanceof Constructor)) {
|
||||
@@ -98,9 +98,17 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
};
|
||||
}
|
||||
|
||||
function filterEnabledTargets(targets) {
|
||||
return _.filter(targets, function (target) {
|
||||
return !(target.hide || !target.group || !target.host || !target.item);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
setters: [function (_lodash) {
|
||||
_ = _lodash.default;
|
||||
}, function (_jquery) {
|
||||
$ = _jquery.default;
|
||||
}, function (_appCoreUtilsDatemath) {
|
||||
dateMath = _appCoreUtilsDatemath;
|
||||
}, function (_utils) {
|
||||
@@ -176,11 +184,12 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
_export('ZabbixAPIDatasource', 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;
|
||||
@@ -228,6 +237,11 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
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 = _.map(options.targets, function (target) {
|
||||
// Prevent changes of original object
|
||||
@@ -555,15 +569,83 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
});
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'alertQuery',
|
||||
value: function alertQuery(options) {
|
||||
var _this7 = this;
|
||||
|
||||
var enabled_targets = filterEnabledTargets(options.targets);
|
||||
var getPanelItems = _.map(enabled_targets, function (target) {
|
||||
return _this7.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
|
||||
});
|
||||
|
||||
return Promise.all(getPanelItems).then(function (results) {
|
||||
var items = _.flatten(results);
|
||||
var itemids = _.map(items, 'itemid');
|
||||
|
||||
return _this7.zabbix.getAlerts(itemids);
|
||||
}).then(function (triggers) {
|
||||
if (!triggers || triggers.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
var state = 'ok';
|
||||
|
||||
var firedTriggers = _.filter(triggers, { value: '1' });
|
||||
if (firedTriggers.length) {
|
||||
state = 'alerting';
|
||||
}
|
||||
|
||||
return {
|
||||
panelId: options.panelId,
|
||||
state: state
|
||||
};
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'setPanelAlertState',
|
||||
value: function setPanelAlertState(panelId, alertState) {
|
||||
var panelContainers = _.filter($('.panel-container'), function (elem) {
|
||||
return elem.clientHeight && elem.clientWidth;
|
||||
});
|
||||
|
||||
var panelModels = _.flatten(_.map(this.dashboardSrv.dash.rows, function (row) {
|
||||
if (row.collapse) {
|
||||
return [];
|
||||
} else {
|
||||
return row.panels;
|
||||
}
|
||||
}));
|
||||
var panelIndex = _.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;
|
||||
$(panelContainers[panelIndex]).addClass(alertClass);
|
||||
}
|
||||
if (alertState === 'ok') {
|
||||
var _alertClass = "panel-alert-state--" + alertState;
|
||||
$(panelContainers[panelIndex]).addClass(_alertClass);
|
||||
$(panelContainers[panelIndex]).removeClass("panel-has-alert");
|
||||
}
|
||||
} else {
|
||||
var _alertClass2 = "panel-has-alert panel-alert-state--ok panel-alert-state--alerting";
|
||||
$(panelContainers[panelIndex]).removeClass(_alertClass2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: 'replaceTargetVariables',
|
||||
value: function replaceTargetVariables(target, options) {
|
||||
var _this7 = this;
|
||||
var _this8 = this;
|
||||
|
||||
var parts = ['group', 'host', 'application', 'item'];
|
||||
_.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);
|
||||
@@ -571,9 +653,9 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
_.forEach(target.functions, function (func) {
|
||||
func.params = _.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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
2
dist/datasource-zabbix/datasource.js.map
vendored
2
dist/datasource-zabbix/datasource.js.map
vendored
File diff suppressed because one or more lines are too long
@@ -19,9 +19,11 @@ describe('ZabbixDatasource', () => {
|
||||
};
|
||||
ctx.templateSrv = {};
|
||||
ctx.alertSrv = {};
|
||||
ctx.dashboardSrv = {};
|
||||
ctx.zabbix = () => {};
|
||||
|
||||
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.zabbix);
|
||||
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.dashboardSrv, ctx.zabbix);
|
||||
ctx.ds.alertQuery = () => Q.when([]);
|
||||
});
|
||||
|
||||
describe('When querying data', () => {
|
||||
|
||||
1
dist/datasource-zabbix/zabbix.js
vendored
1
dist/datasource-zabbix/zabbix.js
vendored
@@ -49,6 +49,7 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
|
||||
|
||||
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);
|
||||
|
||||
2
dist/datasource-zabbix/zabbix.js.map
vendored
2
dist/datasource-zabbix/zabbix.js.map
vendored
File diff suppressed because one or more lines are too long
25
dist/datasource-zabbix/zabbixAPI.service.js
vendored
25
dist/datasource-zabbix/zabbixAPI.service.js
vendored
@@ -375,6 +375,31 @@ System.register(['angular', 'lodash', './utils', './zabbixAPICore.service'], fun
|
||||
});
|
||||
});
|
||||
}
|
||||
}, {
|
||||
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;
|
||||
|
||||
File diff suppressed because one or more lines are too long
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;
|
||||
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
1
dist/test/datasource-zabbix/zabbix.js
vendored
1
dist/test/datasource-zabbix/zabbix.js
vendored
@@ -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);
|
||||
|
||||
25
dist/test/datasource-zabbix/zabbixAPI.service.js
vendored
25
dist/test/datasource-zabbix/zabbixAPI.service.js
vendored
@@ -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;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//import angular from 'angular';
|
||||
import _ from 'lodash';
|
||||
import $ from 'jquery';
|
||||
import * as dateMath from 'app/core/utils/datemath';
|
||||
import * as utils from './utils';
|
||||
import * as migrations from './migrations';
|
||||
@@ -12,9 +12,10 @@ import {ZabbixAPIError} from './zabbixAPICore.service.js';
|
||||
class ZabbixAPIDatasource {
|
||||
|
||||
/** @ngInject */
|
||||
constructor(instanceSettings, templateSrv, alertSrv, Zabbix) {
|
||||
constructor(instanceSettings, templateSrv, alertSrv, dashboardSrv, Zabbix) {
|
||||
this.templateSrv = templateSrv;
|
||||
this.alertSrv = alertSrv;
|
||||
this.dashboardSrv = dashboardSrv;
|
||||
|
||||
// General data source settings
|
||||
this.name = instanceSettings.name;
|
||||
@@ -56,6 +57,11 @@ class ZabbixAPIDatasource {
|
||||
let useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
|
||||
let useTrends = (timeFrom <= useTrendsFrom) && this.trends;
|
||||
|
||||
// Get alerts for current panel
|
||||
this.alertQuery(options).then(alert => {
|
||||
this.setPanelAlertState(options.panelId, alert.state);
|
||||
});
|
||||
|
||||
// Create request for each target
|
||||
let promises = _.map(options.targets, target => {
|
||||
// Prevent changes of original object
|
||||
@@ -394,6 +400,72 @@ class ZabbixAPIDatasource {
|
||||
});
|
||||
}
|
||||
|
||||
alertQuery(options) {
|
||||
let enabled_targets = filterEnabledTargets(options.targets);
|
||||
let getPanelItems = _.map(enabled_targets, target => {
|
||||
return this.zabbix.getItemsFromTarget(target, {itemtype: 'num'});
|
||||
});
|
||||
|
||||
return Promise.all(getPanelItems)
|
||||
.then(results => {
|
||||
let items = _.flatten(results);
|
||||
let itemids = _.map(items, 'itemid');
|
||||
|
||||
return this.zabbix.getAlerts(itemids);
|
||||
})
|
||||
.then(triggers => {
|
||||
if (!triggers || triggers.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
let state = 'ok';
|
||||
|
||||
let firedTriggers = _.filter(triggers, {value: '1'});
|
||||
if (firedTriggers.length) {
|
||||
state = 'alerting';
|
||||
}
|
||||
|
||||
return {
|
||||
panelId: options.panelId,
|
||||
state: state
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
setPanelAlertState(panelId, alertState) {
|
||||
let panelContainers = _.filter($('.panel-container'), elem => {
|
||||
return elem.clientHeight && elem.clientWidth;
|
||||
});
|
||||
|
||||
let panelModels = _.flatten(_.map(this.dashboardSrv.dash.rows, row => {
|
||||
if (row.collapse) {
|
||||
return [];
|
||||
} else {
|
||||
return row.panels;
|
||||
}
|
||||
}));
|
||||
let panelIndex = _.findIndex(panelModels, panel => {
|
||||
return panel.id === panelId;
|
||||
});
|
||||
|
||||
if (panelIndex >= 0) {
|
||||
if (alertState) {
|
||||
if (alertState === 'alerting') {
|
||||
let alertClass = "panel-has-alert panel-alert-state--" + alertState;
|
||||
$(panelContainers[panelIndex]).addClass(alertClass);
|
||||
}
|
||||
if (alertState === 'ok') {
|
||||
let alertClass = "panel-alert-state--" + alertState;
|
||||
$(panelContainers[panelIndex]).addClass(alertClass);
|
||||
$(panelContainers[panelIndex]).removeClass("panel-has-alert");
|
||||
}
|
||||
} else {
|
||||
let alertClass = "panel-has-alert panel-alert-state--ok panel-alert-state--alerting";
|
||||
$(panelContainers[panelIndex]).removeClass(alertClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replace template variables
|
||||
replaceTargetVariables(target, options) {
|
||||
let parts = ['group', 'host', 'application', 'item'];
|
||||
@@ -505,6 +577,12 @@ function sequence(funcsArray) {
|
||||
};
|
||||
}
|
||||
|
||||
function filterEnabledTargets(targets) {
|
||||
return _.filter(targets, target => {
|
||||
return !(target.hide || !target.group || !target.host || !target.item);
|
||||
});
|
||||
}
|
||||
|
||||
export {ZabbixAPIDatasource, zabbixTemplateFormat};
|
||||
|
||||
// Fix for backward compatibility with lodash 2.4
|
||||
|
||||
@@ -19,9 +19,11 @@ describe('ZabbixDatasource', () => {
|
||||
};
|
||||
ctx.templateSrv = {};
|
||||
ctx.alertSrv = {};
|
||||
ctx.dashboardSrv = {};
|
||||
ctx.zabbix = () => {};
|
||||
|
||||
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.zabbix);
|
||||
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.dashboardSrv, ctx.zabbix);
|
||||
ctx.ds.alertQuery = () => Q.when([]);
|
||||
});
|
||||
|
||||
describe('When querying data', () => {
|
||||
|
||||
@@ -30,6 +30,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);
|
||||
|
||||
@@ -401,6 +401,29 @@ function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user