免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5196 | 回复: 10
打印 上一主题 下一主题

监控mysql跟heartbeat的脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-24 16:10 |只看该作者 |倒序浏览
写得不好,大家拍砖

自己写了个shell去监控mysql跟ha的状态……

前景:
很多公司的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秒钟执行一次

  1. #!/bin/sh
  2. cat << EOF
  3. +-----------------------------------------------------------------------------+
  4. |                      === Welcome to LinuxTone===                          |
  5. |---------------------[url]http://www.linuxtone.org[/url]-------------------------|
  6. +-------------------------------By:hamgua--------------------------------+
  7. EOF

  8. MYPORT=`netstat -na|grep "tcp"|grep "3306"|awk -F[:" "]+ '{print $5}'`
  9. HAPORT=`netstat -na|grep "udp"|grep "694"|awk -F[:" "]+ '{print $5}'`
  10. PING=`ping -c 5 [url]www.linuxtone.org[/url]|awk -F, '/packets/{print $3}'|cut -c 2-|awk '{print $1}'`
  11. DB1IP=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-`
  12. #DB2IP is your next mysqlserver and haserver IP,According to it own IP to fill

  13. while [ "$PING" != "100\%" ]
  14. do
  15.   touch /var/log/mysql_ha.log
  16.   if [ "$MYPORT" == "3306"];then
  17.      if [ "$HAPORT" == "694" ];then
  18.         echo "$DB1IPmysql and ha is running......"
  19.      else
  20.          echo "$DB1IPmysql is running,but ha is down,start ha now"
  21.          /etc/rc.d/init.d/heartbeat start
  22.     if [ "$HAPORT" == "694"];then
  23.       echo "$DB1IPha start successful,mysql and ha all running......"
  24.     else
  25.       echo "$DB1IPha is down,let (DB2IP) to take over mysql,please start ha now!" > /var/log/mysql_ha.log
  26.       mail -s "mysql and ha warning!server: $DB1IP  ha is down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
  27.         fi
  28.      fi
  29.   else
  30.      if [ "$HAPORT" == "694" ];then
  31.          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
  32.      mail -s "mysql and ha warning!server: $DB1IP  mysql is down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
  33.          /etc/rc.d/init.d/heartbeat stop
  34.          /etc/rc.d/init.d/mysqld start
  35.         if [ "$MYPORT" == "3306"];then
  36.          /etc/rc.d/init.d/heartbeat start
  37.      echo "$DB1IP$mysql restart successful,now mysql and ha all running......"
  38.         fi
  39.      else
  40.          echo "$DB1IPmysql and ha all down,first restart msyql,when mysql is run,restart ha"
  41.          /etc/rc.d/init.d/mysqld start
  42.         if [ "$MYPORT" == "3306"];then
  43.              /etc/rc.d/init.d/heartbeat start
  44.           if [ "$HAPORT" == "694" ];then
  45.             echo "$DB1IPmysql and ha all running......"
  46.           else
  47.             echo "$DB1IPmysql is running,ha is down,please restart ha" > /var/log/mysql_ha.log
  48.             mail -s "mysql and ha warning!server: $DB1IP ha is down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
  49.           fi
  50.     else
  51.          echo "$DB1IPmysql and ha all down,let (DB2IP) to take over mysql,please restart mysql" > /var/log/mysql_ha.log
  52.          mail -s "mysql and ha warning!server: $DB1IP  mysql and ha all down" [email]hamgua@gmail.com[/email] < /var/log/mysql_ha.log
  53.         fi
  54.      fi
  55.    fi
  56. sleep 10
  57. done
复制代码

[ 本帖最后由 gyl4802959 于 2009-4-24 16:11 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-04-24 16:12 |只看该作者

回复 #1 gyl4802959 的帖子

写得很好,很强大。

论坛徽章:
0
3 [报告]
发表于 2009-04-24 16:16 |只看该作者

回复 #2 我是DBA 的帖子

多谢抬举,但我知道很繁琐,刚学

论坛徽章:
0
4 [报告]
发表于 2009-04-24 18:03 |只看该作者
不错,呵呵,学习了

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
5 [报告]
发表于 2009-06-09 11:56 |只看该作者
呵呵。用用ha crm方式。就不需要这个脚本了。

论坛徽章:
0
6 [报告]
发表于 2009-06-09 13:06 |只看该作者

不错,呵呵,学习了

不错,呵呵,学习了

论坛徽章:
0
7 [报告]
发表于 2010-09-27 14:56 |只看该作者
值得信赖的东西

论坛徽章:
0
8 [报告]
发表于 2010-09-27 15:22 |只看该作者
不错 就是还有些看不懂  正在努力中!

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
9 [报告]
发表于 2010-09-27 15:30 |只看该作者
hehe

论坛徽章:
0
10 [报告]
发表于 2010-09-27 16:27 |只看该作者
回复 3# gyl4802959


    有个要说的是, 某些时候 mysql  的端口也是开着的,但服务是不可用的 ; 最好在脚本中加入 mysql 查询,有正确返回结果,这样才算是服务可用的,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP