Skip to content

Commit

Permalink
Fix querying for Zabbix v7.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanahuckova committed Dec 12, 2024
1 parent ca5c103 commit c08aea2
Show file tree
Hide file tree
Showing 11 changed files with 1,141 additions and 21 deletions.
17 changes: 17 additions & 0 deletions devenv/zabbix72/bootstrap/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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.json"
ENV ZBX_BOOTSTRAP_SCRIPT="bootstrap_config.py"

RUN pip install zabbix_utils

ADD ./bootstrap_config.py /bootstrap_config.py
ADD ${ZBX_CONFIG} /${ZBX_CONFIG}

WORKDIR /

# Run bootstrap_config.py when the container launches
CMD ["python", "/bootstrap_config.py"]
75 changes: 75 additions & 0 deletions devenv/zabbix72/bootstrap/bootstrap_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
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)

for i in range(10):
try:
zapi.login(user=zabbix_user, password=zabbix_password)
print("Connected to Zabbix API Version %s" % zapi.api_version())
break
except Exception as e:
print(e)

config_path = os.environ['ZBX_CONFIG']
import_rules = {
'discoveryRules': {
'createMissing': True,
'updateExisting': True
},
'graphs': {
'createMissing': True,
'updateExisting': True
},
'host_groups': {
'createMissing': True
},
'hosts': {
'createMissing': True,
'updateExisting': True
},
'images': {
'createMissing': True,
'updateExisting': True
},
'items': {
'createMissing': True,
'updateExisting': True
},
'maps': {
'createMissing': True,
'updateExisting': True
},
'templateLinkage': {
'createMissing': True,
},
'templates': {
'createMissing': True,
'updateExisting': True
},
'triggers': {
'createMissing': True,
'updateExisting': True
},
}

print("Importing Zabbix config from %s" % config_path)
with open(config_path, 'r') as f:
config = f.read()

try:
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'])
Loading

0 comments on commit c08aea2

Please sign in to comment.