10
2021
05
23:08:07

zabbix清理历史数据



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

特别提醒:

a、文中测试的Zabbix版本为 3.0.3 。

b、清理数据属于高危操作,请在测试环境中验证后再执行线上操作!!!

 

1、统计数据库中每个表所占的空间:

1
2
3
4
5
mysql> SELECT table_name AS "Tables",
       round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB"
      FROM information_schema.TABLES
      WHERE table_schema = 'zabbixdb'
      ORDER BY (data_length + index_length) DESC;

   

2、清理zabbix一周之前的历史数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
!/bin/bash
User="zabbixuser"
Passwd="zabbixpass"
Date=`date -d $(date -d "-7 day" +%Y%m%d) +%s` #取7天之前的时间戳
$(which mysql) -u${User} -p${Passwd} -e "
use zabbixdb;
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 history_text WHERE 'clock' < $Date;
optimize table history_text;
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;
"

3、添加到系统计划任务:

1
2
#remove the zabbix mysql data before 7 day's ago
0 3 * * 0 /usr/local/script/clearzabbix.sh > /usr/local/script/clearzabbix.log

 

另:可以使用truncate命令直接清空数据库:

1
2
3
4
5
6
7
truncate table history;
truncate table history_uint;
truncate table history_str;
truncate table history_text;
truncate table trends;
truncate table trends_uint;
truncate table events;

如果想要删除表的所有数据,truncate语句要比 delete 语句快

因为 truncate 删除了表,然后根据表结构重新建立它,而 delete 删除的是记录,并没有尝试去修改表。

不过truncate命令虽然快,却不像delete命令那样对事务处理是安全的。

因此,如果我们想要执行truncate删除的表正在进行事务处理,这个命令就会产生退出并产生错误信息。 

 






以下是脚本代码truncate.sh:


#!/bin/bash

#User="zabbixuser"

#Passwd="zabbixpass"

systemctl stop zabbix-server.service

systemctl stop zabbix-agent.service

#$(which mysql) -u${User} -p${Passwd} -e "

$(which mysql)  -e " 

SET foreign_key_checks=0;

use zabbix;

truncate table history;

optimize table history;

truncate table history_uint;

optimize table history_uint;

truncate table history_str;

optimize table history_str;

truncate table history_text;

optimize table history_text;

truncate table history_log;

optimize table history_log;

truncate table trends;

optimize table trends;

truncate table trends_uint;

optimize table trends_uint;

truncate table problem;

optimize table problem;

truncate table events;

optimize table events;

truncate table event_recovery;

optimize table event_recovery;

SET foreign_key_checks=1;

"

#mysqldump -u${User} -p${Passwd} --quick --single-transaction zabbix |gzip >/home/`date

mysqldump --quick --single-transaction zabbix |gzip >/home/`date +%F_%H%M%S`_zabbix.sql.gz

systemctl start zabbix-server.service

systemctl start zabbix-agent.service

##########以上是脚本内容##############


关于mysql密码自己根据实际设置。


执行脚本:


sh truncate.sh

 







abbix运行一段时间之后,会留下大量的历史 数据,会发现zabbix的数据库一直在增大。运行3个月后笔者的数据库达到了5.7G,可能造成系统性能下降,查看历史数据时查询速度缓慢。
zabbix里面最大的表就是历史记录的表了,网上很多人都是写全部清空这些表的数据,其实我们可以按时间来删除里面的历史记录。

里面最大的表是 “history” 和 “history_uint”两个表;
<ignore_js_op>

 

zabbix里面的时间是用的时间戳方式记录,我们可以转换一下,然后根据时间戳来删除;

比如要删除2014年的1月1号以前的数据

1、先将标准时间转换为时间戳

# date +%s -d "2014-01-01 00:00:01"
1388505601

2、mysql清理数据

  • mysql> DELETE FROM `history_uint` WHERE `clock` < 1388505601;


  • mysql> optimize table history_uint;

注:执行过第二行命令之后可能会需要很长的一段时间,中间不要中断,否则容易丢失数据。


这是比较实用的按照时间段删除历史数据,也有方法可以全部清除历史监控数据
zabbix清空历史记录mysql数据库操作:


  • mysql -uroot -p 输入mysql密码

  • 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;

注意:此操作会清空zabbix所有历史监控数据,请操作之前备份好数据库


本文链接:https://hqyman.cn/post/1697.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: