概要
先日、Zabbixの監視間隔を1秒にしたらMySQLのバイナリファイルによってディスクフルになった話を書いた。
akng-engineer.hatenablog.com
しかし、バイナリファイルの肥大化より気にしなければならなかったのは、Zabbixで収集したデータのヒストリ保存期間だった。1秒間隔の監視だから、ヒストリの保存期間は7日程にして様子見ようと思ったが、予想上にデータが蓄積するのが早く1週間でディスクを逼迫してしまった。。。検証環境だとは言え、データベース設計の重要性を思い知ったので、今回実施した対処法を記録しておきたい。
環境
- Zabbix3.0
- MySQL5.6
- 割り当てディスク: 128GB
チャットのアラート「ディスクの空き残り3%」
あれ、2日前にバイナリログの保持期間変更したのに....?(やばい)
急いでdfコマンドでディスクチェックしたが、やっぱり肥大化しているのはMySQL関連のディレクトリだった。しかし、今回はバイナリログそんなに出力されていないので、Zabbixデータベースのテーブルサイズを確認した。
#ディスク使用率を確認 df -h # Zabbixデータベースのテーブルサイズを確認(上位5つを表示) use zabbix; select table_name, engine, table_rows as tbl_rows, avg_row_length as rlen, floor((data_length+index_length)/1024/1024) as allMB, floor((data_length)/1024/1024) as dMB, floor((index_length)/1024/1024) as iMB from information_schema.tables where table_schema=database() order by (data_length+index_length) desc limit 5;
Zabbixデータベースのテーブルを確認すると、historyテーブルのレコードが8億以上でサイズが60GB以上だった。ヒストリ関連のテーブルはほかにもhistory_uint, history_strがあるが、合計するとサイズは100GBを超えていた。なるほど.......(泣)
ZabbixのWebUI画面でディスク使用率の時系列グラフを見たが、1日に約15%の増加量だった。すさまじい。。。
対処策①:テーブル圧縮する
下記コマンドでヒストリ関連テーブルを圧縮しようかと思ったが、空き容量が3%のため巨大なテーブルを圧縮することはできなかったので断念。
#テーブルを圧縮する alter table history row_format=compressed;
対処策②:ヒストリ関連のテーブルを作り直す
どうするかと思い色々調べていたところ、別テーブルを作って一部レコードを挿入するやり方が紹介されていたので試してみた。
phucnw.blogspot.com
#テーブルを複製して新しいテーブルを作る create table history_new like history; create table history_uint_new like history_uint; create table history_str_new like history_str; #元テーブルから一部レコードを新しいテーブルに流し込む insert into history_new select * from history where clock > [unix_time]; insert into history_uint_new select * from history_uint where clock > [unix_time]; insert into history_str_new select * from history_str where clock > [unix_time]; #テーブル名を変更する alter table history rename history_old; alter table history_new rename history; alter table history_uint rename history_uint_old; alter table history_uint _new rename history_uint; alter table history_str rename history_str_old; alter table history_str_new rename history_str; #一度Zabbixサーバーを立ち上げて問題ないか確認する systemctl start zabbix-server #旧テーブルを削除する drop table history_old; drop table history_uint_old; drop table history_str_old; #ディスク容量確認する df -h
このやり方で、ディスク使用量が16%まで減少!!!なるほどー!
うまくいってよかった。めでたし。