#!/bin/bash 

#check MySQL_Slave Status 
#crontab time 00:10 
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $4}'` 
MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'`
STATUS=$(mysql -u root  -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"` 
if [ "$MYSQLPORT" == "3306" ]
then
echo "mysql is running"
else
mail -s "warn!server: $MYSQLIP mysql is down" 18801457794@139.com
fi
if [ "$IO_env" == "Yes" -a "$SQL_env" == "Yes" ] 
then
echo "Slave is running!"
else
echo "########## $DATA ##########" >>/data/data/check_mysql_slave.log
echo "Slave is not running!" >>    /data/data/check_mysql_slave.log 
echo "Slave is not running!" | mail -s "warn! $MYSQLIP MySQL Slave is not running" 18801457794@139.com
fi