Skip to content

Commit

Permalink
fix annotations, closes #964
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed May 26, 2020
1 parent fb56cf0 commit 938b3cd
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 56 deletions.
91 changes: 41 additions & 50 deletions src/datasource-zabbix/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,71 +576,62 @@ export class ZabbixDatasource {
const timeFrom = Math.ceil(dateMath.parse(timeRange.from) / 1000);
const timeTo = Math.ceil(dateMath.parse(timeRange.to) / 1000);
const annotation = options.annotation;
const showOkEvents = annotation.showOkEvents ? c.SHOW_ALL_EVENTS : c.SHOW_OK_EVENTS;

// Show all triggers
const triggersOptions = {
showTriggers: c.SHOW_ALL_TRIGGERS,
hideHostsInMaintenance: false
const problemsOptions: any = {
value: annotation.showOkEvents ? ['0', '1'] : '1',
valueFromEvent: true,
timeFrom,
timeTo,
};

if (annotation.minseverity) {
const severities = [0, 1, 2, 3, 4, 5].filter(v => v >= Number(annotation.minseverity));
problemsOptions.severities = severities;
}

const groupFilter = this.replaceTemplateVars(annotation.group, {});
const hostFilter = this.replaceTemplateVars(annotation.host, {});
const appFilter = this.replaceTemplateVars(annotation.application, {});
const proxyFilter = undefined;

return this.zabbix.getProblems(groupFilter, hostFilter, appFilter, proxyFilter, triggersOptions)
.then(triggers => {
return this.zabbix.getProblemsHistory(groupFilter, hostFilter, appFilter, proxyFilter, problemsOptions)
.then(problems => {
// Filter triggers by description
const triggerName = this.replaceTemplateVars(annotation.trigger, {});
if (utils.isRegex(triggerName)) {
triggers = _.filter(triggers, trigger => {
return utils.buildRegex(triggerName).test(trigger.description);
const problemName = this.replaceTemplateVars(annotation.trigger, {});
if (utils.isRegex(problemName)) {
problems = _.filter(problems, p => {
return utils.buildRegex(problemName).test(p.description);
});
} else if (triggerName) {
triggers = _.filter(triggers, trigger => {
return trigger.description === triggerName;
} else if (problemName) {
problems = _.filter(problems, p => {
return p.description === problemName;
});
}

// Remove events below the chose severity
triggers = _.filter(triggers, trigger => {
return Number(trigger.priority) >= Number(annotation.minseverity);
});

const objectids = _.map(triggers, 'triggerid');
return this.zabbix
.getEvents(objectids, timeFrom, timeTo, showOkEvents)
.then(events => {
const indexedTriggers = _.keyBy(triggers, 'triggerid');

// Hide acknowledged events if option enabled
if (annotation.hideAcknowledged) {
events = _.filter(events, event => {
return !event.acknowledges.length;
});
}

return _.map(events, event => {
let tags;
if (annotation.showHostname) {
tags = _.map(event.hosts, 'name');
}

// Show event type (OK or Problem)
const title = Number(event.value) ? 'Problem' : 'OK';

const formattedAcknowledges = utils.formatAcknowledges(event.acknowledges);
const eventName = event.name || indexedTriggers[event.objectid].description;
return {
annotation: annotation,
time: event.clock * 1000,
title: title,
tags: tags,
text: eventName + formattedAcknowledges
};
});
// Hide acknowledged events if option enabled
if (annotation.hideAcknowledged) {
problems = _.filter(problems, p => {
return !p.acknowledges?.length;
});
}

return _.map(problems, p => {
const formattedAcknowledges = utils.formatAcknowledges(p.acknowledges);

let annotationTags: string[] = [];
if (annotation.showHostname) {
annotationTags = _.map(p.hosts, 'name');
}

return {
title: p.value === '1' ? 'Problem' : 'OK',
time: p.timestamp * 1000,
annotation: annotation,
text: p.name + formattedAcknowledges,
tags: annotationTags,
};
});
});
}

Expand Down
52 changes: 51 additions & 1 deletion src/datasource-zabbix/problemsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';
import * as utils from '../datasource-zabbix/utils';
import { DataFrame, Field, FieldType, ArrayVector } from '@grafana/data';
import { ZBXProblem, ZBXTrigger, ProblemDTO } from './types';
import { ZBXProblem, ZBXTrigger, ProblemDTO, ZBXEvent } from './types';

export function joinTriggersWithProblems(problems: ZBXProblem[], triggers: ZBXTrigger[]): ProblemDTO[] {
const problemDTOList: ProblemDTO[] = [];
Expand Down Expand Up @@ -46,6 +46,54 @@ export function joinTriggersWithProblems(problems: ZBXProblem[], triggers: ZBXTr
return problemDTOList;
}

interface JoinOptions {
valueFromEvent?: boolean;
}

export function joinTriggersWithEvents(events: ZBXEvent[], triggers: ZBXTrigger[], options?: JoinOptions): ProblemDTO[] {
const { valueFromEvent } = options;

const problemDTOList: ProblemDTO[] = [];
for (let i = 0; i < events.length; i++) {
const e = events[i];
const triggerId = Number(e.objectid);
const t = triggers[triggerId];

if (t) {
const problemDTO: ProblemDTO = {
value: valueFromEvent ? e.value : t.value,
timestamp: Number(e.clock),
triggerid: e.objectid,
eventid: e.eventid,
name: e.name,
severity: e.severity,
acknowledged: e.acknowledged,
acknowledges: e.acknowledges,
tags: e.tags,
suppressed: e.suppressed,
description: t.description,
comments: t.comments,
groups: t.groups,
hosts: t.hosts,
items: t.items,
alerts: t.alerts,
url: t.url,
expression: t.expression,
correlation_mode: t.correlation_mode,
correlation_tag: t.correlation_tag,
manual_close: t.manual_close,
state: t.state,
error: t.error,
};

problemDTOList.push(problemDTO);
}

}

return problemDTOList;
}

export function setMaintenanceStatus(triggers) {
_.each(triggers, (trigger) => {
const maintenance_status = _.some(trigger.hosts, (host) => host.maintenance_status === '1');
Expand Down Expand Up @@ -145,6 +193,8 @@ const problemsHandler = {
setAckButtonStatus,
filterTriggersPre,
toDataFrame,
joinTriggersWithProblems,
joinTriggersWithEvents,
};

export default problemsHandler;
5 changes: 4 additions & 1 deletion src/datasource-zabbix/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,16 @@ export interface ZBXEvent {
clock: string;
ns?: string;
value?: string;
name?: string;
source?: string;
object?: string;
objectid?: string;
acknowledged?: string;
severity?: string;
hosts?: ZBXHost[];
acknowledged?: '1' | '0';
acknowledges?: ZBXAcknowledge[];
tags?: ZBXTag[];
suppressed?: string;
}

export interface ZBXTag {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export class ZabbixAPIConnector {
}

getEventsHistory(groupids, hostids, applicationids, options) {
const { timeFrom, timeTo, severities, limit } = options;
const { timeFrom, timeTo, severities, limit, value } = options;

const params: any = {
output: 'extend',
Expand Down Expand Up @@ -539,6 +539,10 @@ export class ZabbixAPIConnector {
params.severities = severities;
}

if (value) {
params.value = value;
}

return this.request('event.get', params);
}

Expand Down
9 changes: 6 additions & 3 deletions src/datasource-zabbix/zabbix/zabbix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { ZabbixAPIConnector } from './connectors/zabbix_api/zabbixAPIConnector';
import { SQLConnector } from './connectors/sql/sqlConnector';
import { InfluxDBConnector } from './connectors/influxdb/influxdbConnector';
import { ZabbixConnector } from './types';
import { joinTriggersWithProblems } from '../problemsHandler';
import { joinTriggersWithProblems, joinTriggersWithEvents } from '../problemsHandler';
import { ProblemDTO } from '../types';

interface AppsResponse extends Array<any> {
appFilterEmpty?: boolean;
Expand Down Expand Up @@ -346,7 +347,9 @@ export class Zabbix implements ZabbixConnector {
.then(triggers => this.expandUserMacro.bind(this)(triggers, true));
}

getProblemsHistory(groupFilter, hostFilter, appFilter, proxyFilter?, options?) {
getProblemsHistory(groupFilter, hostFilter, appFilter, proxyFilter?, options?): Promise<ProblemDTO[]> {
const { valueFromEvent } = options;

const promises = [
this.getGroups(groupFilter),
this.getHosts(groupFilter, hostFilter),
Expand Down Expand Up @@ -375,7 +378,7 @@ export class Zabbix implements ZabbixConnector {
const triggerids = problems?.map(problem => problem.objectid);
return Promise.all([Promise.resolve(problems), this.zabbixAPI.getTriggersByIds(triggerids)]);
})
.then(([problems, triggers]) => joinTriggersWithProblems(problems, triggers))
.then(([problems, triggers]) => joinTriggersWithEvents(problems, triggers, { valueFromEvent }))
.then(triggers => this.filterTriggersByProxy(triggers, proxyFilter))
.then(triggers => this.expandUserMacro.bind(this)(triggers, true));
}
Expand Down

0 comments on commit 938b3cd

Please sign in to comment.