- 論壇徽章:
- 0
|
寫得不好,大家拍磚
自己寫了個shell去監(jiān)控mysql跟ha的狀態(tài)……
前景:
很多公司的mysql服務(wù)器(假設(shè)2臺)之間都是用ha來做切換,可是ha只有在服務(wù)器down機跟ha 停掉的情況下才切換,而mysql down掉,服務(wù)器沒down掉的時候不去切換,所以我寫了這個腳本
說明:
msyql開啟的是tcp 3306端口,ha開啟的是udp 694端口
功能:
檢測mysql跟ha的狀態(tài)
1.當mysql 正常,ha正常,打印報告
2.當mysql正常,ha down掉,重啟ha,啟動成功,打印報告;啟動失敗發(fā)送郵件到管理員郵箱
3.當mysql down掉,而ha正常時,停掉ha,重啟mysql,并發(fā)送郵件給管理員,當mysql重啟成功,再啟動ha,并發(fā)送郵件給管理員報告啟動成功,重啟不成功發(fā)送郵件報告重啟失敗
4.當msyql和ha都down掉,重啟mysql跟ha,成功打印報告,失敗發(fā)送郵件給管理員報告mysql跟ha都down掉
5.sleep 10==》10秒鐘執(zhí)行一次
- #!/bin/sh
- cat << EOF
- +-----------------------------------------------------------------------------+
- | === Welcome to LinuxTone=== |
- |---------------------[url]http://www.linuxtone.org[/url]-------------------------|
- +-------------------------------By:hamgua--------------------------------+
- EOF
- MYPORT=`netstat -na|grep "tcp"|grep "3306"|awk -F[:" "]+ '{print $5}'`
- HAPORT=`netstat -na|grep "udp"|grep "694"|awk -F[:" "]+ '{print $5}'`
- PING=`ping -c 5 [url]www.linuxtone.org[/url]|awk -F, '/packets/{print $3}'|cut -c 2-|awk '{print $1}'`
- DB1IP=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-`
- #DB2IP is your next mysqlserver and haserver IP,According to it own IP to fill
- while [ "$PING" != "100\%" ]
- do
- touch /var/log/mysql_ha.log
- if [ "$MYPORT" == "3306"];then
- if [ "$HAPORT" == "694" ];then
- echo "$DB1IPmysql and ha is running......"
- else
- echo "$DB1IPmysql is running,but ha is down,start ha now"
- /etc/rc.d/init.d/heartbeat start
- if [ "$HAPORT" == "694"];then
- echo "$DB1IPha start successful,mysql and ha all running......"
- else
- echo "$DB1IPha is down,let (DB2IP) to take over mysql,please start ha now!" > /var/log/mysql_ha.log
- mail -s "mysql and ha warning!server: $DB1IP ha is down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
- fi
- fi
- else
- if [ "$HAPORT" == "694" ];then
- echo "$DB1IP mysql is down,but ha is runing,now shutdown ha,let (DB2IP) to take over mysql,then restart mysql....." > /var/log/mysql_ha.log
- mail -s "mysql and ha warning!server: $DB1IP mysql is down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
- /etc/rc.d/init.d/heartbeat stop
- /etc/rc.d/init.d/mysqld start
- if [ "$MYPORT" == "3306"];then
- /etc/rc.d/init.d/heartbeat start
- echo "$DB1IP$mysql restart successful,now mysql and ha all running......"
- fi
- else
- echo "$DB1IPmysql and ha all down,first restart msyql,when mysql is run,restart ha"
- /etc/rc.d/init.d/mysqld start
- if [ "$MYPORT" == "3306"];then
- /etc/rc.d/init.d/heartbeat start
- if [ "$HAPORT" == "694" ];then
- echo "$DB1IPmysql and ha all running......"
- else
- echo "$DB1IPmysql is running,ha is down,please restart ha" > /var/log/mysql_ha.log
- mail -s "mysql and ha warning!server: $DB1IP ha is down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
- fi
- else
- echo "$DB1IPmysql and ha all down,let (DB2IP) to take over mysql,please restart mysql" > /var/log/mysql_ha.log
- mail -s "mysql and ha warning!server: $DB1IP mysql and ha all down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
- fi
- fi
- fi
- sleep 10
- done
復(fù)制代碼
[ 本帖最后由 gyl4802959 于 2009-4-24 16:11 編輯 ] |
|