#!/bin/bash

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
[ -f ${G_INSPECT_MMLPATH}/CheckItems ] && source ${G_INSPECT_MMLPATH}/CheckItems
source ${G_INSPECT_MMLPATH}/CommonFunc.sh

function getMemUsedHisRecFile()
{
    echo "/tmp/memUsedHisRecord"
}

function get64_1600UseMem()
{
    local meminfo=$(echo "$1" |eval sed '0,/ca.0/d' |eval sed '/-----/,\$\d' | grep "Cache:S_")
    local result=$(echo "${meminfo}" |
        awk -F',' 'BEGIN{
                tmpType=0;
                tmpSize=0;
                tmpNum=0;
                totalSize=0;
            }
            {
                 if (($1~/-[0-9]+/) && ($2~/Size:[ \t]*[0-9]+/) && ($4~/used:[ \t]*[0-9]+./ ))
                 {
                     tmpType=substr($1, index($1, "-")+1);
                     if ((tmpType+0) >= 64 && (tmpType+0) <=1600 )
                     {
                         tmpSize=substr($2, index($2, ":")+1);
                         tmpNum=substr($4, index($4, ":")+1);
                         totalSize=tmpSize*tmpNum + totalSize;
                         printf("%d:%dMB ", tmpType, tmpSize*tmpNum/1048576);
                     }
                 }
            }

            END {
                printf("| totalSize:%s", totalSize);
            }'
        )
    echo "$result"
}

