DBのメンテナンス

Zabbixで監視を長期間行うと、収集したベータによってDBのサイズが肥大していきます。 収集したデータは一定期間の制限があるので、ある程度まで肥大すればそれ以上は増えないように思えますが、実際にはフラグメントが発生して肥大を続けていきます。
このため、DBについては定期的にフラグメント対策していないと、気づいた時にはDB(またはディスクスペース)を食いつぶしてZabbixが動作できなくる事態が予想されます。
  1. Zabbixで利用しているDBと各テーブルの使用サイズの確認
普通にインストールしたZabbixであれば、DBにはMySQL(innoDB)、DB名に"zabbix"が利用されている思われます。 この確認はZabbixサーバ上で次の操作で確認できます。
# mysql -u root -p
Enter password: MySQLのパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26706
Server version: 5.1.66-0+squeeze1 (Debian)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database       |
+--------------------+
| information_schema     |
| mysql         |
| zabbix        |
+--------------------+
3 rows in set (0.00 sec)

mysql>
上記の表示内容から、ZabbixのDBを確認します。(ここでは、"zabbix")

次に、データベース"zabbix"内のテーブル確認をします。
mysql> use zabbix; (データベースを"zabbix"に切り替える)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
この後、テーブルステータスを確認して、どのテーブルでどれだけのサイズを使っているか確認しておきます。
mysql> show table status;
     ::
     ::
mysql> exit
history、history_uint、trends、trends_uint の4テーブルが他より大きいと思われます。対策はこの4つのテーブルを主に行います。
  1. DBのデフラグメント用スクリプトの準備
DBのデフラグメント用スクリプトファイルとして、/etc/cron.zabbix を作成します。
(/etc/cron.zabbix の内容)
#!/bin/sh
mysql --user='root' --password='MySQLのパスワード' --database=zabbix <<eof
alter table history ENGINE=INNODB;
alter table history_uint ENGINE=INNODB;
alter table trends ENGINE=INNODB;
alter table trends_uint ENGINE=INNODB;
eof

作成したファイルに実行権を与え、正常に動作するか確認します(DBのサイズによって、かなり時間がかかるかも知れません)。
# chmod +x /etc/cron.zabbix
# /etc/cron.zabbix

あとはこのスクリプト、crontabで適当な日時に実行すればよい。
(/etc/crontab の内容)
MAILTO=root

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
01 0 * * 7 root /etc/cron.zabbix
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )