Cleaning up the Zabbix database

My Zabbix database size increased a lot in the last few months and since my disk was running out space, I decided to clean up the old events from the database.

Looking at the database tables, the biggest one was history_uint, which holds the items history data – over 400 millions of records and over 30 Gb of disk space before the clean up.

Since deleting the old records from this table directly would be a very slow process, I decided to create a new table and insert the latest records from the history_uint table and then just replace the old table with a new one.

Since this is not an offical procedure, use it at your own risk.

Zabbix v2.2
MySql 5.1 – InnoDB with innodb_file_per_table=ON

Step 1 – Stop the Zabbix server
Step 2 – Open your favourite MySQL client and create a new table

CREATE TABLE history_uint_new LIKE history_uint

Step 3 – Insert the latest records from the history_uint table to the history_uint_new table

First you need to decide how many records you need to keep. Please note, that the history data in graphs will still be available since trends data is stored in a different table.

I decided to keep the data for the last 3 weeks. The event time is in UNIX-time (aka POSIX-time or Epoch time) format, so you need to calculate the date and time of the event you will start with.

There is a tool available to do this calculation for you – Epoch Converter.

For example, if you decide to keep all the events from the 1st of May 2014 00:00:00 (GMT) on, the Epoch time would be 1398902400.

Now that we have our Epoch timestamp, we can copy the records to the new table:

INSERT INTO history_uint_new SELECT * FROM history_uint WHERE clock > '1398902400'

Step 4 – Rename the history_uint and history_uint_new tables

ALTER TABLE history_uint RENAME history_uint_old
ALTER TABLE history_uint_new RENAME history_uint

Step 5 – Start the Zabbix server

Start the Zabbix server and check if everything is ok and if you’re happy…

Step 6 – Drop the old table and save some disk space

DROP TABLE history_uint_old

Additionally you can update the items table and set the item history table record to a fewer days, so Zabbix will do the automatic clean up before the table size gets too big.

UPDATE items SET history = '15' WHERE history > '30'

4 thoughts on “Cleaning up the Zabbix database”

  1. My brother suggested I might like this web site. He was entirely right.
    This post actually made my day. You cann’t imagine just how much time I had spent for this information!

  2. Instead hard code the unix timestamp, I think it’s better specify how many days ago the history items should remain. The query above is going to auto-generate unix timestamp for 30 days ago:

    INSERT IGNORE INTO history_uint_new SELECT * FROM history_uint WHERE clock > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 day))

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image