GBase 8a
运维管理
文章

GBase8a数据节点隔离及恢复自动化脚本(shell版本)

发表于2025-07-21 18:49:0466次浏览1个评论

1)隔离脚本set_node_failure.sh

#!/usr/bin/sh
###############################################################################
##creator : Tupac Tu
##create time: 2025-07-17
##Decription: 对GBase集群数据节点进行隔离操作(入参:隔离节点IP DB账号 DB密码 [VC名称])
##Method: sh set_node_failure.sh nip dbUser dbPasswd [vcname]
##Version: 1.0 
###############################################################################
#参数设置
ipAddress=$1
dbUser=$2
dbPasswd=$3
vcName=$4

set_node_failure()
{
    if ! isCmdExist gcadmin; then message error "  Failed: execute command (gcadmin) denied to the user"; exit 1; fi
    coorIp=$(gcadmin |grep "coordinator1 " |awk '{print $4}')
    
    check_password
    
    gbaseMode=$(gcadmin showcluster d|grep "virtual cluster" |wc -l)
    
    isContain=0
    if [ ${gbaseMode} -gt 0 ];then
        if [ "${vcName}" == "" ]; 
            then message error "   Failed: The GBaseMode is MultVC, the VcName parameter cannot be empty."; exit 1; 
        else
            vcList=$(gcadmin showcluster d | grep "virtual cluster" | awk -F: '{print $2}' | sed 's/,//g')
            for vcn in ${vcList}; do if [ "$vcName" == "$vcn" ]; then isContain=1; break; else isContain=0; fi done
            if [ $isContain -eq 0 ]; then message error "  Failed: The GBaseMode is MultVC, but the '$vcName' is not in the vcList ($vcList)."; exit 1; fi
            
            nodelist=$(gcadmin showcluster vc ${vcName}|grep node[1-9]|awk '{print $4}')
            for nip in ${nodelist}; do if [ "$ipAddress" == "$nip" ]; then isContain=1; break; else isContain=0; fi done
            if [ $isContain -eq 0 ]; then message error "  Failed: The node '$ipAddress' is not in the nodelist for $vcName."; exit 1; fi
            
            nodeStatus=$(gcadmin showcluster vc ${vcName}|grep "${ipAddress}" |awk '{print $8}')
            if [ "${nodeStatus}" == "FAILURE" ]; then message warn "  Waring: The node '$ipAddress' has been set to FAILURE for $vcName."; exit 1; fi
        fi
        
        vcEvent="-D${vcName}."
        vcNameStr=" vc ${vcName}"
        vcNameVal="${vcName}"
    else
        nodelist=$(gcadmin |grep node[1-9]|awk '{print $4}')
        for nip in ${nodelist}; do if [ "$ipAddress" == "$nip" ]; then isContain=1; break; else isContain=0; fi done
        if [ $isContain -eq 0 ]; then message error "  Failed: The node '$ipAddress' is not in the nodelist for cluster."; exit 1; fi
        
        nodeStatus=$(gcadmin |grep "${ipAddress}" |awk '{print $8}')
        if [ "${nodeStatus}" == "FAILURE" ]; then message warn "  Waring: The node '$ipAddress' has been set to FAILURE for cluster."; exit 1; fi
            
        vcEvent=""
        vcNameStr=""
        vcNameVal="cluster"
    fi
    
    
    nodes_pri=$($gccli_command -h$coorIp ${vcEvent} -Ns -e "show nodes" | awk '{print $2,$4}' | sed 's/,/ /g')
    nodes_all=$($gccli_command -h$coorIp ${vcEvent} -Ns -e "show nodes" | awk '{print $2,$4,$5}' | sed 's/,/ /g')
    
    array_node=()
    array_nodes_pri=(${nodes_pri})
    array_nodes_all=(${nodes_all})
    
    ## P1D2
    if [[ "${array_nodes_all[3]}" =~ "n"  ]];then 
        for((i=0;i<${#array_nodes_all[@]};i+=4))
        do    
            ip_pri=${array_nodes_all[i]}
            n_pri=${array_nodes_all[i+1]}
            n_bak1=${array_nodes_all[i+2]}
            n_bak2=${array_nodes_all[i+3]}
            
            lines="${ip_pri} ${n_pri} "
            for((j=0;j<${#array_nodes_pri[@]};j+=2))
            do
                ipA=${array_nodes_pri[j]}; nodeA=${array_nodes_pri[j+1]};
                if [ "${nodeA}" == "${n_bak1}" ]; then 
                    lines=${lines}"${ipA} "; 
                fi
            done
            
            for((k=0;k<${#array_nodes_pri[@]};k+=2))
            do
                ipB=${array_nodes_pri[k]}; nodeB=${array_nodes_pri[k+1]};
                if [ "${nodeB}" == "${n_bak2}" ]; then 
                    lines=${lines}"${ipB} "; 
                fi
            done
            
            array_node+=(${lines})
        done
        
        
        for((n=0;n<${#array_node[@]};n+=4))
        do
            ip_pri=${array_node[n]}
            ip_bak1=${array_node[n+2]}
            ip_bak2=${array_node[n+3]}
            
            if [ "${ip_pri}" == "${ipAddress}" ]; then 
                bak1_nodeStatus=$(gcadmin showcluster ${vcNameStr}|grep "${ip_bak1}" |awk '{print $8}')
                bak2_nodeStatus=$(gcadmin showcluster ${vcNameStr}|grep "${ip_bak2}" |awk '{print $8}')
                
                if [ "${bak1_nodeStatus}" != "OPEN" ];then
                    message error "  Failed: The node '$ipAddress' cannot be set to FAILURE, because the status of its duplicate node ($ip_bak1) is $bak1_nodeStatus."; exit 1;
                elif [ "${bak2_nodeStatus}" != "OPEN" ];then
                    message error "  Failed: The node '$ipAddress' cannot be set to FAILURE, because the status of its duplicate node ($ip_bak2) is $bak2_nodeStatus."; exit 1;
                else
                    gcadmin setnodestate ${ipAddress} failure
                    
                    pri_nodeStatus=$(gcadmin showcluster ${vcNameStr}|grep "${ipAddress}" |awk '{print $8}')
                    if [ "${pri_nodeStatus}" == "FAILURE" ]; then
                        message debug "  Success: The node '$ipAddress' has been set to FAILURE for ${vcNameVal}."; exit 1;
                    else
                        message warn "  Waring: The node '$ipAddress' status is ${pri_nodeStatus}, please fix this issue manually."; exit 1;
                    fi
                fi
            fi
        done
    else
        ## P1D1
        for((i=0;i<${#array_nodes_all[@]};i+=3))
        do
            ip_pri=${array_nodes_all[i]}
            n_pri=${array_nodes_all[i+1]}
            n_bak1=${array_nodes_all[i+2]}
            
            lines="${ip_pri} ${n_pri} "
            for((j=0;j<${#array_nodes_pri[@]};j+=2))
            do
                ipA=${array_nodes_pri[j]}; nodeA=${array_nodes_pri[j+1]};
                if [ "${n_bak1}" == "${nodeA}" ]; then 
                    lines=${lines}"${ipA} "; 
                fi
            done
            
            array_node+=(${lines})
        done
        
        for((n=0;n<${#array_node[@]};n+=3))
        do
            ip_pri=${array_node[n]}
            ip_bak1=${array_node[n+2]}
            
            if [ "${ip_pri}" == "${ipAddress}" ]; then 
                bak1_nodeStatus=$(gcadmin showcluster ${vcNameStr}|grep "${ip_bak1}" |awk '{print $8}')
                
                if [ "${bak1_nodeStatus}" != "OPEN" ];then
                    message error "  Failed: The node '$ipAddress' cannot be set to FAILURE, because the status of its duplicate node ($ip_bak1) is $bak1_nodeStatus."; exit 1;
                else
                    gcadmin setnodestate ${ipAddress} failure
                    
                    pri_nodeStatus=$(gcadmin showcluster ${vcNameStr}|grep "${ipAddress}" |awk '{print $8}')
                    if [ "${pri_nodeStatus}" == "FAILURE" ]; then
                        message debug "  Success: The node '$ipAddress' has been set to FAILURE for ${vcNameVal}."; exit 1;
                    else
                        message warn "  Waring: The node '$ipAddress' status is ${pri_nodeStatus}, please fix this issue manually."; exit 1;
                    fi
                fi
            fi
        done
    fi
}

#校验密码有效性
check_password()
{
    #客户端命令
    gccli_command="gccli -u${dbUser} -p${dbPasswd}"
    
    if ! isCmdExist $gccli_command; then message error "  Failed: execute command (gccli) denied to the user"; exit 1; fi
    
    userPasswdLen="${#dbPasswd}"
    sqlResult=""
    sqlResultLen="" 
    
    #if [ "$dbUser" != "gbase" ]; then message error "  Exit! You have to enter the DBA user (gbase)."; exit 1; fi
    
    if [ "${userPasswdLen}" -eq 0 ];then 
        sqlResult="null password"
        message error " * You didn't enter the password for DBA user (gbase)! * "
        exit 1
    else 
        sqlResult="$(${gccli_command} -h${coorIp} -u${dbUser} -p${dbPasswd} -e quit 2>&1)"
    fi
    
    sqlResultLen="${#sqlResult}"
    if [ "${sqlResultLen}" -ne 0 ]; then
        message error " * The password is incorrect, the GBase 8a cannot be connected! * "
        exit 1
    fi 
}
#校验命令是否有效
isCmdExist() 
{
    local cmd="$1"
     if [ -z "$cmd" ]; then
        message error "Usage isCmdExist yourCmd"
        return 1
    fi
    which "$cmd" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        return 0
    fi
    return 2
}
#消息输出
message()
{
    local logtype=$1
    local msg=$2
    
    datetime=`date +'%F %H:%M:%S'`
    logformat="${datetime}\t[${logtype^^}]\t${msg}"
    {    
        case $logtype in
            debug)
                [[ $loglevel -le 0 ]] && echo -e "\e[32m${logformat}\e[0m" ;;
            info)
                [[ $loglevel -le 1 ]] && echo -e "\e[36m${logformat}\e[0m" ;;
            warn)
                [[ $loglevel -le 2 ]] && echo -e "\e[33m${logformat}\e[0m" ;;
            error)
                [[ $loglevel -le 3 ]] && echo -e "\e[31m${logformat}\e[0m" ;;
            esac
    }
}
main()
{
    if [ `whoami` != "gbase" ]; then message error "  Failed: You can only execute the Shell script by the gbase user!"; exit 1; fi
    source ~/.bash_profile
    
    if [ -n "$ipAddress" ] && [ -n "$dbUser" ] && [ -n "$dbPasswd" ];then
        set_node_failure $1 $2 $3 $4
    else
        message error "Usage: $0 ipAddress dbUser dbPasswd [vcName]"; exit 1
    fi
}
main $@

 

2) 恢复脚本set_node_normal.sh

#!/usr/bin/sh
###############################################################################
##creator : Tupac Tu
##create time: 2025-07-17
##Decription: 对GBase集群数据节点进行隔离后加入集群操作(入参:被隔离节点IP [VC名称])
##Method: sh set_node_normal.sh nip [vcname]
##Version: 1.0 
###############################################################################
#参数设置
ipAddress=$1
vcName=$2
set_node_failure()
{
    if ! isCmdExist gcadmin; then message error "  Failed: execute command (gcadmin) denied to the user"; exit 1; fi
    coorIp=$(gcadmin |grep "coordinator1 " |awk '{print $4}')
    
    #check_password
    
    gbaseMode=$(gcadmin showcluster d|grep "virtual cluster" |wc -l)
    
    isContain=0
    if [ ${gbaseMode} -gt 0 ];then
        if [ "${vcName}" == "" ]; 
            then message error "   Failed: The GBaseMode is MultVC, the VcName parameter cannot be empty."; exit 1; 
        else
            vcList=$(gcadmin showcluster d | grep "virtual cluster" | awk -F: '{print $2}' | sed 's/,//g')
            for vcn in ${vcList}; do if [ "$vcName" == "$vcn" ]; then isContain=1; break; else isContain=0; fi done
            if [ $isContain -eq 0 ]; then message error "  Failed: The GBaseMode is MultVC, but the '$vcName' is not in the vcList ($vcList)."; exit 1; fi
            
            nodelist=$(gcadmin showcluster vc ${vcName}|grep node[1-9]|awk '{print $4}')
            for nip in ${nodelist}; do if [ "$ipAddress" == "$nip" ]; then isContain=1; break; else isContain=0; fi done
            if [ $isContain -eq 0 ]; then message error "  Failed: The node '$ipAddress' is not in the nodelist for $vcName."; exit 1; fi
            
            nodeStatus=$(gcadmin showcluster vc ${vcName}|grep "${ipAddress}" |awk '{print $8}')
            if [ "${nodeStatus}" == "OPEN" ]; then message warn "  Waring: The node '$ipAddress' has been set to NORMAL for $vcName."; exit 1; fi
        fi
        
        vcEvent="-D${vcName}."
        vcNameStr=" vc ${vcName}"
        vcNameVal="${vcName}"
    else
        nodelist=$(gcadmin |grep node[1-9]|awk '{print $4}')
        for nip in ${nodelist}; do if [ "$ipAddress" == "$nip" ]; then isContain=1; break; else isContain=0; fi done
        if [ $isContain -eq 0 ]; then message error "  Failed: The node '$ipAddress' is not in the nodelist for cluster."; exit 1; fi
        
        nodeStatus=$(gcadmin |grep "${ipAddress}" |awk '{print $8}')
        if [ "${nodeStatus}" == "OPEN" ]; then message warn "  Waring: The node '$ipAddress' has been set to NORMAL for cluster."; exit 1; fi
            
        vcEvent=""
        vcNameStr=""
        vcNameVal="cluster"
    fi
    
    gcadmin setnodestate ${ipAddress} normal
    nodeStatus=$(gcadmin showcluster ${vcNameStr}|grep "${ipAddress}" |awk '{print $8}')
    if [ "${nodeStatus}" == "OPEN" ]; then
        message debug "  Success: The node '$ipAddress' has been set to NORMAL for ${vcNameVal}."
        message warn "  Waring: please pay attention to the progress of EVENT processing in the cluster."
        exit 1
    else
        message warn "  Waring: The node '$ipAddress' status is ${nodeStatus}, please fix this issue manually."; exit 1;
    fi
}

#校验密码有效性
check_password()
{
    #客户端命令
    gccli_command="gccli -u${dbUser} -p${dbPasswd}"
    
    if ! isCmdExist $gccli_command; then message error "  Failed: execute command (gccli) denied to the user"; exit 1; fi
    
    userPasswdLen="${#dbPasswd}"
    sqlResult=""
    sqlResultLen="" 
    
    #if [ "$dbUser" != "gbase" ]; then message error "  Exit! You have to enter the DBA user (gbase)."; exit 1; fi
    
    if [ "${userPasswdLen}" -eq 0 ];then 
        sqlResult="null password"
        message error " * You didn't enter the password for DBA user (gbase)! * "
        exit 1
    else 
        sqlResult="$(${gccli_command} -h${coorIp} -u${dbUser} -p${dbPasswd} -e quit 2>&1)"
    fi
    
    sqlResultLen="${#sqlResult}"
    if [ "${sqlResultLen}" -ne 0 ]; then
        message error " * The password is incorrect, the GBase 8a cannot be connected! * "
        exit 1
    fi 
}
#校验命令是否有效
isCmdExist() 
{
    local cmd="$1"
     if [ -z "$cmd" ]; then
        message error "Usage isCmdExist yourCmd"
        return 1
    fi
    which "$cmd" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        return 0
    fi
    return 2
}
#消息输出
message()
{
    local logtype=$1
    local msg=$2
    
    datetime=`date +'%F %H:%M:%S'`
    logformat="${datetime}\t[${logtype^^}]\t${msg}"
    {    
        case $logtype in
            debug)
                [[ $loglevel -le 0 ]] && echo -e "\e[32m${logformat}\e[0m" ;;
            info)
                [[ $loglevel -le 1 ]] && echo -e "\e[36m${logformat}\e[0m" ;;
            warn)
                [[ $loglevel -le 2 ]] && echo -e "\e[33m${logformat}\e[0m" ;;
            error)
                [[ $loglevel -le 3 ]] && echo -e "\e[31m${logformat}\e[0m" ;;
            esac
    }
}
main()
{
    if [ `whoami` != "gbase" ]; then message error "  Failed: You can only execute the Shell script by the gbase user!"; exit 1; fi
    source ~/.bash_profile
    
    if [ -n "$ipAddress" ];then
        set_node_failure $1 $2
    else
        message error "Usage: $0 ipAddress [vcName]"; exit 1
    fi
}
main $@

 

评论

登录后才可以发表评论
用户头像
GBASE涂俊兵发表于 1年前
https://www.gbase.cn/community/post/5951?shareId=27033