Remove old alerting feature

This commit is contained in:
Alexander Zobnin
2020-06-18 12:05:06 +03:00
parent 4c40e18c5b
commit dd38425c76
3 changed files with 1 additions and 191 deletions

View File

@@ -24,9 +24,6 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
trendsFrom: string; trendsFrom: string;
trendsRange: string; trendsRange: string;
cacheTTL: any; cacheTTL: any;
alertingEnabled: boolean;
addThresholds: boolean;
alertingMinSeverity: string;
disableReadOnlyUsersAck: boolean; disableReadOnlyUsersAck: boolean;
enableDirectDBConnection: boolean; enableDirectDBConnection: boolean;
dbConnectionDatasourceId: number; dbConnectionDatasourceId: number;
@@ -39,12 +36,10 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
replaceTemplateVars: (target: any, scopedVars?: any) => any; replaceTemplateVars: (target: any, scopedVars?: any) => any;
/** @ngInject */ /** @ngInject */
constructor(instanceSettings: DataSourceInstanceSettings<ZabbixDSOptions>, private templateSrv, private zabbixAlertingSrv) { constructor(instanceSettings: DataSourceInstanceSettings<ZabbixDSOptions>, private templateSrv) {
super(instanceSettings); super(instanceSettings);
this.templateSrv = templateSrv; this.templateSrv = templateSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
this.enableDebugLog = config.buildInfo.env === 'development'; this.enableDebugLog = config.buildInfo.env === 'development';
// Use custom format for template variables // Use custom format for template variables
@@ -67,11 +62,6 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
const ttl = jsonData.cacheTTL || '1h'; const ttl = jsonData.cacheTTL || '1h';
this.cacheTTL = utils.parseInterval(ttl); this.cacheTTL = utils.parseInterval(ttl);
// Alerting options
this.alertingEnabled = jsonData.alerting;
this.addThresholds = jsonData.addThresholds;
this.alertingMinSeverity = jsonData.alertingMinSeverity || c.SEV_WARNING;
// Other options // Other options
this.disableReadOnlyUsersAck = jsonData.disableReadOnlyUsersAck; this.disableReadOnlyUsersAck = jsonData.disableReadOnlyUsersAck;
@@ -105,23 +95,6 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
* @return {Object} Grafana metrics object with timeseries data for each target. * @return {Object} Grafana metrics object with timeseries data for each target.
*/ */
query(options) { query(options) {
// console.log('invoking doTsdbRequest()');
// this.doTsdbRequest(options);
// 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 // Create request for each target
const promises = _.map(options.targets, t => { const promises = _.map(options.targets, t => {
// Don't request for hidden targets // Don't request for hidden targets
@@ -671,58 +644,6 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
}); });
} }
/**
* 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) {
const enabled_targets = filterEnabledTargets(options.targets);
const getPanelItems = _.map(enabled_targets, t => {
let target = _.cloneDeep(t);
target = migrations.migrate(target);
this.replaceTargetVariables(target, options);
return this.zabbix.getItemsFromTarget(target, {itemtype: 'num'});
});
return Promise.all(getPanelItems)
.then(results => {
const items = _.flatten(results);
const itemids = _.map(items, 'itemid');
if (itemids.length === 0) {
return [];
}
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';
const firedTriggers = _.filter(triggers, {value: '1'});
if (firedTriggers.length) {
state = 'alerting';
}
const thresholds = _.map(triggers, trigger => {
return getTriggerThreshold(trigger.expression);
});
return {
panelId: options.panelId,
state: state,
thresholds: thresholds
};
});
}
// Replace template variables // Replace template variables
replaceTargetVariables(target, options) { replaceTargetVariables(target, options) {
const parts = ['group', 'host', 'application', 'item']; const parts = ['group', 'host', 'application', 'item'];
@@ -845,15 +766,3 @@ function filterEnabledTargets(targets) {
return !(target.hide || !target.group || !target.host || !target.item); return !(target.hide || !target.group || !target.host || !target.item);
}); });
} }
function getTriggerThreshold(expression) {
const thresholdPattern = /.*[<>=]{1,2}([\d\.]+)/;
const 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;
}
}

View File

@@ -4,7 +4,6 @@ import { ZabbixDatasource } from './datasource';
import { ZabbixQueryController } from './query.controller'; import { ZabbixQueryController } from './query.controller';
import { ZabbixVariableQueryEditor } from './components/VariableQueryEditor'; import { ZabbixVariableQueryEditor } from './components/VariableQueryEditor';
import { ConfigEditor } from './components/ConfigEditor'; import { ConfigEditor } from './components/ConfigEditor';
import './zabbixAlerting.service.js';
import './add-metric-function.directive'; import './add-metric-function.directive';
import './metric-function-editor.directive'; import './metric-function-editor.directive';

View File

@@ -1,98 +0,0 @@
import _ from 'lodash';
import $ from 'jquery';
import angular from 'angular';
export class ZabbixAlertingService {
/**
* @ngInject
*/
constructor(dashboardSrv) {
this.dashboardSrv = dashboardSrv;
}
isFullScreen() {
return this.getDashboardModel().meta.fullscreen;
}
setPanelAlertState(panelId, alertState) {
if (!alertState) {
return;
}
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) {
alertClass = "panel-has-alert panel-alert-state--" + alertState;
$(panelContainers[panelIndex]).addClass(alertClass);
}
}
}
getDashboardModel() {
return this.dashboardSrv.dash || this.dashboardSrv.dashboard;
}
getPanelModels() {
return _.filter(this.getDashboardModel().panels, panel => panel.type !== 'row');
}
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);