#!/bin/bash
set +x
LOG_FILE="/var/log/inspect.log"
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
source $G_MML_FILE_PATH/CommonFunc.sh
CurInspectNum="127"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
LOCAL_BACK_IP=""
isPass=0
>${RESULTFILE}

function LOG
{
    time=$(date)
    echo [${time}][$$][$CurInspectFun]$@ >> $LOG_FILE
}

function checkCBUstatus()
{
    LOCAL_BACK_IP=`GetLocalIp`
    if [ "${LOCAL_BACK_IP}X" == "X" ];then
        echo "[ERR]Node:$(hostname)||INFO:Can't get backend IP." >> ${RESULTFILE} 2>&1
        LOG "[ERR]Node:$LOCAL_BACK_IP ||INFO:Command exec faild."
        isPass=1
        return
    fi

    cat /opt/huawei/deploy/etc/deploy.ini | grep devicename | grep "9000 C72 Node" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Node:$LOCAL_BACK_IP(Not C72 node)||Board_id:Do not involve.||Health_Status:--||Running_Status:--" >> ${RESULTFILE} 2>&1
        LOG "Node:$LOCAL_BACK_IP(Not C72 node)||Size:0||INFO:No need to check."
        return
    fi
    >/tmp/checkCBUstatus_MmlInfo
    $G_MML_FILE_PATH/ShowInspectMml $LOCAL_BACK_IP 4016 'cm hardwareinfo show' >> /tmp/checkCBUstatus_MmlInfo 2>&1
    iRet_Mml=$?
    sleep 3
    ipmitool fru >> /tmp/checkCBUstatus_MmlInfo 2>&1
    iRet=$?
    if [ $iRet -ne 0 -o $iRet_Mml -ne 0 ]; then
        echo "[ERR]Node: $LOCAL_BACK_IP(C72 node)||INFO:Failed to execute command(MmlBatch 4016 'cm hardwareinfo show' / ipmitool fru)" >> ${RESULTFILE} 2>&1
        LOG "Node: $LOCAL_BACK_IP(C72 node)||INFO: Failed to execute CBUstatus"
        isPass=1
        return
    fi
    iNum=$(cat /tmp/checkCBUstatus_MmlInfo|wc -l)
    if [ $iNum -eq 0 ];then
        return
    fi

    set_ifs

    cat /tmp/checkCBUstatus_MmlInfo >> $LOG_FILE 2>&1

    #无CBU
    CBUexist=$(cat /tmp/checkCBUstatus_MmlInfo | grep "CBU:no"|wc -l)
    if [ $CBUexist -ne 0 ];then
        isPass=1
        echo "[ERR]Node:$LOCAL_BACK_IP(C72 node)||INFO:There is no CBU." >> ${RESULTFILE} 2>&1
        restore_ifs
        return
    fi

    #正常情况下CBU个数为2
    CBUCount=$(cat /tmp/checkCBUstatus_MmlInfo | grep "CBU:" | awk -F: '{print $2}')
    if [ $CBUCount -ne 2 ];then
        isPass=1
        echo "[ERR]Node:$LOCAL_BACK_IP(C72 node)||INFO:CBU number: $CBUCount" >> ${RESULTFILE} 2>&1
        restore_ifs
        return
    fi

    #判断CBU状态(与级联板不同，CBU是共用的)
    CBUstatus=$(cat /tmp/checkCBUstatus_MmlInfo | grep -A2 "CBU:" | grep -v "CBU:"|wc -l)
    if [ $CBUstatus -eq 0 ];then
        isPass=1
        echo "[ERR]Node:$LOCAL_BACK_IP(C72 node)||INFO:Failed to get CBU info." >> ${RESULTFILE} 2>&1
        restore_ifs
        return
    fi

    for loop in 1 2
    do
        CBUInfo=$(cat /tmp/checkCBUstatus_MmlInfo | grep -A2 "CBU:" | grep -v "CBU:" | sed -n "${loop}p")
        if [ "$CBUInfo" == "" ];then
            isPass=1
            echo "[ERR]Node:$LOCAL_BACK_IP(C72 node)||INFO:Failed to get CBU info." >> ${RESULTFILE} 2>&1
            break
        fi
        ID=$(echo "${CBUInfo}" | awk -F'\\|\\|' '{print $1}' | awk -F':' '{print $2}' | tr -d ' ')
        healthStatus=$(echo "${CBUInfo}" | awk -F'\\|\\|' '{print $2}' | awk -F':' '{print $2}' | tr -d ' ')
        runningStatus=$(echo "${CBUInfo}" | awk -F'\\|\\|' '{print $3}' | awk -F':' '{print $2}' | tr -d ' ')
        if [ "$healthStatus" = "1" ] \
        && [ "$runningStatus" = "1" -o "$runningStatus" = "2" -o "$runningStatus" = "27" ];then
            echo "Node:$LOCAL_BACK_IP(C72 node)||Board_id:${ID}||Health_Status:${healthStatus}||Running_Status:${runningStatus}" >> ${RESULTFILE} 2>&1
        else
            isPass=1
            echo "[ERR]Node:$LOCAL_BACK_IP(C72 node)||INFO:Board_id(${ID}),Health_Status(${healthStatus}),Running_Status(${runningStatus})" >> ${RESULTFILE} 2>&1
        fi
    done

    restore_ifs
}

checkCBUstatus
echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1

