diff --git a/.gitignore b/.gitignore index c06a2b0..6831152 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ venv/ export/ *_host_ids.txt *.log -partitioning/tests/ +backup/ diff --git a/zabbix-tests/partitioning/README.md b/zabbix-tests/partitioning/README.md new file mode 100644 index 0000000..801d5e6 --- /dev/null +++ b/zabbix-tests/partitioning/README.md @@ -0,0 +1,36 @@ +# Zabbix Partitioning Tests + +This directory contains a Docker-based test environment for the Zabbix Partitioning script. + +## Prerequisites +- Docker & Docker Compose +- Python 3 + +## Setup & Run +1. Start the database container: + ```bash + docker compose up -d + ``` + This will start a MySQL 8.0 container and import the Zabbix schema. + +2. Create valid config (done automatically): + The `test_config.yaml` references the running container. + +3. Run the partitioning script: + ```bash + # Create virtual environment if needed + python3 -m venv venv + ./venv/bin/pip install pymysql pyyaml + + # Dry Run + ./venv/bin/python3 ../../partitioning/zabbix_partitioning.py -c test_config.yaml --dry-run --init + + # Live Run + ./venv/bin/python3 ../../partitioning/zabbix_partitioning.py -c test_config.yaml --init + ``` + +## Cleanup +```bash +docker compose down +rm -rf venv +``` diff --git a/zabbix-tests/partitioning/docker-compose.yml b/zabbix-tests/partitioning/docker-compose.yml new file mode 100644 index 0000000..7714ae6 --- /dev/null +++ b/zabbix-tests/partitioning/docker-compose.yml @@ -0,0 +1,14 @@ +services: + zabbix-db: + image: mysql:8.0 + container_name: zabbix-partition-test + environment: + MYSQL_ROOT_PASSWORD: root_password + MYSQL_DATABASE: zabbix + MYSQL_USER: zbx_part + MYSQL_PASSWORD: zbx_password + volumes: + - ../../partitioning/schemas/70-schema-mysql.txt:/docker-entrypoint-initdb.d/schema.sql + ports: + - "33060:3306" + command: --default-authentication-plugin=mysql_native_password diff --git a/zabbix-tests/partitioning/find_tables.py b/zabbix-tests/partitioning/find_tables.py new file mode 100644 index 0000000..defc53c --- /dev/null +++ b/zabbix-tests/partitioning/find_tables.py @@ -0,0 +1,31 @@ +import re + +def get_partitionable_tables(schema_path): + with open(schema_path, 'r', encoding='utf-8', errors='ignore') as f: + content = f.read() + + # Split into CREATE TABLE statements + tables = content.split('CREATE TABLE') + valid_tables = [] + + for table_def in tables: + # Extract table name + name_match = re.search(r'`(\w+)`', table_def) + if not name_match: + continue + table_name = name_match.group(1) + + # Check for PRIMARY KEY definition + pk_match = re.search(r'PRIMARY KEY \((.*?)\)', table_def, re.DOTALL) + if pk_match: + pk_cols = pk_match.group(1) + if 'clock' in pk_cols: + valid_tables.append(table_name) + + return valid_tables + +if __name__ == '__main__': + tables = get_partitionable_tables('/opt/git/Zabbix/partitioning/70-schema-mysql.txt') + print("Partitionable tables (PK contains 'clock'):") + for t in tables: + print(f" - {t}") diff --git a/zabbix-tests/partitioning/test_config.yaml b/zabbix-tests/partitioning/test_config.yaml new file mode 100644 index 0000000..0471071 --- /dev/null +++ b/zabbix-tests/partitioning/test_config.yaml @@ -0,0 +1,25 @@ +database: + type: mysql + host: 127.0.0.1 + socket: + user: root + passwd: root_password + db: zabbix + # Port mapping in docker-compose is 33060 + port: 33060 + +partitions: + daily: + - history: 7d + - history_uint: 7d + - history_str: 7d + - history_log: 7d + - history_text: 7d + - history_bin: 7d + - trends: 365d + - trends_uint: 365d + +logging: console +premake: 2 +replicate_sql: False +initial_partitioning_start: retention diff --git a/zabbix-tests/partitioning/wait_for_db.py b/zabbix-tests/partitioning/wait_for_db.py new file mode 100644 index 0000000..47a91b8 --- /dev/null +++ b/zabbix-tests/partitioning/wait_for_db.py @@ -0,0 +1,25 @@ +import time +import pymysql +import sys + +config = { + 'host': '127.0.0.1', + 'port': 33060, + 'user': 'root', + 'password': 'root_password', + 'database': 'zabbix' +} + +max_retries = 90 +for i in range(max_retries): + try: + conn = pymysql.connect(**config) + print("Database is ready!") + conn.close() + sys.exit(0) + except Exception as e: + print(f"Waiting for DB... ({e})") + time.sleep(2) + +print("Timeout waiting for DB") +sys.exit(1)