LoginSignup
10
9

More than 3 years have passed since last update.

Zabbix Server のDB肥大化対処の覚書

Last updated at Posted at 2019-12-18

Zabbix Advent Calendar 2019の21日目の記事です。
Zabbix Server DB肥大化に対する圧縮対応の覚書です。
DB肥大化の対処として下記の2つを実施しました。
①history_log保持期間を7日に変更
②history_logテーブルのtruncate

環境

OS:CentOS 7.5
zabbix_server_version:3.0.10
MySQL:14.14
HDD:120G

状況

/ 配下のディスク使用率が 90% を超えている
とにかくhistory_logが肥大化している (85G)
ディスクの増設は環境制約でできない

ディスク使用状況
[root@zabbixserver ~]# df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/vda2        120G  107G   13G   90% /
:
[root@zabbixserver ~]# ls -ltrh /var/lib/mysql/zabbix/history_log.ibd
-rw-r----- 1 mysql mysql 85G 12月 10 11:14 /var/lib/mysql/zabbix/history_log.ibd

DB上で確認してもhistory_logテーブルの肥大化が確認できた.
合わせてhistory_logの保持期間を確認 (90日)

DB確認結果
mysql> select table_name, table_rows as tbl_rows, ROUND((data_length+index_length+data_free)/1024/1024/1024, 2) as 'total GB', ROUND((data_length+index_length)/1024/1024/1024, 2) as 'alldataGB', ROUND(data_free/1024/1024/1024, 2) as 'freeGB' from  information_schema.tables where table_schema='zabbix' and table_name='history_log';
+-------------+-----------+---------+-----------+--------+
| table_name  | tbl_rows  | totalGB | alldataGB | freeGB |
+-------------+-----------+---------+-----------+--------+
| history_log | 586857032 |   83.10 |     83.09 |   0.00 |
+-------------+-----------+---------+-----------+--------+
1 row in set (0.01 sec)
mysql> select history,count(history) from items where type='7' group by history;
+---------+----------------+
| history | count(history) |
+---------+----------------+
|      90 |            546 |
+---------+----------------+
1 row in set (0.01 sec)

【参考】Zabbixの各テーブルの使用状況
mysql> select table_name,table_rows,round((data_length+index_length+data_free)/1024/1024) as 'TotalUsage(data+index+free)/MB',round((data_length+index_length)/1024/1024) as 'DataUsage(data+index)/MB',round(data_free/1024/1024) as 'DataFree(free)/MB' from information_schema.tables order by table_rows desc;
+---------------------------------------+------------+--------------------------------+--------------------------+-------------------+
| TABLE_NAME                            | TABLE_ROWS | TotalUsage(data+index+free)/MB | DataUsage(data+index)/MB | DataFree(free)/MB |
+---------------------------------------+------------+--------------------------------+--------------------------+-------------------+
| items                                 |       3412 |                              7 |                        3 |                 4 |
| history                               |       2411 |                              0 |                        0 |                 0 |
| functions                             |       2298 |                              0 |                        0 |                 0 |
| items_applications                    |       2205 |                              0 |                        0 |                 0 |
| item_preproc                          |       1889 |                              0 |                        0 |                 0 |
| graphs_items                          |       1203 |                              0 |                        0 |                 0 |
| triggers                              |       1168 |                              6 |                        2 |                 4 |
| mappings                              |       1053 |                              0 |                        0 |                 0 |
| item_discovery                        |       1014 |                              0 |                        0 |                 0 |
| history_uint                          |        815 |                              0 |                        0 |                 0 |
| item_condition                        |        668 |                              0 |                        0 |                 0 |
| hostmacro                             |        633 |                              0 |                        0 |                 0 |
(snip)
| trigger_tag                           |          0 |                              0 |                        0 |                 0 |
+---------------------------------------+------------+--------------------------------+--------------------------+-------------------+
216 rows in set (0.01 sec)

【参考】
MySQLでDBとテーブルのサイズを確認するSQL

カラム名 意味
Data_length データファイルの長さ
Index_length インデックスファイルの長さ
Data_free 割り当てられているが使用されていない容量

DB肥大化の原因と対策方針

DB肥大化の原因

状況で確認した通り、history_logの肥大化が大きな原因
→Serverの調査のためにDEBUG log等を垂れ流していたことが原因
それはそれで監視Server側で対処を実施 (DEBUG logの出力停止)

対策方針

zabbix-server側の対処としては以下の2つを実施
①history_logの保持期間を変更 (logの増加スピードをを抑える、圧縮にはならない)
90日 → 10日
②history_logのtruncate (実際の圧縮作業)

対策1 history_logの保持期間を変更

GUIで1アイテムずつ変更してもよいが、SQLで一括で変更
90日→10日へ変更

history_log保存期間変更
mysql> select history,count(history) from items where type='7' group by history;
+---------+----------------+
| history | count(history) |
+---------+----------------+
|      90 |            546 |
+---------+----------------+
1 row in set (0.01 sec)

mysql> UPDATE items SET history='10' WHERE TYPE='7';
Query OK, 546 rows affected (0.11 sec)
Rows matched: 546  Changed: 546  Warnings: 0

mysql> select history,count(history) from items where type='7' group by history;
+---------+----------------+
| history | count(history) |
+---------+----------------+
|      10 |            546 |
+---------+----------------+
1 row in set (0.00 sec)

対策2 history log tableのtruncate実施

事前確認とzabbix-serverの停止

事前準備
[root@zabbixserver ~]# df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/vda2        120G  107G   14G   89% /
(snip)
[root@zabbixserver ~]# ls -ltrh /var/lib/mysql/zabbix/history_log.ibd
-rw-r----- 1 mysql mysql 86G 12月 11 13:28 /var/lib/mysql/zabbix/history_log.ibd
[root@zabbixserver ~]# systemctl stop zabbix-server.service
[root@zabbixserver ~]# systemctl status zabbix-server.service
● zabbix-server.service - Zabbix Server
   Loaded: loaded (/usr/lib/systemd/system/zabbix-server.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 水 2019-12-11 13:28:13 JST; 2s ago
  Process: 28246 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 1128 (code=exited, status=0/SUCCESS)

12月 11 13:28:10 zabbixserver systemd[1]: Stopping Zabbix Server...
12月 11 13:28:13 zabbixserver systemd[1]: Stopped Zabbix Server...

truncate実施

truncate実施
mysql> select table_name, table_rows as tbl_rows, ROUND((data_length+index_length+data_free)/1024/1024/1024, 2) 'as total GB', ROUND((data_length+index_length)/1024/1024/1024, 2) as 'alldataGB', ROUND(data_free/1024/1024/1024, 2) as 'freeGB' from  information_schema.tables where table_schema=database() and table_name='history_log';
+-------------+----------+---------+-----------+--------+
| table_name  | tbl_rows | totalGB | alldataGB | freeGB |
+-------------+----------+---------+-----------+--------+
| history_log | 63632742 |    9.35 |      9.35 |   0.00 |
+-------------+----------+---------+-----------+--------+
1 row in set (0.00 sec)

mysql> truncate table history_log;
Query OK, 0 rows affected (2.66 sec)

mysql> select table_name, table_rows as tbl_rows, ROUND((data_length+index_length+data_free)/1024/1024/1024, 2) 'as total GB', ROUND((data_length+index_length)/1024/1024/1024, 2) as 'alldataGB', ROUND(data_free/1024/1024/1024, 2) as 'freeGB' from  information_schema.tables where table_schema=database() and table_name='history_log';
+-------------+----------+---------+-----------+--------+
| table_name  | tbl_rows | totalGB | alldataGB | freeGB |
+-------------+----------+---------+-----------+--------+
| history_log |        0 |    0.00 |      0.00 |   0.00 |
+-------------+----------+---------+-----------+--------+
1 row in set (0.00 sec)

mysql> quit

事後確認でDBが圧縮されていることを確認

事後確認
[root@zabbixserver ~]# df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/vda2        120G   21G   99G   18% /
(snip)
[root@zabbixserver ~]# ls -ltrh /var/lib/mysql/zabbix/history_log.ibd
-rw-r----- 1 mysql mysql 128K 12月 11 14:11 /var/lib/mysql/zabbix/history_log.ibd
[root@zabbixserver ~]# systemctl start zabbix-server.service
[root@zabbixserver ~]# systemctl status zabbix-server.service
● zabbix-server.service - Zabbix Server
   Loaded: loaded (/usr/lib/systemd/system/zabbix-server.service; enabled; vendor preset: disabled)
   Active: active (running) since 水 2019-12-11 14:12:24 JST; 1s ago
  Process: 28246 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1312 ExecStart=/usr/sbin/zabbix_server -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 1314 (zabbix_server)
(snip)
12月 11 14:12:24 zabbixserver systemd[1]: Started Zabbix Server.
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9