Add tags filter to triggers query

This commit is contained in:
Alexander Zobnin
2023-08-01 17:41:25 +02:00
parent 4534679e65
commit 4373ef8667
2 changed files with 19 additions and 1 deletions

View File

@@ -542,6 +542,19 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
const hostids = hosts?.map((h) => h.hostid);
const appids = apps?.map((a) => a.applicationid);
const options = getTriggersOptions(target, timeRange);
const tagsFilter = this.replaceTemplateVars(target.tags?.filter, request.scopedVars);
// replaceTemplateVars() builds regex-like string, so we should trim it.
const tagsFilterStr = tagsFilter.replace('/^', '').replace('$/', '');
const tags = utils.parseTags(tagsFilterStr);
tags.forEach((tag) => {
// Zabbix uses {"tag": "<tag>", "value": "<value>", "operator": "<operator>"} format, where 1 means Equal
tag.operator = 1;
});
if (tags && tags.length) {
options.tags = tags;
}
const alerts = await this.zabbix.getHostAlerts(hostids, appids, options);
return responseHandler.handleTriggersResponse(alerts, groups, timeRange, target);
}

View File

@@ -715,7 +715,7 @@ export class ZabbixAPIConnector {
}
async getHostAlerts(hostids, applicationids, options): Promise<ZBXTrigger[]> {
const { minSeverity, acknowledged, count, timeFrom, timeTo } = options;
const { minSeverity, acknowledged, tags, count, timeFrom, timeTo } = options;
const params: any = {
output: 'extend',
hostids: hostids,
@@ -747,6 +747,11 @@ export class ZabbixAPIConnector {
params.lastChangeTill = timeTo;
}
if (tags) {
params.tags = tags;
params.evaltype = 0;
}
let triggers = await this.request('trigger.get', params);
if (!count || acknowledged === 1) {
triggers = filterTriggersByAcknowledge(triggers, acknowledged);