清理 zabbix 历史数据, 缩减 mysql 空间

本文提供了解决Zabbix因历史数据过多导致磁盘空间占用过大的方法,包括停止服务、调整MySQL配置、删除旧数据及优化表空间等步骤。

zabbix 由于历史数据过大, 因此导致磁盘空间暴涨,  下面是解决方法步骤

1. 停止 ZABBIX SERER 操作

[root@82 ~]# killall zabbix_server
[root@82 ~]# lsof -i:10051

2. 停止 mysql 操作

[root@gd0283 dbdat]# mysqladmin -u root -p -h 127.0.0.1 shutdown

3. 修改 my.cnf

添加 skip-new 参数, 目标可用缩减 innodb 磁盘空间

4. 重启启动 mysql

/apps/svr/mysql/bin/mysqld_safe --defaults-file=/apps/conf/mysql5.6/my.cnf --ledir=/apps/svr/mysql/bin  --basedir=/apps/svr/mysql/  --datadir=/apps/dbdat/mysql5_data --user=apps &

[root@83 apps]# lsof -i:3306
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
mysqld  25527 apps   11u  IPv4 29371110      0t0  TCP *:mysql (LISTEN)

5. 分析 history 表

mysql> 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)

mysql> select max(itemid) from history;
+-------------+
| max(itemid) |
+-------------+
|       46582 |
+-------------+
1 row in set (0.00 sec)

mysql> select * from history where itemid=46582 limit 1, 20;
+--------+------------+--------+-----------+
| itemid | clock      | value  | ns        |
+--------+------------+--------+-----------+
|  46582 | 1396361332 | 0.0000 |  81875000 |
|  46582 | 1396361362 | 0.0000 | 768297000 |
|  46582 | 1396361392 | 0.0000 | 656787000 |
|  46582 | 1396361422 | 0.0000 | 665169000 |
|  46582 | 1396361452 | 0.0000 | 973570000 |
|  46582 | 1396361482 | 0.0000 | 625619000 |
|  46582 | 1396361512 | 0.0000 | 292743000 |
|  46582 | 1396361543 | 0.0000 |    340000 |
|  46582 | 1396361572 | 0.0000 |  15651000 |
|  46582 | 1396361602 | 0.0000 | 153264000 |
|  46582 | 1396361632 | 0.0000 |  79316000 |
|  46582 | 1396361662 | 0.0000 | 308107000 |
|  46582 | 1396361692 | 0.0000 | 237146000 |
|  46582 | 1396361722 | 0.0000 | 108810000 |
|  46582 | 1396361752 | 0.0000 | 419398000 |
|  46582 | 1396361782 | 0.0000 | 284113000 |
|  46582 | 1396361812 | 0.0000 | 254230000 |
|  46582 | 1396361842 | 0.0000 | 145938000 |
|  46582 | 1396361872 | 0.0000 | 403163000 |
|  46582 | 1396361902 | 0.3300 | 193302000 |
+--------+------------+--------+-----------+
20 rows in set (0.01 sec)

6. 删除两周前数据方法

取得时间戳, 时间只保留至 2014 3 25 日

[root@gd02-zabbix-db-research api]# date +%s -d "Mar 25, 2014 00:00:00" 
1395676800

删除 history, history_unit 表方法

mysql> delete from history where clock < 1395676800;
Query OK, 8961912 rows affected (17 min 22.06 sec)

mysql> delete from history_uint where clock < 1395676800;
Query OK, 7789494 rows affected (21 min 38.02 sec)

尝试对表进行缩减发生故障

mysql> optimize table history_uint;
ERROR 1114 (HY000): The table '#sql-63b7_2d' is full
mysql> quit
Bye
[root@gd02-qa-plxt2-nodomain-web-96 mysql5_data]# df -h
文件系统              容量  已用 可用 已用% 挂载点
/dev/vda1              20G   18G  1.3G  94% /
tmpfs                 2.0G     0  2.0G   0% /dev/shm

故障原因, 当前 / 下磁盘空间不够.

临时删除 swapfile

[root@gd02-qa-plxt2-nodomain-web-96 /]# swapoff -a
[root@gd02-qa-plxt2-nodomain-web-96 /]# rm -rf swapfile
[root@gd02-qa-plxt2-nodomain-web-96 /]# df -h
文件系统              容量  已用 可用 已用% 挂载点
/dev/vda1              20G   15G  3.7G  81% /
tmpfs                 2.0G     0  2.0G   0% /dev/shm

原理说明, mysql 执行 optimize 过程中, 生成了临时表见下面文件, mysql 直接吧 history_unit 表复制成临时表再重新改名, 实现空间缩减.

[root@gd02-qa-plxt2-nodomain-web-96 zabbix]# ls *63b7_2e* -lh
-rw-rw---- 1 apps apps 8.5K 04-02 12:04 #sql-63b7_2e.frm
-rw-rw---- 1 apps apps 1.1G 04-02 12:10 #sql-63b7_2e.ibd

再次缩减

mysql> optimize table history_uint;
Query OK, 40496963 rows affected (20 min 0.04 sec)
Records: 40496963  Duplicates: 0  Warnings: 0

mysql> optimize table history;
Query OK, 45998084 rows affected (21 min 54.99 sec)
Records: 45998084  Duplicates: 0  Warnings: 0

7. 缩减前后文件大小比较
缩减前

