mysql复制延迟监控脚本(一)

2015-11-21 01:54:53 · 作者: · 浏览: 13
#!/bin/sh
#ocpyang@126.com
#repdelay.sh
#查看复制延迟具体多少event

#####1.juede the rep slave status 

export black='\033[0m'
export boldblack='\033[1;0m'
export red='\033[31m'
export boldred='\033[1;31m'
export green='\033[32m'
export boldgreen='\033[1;32m'
export yellow='\033[33m'
export boldyellow='\033[1;33m'
export blue='\033[34m'
export boldblue='\033[1;34m'
export magenta='\033[35m'
export boldmagenta='\033[1;35m'
export cyan='\033[36m'
export boldcyan='\033[1;36m'
export white='\033[37m'
export boldwhite='\033[1;37m'


cecho ()

## -- Function to easliy print colored text -- ##
	
	# Color-echo.
	# 参数 $1 = message
	# 参数 $2 = color
{
local default_msg="No message passed."

message=${1:-$default_msg}	# 如果$1没有输入则为默认值default_msg.
color=${2:-black}		# 如果$1没有输入则为默认值black.

case $color in
	black)
		 printf "$black" ;;
	boldblack)
		 printf "$boldblack" ;;
	red)
		 printf "$red" ;;
	boldred)
		 printf "$boldred" ;;
	green)
		 printf "$green" ;;
	boldgreen)
		 printf "$boldgreen" ;;
	yellow)
		 printf "$yellow" ;;
	boldyellow)
		 printf "$boldyellow" ;;
	blue)
		 printf "$blue" ;;
	boldblue)
		 printf "$boldblue" ;;
	magenta)
		 printf "$magenta" ;;
	boldmagenta)
		 printf "$boldmagenta" ;;
	cyan)
		 printf "$cyan" ;;
	boldcyan)
		 printf "$boldcyan" ;;
	white)
		 printf "$white" ;;
	boldwhite)
		 printf "$boldwhite" ;;
esac
  printf "%s\n"  "$message"
  tput sgr0			# tput sgr0即恢复默认值
  printf "$black"

return
}


cechon ()		

	# Color-echo.
	# 参数1 $1 = message
	# 参数2 $2 = color
{
local default_msg="No message passed."
				# Doesn't really need to be a local variable.

message=${1:-$default_msg}	# 如果$1没有输入则为默认值default_msg.
color=${2:-black}		# 如果$1没有输入则为默认值black.

case $color in
	black)
		printf "$black" ;;
	boldblack)
		printf "$boldblack" ;;
	red)
		printf "$red" ;;
	boldred)
		printf "$boldred" ;;
	green)
		printf "$green" ;;
	boldgreen)
		printf "$boldgreen" ;;
	yellow)
		printf "$yellow" ;;
	boldyellow)
		printf "$boldyellow" ;;
	blue)
		printf "$blue" ;;
	boldblue)
		printf "$boldblue" ;;
	magenta)
		printf "$magenta" ;;
	boldmagenta)
		printf "$boldmagenta" ;;
	cyan)
		printf "$cyan" ;;
	boldcyan)
		printf "$boldcyan" ;;
	white)
		printf "$white" ;;
	boldwhite)
		printf "$boldwhite" ;;
esac
  printf "%s"  "$message"
  tput sgr0			# tput sgr0即恢复默认值
  printf "$black"

return
}


####2.jude the rep delay status 

#configure rep master and slave env

masterhost="192.168.99.137"
slavehost="192.168.1.220"
mysql
_user=root #mysql的用户名 mysql_pass='123' #mysql的登录用户密码 SQLresponse=`mysql -h${slavehost} -u${mysql_user} -p${mysql_pass} mysql -e "show slave status \G" |grep '\'|awk '{print $2}'` IOresponse=`mysql -h${slavehost} -u${mysql_user} -p${mysql_pass} mysql -e "show slave status \G" |grep '\'|awk '{print $2}'` Behind=`mysql -h${slavehost} -u${mysql_user} -p${mysql_pass} mysql -e "show slave status \G" |grep '\'|awk '{print $2}'` if [ "$SQLresponse" = "No" ]; then echo " " error="mysql服务器($slavehost)的复制已经停止工作:Slave_SQL_Running: No;无法获取复制延迟情况." cechon "${error}" red echo " " exit 0 fi if [ "$IOresponse" = "No" ]; then echo " " error="mysql服务器($slavehost)的复制已经停止工作:Slave_IO_Running: No;无法获取复制延迟情况." cechon "${error}" red echo " " exit 0 fi #set mysql evn MYSQL_USER_MASTER=root MYSQL_PASS_MAS