先将show slave status的结果转换成字典,再取key对应的值去判断相关属性的状态
#!/bin/bash
HOSTNAME="XXXX"
PORT="3306"
USERNAME="XXXX"
PASSWORD="XXXXX"
DBNAME="mysql"
use_db_sql="use ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${use_db_sql}"
select_sql="show slave status\G;"
state=`mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"`
ret=${state//: /:}
####echo $ret
####下面是将字符串分割成数组
OLD_IFS="$IFS"
IFS=" "
arr=($ret)
IFS="$OLD_IFS"
####字典必须先声明
declare -A dic
OLD_IFS="$IFS"
IFS=":"
for s in ${arr[@]}
do
arr2=($s)
##echo ${#arr2[@]} ":count"
if [ ${#arr2[@]} -eq 2 ];then
dic[${arr2[0]}]=${arr2[1]}
else
dic[${arr2[0]}]=""
fi
done
IFS="$OLD_IFS"
io_run=${dic["Slave_IO_Running"]}
sql_run=${dic["Slave_SQL_Running"]}
str="Yes"
if [ "$io_run" != "$str" ] || [ "$sql_run" != "$str" ];then
####发送邮件通知
sh sendmail.sh
fi
#####sendmail.sh
#!/bin/bash
/usr/sbin/sendmail -t <<EOF
From: XXXXXXX<XXX@XXXXX.com>
To: XXX@XXX.com
Cc: XXX@XXX.com
Subject: mysql slave monitor
----------------------------------
mysql slave server exception!
---------------------------------
EOF