Zabbix Database Partitioning Guide (Python based)
This guide describes how to set up and manage database partitioning for Zabbix using the zabbix_partitioning.py script.
Overview
The script manages MySQL table partitions based on time (Range Partitioning on the clock column). It automatically:
- Creates future partitions to ensure new data can be written.
- Drops old partitions based on configured retention periods.
Benefits:
- Performance: Faster cleanup of old data (dropping a partition is instantaneous compared to Zabbix internal housekeeping).
- Recommended: For database bigger than 100GB.
- Must have!: For database bigger than 500G.
Warning
Support for MySQL/MariaDB only. Always BACKUP your database before initializing partitioning!
1. Prerequisites
- Python 3.6+
- Python Libraries:
pymysql,pyyaml# Debian/Ubuntu sudo apt install python3-pymysql python3-yaml # RHEL/AlmaLinux/Rocky sudo dnf install python3-pymysql python3-pyyaml # Or via pip pip3 install pymysql pyyaml - Database Permissions: The user configured in the script needs:
SELECT,INSERT,CREATE,DROP,ALTERon the Zabbix database.SUPERorSESSION_VARIABLES_ADMINprivilege (required to disable binary logging viaSET SESSION sql_log_bin=0ifreplicate_sql: False).
2. Installation
- Copy the script and config to a precise location (e.g.,
/usr/local/binor specialized directory).mkdir -p /opt/zabbix_partitioning cp zabbix_partitioning.py /opt/zabbix_partitioning/ cp zabbix_partitioning.conf /etc/zabbix/ chmod +x /opt/zabbix_partitioning/zabbix_partitioning.py
3. Configuration
Edit /etc/zabbix/zabbix_partitioning.conf:
database:
host: localhost
user: zbx_part
passwd: YOUR_PASSWORD
db: zabbix
# port: 3306 # Optional, default is 3306
partitions:
daily:
- history: 14d
- history_uint: 14d
- trends: 365d
# ... add other options as needed. Please check the config file for more options.
Important Notes:
replicate_sql:False(Default): Partitioning maintenance commands are NOT replicated to slaves. Recommended if you manage partitions separately on each node or want to reduce replication lag.True: Commands are replicated.
auditlog:- In Zabbix 7.0+, the
auditlogtable does not have theclockcolumn in its Primary Key by default. Do not add it to the config unless you have manually altered the table schema.
- In Zabbix 7.0+, the
4. Zabbix Preparation (CRITICAL)
Before partitioning, you must disable Zabbix's internal housekeeping for the tables you intend to partition. If you don't, Zabbix will try to delete individual rows while the script tries to drop partitions, causing conflicts.
- Log in to Zabbix Web Interface.
- Go to Administration -> General -> Housekeeping.
- Uncheck the following (depending on what you partition):
- Enable internal housekeeping for History
- Enable internal housekeeping for Trends
- Click Update.
5. Initialization
This step converts existing standard tables into partitioned tables.
-
Dry Run (Verify what will happen):
/opt/zabbix_partitioning/zabbix_partitioning.py --init --dry-runCheck the output for any errors.
-
Execute Initialization:
/opt/zabbix_partitioning/zabbix_partitioning.py --initThis may take time depending on table size.
6. Automation (Cron Job)
Set up a daily cron job to create new partitions and remove old ones.
- Open crontab:
crontab -e - Add the line (run daily at 00:30):
30 0 * * * /usr/bin/python3 /opt/zabbix_partitioning/zabbix_partitioning.py -c /etc/zabbix/zabbix_partitioning.conf >> /var/log/zabbix_partitioning.log 2>&1
7. Automation (Systemd Timer) — Recommended
Alternatively, use systemd timers for more robust scheduling and logging.
-
Create Service Unit (
/etc/systemd/system/zabbix-partitioning.service):[Unit] Description=Zabbix Database Partitioning Service After=network.target mysql.service [Service] Type=oneshot User=root ExecStart=/usr/bin/python3 /opt/zabbix_partitioning/zabbix_partitioning.py -c /etc/zabbix/zabbix_partitioning.conf -
Create Timer Unit (
/etc/systemd/system/zabbix-partitioning.timer):[Unit] Description=Run Zabbix Partitioning Daily [Timer] OnCalendar=*-*-* 00:30:00 Persistent=true [Install] WantedBy=timers.target -
Enable and Start:
systemctl daemon-reload systemctl enable --now zabbix-partitioning.timer -
View Logs:
journalctl -u zabbix-partitioning.service
8. Troubleshooting
- Connection Refused: Check
host,portin config. Ensure MySQL is running. - Access Denied (1227): The DB user needs
SUPERprivileges to disable binary logging (replicate_sql: False). Either grant the privilege or setreplicate_sql: True(if replication load is acceptable). - Primary Key Error: "Primary Key does not include 'clock'". The table cannot be partitioned by range on
clockwithout schema changes. Remove it from config.