From aecdb4bbee1d141e01f2c83c4bca6b181b3ec546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Mon, 10 Jun 2024 14:53:15 +0200 Subject: [PATCH] Add support for v7 (#1835) * Add support for v7 * Bump version --- CHANGELOG.md | 4 + devenv/zabbix70/bootstrap/Dockerfile | 6 +- devenv/zabbix70/bootstrap/bootstrap_config.py | 29 +- .../zabbix70/bootstrap/zbx_export_hosts.json | 427 ++++++++++++++++++ package.json | 2 +- src/datasource/problemsHandler.ts | 6 +- .../zabbix_api/zabbixAPIConnector.ts | 29 +- src/datasource/zabbix/zabbix.ts | 4 +- 8 files changed, 478 insertions(+), 29 deletions(-) create mode 100644 devenv/zabbix70/bootstrap/zbx_export_hosts.json diff --git a/CHANGELOG.md b/CHANGELOG.md index e754399f1..ff80c7744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## [4.5.0] - 2024-06-10 + +- Add support for Zabbix version 7 + ## [4.4.9] - 2024-04-30 - Fix: Improve compatibility with the scenes framework (#1822) diff --git a/devenv/zabbix70/bootstrap/Dockerfile b/devenv/zabbix70/bootstrap/Dockerfile index da6457fd1..ac31af8e7 100644 --- a/devenv/zabbix70/bootstrap/Dockerfile +++ b/devenv/zabbix70/bootstrap/Dockerfile @@ -1,12 +1,12 @@ -FROM python:2.7 +FROM python:3.12-slim-bullseye ENV ZBX_API_URL=http://zabbix-web:8080 ENV ZBX_API_USER="Admin" ENV ZBX_API_PASSWORD="zabbix" -ENV ZBX_CONFIG="zbx_export_hosts.xml" +ENV ZBX_CONFIG="zbx_export_hosts.json" ENV ZBX_BOOTSTRAP_SCRIPT="bootstrap_config.py" -RUN pip install pyzabbix +RUN pip install zabbix_utils ADD ./bootstrap_config.py /bootstrap_config.py ADD ${ZBX_CONFIG} /${ZBX_CONFIG} diff --git a/devenv/zabbix70/bootstrap/bootstrap_config.py b/devenv/zabbix70/bootstrap/bootstrap_config.py index 650a3f0d3..c75721f50 100644 --- a/devenv/zabbix70/bootstrap/bootstrap_config.py +++ b/devenv/zabbix70/bootstrap/bootstrap_config.py @@ -1,27 +1,20 @@ import os -from time import sleep -from pyzabbix import ZabbixAPI, ZabbixAPIException +from zabbix_utils import ZabbixAPI zabbix_url = os.environ['ZBX_API_URL'] zabbix_user = os.environ['ZBX_API_USER'] zabbix_password = os.environ['ZBX_API_PASSWORD'] print(zabbix_url, zabbix_user, zabbix_password) -zapi = ZabbixAPI(zabbix_url, timeout=5) +zapi = ZabbixAPI(zabbix_url) for i in range(10): - print("Trying to connected to Zabbix API %s" % zabbix_url) try: - zapi.login(zabbix_user, zabbix_password) + zapi.login(user=zabbix_user, password=zabbix_password) print("Connected to Zabbix API Version %s" % zapi.api_version()) break - except ZabbixAPIException as e: - print e - sleep(5) - except: - print("Waiting") - sleep(5) - + except Exception as e: + print(e) config_path = os.environ['ZBX_CONFIG'] import_rules = { @@ -70,11 +63,13 @@ config = f.read() try: - # https://github.com/lukecyca/pyzabbix/issues/62 - import_result = zapi.confimport("xml", config, import_rules) - print(import_result) - except ZabbixAPIException as e: - print e + import_result = zapi.configuration.import_(source=config, format="json", rules=import_rules) + if import_result == True: + print("Zabbix config imported successfully") + else: + print("Failed to import Zabbix config") + except Exception as e: + print(e) for h in zapi.host.get(output="extend"): print(h['name']) diff --git a/devenv/zabbix70/bootstrap/zbx_export_hosts.json b/devenv/zabbix70/bootstrap/zbx_export_hosts.json new file mode 100644 index 000000000..164faf019 --- /dev/null +++ b/devenv/zabbix70/bootstrap/zbx_export_hosts.json @@ -0,0 +1,427 @@ +{ + "zabbix_export": { + "version": "7.0", + "host_groups": [ + { + "uuid": "2e427c268ac1468b9add94b65e2d6c14", + "name": "Backend" + }, + { + "uuid": "d97ba66b283544339628b71975a6e68d", + "name": "Frontend" + }, + { + "uuid": "dc579cd7a1a34222933f24f52a68bcd8", + "name": "Linux servers" + }, + { + "uuid": "6f6799aa69e844b4b3918f779f2abf08", + "name": "Zabbix servers" + } + ], + "hosts": [ + { + "host": "backend01", + "name": "backend01", + "templates": [ + { + "name": "Template ZAS Agent" + } + ], + "groups": [ + { + "name": "Backend" + }, + { + "name": "Linux servers" + } + ], + "interfaces": [ + { + "useip": "NO", + "dns": "zas_backend_01", + "interface_ref": "if1" + } + ], + "tags": [ + { + "tag": "backend" + }, + { + "tag": "service", + "value": "backend" + } + ], + "inventory_mode": "DISABLED" + }, + { + "host": "backend02", + "name": "backend02", + "templates": [ + { + "name": "Template ZAS Agent" + } + ], + "groups": [ + { + "name": "Backend" + }, + { + "name": "Linux servers" + } + ], + "interfaces": [ + { + "useip": "NO", + "dns": "zas_backend_02", + "interface_ref": "if1" + } + ], + "tags": [ + { + "tag": "backend" + }, + { + "tag": "service", + "value": "backend" + } + ], + "inventory_mode": "DISABLED" + }, + { + "host": "frontend01", + "name": "frontend01", + "templates": [ + { + "name": "Template ZAS Agent" + } + ], + "groups": [ + { + "name": "Frontend" + }, + { + "name": "Linux servers" + } + ], + "interfaces": [ + { + "useip": "NO", + "dns": "zas_frontend_01", + "interface_ref": "if1" + } + ], + "tags": [ + { + "tag": "frontend" + }, + { + "tag": "service", + "value": "frontend" + } + ], + "inventory_mode": "DISABLED" + }, + { + "host": "frontend02", + "name": "frontend02", + "templates": [ + { + "name": "Template ZAS Agent" + } + ], + "groups": [ + { + "name": "Frontend" + }, + { + "name": "Linux servers" + } + ], + "interfaces": [ + { + "useip": "NO", + "dns": "zas_frontend_02", + "interface_ref": "if1" + } + ], + "tags": [ + { + "tag": "frontend" + }, + { + "tag": "service", + "value": "frontend" + } + ], + "inventory_mode": "DISABLED" + }, + { + "host": "Zabbix server", + "name": "Zabbix server", + "templates": [ + { + "name": "Linux by Zabbix agent" + }, + { + "name": "Zabbix server health" + } + ], + "groups": [ + { + "name": "Zabbix servers" + } + ], + "interfaces": [ + { + "useip": "NO", + "dns": "zabbix-agent", + "interface_ref": "if1" + } + ], + "inventory_mode": "DISABLED" + } + ], + "template_groups": [ + { + "uuid": "7df96b18c230490a9a0a9e2307226338", + "name": "Templates" + } + ], + "templates": [ + { + "uuid": "2d7a65bb369c48199361913b223b1695", + "template": "Template ZAS Agent", + "name": "Template ZAS Agent", + "templates": [ + { + "name": "Zabbix agent" + } + ], + "groups": [ + { + "name": "Templates" + } + ], + "items": [ + { + "uuid": "e79d4215ec014b74923b905bb8f82410", + "name": "Incoming network traffic on eth0", + "key": "net.if.in[eth0]", + "history": "1d", + "units": "bps", + "tags": [ + { + "tag": "Application", + "value": "Network interfaces" + } + ] + }, + { + "uuid": "18b377dae9fe48d093c16ee7a7c93320", + "name": "Outgoing network traffic on eth0", + "key": "net.if.out[eth0]", + "history": "1d", + "units": "bps", + "tags": [ + { + "tag": "Application", + "value": "Network interfaces" + } + ] + }, + { + "uuid": "2479e055e91c476f9be93b5f363cfa2f", + "name": "Processor load (1 min average per core)", + "key": "system.cpu.load[percpu,avg1]", + "history": "1d", + "value_type": "FLOAT", + "description": "The processor load is calculated as system CPU load divided by number of CPU cores.", + "tags": [ + { + "tag": "Application", + "value": "CPU" + }, + { + "tag": "Application", + "value": "Performance" + } + ], + "triggers": [ + { + "uuid": "60e5484b60ca4061b21bd23f8364bd6e", + "expression": "last(/Template ZAS Agent/system.cpu.load[percpu,avg1])>2", + "name": "Processor load is too high on {HOST.NAME}", + "priority": "WARNING", + "tags": [ + { + "tag": "app", + "value": "performance" + }, + { + "tag": "type", + "value": "cpu" + } + ] + } + ] + }, + { + "uuid": "f0dd4221793c49889cf2789806afa597", + "name": "Processor load (15 min average per core)", + "key": "system.cpu.load[percpu,avg15]", + "history": "1d", + "value_type": "FLOAT", + "description": "The processor load is calculated as system CPU load divided by number of CPU cores.", + "tags": [ + { + "tag": "Application", + "value": "CPU" + }, + { + "tag": "Application", + "value": "Performance" + } + ] + }, + { + "uuid": "9a8b4a1f173b4f209723d820dc2e054a", + "name": "CPU $2 time", + "key": "system.cpu.util[,iowait]", + "history": "1d", + "value_type": "FLOAT", + "units": "%", + "description": "Amount of time the CPU has been waiting for I/O to complete.", + "tags": [ + { + "tag": "Application", + "value": "CPU" + }, + { + "tag": "Application", + "value": "Performance" + } + ], + "triggers": [ + { + "uuid": "ceb468b9eb434fa6bd4c8a5d7507fd87", + "expression": "avg(/Template ZAS Agent/system.cpu.util[,iowait],5m)>20", + "name": "Disk I/O is overloaded on {HOST.NAME}", + "priority": "WARNING", + "description": "OS spends significant time waiting for I/O (input/output) operations. It could be indicator of performance issues with storage system.", + "tags": [ + { + "tag": "disk" + }, + { + "tag": "type", + "value": "disk" + } + ] + } + ] + }, + { + "uuid": "e6d19d47cf60452ead1e791da2d5c0dc", + "name": "CPU $2 time", + "key": "system.cpu.util[,system]", + "history": "1d", + "value_type": "FLOAT", + "units": "%", + "description": "The time the CPU has spent running the kernel and its processes.", + "tags": [ + { + "tag": "Application", + "value": "CPU" + }, + { + "tag": "Application", + "value": "Performance" + } + ] + }, + { + "uuid": "2d81fbc139774306959712a627c99b9a", + "name": "CPU $2 time", + "key": "system.cpu.util[,user]", + "history": "1d", + "value_type": "FLOAT", + "units": "%", + "description": "The time the CPU has spent running users' processes that are not niced.", + "tags": [ + { + "tag": "Application", + "value": "CPU" + }, + { + "tag": "Application", + "value": "Performance" + } + ] + } + ] + } + ], + "graphs": [ + { + "uuid": "7aac0ec0c0e04b7a8bb6472d1faa7a09", + "name": "CPU load", + "ymin_type_1": "FIXED", + "graph_items": [ + { + "color": "009900", + "item": { + "host": "Template ZAS Agent", + "key": "system.cpu.load[percpu,avg1]" + } + }, + { + "sortorder": "2", + "color": "990000", + "item": { + "host": "Template ZAS Agent", + "key": "system.cpu.load[percpu,avg15]" + } + } + ] + }, + { + "uuid": "f25064d88b964a678fac7ea6095b238a", + "name": "CPU utilization", + "show_triggers": "NO", + "type": "STACKED", + "ymin_type_1": "FIXED", + "ymax_type_1": "FIXED", + "graph_items": [ + { + "sortorder": "4", + "drawtype": "FILLED_REGION", + "color": "999900", + "item": { + "host": "Template ZAS Agent", + "key": "system.cpu.util[,iowait]" + } + }, + { + "sortorder": "5", + "drawtype": "FILLED_REGION", + "color": "990000", + "item": { + "host": "Template ZAS Agent", + "key": "system.cpu.util[,system]" + } + }, + { + "sortorder": "6", + "drawtype": "FILLED_REGION", + "color": "000099", + "item": { + "host": "Template ZAS Agent", + "key": "system.cpu.util[,user]" + } + } + ] + } + ] + } +} diff --git a/package.json b/package.json index 7c06958aa..307e082c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "grafana-zabbix", - "version": "4.4.9", + "version": "4.5.0", "description": "Zabbix plugin for Grafana", "homepage": "http://grafana-zabbix.org", "bugs": { diff --git a/src/datasource/problemsHandler.ts b/src/datasource/problemsHandler.ts index 2ce2c6d0a..1d043257b 100644 --- a/src/datasource/problemsHandler.ts +++ b/src/datasource/problemsHandler.ts @@ -128,8 +128,10 @@ export function addTriggerHostProxy(triggers, proxies) { triggers.forEach((trigger) => { if (trigger.hosts && trigger.hosts.length) { const host = trigger.hosts[0]; - if (host.proxy_hostid !== '0') { - const hostProxy = proxies[host.proxy_hostid]; + // Before version 7.0.0 proxy_hostid was used, after - proxyid + const proxyId = host.proxyid || host.proxy_hostid; + if (proxyId !== '0') { + const hostProxy = proxies[proxyId]; host.proxy = hostProxy ? hostProxy.host : ''; } } diff --git a/src/datasource/zabbix/connectors/zabbix_api/zabbixAPIConnector.ts b/src/datasource/zabbix/connectors/zabbix_api/zabbixAPIConnector.ts index f9ad4e698..89ef22000 100644 --- a/src/datasource/zabbix/connectors/zabbix_api/zabbixAPIConnector.ts +++ b/src/datasource/zabbix/connectors/zabbix_api/zabbixAPIConnector.ts @@ -530,7 +530,7 @@ export class ZabbixAPIConnector { } getTriggersByIds(triggerids: string[]) { - const params: any = { + const params = { output: 'extend', triggerids: triggerids, expandDescription: true, @@ -540,13 +540,20 @@ export class ZabbixAPIConnector { monitored: true, skipDependent: true, selectGroups: ['name', 'groupid'], - selectHosts: ['hostid', 'name', 'host', 'maintenance_status', 'proxy_hostid', 'description'], + selectHosts: ['hostid', 'name', 'host', 'maintenance_status', 'description'], selectItems: ['itemid', 'name', 'key_', 'lastvalue'], // selectLastEvent: 'extend', // selectTags: 'extend', preservekeys: '1', }; + // Before version 7.0.0 proxy_hostid was used, after - proxyid + if (semver.lte(this.version, '7.0.0')) { + params.selectHosts.push('proxy_hostid'); + } else { + params.selectHosts.push('proxyid'); + } + return this.request('trigger.get', params).then(utils.mustArray); } @@ -568,12 +575,19 @@ export class ZabbixAPIConnector { value: 1, }, selectGroups: ['groupid', 'name'], - selectHosts: ['hostid', 'name', 'host', 'maintenance_status', 'proxy_hostid'], + selectHosts: ['hostid', 'name', 'host', 'maintenance_status'], selectItems: ['itemid', 'name', 'key_', 'lastvalue'], selectLastEvent: 'extend', selectTags: 'extend', }; + // Before version 7.0.0 proxy_hostid was used, after - proxyid + if (semver.lte(this.version, '7.0.0')) { + params.selectHosts.push('proxy_hostid'); + } else { + params.selectHosts.push('proxyid'); + } + if (showTriggers === ShowProblemTypes.Problems) { params.filter.value = 1; } else if (showTriggers === ShowProblemTypes.Recent || showTriggers === ShowProblemTypes.History) { @@ -674,7 +688,7 @@ export class ZabbixAPIConnector { const params = { eventids: eventids, output: ['alertid', 'eventid', 'message', 'clock', 'error'], - selectUsers: true, + selectUsers: 'extend', }; return this.request('alert.get', params); @@ -861,8 +875,13 @@ export class ZabbixAPIConnector { getProxies() { const params = { - output: ['proxyid', 'host'], + output: ['proxyid'], }; + if (semver.lte(this.version, '7.0.0')) { + params.output.push('name'); + } else { + params.output.push('host'); + } return this.request('proxy.get', params); } diff --git a/src/datasource/zabbix/zabbix.ts b/src/datasource/zabbix/zabbix.ts index 9df30e7b3..805256bbf 100644 --- a/src/datasource/zabbix/zabbix.ts +++ b/src/datasource/zabbix/zabbix.ts @@ -599,7 +599,9 @@ export class Zabbix implements ZabbixConnector { triggers = triggers.filter((trigger) => { for (let i = 0; i < trigger.hosts.length; i++) { const host = trigger.hosts[i]; - if (proxy_ids.includes(host.proxy_hostid)) { + // Before version 7.0.0 proxy_hostid was used, after - proxyid + const proxyId = host.proxyid || host.proxy_hostid; + if (proxy_ids.includes(proxyId)) { return true; } }