监控mysql跟heartbeat的脚本

前景:
很多公司的mysql服务器(假设2台)之间都是用ha来做切换,可是ha只有在服务器down机跟ha 停掉的情况下才切换,而mysql down掉,服务器没down掉的时候不去切换,那么以下这个脚本可以实现这一功能。

说明:
msyql开启的是tcp 3306端口,ha开启的是udp 694端口

功能:
检测mysql跟ha的状态
1.当mysql 正常,ha正常,打印报告
2.当mysql正常,ha down掉,重启ha,启动成功,打印报告;启动失败发送邮件到管理员邮箱
3.当mysql down掉,而ha正常时,停掉ha,重启mysql,并发送邮件给管理员,当mysql重启成功,再启动ha,并发送邮件给管理员报告启动成功,重启不成功发送邮件报告重启失败
4.当msyql和ha都down掉,重启mysql跟ha,成功打印报告,失败发送邮件给管理员报告mysql跟ha都down掉
5.sleep 10==》10秒钟执行一次
引用:
#!/bin/sh
cat << EOF
+--------------------------------------------------------------------------+
| === Welcome to LinuxTone=== |
|------------------------- http://www.linuxtone.org------------------------ |
+-------------------------------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 www.linuxtone.org|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 protected] < /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 protected] < /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 protected] < /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 protected] < /var/log/mysql_ha.log
fi
fi
fi
sleep 10
done


你可能感兴趣的:(监控mysql跟heartbeat的脚本)