function CheckLDBLoadMessage()
{
    local isPass=0
    local patchVersion=""
    local bigFileFlag=""
    local nodeNum=""
    local productVersion=""
    local curInspectNum="361"
    local curInspectFun="$(GetInspectType $curInspectNum)"
    local resultFile="/tmp/tmpResult${curInspectFun}"
    local chinaAreaKey="false"
    local tmpInfo=""
    local Repeat=(1 2 3)
    >${resultFile}

    areaKey=$(GetAreaKey)
    if [[ "${areaKey}" == "true" ]];then
        bigFileFlag=$(/usr/local/bin/MmlBatch 988 "show wal switch" | grep bigfile | awk -F':' '{print $2}' | tr -d ' ')
        if [[ "${bigFileFlag}" == "1" ]];then
            /usr/local/bin/MmlBatch 988 "set wal bigfile 0"
        fi
    fi

    #ڵ10ڵѲͨ
    for time in ${Repeat[*]}; do
        nodeNum=$(sqlite3 /opt/huawei/snas/etc/cm_conf.db "select count(NID) from CM_NODE_T")
        iRet=$?
        if [ $iRet -ne 0 ]; then
            sleep 2
        else
            break
        fi
    done

    if [[ ${nodeNum} -lt 10 ]];then
        isPass=$(check_pass ${isPass} 0)
        echo "NodeNum:$nodeNum||PatchVersion:--||ServerType:--||Total:--||Free:--||Percent:--||AllocFailTimes:--" >> ${resultFile}  2>&1
        echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
        return
    fi

    local back_ip=$(GetLocalIp)

    if [ -z "${back_ip}" ]; then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO:Get local back ip fail!" >> ${resultFile} 2>&1
        echo "${CurInspectFun}_Pass $isPass" >>${resultFile}
        LOG "[$LINENO]ERR: ${curInspectFun} local back ip is empty, abort!"
        return
    fi

    productVersion=$(grep ExternalVersion /opt/huawei/deploy/package/version |awk -F= '{print $2}')
    tmpInfo=$(/opt/huawei/snas/upd/patch_script/cluster_patch.sh "$back_ip" show)
    iRet=$?
    if [[ ${iRet} -ne 0 ]];then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO: Get hot patch version Failed!" >> ${resultFile} 2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile}
        LOG "[$LINENO]ERR: ${curInspectFun} Get hot patch version Failed!"
        return
    fi

    patchVersion=$(echo "${tmpInfo}"| grep "Version:" |awk -F, '{print $3}' | awk -F: '{print $2}' 2>/dev/dull)
    patchVersion=${patchVersion:-null}
    if [[ "${patchVersion}" != "null" ]];then
        local ver=$(echo "${patchVersion}" | grep -Po '(?<=SPH)[0-9]+')
        if [[ "$ver" == "" ]];then
            isPass=$(check_pass ${isPass} 1)
            echo "NodeNum:$nodeNum||PatchVersion:${patchVersion}||ServerType:--||Total:--||Free:--||Percent:--||AllocFailTimes:--" >> ${resultFile}  2>&1
            echo "[ERR]INFO:Malformed patch name format error!" >> ${resultFile} 2>&1
            echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
            LOG "[$LINENO]ERR: ${curInspectFun} Malformed patch name (${patchVersion}) format errord!"
            return
        fi

        local tmpflag=1
        if [[ "${productVersion}" == "7.0.1" ]] && [ ${ver} -ge 3 ] ; then
            tmpflag=0
        elif [[ "${productVersion}" == "7.0.2" ]] && [ ${ver} -ge 6 ] ;then
            tmpflag=0
        elif [[ "${productVersion}" == "7.1.0" ]] && [ ${ver} -ge 2 ]; then
            tmpflag=0
        fi
        if [ ${tmpflag} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 0)
            echo "NodeNum:$nodeNum||PatchVersion:${patchVersion}||ServerType:--||Total:--||Free:--||Percent:--||AllocFailTimes:--" >> ${resultFile}  2>&1
            echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
            return
        fi
    fi

    local servertype=$(cat /opt/huawei/snas/etc/snas.ini |grep ^serviceType |awk -F= '{print $2}')

    tmpInfo=$(/usr/local/bin/MmlBatch 988 "mem show stat 3")
    iRet=$?
    if [[ ${iRet} -ne 0 ]];then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO:MML command execution failed!" >> ${resultFile} 2>&1
        echo "NodeNum:$nodeNum||PatchVersion:${patchVersion}||ServerType:${servertype}||Total:--||Free:--||Percent:--||AllocFailTimes:--" >> ${resultFile}  2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile}
        LOG "[$LINENO]ERR: ${curInspectFun} MML command execution failed!"
        return
    fi
    tmpInfo=$(echo "${tmpInfo}" | sed -e 's/\x1B\[0;[3-4][0-9]m//g' -e 's/\x0D//g' -e 's/\x00//g')
    local pool0info=$(echo "${tmpInfo}" | grep  "PooId 0" )
    local Percent=$(echo "$pool0info" | grep -Po '(?<=Percent )[0-9+]+')
    local curAllocFailTimes=$(echo "$pool0info" | grep -Po '(?<=AllocFailTimes )[0-9+]+')
    local totalSize=$(echo "$pool0info" | grep -Po '(?<=Total )[0-9+]+' )
    local free=$(echo "$pool0info" | grep -Po '(?<=Free )[0-9+]+')

    LOG "[$FUNCNAME]servertype=$servertype Percent=$Percent"
    if [[ "${Percent}" == "" ]] || [[ "${totalSize}" == "" ]] || [[ "${curAllocFailTimes}" == "" ]] || [[ "${free}" == "" ]];then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO:MML command result format mismatch!" >> ${resultFile} 2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile}
        return
    fi

    if [ ${totalSize} -eq 0 ];then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO:Pool 0 total memory is 0!" >> ${resultFile} 2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile}
        return
    fi

    #ˮλĬϳ65%85%
    local flowControlledLv=65
    if [[ "${servertype}" != "other" ]];then
        flowControlledLv=85
    fi
    #̭ˮλýʺƵ̭ˮλ70%̭ˮλ45%
    local eliminateLv=45
    if [[ "${servertype}" == "media " ]] || [[ "${servertype}" == "surveillance" ]];then
        eliminateLv=70
    fi

    #PooId 0ڴ,#ﵽˮλ,Ѳ첻ͨ
    if [[ $Percent -ge $flowControlledLv ]] ;then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO: Pool0 memory usage (${Percent}%) is higher than the flow control water level (${flowControlledLv}%)" >> ${resultFile} 2>&1
    elif [[ $Percent -lt ${eliminateLv} ]];then
        #̭ˮλѲͨ
        isPass=$(check_pass ${isPass} 0)
    else
        #򣬲鿴ڴʧܴǰһʧܴûӣѲͨ 򣬲鿴641600֮ڴ䣬ռȳܴС50%Ѳ첻ͨ
        local memSuccessiveArise=$(cat $(getMemUsedHisRecFile) | sed -n "s/.*CA_LDB \([0-9]\).*/\1/p")
        memSuccessiveArise=${memSuccessiveArise:-0}

        local useMeminfo=$(get64_1600UseMem "${tmpInfo}")
        local useTotalSize=$(echo "$useMeminfo" | grep -Po '(?<=totalSize:)[0-9]+')
        local usePercent=$(echo "$useTotalSize $totalSize" | awk '{printf("%g\n",$1/$2*100)}')

        if [[ ${memSuccessiveArise} -eq 1 ]] && [[ $usePercent > 50 ]] ;then
            isPass=$(check_pass ${isPass} 1)
            local tmpUseMeminfo=$(echo "$useMeminfo" | awk -F'|' '{print $1}' )
            echo "[ERR]INFO:AllocFailTimes increase, memory allocation between Pool0 64-1600:$tmpUseMeminfo" >> ${resultFile} 2>&1
        fi

    fi
    echo "NodeNum:$nodeNum||PatchVersion:${patchVersion}||ServerType:${servertype}||Total:${totalSize}||Free:${free}||Percent:${Percent}||AllocFailTimes:${curAllocFailTimes}" >> ${resultFile}  2>&1
    echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1

}

CheckLDBLoadMessage
sh ${G_MML_FILE_PATH}/dynamic_mem_inspect.sh "init" >/dev/null 2>&1

#Ƿسɹ[ERR]ʶǷ
exit 0

