文档章节

zabbix优化之partition

IT--小哥
 IT--小哥
发布于 2017/02/17 13:03
字数 1375
阅读 47
收藏 0

一、对zabbix进行partition

1.1 我的思路

1、在不影响zabbix业务正常写入的情况下,在线搭建一个从库

2、对从库历史数据进行表分区,查看每个分区业务增长量

3、根据业务增长量和字段大小规划数据库容量,然后进行合理的分区

4、从库运行一段时间,查看从库状态,状态正常就切换zabbix连接从库

1.2 对历史数据进行分析

线上情况如上图所示,线上items按2W个计算

1.2.1 历史数据的保存

zabbix的数据库大小取决于NVPS(每秒处理的平均数量),我们线上按2W个监控项,刷新周期按60s,那么每秒处理的数据为20000/60=333(条),说明此刻每秒将会向数据库新增加333条数据,可以设置历史数据库保存天数,

计算公式:历史数据大小=天数*每秒处理的数据*24h*3600s*50Bytes

1.2.2 趋势数据的保存

每个Items趋势的数据大约90B,大小取决于数据库类型。trends每小时收集一次数据,

计算公式为:趋势数据大小=监控项*24h*90B

 

1.3 在从库进行操作

3.4.1 index change

Zabbix3.2不用进行这步操作,可惜我是3.0

如果数据量特别大,这可能花费很长时间,几秒,几分钟,几个小时,几天都是可能的

mysql版本中,DDL不是online的,5.6之前

需要修改的表结构如下

mysql> Alter table trends drop primary key, add index (itemid),add index (clock);

mysql> Alter table trends_uint drop primary key, add index (itemid),add index (clock);

3.4.2 Stored Procedures

DELIMITER $$

CREATE PROCEDURE `partition_create`(SCHEMANAME varchar(64), TABLENAME varchar(64), PARTITIONNAME varchar(64), CLOCK int)

BEGIN

        /*

           SCHEMANAME = The DB schema in which to make changes

           TABLENAME = The table with partitions to potentially delete

           PARTITIONNAME = The name of the partition to create

        */

        /*

           Verify that the partition does not already exist

        */



        DECLARE RETROWS INT;

        SELECT COUNT(1) INTO RETROWS

        FROM information_schema.partitions

        WHERE table_schema = SCHEMANAME AND table_name = TABLENAME AND partition_description >= CLOCK;



        IF RETROWS = 0 THEN

                /*

                   1. Print a message indicating that a partition was created.

                   2. Create the SQL to create the partition.

                   3. Execute the SQL from #2.

                */

                SELECT CONCAT( "partition_create(", SCHEMANAME, ",", TABLENAME, ",", PARTITIONNAME, ",", CLOCK, ")" ) AS msg;

                SET @sql = CONCAT( 'ALTER TABLE ', SCHEMANAME, '.', TABLENAME, ' ADD PARTITION (PARTITION ', PARTITIONNAME, ' VALUES LESS THAN (', CLOCK, '));' );

                PREPARE STMT FROM @sql;

                EXECUTE STMT;

                DEALLOCATE PREPARE STMT;

        END IF;

END$$

DELIMITER ;
DELIMITER $$

CREATE PROCEDURE `partition_drop`(SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), DELETE_BELOW_PARTITION_DATE BIGINT)

BEGIN

        /*

           SCHEMANAME = The DB schema in which to make changes

           TABLENAME = The table with partitions to potentially delete

           DELETE_BELOW_PARTITION_DATE = Delete any partitions with names that are dates older than this one (yyyy-mm-dd)

        */

        DECLARE done INT DEFAULT FALSE;

        DECLARE drop_part_name VARCHAR(16);



        /*

           Get a list of all the partitions that are older than the date

           in DELETE_BELOW_PARTITION_DATE.  All partitions are prefixed with

           a "p", so use SUBSTRING TO get rid of that character.

        */

        DECLARE myCursor CURSOR FOR

                SELECT partition_name

                FROM information_schema.partitions

                WHERE table_schema = SCHEMANAME AND table_name = TABLENAME AND CAST(SUBSTRING(partition_name FROM 2) AS UNSIGNED) < DELETE_BELOW_PARTITION_DATE;

        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;



        /*

           Create the basics for when we need to drop the partition.  Also, create

           @drop_partitions to hold a comma-delimited list of all partitions that

           should be deleted.

        */

        SET @alter_header = CONCAT("ALTER TABLE ", SCHEMANAME, ".", TABLENAME, " DROP PARTITION ");

        SET @drop_partitions = "";



        /*

           Start looping through all the partitions that are too old.

        */

        OPEN myCursor;

        read_loop: LOOP

                FETCH myCursor INTO drop_part_name;

                IF done THEN

                        LEAVE read_loop;

                END IF;

                SET @drop_partitions = IF(@drop_partitions = "", drop_part_name, CONCAT(@drop_partitions, ",", drop_part_name));

        END LOOP;

        IF @drop_partitions != "" THEN

                /*

                   1. Build the SQL to drop all the necessary partitions.

                   2. Run the SQL to drop the partitions.

                   3. Print out the table partitions that were deleted.

                */

                SET @full_sql = CONCAT(@alter_header, @drop_partitions, ";");

                PREPARE STMT FROM @full_sql;

                EXECUTE STMT;

                DEALLOCATE PREPARE STMT;



                SELECT CONCAT(SCHEMANAME, ".", TABLENAME) AS `table`, @drop_partitions AS `partitions_deleted`;

        ELSE

                /*

                   No partitions are being deleted, so print out "N/A" (Not applicable) to indicate

                   that no changes were made.

                */

                SELECT CONCAT(SCHEMANAME, ".", TABLENAME) AS `table`, "N/A" AS `partitions_deleted`;

        END IF;

