阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Zabbix监控历史数据清理

204次阅读
没有评论

共计 3706 个字符,预计需要花费 10 分钟才能阅读完成。

Zabbix 监控运行一段时间以后,会留下大量的历史监控数据,Zabbix 数据库一直在增大;可能会造成系统性能下降,查看历史数据室查询速度缓慢。

Zabbix 里面最大的表就是 history 和 history_uint 两个表,而且 zabbix 里面的时间是使用的时间戳方式记录,所以可以根据时间戳来删除历史数据

 一、关闭 zabbix、http 服务

    pkill -9 zabbix
    service httpd stop
二、清理 zabbix 历史数据

1、查看数据库目录文件

    [root@zabbix-server zabbix]# cd /var/lib/mysql/zabbix/
    [root@zabbix-server zabbix]# ls -lh | grep G
    total 177G
    -rw-r—– 1 mysql mysql 1.7G Dec 24 13:49 events.ibd
    -rw-r—– 1 mysql mysql  60G Dec 24 13:49 history.ibd
    -rw-r—– 1 mysql mysql 2.4G Dec 24 13:49 history_str.ibd
    -rw-r—– 1 mysql mysql  99G Dec 24 13:49 history_uint.ibd
    -rw-r—– 1 mysql mysql 4.6G Dec 24 13:02 trends.ibd
    -rw-r—– 1 mysql mysql 9.5G Dec 24 13:49 trends_uint.ibd
    [root@zabbix-server zabbix]#
    生成 Unix 时间戳。时间定为 2018 年 2 月 1 日(暂定是保存 18 年 2 月以后的监控数据)
    [root@zabbix-server zabbix]# date +%s -d “Feb 1, 2018 00:00:00”    #执行此命令以后会生成一个 ID
    1517414400            #这是生成的 ID

2、数据备份

    [root@zabbix-server zabbix]#mysql -uroot -p zabbix > /root/mysqlback/zabbix.sql    #需要创建 mysqlback 目录

3、登录数据库

    [root@zabbix-server zabbix]# mysql -uroot -p
    Enter password:
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 7
    Server version: 5.5.60-MariaDB MariaDB Server

    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

    MariaDB [(none)]>

    执行 sql 查看指定日期之前的数据大小:
        SELECT table_schema as `Database`,table_name AS `Table`,round(((data_length + index_length) / 1024 / 1024 / 1024), 2) `Size in MB`FROM information_schema.TABLES where CREATE_TIME < ‘2018-02-01 00:00:00′ and table_name=’history.ibd’;

        根据需要修改日期和查询的表名称(如果查询出来的结果是 0.0,需要将 sql 中的三个 1024 删除一个,以 G 为单位显示)
   
4、执行以下命令,清理指定时间之前的数据、对 zabbix 数据库执行 sql 命令

    use zabbix;
    delete from history where clock < 1517414400;
    optimize table history;

    delete from history_uint where clock < 1517414400;
    optimize table history_uint;

    delete from trends where clock < 1517414400;
    optimize table trends;

    delete from trends_uint where clock < 1517414400;
    optimize table trends_uint;

    注意:sql 中的 ID 是生成 Unix 时间戳的 ID 号, 需要改为自己生成的 ID 号
 三、启动服务

    /usr/sbin/zabbix_server -c /etc/zabbix/zabbix_server.conf    #zabbix server
    /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf    #zabbix agent
    service httpd start

    =============== 分 =========== 隔 ========== 符 ============

1、使用 truncate 命令清空 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 所有的监控数据清空,操作前注意备份数据库

truncate 是删除了表,然后根据表结构重新建立,delete 删除的是记录的数据没有修改表

truncate 执行删除比较快,但是在事务处理安全性方面不如 delete, 如果我们执行 truncat 的表正在处理事务,这个命令退出并会产生错误信息

一些 Zabbix 相关教程集合

Ubuntu 14.04 下 Zabbix2.4.5 源码编译安装  https://www.linuxidc.com/Linux/2015-05/117657.htm
CentOS 7 LNMP 环境搭建 Zabbix3.0  https://www.linuxidc.com/Linux/2017-02/140134.htm
Ubuntu 16.04 安装部署监控系统 Zabbix2.4  https://www.linuxidc.com/Linux/2017-03/141436.htm
Zabbix 监控安装部署及警报配置  https://www.linuxidc.com/Linux/2017-03/141611.htm
Zabbix 触发器表达式详解 https://www.linuxidc.com/Linux/2017-03/141921.htm
Ubuntu 16.04 下安装部署 Zabbix3.0  https://www.linuxidc.com/Linux/2017-02/140395.htm
CentOS 6.3 下 Zabbix 监控 apache server-status https://www.linuxidc.com/Linux/2013-05/84740.htm
CentOS 7 下 Zabbix 3.0 安装详解 https://www.linuxidc.com/Linux/2017-03/141716.htm
64 位 CentOS 6.2 下安装 Zabbix 2.0.6  https://www.linuxidc.com/Linux/2014-11/109541.htm

ZABBIX 的详细介绍:请点这里
ZABBIX 的下载地址:请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3706字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中