一、Zabbix监控如何清空历史数据
概述:
zabbix在运行一段时间过后,会留下大量的历史数据,我们会发现zabbix的数据库会一直越来越大。运行三个月的zabbix的数据库会达到10个G左右根据监控的服务器和交换机数量有关以及模板里面的监控项和数据。
zabbix里面最大的就是表就是历史记录的表了,网上很多人都是在写全部清空这些表的数据,其实我们可以按照时间来删除里面的数据。
里面最大的表就是“history”和history_unit两个表
zabbix里面的时间是用的时间戳方式记录,我们可以转换一下,然后根据时间戳来删除;
# 将当前时间转换为时间戳
[root@Halo ~]# date +%s
1695256938
mysql清理数据
删除指定日期数据
停止相关服务,避免数据写入
netstat -tnlp |grep mysql
systemctl stop zabbix-server
1.进入mysql
mysql -uroot -p 输入mysql密码:Nihao123456!
2.执行命令 替换时间戳1695256938
mysql> DELETE FROM `history_uint` WHERE `clock` < 1695256938;
# 删除小于这个时间戳的数据
mysql> optimize table history_uint;
清空历史数据
上面是比较实用的按照时间段删除历史数据,也有方法可以全部清除历史监控数据,Zabbix清空历史记录mysql数据库操作:
mysql -uroot -p 输入mysql密码:Nihao123456!
use 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;
二、清空zabbix6.0历史数据脚本
密码写你自己的
先停止服务
systemctl stop zabbix-server
第一步:建立文件jiaoben.sh
vim jiaoben.sh
如下
#!/bin/bash
User="root"
Passwd="123456789"
Date=`date -d $(date -d "-20 day" +%Y%m%d) +%s` #取20天之前的时间戳
$(which mysql) -u${User} -p${Passwd} -e "
use zabbix;
DELETE FROM history WHERE clock < $Date;
optimize table history;
DELETE FROM history_str WHERE clock < $Date;
optimize table history_str;
DELETE FROM history_uint WHERE clock < $Date;
optimize table history_uint;
DELETE FROM trends WHERE clock < $Date;
optimize table trends;
DELETE FROM trends_uint WHERE clock < $Date;
optimize table trends_uint;
DELETE FROM events WHERE clock < $Date;
optimize table events; "
保存退出
第二步:给脚本执行权限
chmod 777 jiaoben.sh
第三步执行
./jiaoben.sh
最后
启动服务
systemctl start zabbix-server
从启动所有服务
systemctl restart zabbix-server zabbix-agent apache2