shell監控腳本-監控mysql 主從複製
注意:請先參考 shell監控腳本-準備工做,監控腳本在 rhel5 下測試正常,其它版本的linux 系統請自行測試
#監控mysql 主從複製mysql
cat chk_mysql_rep.sh linux
#!/bin/bash sql
# shell
#script_name:chk_mysql_rep.sh bash
#check mysql replication 運維
# ssh
#last update 20130320 by dongnan ide
#bbs# http://bbs.ywwd.net/ 測試
#blog# http://dngood.blog.51cto.com spa
#
#ssh root@xen "/usr/local/mysql/bin/mysql -uroot -pdongnan -e 'show slave status\G' -ss" | awk '/Running:/ {print $2}'
#Yes
#Yes
#
#variables
ssh=/usr/bin/ssh
sh_dir=/root/sh/
crondir=${sh_dir}crontab
source ${sh_dir}CONFIG
hosts="$DB_SLAVE_HOSTS"
#main
#主循環遍歷機器
for HOST in $hosts;do
log=$crondir/log/mysql_replication_error.log
key=$($ssh root@$HOST "/usr/local/mysql/bin/mysql -uroot -pdongnan -e 'show slave status\G' -ss" | awk '/Running:/ {printf $2}')
#沒法鏈接的主機,跳過本次循環
test -z "$key" && continue
#返回結果然
if [ "$key" == "YesYes" ];then
#flag真,解除報警
if [ -f "${crondir}/log/$HOST.mysql" ];then
#sms
#for mobile in $MOBILES;do
#echo "$HOST replication ok" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
#done
for mail in $MAILS;do
echo "$HOST replication ok" | mail -s "$HOST replication ok" $mail
done
#flag
rm -f "${crondir}/log/$HOST.mysql"
fi
#返回結果假
else
check_date=$(date '+ %F %T')
#flag假,報警
if [ ! -f "${crondir}/log/$HOST.mysql" ];then
#sms
#for mobile in $MOBILES;do
#echo "$HOST replication error" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
#done
for mail in $MAILS;do
echo "$HOST replication error" | mail -s "$HOST replication error" $mail
done
#flag
echo "replication error" >"${crondir}/log/$HOST.mysql"
#log
echo "$check_date $HOST mysql replicaton error" >> $log
fi
fi
#
done
#
結束更多請: linux 系統運維 37275208 vmware 虛擬化 166682360