Chinaunix首页 | 论坛 | 博客
  • 博客访问: 214607
  • 博文数量: 73
  • 博客积分: 1879
  • 博客等级: 上尉
  • 技术积分: 920
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-22 15:57
文章分类

全部博文(73)

文章存档

2012年(3)

2011年(15)

2008年(55)

分类: Mysql/postgreSQL

2008-09-03 11:09:43

#!/bin/bash
#written by zhanghy
##### Setting some variables #####
#192.168.10.20-----/tmp/mysql2.sock
#192.168.10.100----/tmp/mysql3.sock
#192.168.10.105----/tmp/mysql4.sock
mycommand=/usr/local/mysql/bin/mysql
server="192.168.10.20 192.168.10.100 192.168.10.105"
socket="/tmp/mysql2.sock /tmp/mysql3.sock /tmp/mysql4.sock"
ruser2=root
myuser1=root


##### SLAVES: Check replication #####

#check replication status
for i in $socket
    do

    tmpmysql1=$($mycommand -u $myuser1  --socket=$i -e "show slave status\G" | \
                awk 'BEGIN {
                                tel = 0
                        }
                        /Slave_IO_Running: Yes/  { tel++ }
                        /Slave_SQL_Running: Yes/ { tel++ }
                        END {
                                if ( tel == 2 ) {
                                        print "Yes"
                                } else {
                                        print "No"
                                }
                        }'
    )
done
#define display function
OutMasterLogbinPosition()
{
        echo -e "\n"
        echo  "$replication"
        echo  "Master log_bin Position:$tmp2mysql1"
}

OutSlaveRMLAndSBM()
{
        echo -e "\n"
        echo  "Slave Read_Master_Log_Pos($n):$tmp2mysql2"
        echo  "Seconds_Behind_Master:$tmp2mysql3"
}


OutErrorInfo()
{
    echo "Last_Errno($n):$tmp2mysql2"
    echo "Last_Error($n):$tmp2mysql3"
    echo -e "\n"
}
#get mysql master server logbin position 
GetMasterServerLogbinPosition()
{

    for hostname in $server
        do
        replication="REPL OK For $hostname - Replicationrocess is fine"
        tmp2mysql1=$($mycommand -u $myuser1  -h "$hostname" -e "show master status\G" | \
            awk '/Position/ {print $2}')
    OutMasterLogbinPosition   
    done
}

#get replication slave status where slave is ok
#you can get more information by set variables
GetSlaveServerStatusInfo()
{
    GetMasterServerLogbinPosition   
    for n in $socket
        do
        tmp2mysql2=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
            awk '/Read_Master_Log_Pos/ {print $2}')
        tmp2mysql3=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
            awk '/Seconds_Behind_Master/ {print $2}')
    OutSlaveRMLAndSBM   
    done
   
}
#get slave error information when slave is error
#you can get more information by set variables
GetSlaveServerErrorStatusInfo()
{
    GetMasterServerLogbinPosition   
    for n in $socket
            do
            tmp2mysql2=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
                awk '/Last_Errno/ {print $2}')

            tmp2mysql3=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
                awk '/Last_Error/ {print $2}')
        OutErrorInfo   
        done   
}
# Here you generate the part of the output
if [ $tmpmysql1 = 'Yes' ]
then
    GetSlaveServerStatusInfo   
else
    GetSlaveServerErrorStatusInfo
fi
##### END OF SCRIPT #####

阅读(1712) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-09-10 18:36:33

收藏下来,虽然我已经实现监控,保留下来也学习一下!感谢