采集MySQL top ten 体积的表明细情况:
脚本如下:
source /etc/profile
USER='root'
PASSWD='123456'
SOCKET='/tmp/mysql.sock'
HOST='localhost'
datadir=$(/usr/local/mysql/bin/mysql 2>/dev/null -u${USER} -p${PASSWD} -h${HOST} -S ${SOCKET} -BNe 'select @@datadir')
total_size=$(du -sc $datadir | tail -1 | sed 's/total//g' | sed 's/total//g' )
IP_ADDR=$(/sbin/ip a|egrep '10.[0|1].[1-9]+.[1-9]+*' | awk '{print $2}'| cut -d '/' -f 1);
mysql -u${USER} -p${PASSWORD} -h ${HOST} -S ${SOCKET} 2>/dev/null -e "set @ip='$IP_ADDR'; set @total_size='$total_size';
SELECT @ip AS IP_ADDR,
date_format(now(),'%Y-%m-%d') AS INSERT_DATE ,
CONCAT(table_schema, '.', table_name) AS TB_NAME,
table_rows AS ROWS,
data_length AS DATA,
index_length AS idx,
( data_length + index_length ) AS total_size,
index_length / data_length AS idxfrac,
ROUND((data_length+index_length)/@total_size/1024,2) as pct
FROM information_schema.TABLES where table_schema NOT IN ('mysql','test','information_schema','heartbeat','performance_schema')
ORDER BY ( data_length + index_length ) DESC LIMIT 10 ; "
脚本里面带上了采集时间、机器的IP、datadir总大小。
我们还可以用python脚本将这些采集到的数据按行插入到远程数据库中,或者json格式上送到数据库运维平台的接口达到metrics入库的目的。