[root@gd02-qa-plxt2-nodomain-web-96 mysql5_data]# find -size +50M -exec ls -lh {} \;
-rw-rw---- 1 apps apps 80M 04-02 10:29 ./zabbix/events.ibd
-rw-rw---- 1 apps apps 5.1G 04-02 11:44 ./zabbix/history.ibd
-rw-rw---- 1 apps apps 152M 04-02 10:29 ./zabbix/trends.ibd
-rw-rw---- 1 apps apps 4.3G 04-02 11:47 ./zabbix/history_uint.ibd
-rw-rw---- 1 apps apps 328M 04-02 10:29 ./zabbix/trends_uint.ibd
-rw-rw---- 1 apps apps 1.1G 04-02 11:47 ./ibdata1

缩减后

[root@gd02-qa-plxt2-nodomain-web-96 mysql5_data]# find -size +50M -exec ls -lh {} \;
-rw-rw---- 1 apps apps 80M 04-02 10:29 ./zabbix/events.ibd
-rw-rw---- 1 apps apps 3.6G 04-02 13:30 ./zabbix/history.ibd
-rw-rw---- 1 apps apps 152M 04-02 10:29 ./zabbix/trends.ibd
-rw-rw---- 1 apps apps 3.2G 04-02 12:24 ./zabbix/history_uint.ibd
-rw-rw---- 1 apps apps 328M 04-02 10:29 ./zabbix/trends_uint.ibd
-rw-rw---- 1 apps apps 1.1G 04-02 13:30 ./ibdata1

8. 重启启动 zabbix,  php, nginx, mysql

新问题出现:

当前 zabbix 进行初始化, 会对 mysql 进行大量数据 r/w 操作

因此可能会发生下面警报, 经过 5 分钟后初始化, 下面报警会自动消除, 不用担心. 

Disk I/O is overloaded on gd02-qa-plxt2-nodomain-web-96.vclound.com

Zabbix history syncer processes more than 75% busy

Zabbix timer processes more than 75% busy

 

转载于:https://www.cnblogs.com/Oman/p/5945017.html

### Zabbix历史数据清理方法及配置教程 Zabbix 是一种强大的开源监控解决方案,随着其运行时间的增长,可能会积累大量的历史数据。这些数据如果得不到及时清理,会占用大量磁盘空间并影响性能。以下是针对 Zabbix 历史数据清理的具体方法和配置指南。 #### 数据库表结构概述 在 Zabbix 中,主要涉及以下几个与历史数据相关的表: - `history` 和 `history_uint`: 存储浮点型和整数型的历史数据。 - `trends` 和 `trends_uint`: 存储趋势数据(通常用于长期存储)。 为了减少冗余数据的影响,可以定期清理不必要的记录[^4]。 --- #### 清理策略一:按天保留每项指标的一条数据 通过 SQL 查询,可以从 `history`, `history_uint`, `trends`, 和 `trends_uint` 表中删除多余的记录,仅保留每日一条的数据。具体操作如下: ```sql -- 删除多余的历史数据 (float 类型) delete from history where (itemid, clock) not in ( select itemid, min(clock) from history group by itemid, date_format(from_unixtime(clock), '%Y-%m-%d') ); -- 删除多余的历史数据 (integer 类型) delete from history_uint where (itemid, clock) not in ( select itemid, min(clock) from history_uint group by itemid, date_format(from_unixtime(clock), '%Y-%m-%d') ); -- 删除多余的趋势数据 (float 类型) delete from trends where (itemid, clock) not in ( select itemid, min(clock) from trends group by itemid, date_format(from_unixtime(clock), '%Y-%m-%d') ); -- 删除多余的趋势数据 (integer 类型) delete from trends_uint where (itemid, clock) not in ( select itemid, min(clock) from trends_uint group by itemid, date_format(from_unixtime(clock), '%Y-%m-%d') ); ``` 上述脚本的作用是从每个项目 (`itemid`) 的每日数据中只保留最早的时间戳(`clock`)对应的记录[^4]。 --- #### 清理策略二:设置自动清理机制 除了手动执行 SQL 脚本外,还可以利用 Zabbix 自带的功能来管理过期数据。这可以通过调整以下参数实现: 1. **修改全局保持周期** 编辑 Zabbix Server 配置文件 `/etc/zabbix/zabbix_server.conf` 或者相应的路径,找到或添加以下选项: ```ini # 设置历史数据保存期限为90天 HistoryStoragePeriod=90d # 设置趋势数据保存期限为365天 TrendStoragePeriod=365d ``` 2. **启用维护任务** 确保 Zabbix Server 启用了后台进程以处理数据清理工作。默认情况下,此功能已开启,但如果未生效,则需要检查日志文件确认是否有错误发生。 3. **计划任务补充** 如果希望更灵活地控制清理逻辑,可以在操作系统层面创建 cron 定时任务,调用自定义的 SQL 脚本来完成特定需求。 --- #### 注意事项 - 在执行任何大规模 DELETE 操作之前,请务必备份数据库以防误删重要信息。 - 对于生产环境中的大型数据库,建议分批逐步清除旧数据而不是一次性全部移除,以免造成锁表现象从而中断服务。 - 当升级到新版本如 Zabbix 7.x 之后,应重新评估现有架构是否满足新的特性支持情况,并考虑引入智能化运维手段优化整体流程[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值