概述

由于Zabbix数据库历史数据过大,占用磁盘过多空间,需清理数据并释放空间。

处理

1、查看表占用空间情况
SELECT table_name AS "Tables",
 round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB"
 FROM information_schema.TABLES
 WHERE table_schema = 'zabbix'
 ORDER BY (data_length + index_length) DESC;

0##### 2、分析表数据

从上面较大的表来看,主要集中在historyhistory_uint 两张表,而且是存储历史数据。

查看数据表结构

查看history_uint和history数据表结构,可以根据clock时间戳来进行数据删除

MariaDB [zabbix]> desc history;
  +--------+---------------------+------+-----+---------+-------+
  | Field  | Type                | Null | Key | Default | Extra |
  +--------+---------------------+------+-----+---------+-------+
  | itemid | bigint(20) unsigned | NO   | MUL | NULL    |       |
  | clock  | int(11)             | NO   |     | 0       |       |
  | value  | double(16,4)        | NO   |     | 0.0000  |       |
  | ns     | int(11)             | NO   |     | 0       |       |
  +--------+---------------------+------+-----+---------+-------+
  4 rows in set (0.00 sec)
MariaDB [zabbix]> desc history_uint;
 +--------+---------------------+------+-----+---------+-------+
 | Field  | Type                | Null | Key | Default | Extra |
 +--------+---------------------+------+-----+---------+-------+
 | itemid | bigint(20) unsigned | NO   | MUL | NULL    |       |
 | clock  | int(11)             | NO   |     | 0       |       |
 | value  | bigint(20) unsigned | NO   |     | 0       |       |
 | ns     | int(11)             | NO   |     | 0       |       |
 +--------+---------------------+------+-----+---------+-------+
 4 rows in set (0.26 sec)
3、清理表数据

准备清理时建议停止zabbix-server服务以免出现其他问题。

获取30天前时间戳
date -d "30 days ago" +%s
按照时间戳删除数据,并优化
delete from history where clock < 1606905274;
#这个操作会锁定表,不要经常操作
optimize table history;

自动删除历史数据脚本:

#!/bin/bash
User="zabbix"
Passwd="zabbix"
#取30天之前的时间戳
Date=`date -d "30 days ago" +%s`
$(which mysql) -u${User} -p${Passwd} -e "
use zabbix;
DELETE FROM history WHERE 'clock' < $Date;
optimize table history;
DELETE FROM history_uint WHERE 'clock' < $Date;
optimize table history_uint;
END

本文标题:Zabbix-Server数据库清除历史数据

本文作者:宇宙最帅的男人

本文链接:https://lolicp.com/linux/20211599.html

版权声明:转载或者引用本文内容请注明来源及原作者,本文著作权归作者 (宇宙最帅的男人) 所有。

除非另有说明,本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

最后修改:2021 年 06 月 15 日
如果觉得我的文章对你有用,请随意赞赏