MySQL:shell手动监控db存活及slave状态

来源:互联网 发布:古天乐 希望小学 知乎 编辑:程序博客网 时间:2024/05/10 21:16

1、所有db信息

all_db.txt

192.168.x.x     3306 S192.168.x.x     3306 M

2、检查脚本

check_all_db.sh

#!/bin/bashuser='' password=''errFile=/tmp/sms_mon_db.txtcall(){for phone in 186xxxxdo    curl "短信接口:{$ip}+{$port}+{$stat}"    echo "[ `date +"%Y-%m-%d %H:%M:%S"` ] {$ip}+{$port}+{$stat}"| tee -a $errFiledone}dbcount=`cat /shell/all_db.txt |grep -v '^#'|grep -v '^$'| wc -l` #检查数据库是否存活for((i=1;i<=$dbcount;i++))do  ip=`cat /shell/all_db.txt |grep -v '^#'|grep -v '^$'|awk -F" " '{print $1}'|sed -n "$i p"`  port=`cat /shell/all_db.txt |grep -v '^#'|grep -v '^$'|awk -F" " '{print $2}'|sed -n "$i p"`  dbstat=`/usr/local/mysql/bin/mysqladmin -u$user -p$password -h$ip -P$port ping |grep alive|wc -l`  stat='down'  if [ $dbstat -ne 1 ]        then call  fidone#检查slave状态for((i=1;i<=$dbcount;i++))do  ip=`cat /shell/all_db.txt |grep -v '^#'|grep -v '^$'|awk -F" " '{print $1}'|sed -n "$i p"`  port=`cat /shell/all_db.txt |grep -v '^#'|grep -v '^$'|awk -F" " '{print $2}'|sed -n "$i p"`  is_slave=`cat /shell/all_db.txt |grep -v '^#'|grep -v '^$'|awk -F" " '{print $3}'|sed -n "$i p"`    if [ $is_slave = 'S' ]    then      status_array=($(/usr/local/mysql/bin/mysql -u$user -p$password -h$ip -P$port -e "show slave status\G"|grep Running|awk -F : '{print $2}'))      stat='slave+error'        if [ "${status_array[0]}" != "Yes" ] || [ "${status_array[1]}" != "Yes" ]      then call      fi  fidone


0 0