zabbix監控運行一段時間以后,會留下大量的歷史監控數據,zabbix數據庫一直在增大;可能會造成系統性能下降,查看歷史數據室查詢速度緩慢。
zabbix里面最大的表就是history和history_uint兩個表,而且zabbix里面的時間是使用的時間戳方式記錄,所以可以根據時間戳來刪除歷史數據
清空部分歷史數據
一、關閉zabbix、http服務
[root@localhost repo]# systemctl stop zabbix-server httpd
二、清理zabbix歷史數據
1. 查看數據庫目錄文件
[root@localhost ~]# cd /var/lib/mysql/zabbix/ [root@localhost zabbix]# ls -lh | grep G -rw-rw---- 1 mysql mysql 463 7月 22 11:53 maplist.TRG -rw-rw---- 1 mysql mysql 645 7月 13 11:31 switcharp_copy.TRG -rw-rw---- 1 mysql mysql 629 7月 22 11:53 switcharp.TRG #生成Unix時間戳。時間定為2020年11月15日(暫定是保存2020年11月15日以后的監控數據) [root@localhost zabbix]# date +%s -d "Nov 15, 2020 00:00:00" #執行此命令以后會生成一個ID 1605369600 #這是生成的ID
2、數據備份
[root@localhost repo]# mysqldump -uroot -p123456 zabbix>./zabbix.sql mysql -uroot -p123456 zabbix <./zabbix.sql; # 導入
3、 登錄數據庫
[root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 76523 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> # 執行sql查看指定日期之前的數據大小: SELECT table_schema as `Database`,table_name AS `Table`,round(((data_length + index_length) / 1024 / 1024 / 1024), 2) `Size in MB`FROM information_schema.TABLES where CREATE_TIME < '2018-02-01 00:00:00' and table_name='history.ibd'; # 根據需要修改日期和查詢的表名稱(如果查詢出來的結果是0.0,需要將sql中的三個1024刪除一個,以G為單位顯示)
4、 執行以下命令,清理指定時間之前的數據、對zabbix數據庫執行sql命令
MariaDB [(none)]> use 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 # 清除部分歷史,優化查詢速度 MariaDB [zabbix]> delete from history where clock < 1605369600; Query OK, 6913 rows affected (1.97 sec) MariaDB [zabbix]> optimize table history; +----------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +----------------+----------+----------+-------------------------------------------------------------------+ | zabbix.history | optimize | note | Table does not support optimize, doing recreate + analyze instead | | zabbix.history | optimize | status | OK | +----------------+----------+----------+-------------------------------------------------------------------+ 2 rows in set (8.09 sec) MariaDB [zabbix]> delete from history_uint where clock < 1605369600; Query OK, 28563 rows affected (2.91 sec) MariaDB [zabbix]> optimize table history_uint; +---------------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +---------------------+----------+----------+-------------------------------------------------------------------+ | zabbix.history_uint | optimize | note | Table does not support optimize, doing recreate + analyze instead | | zabbix.history_uint | optimize | status | OK | +---------------------+----------+----------+-------------------------------------------------------------------+ 2 rows in set (10.42 sec) MariaDB [zabbix]> delete from trends where clock < 1605369600; Query OK, 28791 rows affected (0.20 sec) MariaDB [zabbix]> optimize table trends; +---------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +---------------+----------+----------+-------------------------------------------------------------------+ | zabbix.trends | optimize | note | Table does not support optimize, doing recreate + analyze instead | | zabbix.trends | optimize | status | OK | +---------------+----------+----------+-------------------------------------------------------------------+ 2 rows in set (0.13 sec) MariaDB [zabbix]> delete from trends_uint where clock < 1605369600; Query OK, 67899 rows affected (0.54 sec) MariaDB [zabbix]> optimize table trends_uint; +--------------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +--------------------+----------+----------+-------------------------------------------------------------------+ | zabbix.trends_uint | optimize | note | Table does not support optimize, doing recreate + analyze instead | | zabbix.trends_uint | optimize | status | OK | +--------------------+----------+----------+-------------------------------------------------------------------+ 2 rows in set (0.21 sec) # 動作日志 告警統計 MariaDB [zabbix]> delete from events where clock < 1605369600; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 1675 Current database: zabbix Query OK, 3060 rows affected (1.38 sec) MariaDB [zabbix]> optimize table events; +---------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +---------------+----------+----------+-------------------------------------------------------------------+ | zabbix.events | optimize | note | Table does not support optimize, doing recreate + analyze instead | | zabbix.events | optimize | status | OK | +---------------+----------+----------+-------------------------------------------------------------------+ 2 rows in set (0.07 sec)
delete from alerts where clock < 1605369600;
optimize table alerts;
# 審計日志 MariaDB [zabbix]> delete from auditlog where clock < 1605369600; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 2143 Current database: zabbix Query OK, 69440 rows affected (3.22 sec) MariaDB [zabbix]> optimize table auditlog; +-----------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +-----------------+----------+----------+-------------------------------------------------------------------+ | zabbix.auditlog | optimize | note | Table does not support optimize, doing recreate + analyze instead | | zabbix.auditlog | optimize | status | OK | +-----------------+----------+----------+-------------------------------------------------------------------+ 2 rows in set (0.04 sec) MariaDB [zabbix]> 注意:sql中的ID是生成Unix時間戳的ID號,需要改為自己生成的ID號
三、啟動服務
[root@localhost ~]# systemctl restart zabbix-server zabbix-agent httpd mariadb
使用truncate命令清空zabbix 所有監控數據 (未嘗試)
------------------------------------------------------- truncate table history; optimize table history; ------------------------------------------------------- truncate table history_str; optimize table history_str; ------------------------------------------------------- truncate table history_uint; optimize table history_uint; ------------------------------------------------------- truncate table trends; optimize table trends; ------------------------------------------------------- truncate table trends_uint; optimize table trends_uint; ------------------------------------------------------- truncate table events; optimize table events; ------------------------------------------------------- # 注意:這些命令會把zabbix所有的監控數據清空,操作前注意備份數據庫 # truncate是刪除了表,然后根據表結構重新建立,delete刪除的是記錄的數據沒有修改表 # truncate執行刪除比較快,但是在事務處理安全性方面不如delete,如果我們執行truncat的表正在處理事務,這個命令退出並會產生錯誤信息
轉載於:https://www.cnblogs.com/ArchitecTang/p/10173906.html