prometheus 统计MySQL 自增主键的剩余可用百分比

mysqld_exporter自带的这个功能,下面是我使用的启动参数:

nohup ./mysqld_exporter --config.my-cnf="./my.cnf" --web.listen-address=":9104" --collect.heartbeat --collect.auto_increment.columns --collect.binlog_size --collect.engine_innodb_status --collect.engine_tokudb_status --collect.slave_hosts --collect.slave_status --collect.info_schema.processlist --collect.info_schema.innodb_metrics > /dev/null 2>&1 &

红色高亮的参数,就是用来采集到自增id的使用情况的。

实际上执行的类似这个SQL:
SELECT
table_schema,
table_name,
column_name,
AUTO_INCREMENT,
c.COLUMN_TYPE,
POW(2, CASE data_type
WHEN 'tinyint' THEN 7
WHEN 'smallint' THEN 15
WHEN 'mediumint' THEN 23
WHEN 'int' THEN 31
WHEN 'bigint' THEN 63
END+(column_type LIKE '% unsigned'))-1 AS max_int
FROM information_schema.tables t
JOIN information_schema.columns c USING (table_schema,table_name)
WHERE
c.extra = 'auto_increment'
AND
t.TABLE_SCHEMA NOT IN ('information_schema','mysql', 'sys','test','performance_schema')
AND
t.auto_increment IS NOT NULL ;


在prometheus的web界面,我们可以测试编写如下的promql, 找出剩余自增id可以率少于40%的实例的库+表名
(mysql_info_schema_auto_increment_column_max{schema!~'test|mysql'} - mysql_info_schema_auto_increment_column{schema!~'test|mysql'})/mysql_info_schema_auto_increment_column_max{schema!~'test|mysql'}*100 < 40

在zabbix取值,用当时id/最大值的占比,然后通过grafana去取值,红色框里的值是每个表对应的自增ID值占比

posted @ 2020-01-20 18:01  __Yoon  阅读(321)  评论(0编辑  收藏  举报