2.1 KiB
2.1 KiB
Refactoring Notes: Zabbix Partitioning Script
Overview
The zabbix_partitioning.py script has been significantly refactored to improve maintainability, reliability, and compatibility with modern Zabbix versions (7.x).
Key Changes
1. Architecture: Class-Based Structure
- Old: Procedural script with global variables and scattered logic.
- New: Encapsulated in a
ZabbixPartitionerclass. - Purpose: Improves modularity, testability, and state management. Allows the script to be easily imported or extended.
2. Database Connection Management
- Change: Implemented
contextlib.contextmanagerfor database connections. - Purpose: Ensures database connections are robustly opened and closed, even if errors occur. Handles
wait_timeoutand binary logging settings automatically for every session.
3. Logging
- Change: Replaced custom
printstatements with Python's standardloggingmodule. - Purpose:
- Allows consistent log formatting.
- Supports configurable output destinations (Console vs Syslog) via the config file.
- Granular log levels (INFO for standard ops, DEBUG for SQL queries).
4. Configuration Handling
- Change: Improved validation and parsing of the YAML configuration.
- Purpose:
- Removed unused parameters (e.g.,
timezone, as the script relies on system local time). - Added support for custom database ports (critical for non-standard deployments or containerized tests).
- Explicitly handles the
replicate_sqlflag to control binary logging (it was intergrated into the partitioning logic).
- Removed unused parameters (e.g.,
5. Type Safety
- Change: Added comprehensive Python type hinting (e.g.,
List,Dict,Optional). - Purpose: Makes the code self-documenting and allows IDEs/linters to catch potential errors before execution.
6. Zabbix 7.x Compatibility
- Change: Added logic to verify Zabbix database version and schema requirements.
- Purpose:
- Checks
dbversiontable. - Critical: Validates that target tables have the
clockcolumn as part of their Primary Key before attempting partitioning, preventing potential data corruption or MySQL errors.
- Checks