Merge branch 'alerting-poc'

This commit is contained in:
Alexander Zobnin
2017-03-10 13:36:01 +03:00
25 changed files with 817 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
'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', 'app/core/utils/datemath', './utils', './migrations', './metricFunctions', './dataProcessor', './responseHandler', './zabbix.js', './zabbixAlerting.service.js', './zabbixAPICore.service.js'], function (_export, _context) {
"use strict";
var _, dateMath, utils, migrations, metricFunctions, dataProcessor, responseHandler, ZabbixAPIError, _slicedToArray, _createClass, ZabbixAPIDatasource;
@@ -98,6 +98,24 @@ 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);
});
}
function getTriggerThreshold(expression) {
var thresholdPattern = /.*[<>]([\d\.]+)/;
var finded_thresholds = expression.match(thresholdPattern);
if (finded_thresholds && finded_thresholds.length >= 2) {
var threshold = finded_thresholds[1];
threshold = Number(threshold);
return threshold;
} else {
return null;
}
}
return {
setters: [function (_lodash) {
_ = _lodash.default;
@@ -113,7 +131,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
dataProcessor = _dataProcessor.default;
}, function (_responseHandler) {
responseHandler = _responseHandler.default;
}, function (_zabbixJs) {}, function (_zabbixAPICoreServiceJs) {
}, function (_zabbixJs) {}, function (_zabbixAlertingServiceJs) {}, function (_zabbixAPICoreServiceJs) {
ZabbixAPIError = _zabbixAPICoreServiceJs.ZabbixAPIError;
}],
execute: function () {
@@ -176,11 +194,13 @@ 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, zabbixAlertingSrv, Zabbix) {
_classCallCheck(this, ZabbixAPIDatasource);
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
this.dashboardSrv = dashboardSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
// General data source settings
this.name = instanceSettings.name;
@@ -200,6 +220,11 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
var ttl = instanceSettings.jsonData.cacheTTL || '1h';
this.cacheTTL = utils.parseInterval(ttl);
// Alerting options
this.alertingEnabled = instanceSettings.jsonData.alerting;
this.addThresholds = instanceSettings.jsonData.addThresholds;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || 2;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Use custom format for template variables
@@ -228,6 +253,20 @@ 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
if (this.alertingEnabled) {
this.alertQuery(options).then(function (alert) {
_this.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
_this.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
if (_this.addThresholds) {
_.forEach(alert.thresholds, function (threshold) {
_this.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
});
}
});
}
// Create request for each target
var promises = _.map(options.targets, function (target) {
// Prevent changes of original object
@@ -555,15 +594,57 @@ 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) {
triggers = _.filter(triggers, function (trigger) {
return trigger.priority >= _this7.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
return {};
}
var state = 'ok';
var firedTriggers = _.filter(triggers, { value: '1' });
if (firedTriggers.length) {
state = 'alerting';
}
var thresholds = _.map(triggers, function (trigger) {
return getTriggerThreshold(trigger.expression);
});
return {
panelId: options.panelId,
state: state,
thresholds: thresholds
};
});
}
}, {
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 +652,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);
}
});
});

File diff suppressed because one or more lines are too long

View File

@@ -31,9 +31,10 @@
<div class="gf-form">
<label class="gf-form-label width-7">Trends</label>
</div>
<gf-form-switch class="gf-form"
<gf-form-switch class="gf-form" label-class="width-5"
label="Enable"
checked="ctrl.current.jsonData.trends" switch-class="max-width-6">
checked="ctrl.current.jsonData.trends"
switch-class="max-width-6">
</gf-form-switch>
<div class="gf-form" ng-if="ctrl.current.jsonData.trends">
<span class="gf-form-label width-7">
@@ -58,3 +59,26 @@
</input>
</div>
</div>
<div class="gf-form-group">
<h3 class="page-heading">Alerting</h3>
<gf-form-switch class="gf-form" label-class="width-8"
label="Enable alerting"
checked="ctrl.current.jsonData.alerting">
</gf-form-switch>
<gf-form-switch class="gf-form" label-class="width-8"
label="Add thresholds"
checked="ctrl.current.jsonData.addThresholds">
</gf-form-switch>
<div class="gf-form max-width-20">
<span class="gf-form-label width-8">Min severity</span>
<div class="gf-form-select-wrapper max-width-16">
<select class="gf-form-input" ng-model="ctrl.current.jsonData.alertingMinSeverity"
ng-options="s.val as s.text for s in [
{val: 0, text: 'Not classified'}, {val: 1, text:'Information'},
{val: 2, text: 'Warning'}, {val: 3, text: 'Average'},
{val: 4, text: 'High'}, {val: 5, text: 'Disaster'}]">
</select>
</div>
</div>
</div>

