mysql 效能收集

wenaini發表於2009-02-05
mysql的效能問題監控一向比較簡單,除了slow log和show processlist就沒有什麼特別好的辦法,下面的指令碼可以幫助收集資料庫一段時間內的行為,還可以匯入表中,做成圖表,方便判斷資料庫的效能和容量趨勢。[@more@]

touch lastdat.txt
touch tempdat.txt
touch mystat.sh
chmod 755 mystat.sh

mysqladmin extended-status |grep -E 'Bytes_received>|Bytes_sent>|Com_delete>|Com_insert>|Com_select>|Com_update>|Connections>'|awk '{print $4}'>/root/tempdat.txt
byr=`sed -n '1p' /root/tempdat.txt`
bys=`sed -n '2p' /root/tempdat.txt`
del=`sed -n '3p' /root/tempdat.txt`
ins=`sed -n '4p' /root/tempdat.txt`
sel=`sed -n '5p' /root/tempdat.txt`
upd=`sed -n '6p' /root/tempdat.txt`
conn=`sed -n '7p' /root/tempdat.txt`
ct=`wc -l /root/lastdat.txt|awk '{print $1}'`
if [ $ct -eq 0 ]
then
echo $byr>/root/lastdat.txt
echo $bys>>/root/lastdat.txt
echo $del>>/root/lastdat.txt
echo $ins>>/root/lastdat.txt
echo $sel>>/root/lastdat.txt
echo $upd>>/root/lastdat.txt
echo $conn>>/root/lastdat.txt
else
val_new=$byr
val_old=`sed -n '1p' /root/lastdat.txt`
valbyr=`expr $val_new - $val_old `
valbyr=`expr $valbyr / 1024`

val_new=$bys
val_old=`sed -n '2p' /root/lastdat.txt`
valbys=`expr $val_new - $val_old `
valbys=`expr $valbys / 1024`

val_new=$del
val_old=`sed -n '3p' /root/lastdat.txt`
valdel=`expr $val_new - $val_old `

val_new=$ins
val_old=`sed -n '4p' /root/lastdat.txt`
valins=`expr $val_new - $val_old `

val_new=$sel
val_old=`sed -n '5p' /root/lastdat.txt`
valsel=`expr $val_new - $val_old `

val_new=$upd
val_old=`sed -n '6p' /root/lastdat.txt`
valupd=`expr $val_new - $val_old `

val_new=$conn
val_old=`sed -n '7p' /root/lastdat.txt`
valconn=`expr $val_new - $val_old `

echo `date +"%Y-%m-%d %k:%M:%S"`,$valbyr,$valbys,$valdel,$valins,$valsel,$valupd,$valconn>>/root/`hostname`.csv
echo $byr>/root/lastdat.txt
echo $bys>>/root/lastdat.txt
echo $del>>/root/lastdat.txt
echo $ins>>/root/lastdat.txt
echo $sel>>/root/lastdat.txt
echo $upd>>/root/lastdat.txt
echo $conn>>/root/lastdat.txt
fi

--------------------------------------------------------------------------

#catchsql.sh
log_dir=/usr/checklog
interval=3

#每3秒執行一次

wtlimit=10

#抓取執行時間超過10秒的sql

maxtime=20

#最慢SQL的時間報警限制

trap "exit;exit" 2
while [ 1 -eq 1 ]
do
mysql -e "show full processlist" |awk '$2!~/'"(system)"'/ {print $0}'|sed -n '2,$p'|sort -n -k 6 -r|awk '$6>'"$wtlimit"'&&$7!="NULL"&&$6!="NULL"&&$5!="Sleep"&&$5!="Binlog" {print $0}'>$log_dir/topsql.log
ct=`cat $log_dir/topsql.log|wc -l`
if [ $ct -gt 0 ]
then
echo '-------------------------------------------------------------------------------------------------------------------------------------------------------'>>$log_dir/slowsql.log
echo `date "+%Y-%m-%d %H:%M:%S"`>>$log_dir/slowsql.log
echo '-------------------------------------------------------------------------------------------------------------------------------------------------------'>>$log_dir/slowsql.log
cat $log_dir/topsql.log| tee -a >>$log_dir/slowsql.log
mt=`cat $log_dir/topsql.log |sed -n '1p'|awk '{print $6}'`
if [ $mt -gt $maxtime ]
then
echo "-----------------------------------------------------------------------------------------------">>$log_dir/`date +%Y%m%d`.log
echo "`date +%k:%M:%S` Error:some SQL elapse too long ,pls check slowsql.log.Time Threshold: $maxtime">>$log_dir/`date +%Y%m%d`.log
echo "-----------------------------------------------------------------------------------------------">>$log_dir/`date +%Y%m%d`.log
cat $log_dir/topsql.log>>$log_dir/`date +%Y%m%d`.log
fi
fi
sleep $interval
done

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/79686/viewspace-1016769/,如需轉載,請註明出處,否則將追究法律責任。

相關文章