#!/bin/bash
set +x

CMD_MML_PORT=988
MML="/usr/local/bin/MmlBatch"
CMD_MML_988="${MML} ${CMD_MML_PORT}"

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="301"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="${G_TMP_INSPECT_PATH}tmpResult${CurInspectFun}"
>${RESULTFILE}

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

function get_nluninfo_log()
{
    local result_log="/var/log/nluninfo.log"
    local tmpMmlfilepath="${G_TMP_INSPECT_PATH}checkNlunPosbalanceMmlfile_getnluninfo"
    local tmpResultfilepath="${G_TMP_INSPECT_PATH}checkNlunPosbalancefile_nluninfolog"
    #先将/var/log/nluninfo.log清空
    >${result_log}

    ${CMD_MML_988} "monc nluninfo 0"  >  ${tmpMmlfilepath} 2>&1
    iRet=$?
    if [ $iRet -ne 0 ]; then
        Info=`cat ${tmpMmlfilepath}`
        LOG "[$FUNCNAME][${LINENO}]mml_result:$Info"
        ErrInfoCommon "INFO:Failed to execute command(MmlBatch 988 'monc nluninfo 0'). Error(${iRet})." "${RESULTFILE}"
        rm -f ${tmpMmlfilepath}
        return 1
    fi

    cp ${result_log} ${tmpResultfilepath}
    iRet=$?
    if [ $iRet -ne 0 ]; then
        LOG "[$FUNCNAME][${LINENO}]Failed to get nluninfo"
        ErrInfoCommon "INFO:Failed to get nluninfo." "${RESULTFILE}"
        rm -f ${tmpMmlfilepath}
        return 1
    fi
    if [[ ! -f ${tmpResultfilepath} ]]; then
        LOG "[$FUNCNAME][${LINENO}]nluninfofile is not exists"
        ErrInfoCommon "INFO:Failed to get nluninfo." "${RESULTFILE}"
        rm -f ${tmpMmlfilepath}
        return 1
    fi

    CheckResultFile=`cat ${tmpResultfilepath} | grep "End dump full nlunmap" | wc -l`
    if [[ ${CheckResultFile} -eq 0 ]]; then
        LOG "[$FUNCNAME][${LINENO}nluninfofile is empty"
        ErrInfoCommon "INFO:Failed to get nluninfo." "${RESULTFILE}"
        rm -f ${tmpMmlfilepath}
        return 1
    fi

    rm -f ${tmpMmlfilepath}
    return 0
}

# 301 位置均衡检查
function checkNlunPosbalancefunc()
{
    local isPass=0
    local result_nlunmap_log="${G_TMP_INSPECT_PATH}checkNlunPosbalancefile_nluninfolog"
    local local_Dsid=""
    LOG "[$LINENO]:start check NlunPosbalance"
    while true;do
        #获取nlunmap
        get_nluninfo_log
        iRet=$?
        if [ $iRet -ne 0 ];then
            isPass=$(check_pass ${isPass} 1)
            break
        fi
        
        #检查位置均衡开关是否开启
        libVerNum=`cat ${result_nlunmap_log} | grep -aw "libVer 1" | head -n1 | wc -l`
        if [ ${libVerNum} -eq 0 ];then
            isPass=$(check_pass ${isPass} 0)
            echo "INFO:pass" >> ${RESULTFILE}
            break
        fi

        ErrInfoCommon "INFO:pos balance switch is turnning on" "${RESULTFILE}"
        isPass=$(check_pass ${isPass} 4)
        break
    done
    rm -f ${result_nlunmap_log}

    echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE}
    LOG "[$LINENO]:End check NlunPosbalance"
}

checkNlunPosbalancefunc