View File

@@ -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', () => {

View File

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

File diff suppressed because one or more lines are too long

View File

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

View File

@@ -0,0 +1,151 @@
'use strict';
System.register(['lodash', 'jquery', 'angular'], function (_export, _context) {
"use strict";
var _, $, angular, _createClass, ZabbixAlertingService;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
return {
setters: [function (_lodash) {
_ = _lodash.default;
}, function (_jquery) {
$ = _jquery.default;
}, function (_angular) {
angular = _angular.default;
}],
execute: function () {
_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;
};
}();
ZabbixAlertingService = function () {
/** @ngInject */
function ZabbixAlertingService(dashboardSrv) {
_classCallCheck(this, ZabbixAlertingService);
this.dashboardSrv = dashboardSrv;
}
_createClass(ZabbixAlertingService, [{
key: 'isFullScreen',
value: function isFullScreen() {
return this.dashboardSrv.dash.meta.fullscreen;
}
}, {
key: 'setPanelAlertState',
value: function setPanelAlertState(panelId, alertState) {
var panelIndex = void 0;
var panelContainers = _.filter($('.panel-container'), function (elem) {
return elem.clientHeight && elem.clientWidth;
});
var panelModels = this.getPanelModels();
if (this.isFullScreen()) {
panelIndex = 0;
} else {
panelIndex = _.findIndex(panelModels, function (panel) {
return panel.id === panelId;
});
}
if (panelIndex >= 0) {
var alertClass = "panel-has-alert panel-alert-state--ok panel-alert-state--alerting";
$(panelContainers[panelIndex]).removeClass(alertClass);
if (alertState) {
if (alertState === 'alerting') {
alertClass = "panel-has-alert panel-alert-state--" + alertState;
$(panelContainers[panelIndex]).addClass(alertClass);
}
if (alertState === 'ok') {
alertClass = "panel-alert-state--" + alertState;
$(panelContainers[panelIndex]).addClass(alertClass);
$(panelContainers[panelIndex]).removeClass("panel-has-alert");
}
}
}
}
}, {
key: 'getPanelModels',
value: function getPanelModels() {
return _.flatten(_.map(this.dashboardSrv.dash.rows, function (row) {
if (row.collapse) {
return [];
} else {
return row.panels;
}
}));
}
}, {
key: 'getPanelModel',
value: function getPanelModel(panelId) {
var panelModels = this.getPanelModels();
return _.find(panelModels, function (panel) {
return panel.id === panelId;
});
}
}, {
key: 'setPanelThreshold',
value: function setPanelThreshold(panelId, threshold) {
var panel = this.getPanelModel(panelId);
var containsThreshold = _.find(panel.thresholds, { value: threshold });
if (panel && panel.type === "graph" && !containsThreshold) {
var thresholdOptions = {
colorMode: "custom",
fill: false,
line: true,
lineColor: "rgb(255, 0, 0)",
op: "gt",
value: threshold,
source: "zabbix"
};
panel.thresholds.push(thresholdOptions);
}
}
}, {
key: 'removeZabbixThreshold',
value: function removeZabbixThreshold(panelId) {
var panel = this.getPanelModel(panelId);
if (panel && panel.type === "graph") {
panel.thresholds = _.filter(panel.thresholds, function (threshold) {
return threshold.source !== "zabbix";
});
}
}
}]);
return ZabbixAlertingService;
}();
angular.module('grafana.services').service('zabbixAlertingSrv', ZabbixAlertingService);
}
};
});
//# sourceMappingURL=zabbixAlerting.service.js.map

File diff suppressed because one or more lines are too long

View File

