您好,登錄后才能下訂單哦!
走zabbix的1.6版本開始測試,1.8的版本開始線上使用,線上使用過1.9、2.0、2.2、3.0、4.0的版本,使用或是測試過zabbix1.6之后的所有版本。個人也有之前的SA轉變為DBA,就zabbix的運維走數據庫層面有一些自己的心得,希望對讀者有所幫助。
MySQL5.7及以上版本,便捷的在線DDL方便zabbix的快速升級
鏈接數據庫方式:zabbix的server、proxy、MySQL數據庫盡量使用域名方式連接,方便進行故障切換。
讀寫權限,用作zabbix自身訪問:
grant all privileges on zabbix. to 'zabbix'@'1.1.1.1' identified by 'zabbix';
只讀權限,用作二次開發只讀zabbix數據庫:
grant SELECT on zabbix. to 'zabbix_ro'@'1.1.1.1' identified by 'zabbixro';
innodb_log_files_in_group = 16
innodb_log_file_size = 1G
innodb_file_per_table = 1
max_allowed_packet = 64M
back_log = 1024
max-connections = 2000
sync_binlog = 0
innodb_flush_log_at_trx_commit = 0
binlog_format = row
expire_logs_days = 3
使用SQL語句更新監控項或是觸發器,提升變更效率,減少對數據庫影響:
/
update zabbix.items set delay=5 where key='icmpping';
update zabbix.items t,zabbix.functions f set f.parameter='30' where f.itemid=t.itemid and t.key='agent.ping'
/
在做二次開發時,為避免對線上監控server端的影響,盡量使用獨立的數據庫從庫讀取數據(可能有大量通過API讀取歷史數據庫等只讀操作),需要一個只讀的api接口,此時需要做zabbix數據庫讀寫分離,可以在從庫上過濾掉幾張表,打開從庫的寫權限,這幾張表主要用作用戶登錄的審計、session的記錄,否則可能登錄失敗,:
replicate-wild-ignore-table =zabbix.auditlog
replicate-wild-ignore-table =zabbix.sessions
replicate-wild-ignore-table =zabbix.ids
同時zabbix web_api做負載均衡的時候,請使用IP hash的方式,因為涉及到session的問題。
做zabbix的升級,其實是做zabbix數據庫庫表的變更,可能包含表結構的表更、索引的變更、數據的重組等操作,此時需要做一套新的數據庫集群作為線上集群的存庫存在,等數據同步完成,斷掉同步,使用新的zabbix server程序連接新的數據庫集群完成升級,升級完成后切換線上zabbix server、數據庫域名到新的server和新的數據庫上,這樣能最低限度影響正常的監控。
備份主要信息即可,監控數據和審計日志、events不需要備份,數據量太大。
歷史數據的刪除:包含監控歷史詳情數據和趨勢數據,使用分區的方式進行刪除;另外events表也要定時的清理,使用主鍵進行清理,保留5天左右的即可,并需要定期的進行碎片整理,否則監控大屏顯示會逐步變慢。
分區sql腳本下載地址:
https://www.zabbix.org/wiki/Docs/howto/mysql_partition
/ 為歷史表分區做準備。zabbix2.0/3.0 only,3.2以上的版本不需要執行 /
ALTER TABLE history_text DROP PRIMARY KEY, ADD INDEX (id), DROP INDEX history_text_2, ADD INDEX history_text_2 (itemid, id);
ALTER TABLE history_log DROP PRIMARY KEY, ADD INDEX (id), DROP INDEX history_log_2, ADD INDEX history_log_2 (itemid, id);
/ 在server的配置文件中設置HousekeepingFrequency=0,禁用zabbix自帶的清理歷史數據任務 /
/設置歷史監控數據表中的時間字段為索引,方便后續按照時間取值二次開發/
ALTER TABLE history ADD INDEX clock (clock);
ALTER TABLE history_log ADD INDEX clock (clock);
ALTER TABLE history_str ADD INDEX clock (clock);
ALTER TABLE history_str_sync ADD INDEX clock (clock);
ALTER TABLE history_sync ADD INDEX clock (clock);
ALTER TABLE history_text ADD INDEX clock (clock);
ALTER TABLE history_uint ADD INDEX clock (clock);
ALTER TABLE history_uint_sync ADD INDEX clock (clock);
ALTER TABLE trends ADD INDEX clock (clock);
ALTER TABLE trends_uint ADD INDEX clock (clock);
ALTER TABLE EVENTS ADD INDEX objectid (objectid,VALUE,clock);
/ 如果存在這五個存儲過程則刪除 /
DROP PROCEDURE IF EXISTS partition_create;
DROP PROCEDURE IF EXISTS partition_drop;
DROP PROCEDURE IF EXISTS partition_maintenance;
DROP PROCEDURE IF EXISTS partition_maintenance_all;
DROP PROCEDURE IF EXISTS partition_verify;
/第一次執行存儲過程/
CALL partition_maintenance_all('zabbix');
/
inser to crontab 每天執行一次存儲過程,生成新的分區和刪除老的分區
#!/bin/sh
/xxxxx/mysql/bin/mysql -uzabbix -pzabbixmonitor --socket=/xxxx/mysql/tmp/mysql.sock --database=zabbix -e "CALL partition_maintenance_all('zabbix');"
/
存儲過程詳情如下:
/ partition_create /
DELIMITER $$
CREATE PROCEDUREpartition_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 ;/ partition_drop /
DELIMITER $$
CREATE PROCEDUREpartition_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 ;/ partition_maintenance /
DELIMITER $$
CREATE PROCEDUREpartition_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 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'); CALL partition_create(SCHEMA_NAME, TABLE_NAME, PARTITION_NAME, LESS_THAN_TIMESTAMP); SET @__interval=@__interval+1; 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 ;/ partition_verify /
DELIMITER $$
CREATE PROCEDUREpartition_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 ;/ partition_maintenance_all /
DELIMITER $$
CREATE PROCEDUREpartition_maintenance_all
(SCHEMA_NAME VARCHAR(32))
BEGIN
CALL partition_maintenance(SCHEMA_NAME, 'history', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_log', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_str', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_text', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_uint', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'trends', 730, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'trends_uint', 730, 24, 14);
END$$
DELIMITER ;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。