本节内容:
监控的shell脚本。
说明:
监控脚本在 rhel5 下测试正常,其它版本的请自行测试。
代码:
1 #监控mysql 主从复制 2 cat chk_mysql_rep.sh 3 #!/bin/bash 4 # 5 #script_name:chk_mysql_rep.sh 6 #check mysql replication 7 # 8 #ssh root@xen "/usr/local/mysql/bin/mysql -uroot -pdongnan -e 'show slave status\G' -ss" | awk '/Running:/ {print $2}' 9 #Yes 10 #Yes 11 # 12 #variables 13 ssh=/usr/bin/ssh 14 sh_dir=/root/sh/ 15 crondir=${sh_dir}crontab 16 source ${sh_dir}CONFIG 17 hosts="$DB_SLAVE_HOSTS" 18 #main 19 #主循环遍历机器 20 for HOST in $hosts;do 21 log=$crondir/log/mysql_replication_error.log 22 key=$($ssh root@$HOST "/usr/local/mysql/bin/mysql -uroot -pdongnan -e 'show slave status\G' -ss" | awk '/Running:/ {printf $2}') 23 #无法连接的主机,跳过本次循环 24 test -z "$key" && continue 25 #返回结果真 26 if [ "$key" == "YesYes" ];then 27 #flag真,解除报警 28 if [ -f "${crondir}/log/$HOST.mysql" ];then 29 #sms 30 #for mobile in $MOBILES;do 31 #echo "$HOST replication ok" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode 32 #done 33 #mail 34 for mail in $MAILS;do 35 echo "$HOST replication ok" | mail -s "$HOST replication ok" $mail 36 done 37 #flag 38 rm -f "${crondir}/log/$HOST.mysql" 39 fi 40 #返回结果假 41 else 42 check_date=$(date '+ %F %T') 43 #flag假,报警 44 if [ ! -f "${crondir}/log/$HOST.mysql" ];then 45 #sms 46 #for mobile in $MOBILES;do 47 #echo "$HOST replication error" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode 48 #done 49 #mail 50 for mail in $MAILS;do 51 echo "$HOST replication error" | mail -s "$HOST replication error" $mail 52 done 53 #flag 54 echo "replication error" >"${crondir}/log/$HOST.mysql" 55 #log 56 echo "$check_date $HOST mysql replicaton error" >> $log 57 fi 58 fi 59 # 60 done 61 #