#!/bin/bash

#Author:RELEARN

#check MySQL and MYSQL_SLAVE STATUS

MYSQL_PORT=`netstat -an | grep "LISTEN" | grep "3306" | awk -F[:" "]+ '{print $5}'`

MYSQL_IP=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F[:" "]+ '{print $4}'`

STATUS=`/usr/bin/mysql -uroot -proot -e "show slave status\G" | grep -i "running"`

IO_PROC=`echo $STATUS | grep IO | awk '{print $2}'`

SQL_PROC=`echo $STATUS | grep SQL | awk '{print $2}'`

if [ "$MYSQL_PORT" == "3306" ]

then

       echo `date +%Y.%m.%d-%T` "Mysql is running"

else

       echo `date +%Y.%m.%d-%T` "Error Mysql is not running"

       /usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.163.com -u "MySQL Service Alert" -xu xyxsender -xp 123456 -m "Service:MySQL is stop\n\nAddress:$MYSQL_IP\n\nDate/Time: `date`"

fi


if [ "$IO_PROC" = "Yes" -a "$SQL_PROC" = "Yes" ]

then

       echo `date +%Y.%m.%d-%T` "Slave is running"

else

       echo `date +%Y.%m.%d-%T` "Error MySQL_Slave is not running"

       /usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.163.com -u "MySQL Service Alert" -xu xyxsender -xp 123456 -m "Service:MySQL_Slave Error\n\nAddress:$MYSQL_IP\n\nDate/Time: `date`"

fi

echo ""