END$$

DELIMITER ;
DELIMITER $$

CREATE PROCEDURE `partition_maintenance`(SCHEMA_NAME VARCHAR(32), TABLE_NAME VARCHAR(32), KEEP_DATA_DAYS INT, HOURLY_INTERVAL INT, CREATE_NEXT_INTERVALS INT)

BEGIN

        DECLARE OLDER_THAN_PARTITION_DATE VARCHAR(16);

        DECLARE PARTITION_NAME VARCHAR(16);

        DECLARE OLD_PARTITION_NAME VARCHAR(16);

        DECLARE LESS_THAN_TIMESTAMP INT;

        DECLARE CUR_TIME INT;



        CALL partition_verify(SCHEMA_NAME, TABLE_NAME, HOURLY_INTERVAL);

        SET CUR_TIME = UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00'));



        SET @__interval = 1;

        create_loop: LOOP

                IF @__interval > CREATE_NEXT_INTERVALS THEN

                        LEAVE create_loop;

                END IF;



                SET LESS_THAN_TIMESTAMP = CUR_TIME + (HOURLY_INTERVAL * @__interval * 3600);

                SET PARTITION_NAME = FROM_UNIXTIME(CUR_TIME + HOURLY_INTERVAL * (@__interval - 1) * 3600, 'p%Y%m%d%H00');

                IF(PARTITION_NAME != OLD_PARTITION_NAME) THEN

                        CALL partition_create(SCHEMA_NAME, TABLE_NAME, PARTITION_NAME, LESS_THAN_TIMESTAMP);

                END IF;

                SET @__interval=@__interval+1;

                SET OLD_PARTITION_NAME = PARTITION_NAME;

        END LOOP;



        SET OLDER_THAN_PARTITION_DATE=DATE_FORMAT(DATE_SUB(NOW(), INTERVAL KEEP_DATA_DAYS DAY), '%Y%m%d0000');

        CALL partition_drop(SCHEMA_NAME, TABLE_NAME, OLDER_THAN_PARTITION_DATE);



END$$

DELIMITER ;
DELIMITER $$

CREATE PROCEDURE `partition_verify`(SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), HOURLYINTERVAL INT(11))

BEGIN

        DECLARE PARTITION_NAME VARCHAR(16);

        DECLARE RETROWS INT(11);

        DECLARE FUTURE_TIMESTAMP TIMESTAMP;



        /*

         * Check if any partitions exist for the given SCHEMANAME.TABLENAME.

         */

        SELECT COUNT(1) INTO RETROWS

        FROM information_schema.partitions

        WHERE table_schema = SCHEMANAME AND table_name = TABLENAME AND partition_name IS NULL;



        /*

         * If partitions do not exist, go ahead and partition the table

         */

        IF RETROWS = 1 THEN

                /*

                 * Take the current date at 00:00:00 and add HOURLYINTERVAL to it.  This is the timestamp below which we will store values.

                 * We begin partitioning based on the beginning of a day.  This is because we don't want to generate a random partition

                 * that won't necessarily fall in line with the desired partition naming (ie: if the hour interval is 24 hours, we could

                 * end up creating a partition now named "p201403270600" when all other partitions will be like "p201403280000").

                 */

                SET FUTURE_TIMESTAMP = TIMESTAMPADD(HOUR, HOURLYINTERVAL, CONCAT(CURDATE(), " ", '00:00:00'));

                SET PARTITION_NAME = DATE_FORMAT(CURDATE(), 'p%Y%m%d%H00');



                -- Create the partitioning query

                SET @__PARTITION_SQL = CONCAT("ALTER TABLE ", SCHEMANAME, ".", TABLENAME, " PARTITION BY RANGE(`clock`)");

                SET @__PARTITION_SQL = CONCAT(@__PARTITION_SQL, "(PARTITION ", PARTITION_NAME, " VALUES LESS THAN (", UNIX_TIMESTAMP(FUTURE_TIMESTAMP), "));");



                -- Run the partitioning query

                PREPARE STMT FROM @__PARTITION_SQL;

                EXECUTE STMT;

                DEALLOCATE PREPARE STMT;

        END IF;

END$$

DELIMITER ;

3.4.3 Using the stored procedures

你不需要理解这些是怎么工作的,下面是一个例子:

CALL partition_maintenance('<zabbix_db_name>', '<table_name>', <days_to_keep_data>, <hourly_interval>, <num_future_intervals_to_create>);

下面是一个例子