@@ -117,7 +117,7 @@ System.register(['lodash', 'jquery', 'moment', '../datasource-zabbix/utils', 'ap
_inherits(TriggerPanelCtrl, _PanelCtrl);
/** @ngInject */
function TriggerPanelCtrl($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv) {
function TriggerPanelCtrl($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv, dashboardSrv) {
_classCallCheck(this, TriggerPanelCtrl);
var _this = _possibleConstructorReturn(this, (TriggerPanelCtrl.__proto__ || Object.getPrototypeOf(TriggerPanelCtrl)).call(this, $scope, $injector));
@@ -125,6 +125,8 @@ System.register(['lodash', 'jquery', 'moment', '../datasource-zabbix/utils', 'ap
_this.datasourceSrv = datasourceSrv;
_this.templateSrv = templateSrv;
_this.contextSrv = contextSrv;
_this.dashboardSrv = dashboardSrv;
_this.triggerStatusMap = triggerStatusMap;
_this.defaultTimeFormat = defaultTimeFormat;
_this.pageIndex = 0;
@@ -151,6 +153,11 @@ System.register(['lodash', 'jquery', 'moment', '../datasource-zabbix/utils', 'ap
value: function onRefresh() {
var _this2 = this;
// ignore fetching data if another panel is in fullscreen
if (this.otherPanelInFullscreenMode()) {
return;
}
// clear loading/error state
delete this.error;
this.loading = true;

File diff suppressed because one or more lines are too long

View File

@@ -7,8 +7,7 @@ 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');
@@ -40,6 +39,8 @@ var _responseHandler2 = _interopRequireDefault(_responseHandler);
require('./zabbix.js');
require('./zabbixAlerting.service.js');
var _zabbixAPICoreService = require('./zabbixAPICore.service.js');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
@@ -51,11 +52,13 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var ZabbixAPIDatasource = function () {
/** @ngInject */
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, Zabbix) {
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, dashboardSrv, zabbixAlertingSrv, Zabbix) {
_classCallCheck(this, ZabbixAPIDatasource);
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
this.dashboardSrv = dashboardSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
// General data source settings
this.name = instanceSettings.name;
@@ -75,6 +78,11 @@ var ZabbixAPIDatasource = function () {
var ttl = instanceSettings.jsonData.cacheTTL || '1h';
this.cacheTTL = utils.parseInterval(ttl);
// Alerting options
this.alertingEnabled = instanceSettings.jsonData.alerting;
this.addThresholds = instanceSettings.jsonData.addThresholds;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || 2;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Use custom format for template variables
@@ -103,6 +111,20 @@ var ZabbixAPIDatasource = function () {
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
var useTrends = timeFrom <= useTrendsFrom && this.trends;
// Get alerts for current panel
if (this.alertingEnabled) {
this.alertQuery(options).then(function (alert) {
_this.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
_this.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
if (_this.addThresholds) {
_lodash2.default.forEach(alert.thresholds, function (threshold) {
_this.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
});
}
});
}
// Create request for each target
var promises = _lodash2.default.map(options.targets, function (target) {
// Prevent changes of original object
@@ -455,17 +477,66 @@ var ZabbixAPIDatasource = function () {
});
}
/**
* Get triggers and its details for panel's targets
* Returns alert state ('ok' if no fired triggers, or 'alerting' if at least 1 trigger is fired)
* or empty object if no related triggers are finded.
*/
}, {
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) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return trigger.priority >= _this7.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
return {};
}
var state = 'ok';
var firedTriggers = _lodash2.default.filter(triggers, { value: '1' });
if (firedTriggers.length) {
state = 'alerting';
}
var thresholds = _lodash2.default.map(triggers, function (trigger) {
return getTriggerThreshold(trigger.expression);
});
return {
panelId: options.panelId,
state: state,
thresholds: thresholds
};
});
}
// 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 +544,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 +643,24 @@ function sequence(funcsArray) {
};
}
function filterEnabledTargets(targets) {
return _lodash2.default.filter(targets, function (target) {
return !(target.hide || !target.group || !target.host || !target.item);
});
}
function getTriggerThreshold(expression) {
var thresholdPattern = /.*[<>]([\d\.]+)/;
var finded_thresholds = expression.match(thresholdPattern);
if (finded_thresholds && finded_thresholds.length >= 2) {
var threshold = finded_thresholds[1];
threshold = Number(threshold);
return threshold;
} else {
return null;
}
}
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;

View File

@@ -0,0 +1,127 @@
'use strict';
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 _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ZabbixAlertingService = function () {
/** @ngInject */
function ZabbixAlertingService(dashboardSrv) {
_classCallCheck(this, ZabbixAlertingService);
this.dashboardSrv = dashboardSrv;
}
_createClass(ZabbixAlertingService, [{
key: 'isFullScreen',
value: function isFullScreen() {
return this.dashboardSrv.dash.meta.fullscreen;
}
}, {
key: 'setPanelAlertState',
value: function setPanelAlertState(panelId, alertState) {
var panelIndex = void 0;
var panelContainers = _lodash2.default.filter((0, _jquery2.default)('.panel-container'), function (elem) {
return elem.clientHeight && elem.clientWidth;
});
var panelModels = this.getPanelModels();
if (this.isFullScreen()) {
panelIndex = 0;
} else {
panelIndex = _lodash2.default.findIndex(panelModels, function (panel) {
return panel.id === panelId;
});
}
if (panelIndex >= 0) {
var alertClass = "panel-has-alert panel-alert-state--ok panel-alert-state--alerting";
(0, _jquery2.default)(panelContainers[panelIndex]).removeClass(alertClass);
if (alertState) {
if (alertState === 'alerting') {
alertClass = "panel-has-alert panel-alert-state--" + alertState;
(0, _jquery2.default)(panelContainers[panelIndex]).addClass(alertClass);
}
if (alertState === 'ok') {
alertClass = "panel-alert-state--" + alertState;
(0, _jquery2.default)(panelContainers[panelIndex]).addClass(alertClass);
(0, _jquery2.default)(panelContainers[panelIndex]).removeClass("panel-has-alert");
}
}
}
}
}, {
key: 'getPanelModels',
value: function getPanelModels() {
return _lodash2.default.flatten(_lodash2.default.map(this.dashboardSrv.dash.rows, function (row) {
if (row.collapse) {
return [];
} else {
return row.panels;
}
}));
}
}, {
key: 'getPanelModel',
value: function getPanelModel(panelId) {
var panelModels = this.getPanelModels();
return _lodash2.default.find(panelModels, function (panel) {
return panel.id === panelId;
});
}
}, {
key: 'setPanelThreshold',
value: function setPanelThreshold(panelId, threshold) {
var panel = this.getPanelModel(panelId);
var containsThreshold = _lodash2.default.find(panel.thresholds, { value: threshold });
if (panel && panel.type === "graph" && !containsThreshold) {
var thresholdOptions = {
colorMode: "custom",
fill: false,
line: true,
lineColor: "rgb(255, 0, 0)",
op: "gt",
value: threshold,
source: "zabbix"
};
panel.thresholds.push(thresholdOptions);
}
}
}, {
key: 'removeZabbixThreshold',
value: function removeZabbixThreshold(panelId) {
var panel = this.getPanelModel(panelId);
if (panel && panel.type === "graph") {
panel.thresholds = _lodash2.default.filter(panel.thresholds, function (threshold) {
return threshold.source !== "zabbix";
});
}
}
}]);
return ZabbixAlertingService;
}();
_angular2.default.module('grafana.services').service('zabbixAlertingSrv', ZabbixAlertingService);

View File

@@ -92,7 +92,7 @@ var TriggerPanelCtrl = function (_PanelCtrl) {
_inherits(TriggerPanelCtrl, _PanelCtrl);
/** @ngInject */
function TriggerPanelCtrl($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv) {
function TriggerPanelCtrl($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv, dashboardSrv) {
_classCallCheck(this, TriggerPanelCtrl);
var _this = _possibleConstructorReturn(this, (TriggerPanelCtrl.__proto__ || Object.getPrototypeOf(TriggerPanelCtrl)).call(this, $scope, $injector));
@@ -100,6 +100,8 @@ var TriggerPanelCtrl = function (_PanelCtrl) {
_this.datasourceSrv = datasourceSrv;
_this.templateSrv = templateSrv;
_this.contextSrv = contextSrv;
_this.dashboardSrv = dashboardSrv;
_this.triggerStatusMap = triggerStatusMap;
_this.defaultTimeFormat = defaultTimeFormat;
_this.pageIndex = 0;
@@ -126,6 +128,11 @@ var TriggerPanelCtrl = function (_PanelCtrl) {
value: function onRefresh() {
var _this2 = this;
// ignore fetching data if another panel is in fullscreen
if (this.otherPanelInFullscreenMode()) {
return;
}
// clear loading/error state
delete this.error;
this.loading = true;

View File

@@ -1,4 +1,3 @@
//import angular from 'angular';
import _ from 'lodash';
import * as dateMath from 'app/core/utils/datemath';
import * as utils from './utils';
@@ -7,14 +6,17 @@ import * as metricFunctions from './metricFunctions';
import dataProcessor from './dataProcessor';
import responseHandler from './responseHandler';
import './zabbix.js';
import './zabbixAlerting.service.js';
import {ZabbixAPIError} from './zabbixAPICore.service.js';
class ZabbixAPIDatasource {
/** @ngInject */
constructor(instanceSettings, templateSrv, alertSrv, Zabbix) {
constructor(instanceSettings, templateSrv, alertSrv, dashboardSrv, zabbixAlertingSrv, Zabbix) {
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
this.dashboardSrv = dashboardSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
// General data source settings
this.name = instanceSettings.name;
@@ -34,6 +36,11 @@ class ZabbixAPIDatasource {
var ttl = instanceSettings.jsonData.cacheTTL || '1h';
this.cacheTTL = utils.parseInterval(ttl);
// Alerting options
this.alertingEnabled = instanceSettings.jsonData.alerting;
this.addThresholds = instanceSettings.jsonData.addThresholds;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || 2;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Use custom format for template variables
@@ -56,6 +63,20 @@ class ZabbixAPIDatasource {
let useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
let useTrends = (timeFrom <= useTrendsFrom) && this.trends;
// Get alerts for current panel
if (this.alertingEnabled) {
this.alertQuery(options).then(alert => {
this.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
this.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
if (this.addThresholds) {
_.forEach(alert.thresholds, threshold => {
this.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
});
}
});
}
// Create request for each target
let promises = _.map(options.targets, target => {
// Prevent changes of original object
@@ -394,6 +415,52 @@ class ZabbixAPIDatasource {
});
}
/**
* Get triggers and its details for panel's targets
* Returns alert state ('ok' if no fired triggers, or 'alerting' if at least 1 trigger is fired)
* or empty object if no related triggers are finded.
*/
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 => {
triggers = _.filter(triggers, trigger => {
return trigger.priority >= this.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
return {};
}
let state = 'ok';
let firedTriggers = _.filter(triggers, {value: '1'});
if (firedTriggers.length) {
state = 'alerting';
}
let thresholds = _.map(triggers, trigger => {
return getTriggerThreshold(trigger.expression);
});
return {
panelId: options.panelId,
state: state,
thresholds: thresholds
};
});
}
// Replace template variables
replaceTargetVariables(target, options) {
let parts = ['group', 'host', 'application', 'item'];
@@ -505,6 +572,24 @@ function sequence(funcsArray) {
};
}
function filterEnabledTargets(targets) {
return _.filter(targets, target => {
return !(target.hide || !target.group || !target.host || !target.item);
});
}
function getTriggerThreshold(expression) {
let thresholdPattern = /.*[<>]([\d\.]+)/;
let finded_thresholds = expression.match(thresholdPattern);
if (finded_thresholds && finded_thresholds.length >= 2) {
let threshold = finded_thresholds[1];
threshold = Number(threshold);
return threshold;
} else {
return null;
}
}
export {ZabbixAPIDatasource, zabbixTemplateFormat};
// Fix for backward compatibility with lodash 2.4

View File

@@ -31,9 +31,10 @@
<div class="gf-form">
<label class="gf-form-label width-7">Trends</label>
</div>
<gf-form-switch class="gf-form"
<gf-form-switch class="gf-form" label-class="width-5"
label="Enable"
checked="ctrl.current.jsonData.trends" switch-class="max-width-6">
checked="ctrl.current.jsonData.trends"
switch-class="max-width-6">
</gf-form-switch>
<div class="gf-form" ng-if="ctrl.current.jsonData.trends">
<span class="gf-form-label width-7">
@@ -58,3 +59,26 @@
</input>
</div>
</div>
<div class="gf-form-group">
<h3 class="page-heading">Alerting</h3>
<gf-form-switch class="gf-form" label-class="width-8"
label="Enable alerting"
checked="ctrl.current.jsonData.alerting">
</gf-form-switch>
<gf-form-switch class="gf-form" label-class="width-8"
label="Add thresholds"
checked="ctrl.current.jsonData.addThresholds">
</gf-form-switch>
<div class="gf-form max-width-20">
<span class="gf-form-label width-8">Min severity</span>
<div class="gf-form-select-wrapper max-width-16">
<select class="gf-form-input" ng-model="ctrl.current.jsonData.alertingMinSeverity"
ng-options="s.val as s.text for s in [
{val: 0, text: 'Not classified'}, {val: 1, text:'Information'},
{val: 2, text: 'Warning'}, {val: 3, text: 'Average'},
{val: 4, text: 'High'}, {val: 5, text: 'Disaster'}]">
</select>
</div>
</div>
</div>

View File

@@ -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', () => {

View File

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

View File

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

View File

@@ -0,0 +1,102 @@
import _ from 'lodash';
import $ from 'jquery';
import angular from 'angular';
class ZabbixAlertingService {
/** @ngInject */
constructor(dashboardSrv) {
this.dashboardSrv = dashboardSrv;
}
isFullScreen() {
return this.dashboardSrv.dash.meta.fullscreen;
}
setPanelAlertState(panelId, alertState) {
let panelIndex;
let panelContainers = _.filter($('.panel-container'), elem => {
return elem.clientHeight && elem.clientWidth;
});
let panelModels = this.getPanelModels();
if (this.isFullScreen()) {
panelIndex = 0;
} else {
panelIndex = _.findIndex(panelModels, panel => {
return panel.id === panelId;
});
}
if (panelIndex >= 0) {
let alertClass = "panel-has-alert panel-alert-state--ok panel-alert-state--alerting";
$(panelContainers[panelIndex]).removeClass(alertClass);
if (alertState) {
if (alertState === 'alerting') {
alertClass = "panel-has-alert panel-alert-state--" + alertState;
$(panelContainers[panelIndex]).addClass(alertClass);
}
if (alertState === 'ok') {
alertClass = "panel-alert-state--" + alertState;
$(panelContainers[panelIndex]).addClass(alertClass);
$(panelContainers[panelIndex]).removeClass("panel-has-alert");
}
}
}
}
getPanelModels() {
return _.flatten(_.map(this.dashboardSrv.dash.rows, row => {
if (row.collapse) {
return [];
} else {
return row.panels;
}
}));
}
getPanelModel(panelId) {
let panelModels = this.getPanelModels();
return _.find(panelModels, panel => {
return panel.id === panelId;
});
}
setPanelThreshold(panelId, threshold) {
let panel = this.getPanelModel(panelId);
let containsThreshold = _.find(panel.thresholds, {value: threshold});
if (panel && panel.type === "graph" && !containsThreshold) {
let thresholdOptions = {
colorMode : "custom",
fill : false,
line : true,
lineColor: "rgb(255, 0, 0)",
op: "gt",
value: threshold,
source: "zabbix"
};
panel.thresholds.push(thresholdOptions);
}
}
removeZabbixThreshold(panelId) {
let panel = this.getPanelModel(panelId);
if (panel && panel.type === "graph") {
panel.thresholds = _.filter(panel.thresholds, threshold => {
return threshold.source !== "zabbix";
});
}
}
}
angular
.module('grafana.services')
.service('zabbixAlertingSrv', ZabbixAlertingService);

View File

@@ -66,11 +66,13 @@ var defaultTimeFormat = "DD MMM YYYY HH:mm:ss";
class TriggerPanelCtrl extends PanelCtrl {
/** @ngInject */
constructor($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv) {
constructor($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv, dashboardSrv) {
super($scope, $injector);
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
this.contextSrv = contextSrv;
this.dashboardSrv = dashboardSrv;
this.triggerStatusMap = triggerStatusMap;
this.defaultTimeFormat = defaultTimeFormat;
this.pageIndex = 0;
@@ -91,6 +93,9 @@ class TriggerPanelCtrl extends PanelCtrl {
}
onRefresh() {
// ignore fetching data if another panel is in fullscreen
if (this.otherPanelInFullscreenMode()) { return; }
// clear loading/error state
delete this.error;
this.loading = true;