监控mysql主从健康状态shell脚本

  1. #!/bin/bash
  2. #define mysql variable
  3. mysql_user="root"
  4. mysql_pass="123456"
  5. email_addr="slave@centos.bz"
  6.  
  7. mysql_status=`netstat -nl | awk 'NR>2{if ($4 ~ /.*:3306/) {print "Yes";exit 0}}'`
  8. if [ "$mysql_status" == "Yes" ];then
  9.         slave_status=`mysql -u${mysql_user} -p${mysql_pass} -e"show slave status\G" | grep "Running" | awk '{if ($2 != "Yes") {print "No";exit 1}}'`
  10.         if [ "$slave_status" == "No" ];then
  11.                 echo "slave is not working!"
  12.                 [ ! -f "/tmp/slave" ] && echo "Slave is not working!" | mail -s "Warn!MySQL Slave is not working" ${email_addr}
  13.                 touch /tmp/slave
  14.         else
  15.                 echo "slave is working."
  16.                 [ -f "/tmp/slave" ] && rm -f /tmp/slave
  17.         fi
  18.         [ -f "/tmp/mysql_down" ] && rm -f /tmp/mysql_down
  19. else
  20.         [ ! -f "/tmp/mysql_down" ] && echo "Mysql Server is down!" | mail -s "Warn!MySQL server is down!" ${email_addr}
  21.         touch /tmp/mysql_down
  22. fi

此脚本首先判断mysql服务器是否运行,如果正常,继续判断主从,否则发邮件告警,只发一次。
判断主从状态是判断IO和SQL线程是否都为yes,如果不是则发邮件通知,只发一次。

标签:MySQLShell监控 发布于:2019-11-22 22:09:03