mysql> CALL partition_maintenance('zabbix', 'history', 28, 24, 14);

+-----------------------------------------------------------+

| msg                                                       |

+-----------------------------------------------------------+

| partition_create(zabbix,history,p201404160000,1397718000) |

+-----------------------------------------------------------+

1 row in set (0.39 sec)

……

…..

3.4.4 Improving procedure calls

DELIMITER $$

CREATE PROCEDURE `partition_maintenance_all`(SCHEMA_NAME VARCHAR(32))

BEGIN

                CALL partition_maintenance(SCHEMA_NAME, 'history', 30, 24, 30);

                CALL partition_maintenance(SCHEMA_NAME, 'history_uint', 30, 24, 30);

                CALL partition_maintenance(SCHEMA_NAME, 'trends', 720, 1440, 30);

                CALL partition_maintenance(SCHEMA_NAME, 'trends_uint', 720, 1440, 30);

END$$

DELIMITER ;

然后统一调用

mysql> CALL partition_maintenance_all('zabbix');

+----------------+--------------------+

| table          | partitions_deleted |

+----------------+--------------------+

| zabbix.history | N/A                |

+----------------+--------------------+

1 row in set (0.01 sec)



....

....

....

3.4.5 Housekeeper changes

使用zabbix2.0.x的用户比较悲惨,自己体会吧

Zabbix2.2以上版本

进入zabbix页面,"Administration" -> "General" section.选择Housekeeping

3.4.5 All done

00 00 29 * * mysql zabbix -e "CALL partition_maintenance_all('zabbix');"

 

参考文档:

https://www.zabbix.org/wiki/Docs/howto/mysql_partition

 

为了方便大家交流,本人开通了微信公众号,和QQ群291519319。喜欢技术的一起来交流吧

© 著作权归作者所有

IT--小哥
粉丝 47
博文 148
码字总数 151015
作品 0
东城
数据库管理员
私信 提问
Zabbix优化:数据库表分区

参考:https://www.zabbix.org/wiki/Docs/howto/mysqlpartition(官网) 当zabbix监控的服务器数量越来越多,就会报“Zabbix housekeeper processes more than 75% busy”这个错误 机器数量多...

jeff_1
2017/12/28
0
0
zabbix history分区 or TokuDB

http://tengxiansheng.blog.51cto.com/10693373/1939178 实施过程 http://lihuipeng.blog.51cto.com/3064864/1561221 逻辑 一天创建一个分区,分区比目前时间少7天的删除掉,等于说保留一个礼...

liqius
2017/10/31
0
0
zabbix 一次惨痛的分区经历

问题 今天来到公司,像往常一样打开zabbix,突然发现没有数据被收集,于是快速检查了其他的图形,发现都没有数据,于是定位到了服务端的问题,检查日志发现:zabbix日志突然报查询history和h...

滕先生
2018/06/27
0
0
zabbix 遇到的一些异常

Session terminated, re-login, please 用几天前备份的数据库来恢复zabbix数据库,恢复后用任何用户登录都会变成guest用户。 解决:调时间或者等第二天自己变好 注释掉' AND s.userid=u.user...

Leyin
2016/12/30
0
0
zabbix企业应用之low level discovery监控磁盘吞吐量与iops

最近比较忙,所以很久没有写博客了,现在分享一下如何使用lld来监控磁盘的吞吐量与iops。 之前使用zabbix监控磁盘的时候,都是固定的磁盘,比如sda、sdb什么的,然后不同监控磁盘名做不同的模...

赵小宾
2015/05/19
939
0

没有更多内容

加载失败,请刷新页面

加载更多

CSS盒子模型

一、什么叫框模型 页面元素皆为框(盒子) 定义了元素框处理元素内容,内边距,外边距以及边框的计算方式 二、外边距 围绕在元素边框外的空白距离(元素与元素之间的距离) 语法:margin,定...

wytao1995
今天
4
0
Replugin借助“UI进程”来快速释放Dex

public static boolean preload(PluginInfo pi) { if (pi == null) { return false; } // 借助“UI进程”来快速释放Dex(见PluginFastInstallProviderProxy的说明) return PluginFastInsta......

Gemini-Lin
今天
4
0
Hibernate 5 的模块/包(modules/artifacts)

Hibernate 的功能被拆分成一系列的模块/包(modules/artifacts),其目的是为了对依赖进行独立(模块化)。 模块名称 说明 hibernate-core 这个是 Hibernate 的主要(main (core))模块。定义...

honeymoose
今天
4
0
精华帖

第一章 jQuery简介 jQuery是一个JavaScript库 jQuery具备简洁的语法和跨平台的兼容性 简化了JavaScript的操作。 在页面中引入jQuery jQuery是一个JavaScript脚本库,不需要特别的安装,只需要...

流川偑
今天
7
0
语音对话英语翻译在线翻译成中文哪个方法好用

想要进行将中文翻译成英文,或者将英文翻译成中文的操作,其实有一个非常简单的工具就能够帮助完成将语音进行翻译转换的软件。 在应用市场或者百度手机助手等各大应用渠道里面就能够找到一款...

401恶户
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部