zabbix数据库清理脚本(需要做表分区)

由于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;

在这里插入图片描述

2、分析表数据
从上面较大的表看,主要集中history_unit,history两张表,而且是存储历史数据,
a、查看history_uint和history数据表结构,可以根据clock时间戳来进行数据删除

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)

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]>

3、清理表数据,建议停止zabbix server服务。
a、取30天前时间戳

[root@docker ~]#
[root@docker ~]# date +%s -d "Jun 4, 2019 00:00:00"
1559577600
[root@docker ~]#

b、按照时间戳删除数据,并优化

delete from history where clock < 1559577600;
optimize table history

删除历史数据脚本:

#!/bin/bash
User="zabbix"
Passwd="zabbix"
Date=`date -d $(date -d "-30 day" +%Y%m%d) +%s` #取30天之前的时间戳
$(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;

将以上脚本放到crontab里就可以实现自动清理啦

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值