vps mysql 主从监测

来源:互联网 发布:淘宝客软件是诈骗吗 编辑:程序博客网 时间:2024/04/30 04:43
#!/bin/sh#check MySQL_Slave Status#crontab time 00:10function mss(){echo $(mysql -e "show slave status \G" | awk -F':[ \t]*' '{IGNORECASE=1} /\<'$1'\>/ {print $2}')return 0}email="123456789@qq.com"master_ip="12,34,56,789"master_log_dir="/var/lib/mysql/"ssh_rsa="/root/.ssh/id_rsa"mysql_port=$(netstat -na | grep -c ':\s*\<3306\>.*\<LISTEN\>')mysql_ip=$(hostname -i)mysql_restart='/etc/init.d/mysql restart'master_log_file=$(mss 'master_log_file')real_master_log_pos="ssh -i ${ssh_rsa} root@${master_ip}"' '"[ -f ${master_log_dir}${master_log_file} ] && mysqlbinlog --no-defaults ${master_log_dir}${master_log_file} | awk '/# at/' | tail -n 1 "' '"| awk '{print \$3}'"now="date -d today +%F%t%T"if [[ $mysql_port -le 0 ]];thenecho $($mysql_restart)waitif [[ $mysql_port -le 0 ]];thenmail -s "Replication: ${mysql_ip} mysql-server is down ..." "$email"fifiif [[ $(mss 'slave_io_running') != 'Yes' ]] || [[ $(mss 'slave_sql_running') != 'Yes' ]];then  if [[ $(mss 'last_io_errno') -eq 1236 ]];then[ -n $($real_master_log_pos) ] && mysql -e "stop slave;change master to master_log_pos=$($real_master_log_pos);start slave;"elseecho $($mysql_restart)fifiif [[ $(expr $(date -d today +%k) % 6) -eq 0 ]];thenfor((i=1;i<=5;i++));dosleep 60if [[ $(mss 'slave_io_running') = 'Yes' ]] && [[ $(mss 'slave_sql_running') = 'Yes' ]];thenbreakelse if [[ $i -eq 5 ]];then   echo "$($now)Slave is not running ..." | mail -s "Replication: ${mysql_ip} slave-status is weak ..." "$email"fifidonefiexit 0


原创粉丝点击