From 103938511ad12888868955405583fb9a79846d42 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 1 Aug 2023 12:28:21 +0200 Subject: [PATCH] Devenv: Add zabbix 5.2 --- devenv/zabbix52/bootstrap/Dockerfile | 18 + devenv/zabbix52/bootstrap/bootstrap_config.py | 93 ++++ devenv/zabbix52/bootstrap/host_template.xml | 63 +++ .../zabbix52/bootstrap/zbx_export_hosts_5.xml | 407 ++++++++++++++++++ devenv/zabbix52/docker-compose.yml | 126 ++++++ 5 files changed, 707 insertions(+) create mode 100644 devenv/zabbix52/bootstrap/Dockerfile create mode 100644 devenv/zabbix52/bootstrap/bootstrap_config.py create mode 100644 devenv/zabbix52/bootstrap/host_template.xml create mode 100644 devenv/zabbix52/bootstrap/zbx_export_hosts_5.xml create mode 100644 devenv/zabbix52/docker-compose.yml diff --git a/devenv/zabbix52/bootstrap/Dockerfile b/devenv/zabbix52/bootstrap/Dockerfile new file mode 100644 index 0000000..21b536c --- /dev/null +++ b/devenv/zabbix52/bootstrap/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3 + +ENV ZBX_API_URL=http://zabbix-web +ENV ZBX_API_USER="Admin" +ENV ZBX_API_PASSWORD="zabbix" +ENV ZBX_CONFIG="zbx_export_hosts_5.xml" +ENV ZBX_BOOTSTRAP_SCRIPT="bootstrap_config.py" + +RUN pip install pyzabbix + +WORKDIR / + +ADD ./${ZBX_CONFIG} /${ZBX_CONFIG} +ADD ./bootstrap_config.py /bootstrap_config.py +# RUN cp ./${ZBX_CONFIG} /${ZBX_CONFIG} + +# Run bootstrap_config.py when the container launches +CMD ["python", "/bootstrap_config.py"] diff --git a/devenv/zabbix52/bootstrap/bootstrap_config.py b/devenv/zabbix52/bootstrap/bootstrap_config.py new file mode 100644 index 0000000..b035adb --- /dev/null +++ b/devenv/zabbix52/bootstrap/bootstrap_config.py @@ -0,0 +1,93 @@ +import os +from time import sleep +from pyzabbix import ZabbixAPI, ZabbixAPIException + +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) + +for i in range(10): + print("Trying to connected to Zabbix API %s" % zabbix_url) + try: + zapi.login(zabbix_user, zabbix_password) + print("Connected to Zabbix API Version %s" % zapi.api_version()) + break + except ZabbixAPIException as e: + print(e) + sleep(10) + except: + print("Waiting") + sleep(10) + + +zapi.timeout = 600 + +config_path = os.environ['ZBX_CONFIG'] +import_rules = { + 'applications': { + 'createMissing': True, + }, + 'discoveryRules': { + 'createMissing': True, + 'updateExisting': True + }, + 'graphs': { + 'createMissing': True, + 'updateExisting': True + }, + 'groups': { + 'createMissing': True + }, + 'hosts': { + 'createMissing': True, + 'updateExisting': True + }, + 'images': { + 'createMissing': True, + 'updateExisting': True + }, + 'items': { + 'createMissing': True, + 'updateExisting': True + }, + 'maps': { + 'createMissing': True, + 'updateExisting': True + }, +# 'screens': { +# 'createMissing': True, +# 'updateExisting': True +# }, + 'templateLinkage': { + 'createMissing': True, + }, + 'templates': { + 'createMissing': True, + 'updateExisting': True + }, +# 'templateScreens': { +# '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: + # https://github.com/lukecyca/pyzabbix/issues/62 + import_result = zapi.confimport("xml", config, import_rules) + print(import_result) + except ZabbixAPIException as e: + print(e) + +for h in zapi.host.get(output="extend"): + print(h['name']) diff --git a/devenv/zabbix52/bootstrap/host_template.xml b/devenv/zabbix52/bootstrap/host_template.xml new file mode 100644 index 0000000..ef96e70 --- /dev/null +++ b/devenv/zabbix52/bootstrap/host_template.xml @@ -0,0 +1,63 @@ + + backend01 + backend01 + + + 0 + -1 + 2 + + + 1 + 1 + + + + + + + + + + Backend + + + Linux servers + + + + + 1 + 1 + 0 + 127.0.0.1 + zas_backend_01 + 10050 + 1 + if1 + + + + + + + + -1 + + + + env + staging + + + group + backend + + + region + EU + + + diff --git a/devenv/zabbix52/bootstrap/zbx_export_hosts_5.xml b/devenv/zabbix52/bootstrap/zbx_export_hosts_5.xml new file mode 100644 index 0000000..9ea8804 --- /dev/null +++ b/devenv/zabbix52/bootstrap/zbx_export_hosts_5.xml @@ -0,0 +1,407 @@ + + + 5.0 + 2021-09-10T12:42:13Z + + + Backend + + + Database + + + Frontend + + + Linux servers + + + Zabbix servers + + + + + backend01 + backend01 + + + + + + Backend + + + Linux servers + + + + + NO + zas_backend_01 + if1 + + + DISABLED + + + backend02 + backend02 + + + + + + Backend + + + Linux servers + + + + + NO + zas_backend_02 + if1 + + + DISABLED + + + frontend01 + frontend01 + + + + + + Frontend + + + Linux servers + + + + + NO + zas_frontend_01 + if1 + + + DISABLED + + + frontend02 + frontend02 + + + + + + Frontend + + + Linux servers + + + + + NO + zas_frontend_02 + if1 + + + DISABLED + + + Zabbix server + Zabbix server + + + + + + + Zabbix servers + + + + + if1 + + + DISABLED + + + + + + + + + Zabbix agent ping status + + + 1 + Up + + + + + \ No newline at end of file diff --git a/devenv/zabbix52/docker-compose.yml b/devenv/zabbix52/docker-compose.yml new file mode 100644 index 0000000..7a6d00e --- /dev/null +++ b/devenv/zabbix52/docker-compose.yml @@ -0,0 +1,126 @@ +version: "3" + +services: + # Zabbix + zabbix-server: + image: zabbix/zabbix-server-pgsql:alpine-5.2.1 + ports: + - "10051:10051" + depends_on: + - database + environment: + DB_SERVER_HOST: database + DB_SERVER_PORT: 5432 + POSTGRES_USER: zabbix + POSTGRES_PASSWORD: zabbix + POSTGRES_DB: zabbix + + zabbix-web: + image: zabbix/zabbix-web-apache-pgsql:alpine-5.2.1 + ports: + - "8188:8080" + - "8189:8443" + # - "8188:80" + depends_on: + - database + - zabbix-server + environment: + ZBX_SERVER_HOST: zabbix-server + ZBX_SERVER_PORT: 10051 + DB_SERVER_HOST: database + DB_SERVER_PORT: 5432 + POSTGRES_USER: zabbix + POSTGRES_PASSWORD: zabbix + POSTGRES_DB: zabbix + ZBX_SESSION_NAME: zbx_sessionid_new + volumes: + - ./ssl:/etc/ssl/apache2:ro + + database: + image: postgres + ports: + - "15432:5432" + environment: + POSTGRES_USER: zabbix + POSTGRES_PASSWORD: zabbix + + zabbix-agent: + image: zabbix/zabbix-agent:alpine-5.2.1 + environment: + ZBX_SERVER_HOST: zabbix-server + ZBX_SERVER_PORT: 10051 + + zabbix-proxy: + image: zabbix/zabbix-proxy-sqlite3:alpine-5.2.1 + environment: + ZBX_PROXYMODE: 0 + ZBX_HOSTNAME: zabbix-proxy + ZBX_SERVER_HOST: zabbix-server + ZBX_SERVER_PORT: 10051 + + zabbix-agent-proxy: + image: zabbix/zabbix-agent:alpine-5.2.1 + environment: + ZBX_SERVER_HOST: zabbix-proxy + ZBX_SERVER_PORT: 10051 + + ######################################################### + # Bootstrap config + ######################################################### + + bootstrap: + build: + context: ./bootstrap + dockerfile: Dockerfile + args: + ZBX_HOSTS_NUMBER: 10 + environment: + ZBX_API_URL: http://zabbix-web:8080 + # ZBX_API_URL: http://zabbix-web:80 + ZBX_API_USER: Admin + ZBX_API_PASSWORD: zabbix + depends_on: + - database + - zabbix-server + - zabbix-web + + ######################################################### + # Fake agents + ######################################################### + + # backend + redis_backend: + image: redis:alpine + + zas_backend_01: + build: ../zas-agent + volumes: + - ../zas-agent/conf/zas_scenario_backend.cfg:/etc/zas_scenario.cfg + environment: + REDIS_HOST: redis_backend + # restart: always + + zas_backend_02: + build: ../zas-agent + volumes: + - ../zas-agent/conf/zas_scenario_backend.cfg:/etc/zas_scenario.cfg + environment: + REDIS_HOST: redis_backend + + # frontend + redis_frontend: + image: redis:alpine + + zas_frontend_01: + build: ../zas-agent + volumes: + - ../zas-agent/conf/zas_scenario_frontend.cfg:/etc/zas_scenario.cfg + environment: + REDIS_HOST: redis_frontend + + zas_frontend_02: + build: ../zas-agent + volumes: + - ../zas-agent/conf/zas_scenario_frontend.cfg:/etc/zas_scenario.cfg + environment: + REDIS_HOST: redis_frontend