mysql主从监控

#!/bin/bash   
#Check MySQL Slave's Runnning Status  
#Crontab time 00:10




mysql_user="root"
mysql_pass="*********"
email_addr="******@qq.com"


  
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`  
MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'`  
STATUS=$(/usr/bin/mysql  -u${mysql_user} -p${mysql_pass} -S /var/lib/mysql/mysql.sock -e "show slave status\G" | grep -i "running")  
IO_env=`echo $STATUS | grep IO | awk  ' {print $2}'`  
SQL_env=`echo $STATUS | grep SQL | awk  '{print $2}'`  
DATA=`date +"%y-%m-%d %H:%M:%S"`  
  
function checkMysqlStatus(){  
    if [ "$MYSQLPORT" == "3306" ]  
    then  
        /usr/bin/mysql -uroot -pQesyNEvmMoLH --connect_timeout=5 -e "show databases;" &>/dev/null 2>&1  
        if [ $? -ne 0 ]  
        then  
            echo "Server: $MYSQLIP mysql is down, please try to restart mysql by manual!" > /var/log/mysqlerr    
            mail -s "WARN! server: $MYSQLIP  mysql is down." ${email_addr} < /var/log/mysqlerr  
        else  
            echo "mysql is running..."  
        fi  
    else  
        mail -s "WARN!Server: $MYSQLIP mysql is down." ${email_addr}  
    fi  
}  
   
checkMysqlStatus  
  
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]  
then  
  echo "MySQL Slave is running!"  
else  
  echo "####### $DATA #########">> /home/logs/check_mysql_slave.log  
  echo "MySQL Slave is not running!" >> /home/logs/check_mysql_slave.log  
  echo "MySQL Slave is not running!" | mail -s "WARN! $MYSQLIP MySQL Slave is not running." ${email_addr}  
fi 

你可能感兴趣的:(mysql主从监控)