#!/bin/bash
set +x
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
[ -f $G_MML_FILE_PATH/CheckItems ] && source $G_MML_FILE_PATH/CheckItems
[ -f /opt/inspect/inspect_map.sh ] && source /opt/inspect/inspect_map.sh
[ -f /opt/huawei/snas/script/inspect_mml/CommonNodeTypeRecognize.sh ] && source /opt/huawei/snas/script/inspect_mml/CommonNodeTypeRecognize.sh

function LOG
{
    local LOG_FILE="/var/log/inspect.log"
    echo [$(date)][$$]$@ >> $LOG_FILE
}

function IsSMRDisk()
{
    local type=0
    local tmp=""
    local snas_ini="/opt/huawei/snas/etc/snas.ini"

    if [ -f ${sans_ini} ]; then
        tmp=$(sed -n '/DiskNodeType/p' ${snas_ini})
    else
        tmp=""
    fi

    if [ -z "$tmp" ]; then
        smr_cnt=$(lsscsi -g | awk '{if ($2=="(0x14)") print $NF}'| wc -l)
        if [ ${smr_cnt} -gt 0 ]; then
            type=1
        fi
    else
        type=$(grep -w DiskNodeType ${snas_ini} | awk -F '=' '{print $2}')
    fi

    return $type
}

function checkPyhMem()
{
    local isPass=0
    local Result=""
    local ProductType=""
    local isC72=0
    local stdSpeed=0
    local stdNvSize=0
    local totalMemSize=0
    local firstMemSize=0
    local nvSize=0
    local isMemSizeDiff=0
    local lessSpeedList=""
    local memresultFile="/tmp/tmpfilecheckPhyMemory"
    local curInspectNum="133"
    local curInspectFun="$(GetInspectType $curInspectNum)"
    local resultFile="/tmp/tmpResult${curInspectFun}"
    local ddr3list="0 1 3 5 6 7 8" #P(0) C(1) M(3) P25(5) P36(6) C36(7) P12(8)  1333(MHZ)DDR3

    > $memresultFile
    > ${resultFile}

    IsStandardNode
    if [ $? -eq 0 ];then
        echo "Locator:not involve||size:--||Speed:--" >> ${resultFile}  2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile} 2>&1
        return
    fi

    nodepool=$(cat /opt/huawei/snas/etc/snas.ini | grep ^nodepool | awk -F'=' '{print $2}' | awk '{print $1}')
    nodetype=$(cat /opt/huawei/deploy/etc/deploy.ini | grep -Po '(?<=^devicetype=)[0-9]+')
    if [ "${nodetype}" == "" ];then
        isPass=1
        echo "[ERR]INFO:nodetype is empty!" >> ${resultFile}  2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile} 2>&1
        return
    fi
    ProductType=$(cat "/opt/huawei/snas/etc/snas.ini"|grep ^productType |awk -F'=' '{print $2}' | awk '{print $1}')

    #判断是不是C72,#ARM服务器中不存在NVdimm内存，因此DIMM000按照C72的处理
    IsC72Node
    isC72=$?
    IsTSNode
    isARM=$?
    if [ ${isARM} -eq 0 ] || [ $isC72 -eq 0 ];then
        isC72=1
    else
        isC72=0
    fi

    IsRHNode
    if [ $? -eq 0 ]; then
        IsRHV5Node
        if [ $? -eq 0 ];then
            isRH="V5RH"
            stdSpeed=2400
            stdNvSize=16384
        else
            isRH="V3RH"
            stdNvSize=8192
            stdSpeed=2133
        fi
    else
        isRH=0
        stdNvSize=2048
        stdSpeed=1600
        echo "${ddr3list}" | grep -w "${nodetype}" >/dev/null
        if [ $? -eq 0 ];then
            stdSpeed=1333
        elif [ ${isARM} -eq 0 ];then
            stdSpeed=2933
        fi
    fi
    IsSMRDisk
    if [ $? -eq 1 ]; then
        SMRFlag=1    #SMR disk on node
    else
        SMRFlag=0
    fi

    dmidecode -t memory | grep -P "^\s+(Locator:|Size:|Speed:)" >> $memresultFile  2>&1
    while read line; do
        local tmpSize=$(echo $line |grep "Size:" |awk '{print $2}')
        local tmpUnit=$(echo $line |grep "Size:" |awk '{print $3}')
        local tmpSlot=$(echo $line |grep "Locator:" | awk -F":" '{print $2}' | sed 's/Slot//g' | awk '{print $1}')
        local tmpSpeed=$(echo $line |grep "Speed:" |awk '{print $2}')
        #统一单位为MB
        if [ "${tmpUnit}X" = "GBX" ];then
            tmpSize=$(($tmpSize*1024))
        elif [ "${tmpUnit}X" = "KBX" ];then
            tmpSize=$(($tmpSize/1024))
        fi
        Result="${Result} ${tmpSize} ${tmpSlot}"
        if [ "${tmpSpeed}X" != "X" ]; then
            tmpSize=$(echo $Result |awk '{print $1}')
            tmpSlot=$(echo $Result |awk '{print $2}')
            if [ "${tmpSize}" == "No" -o "${tmpSlot}" == "" ];then
                Result=""
                continue
            fi
            if [ "$tmpSlot" = "F0" ] || [ "$tmpSlot" = "DIMM000" -a $isC72 != 1 ]; then
                nvSize=$tmpSize
            else
                if [  ${tmpSpeed} -lt ${stdSpeed}  ]; then
                    lessSpeedList="$lessSpeedList $tmpSlot"
                fi
                if [ $firstMemSize -eq 0 ];then
                    firstMemSize=$tmpSize
                else
                    if [ $firstMemSize -ne $tmpSize ];then
                        isMemSizeDiff=1
                    fi
                fi
                totalMemSize=$(expr $tmpSize + $totalMemSize)
            fi
            echo  "Locator:$tmpSlot||size:$tmpSize(MB)||Speed:$tmpSpeed(MHZ)"  >> ${resultFile}  2>&1
            Result=""
        fi
    done < $memresultFile
    #restore_ifs
    if [ "${lessSpeedList}" != "" ];then
        echo  "[ERR]INFO:The physical memory frequency of slots(${lessSpeedList}) is lower than ${stdSpeed}(MHZ)."  >>${resultFile} 2>&1
        isPass=1
    fi

    if [ "$isMemSizeDiff" -ne 0 ] ; then        #不通过
        echo  "[ERR]INFO:Node physical memory sizes are different."  >> ${resultFile} 2>&1
        isPass=1
    fi

    if [ $isC72 != 1 -a ${nvSize} -lt ${stdNvSize} ]; then
        echo  "[ERR]INFO: nvdimm ${nvSize}(MB) less than ${stdNvSize}(MB)" >> ${resultFile} 2>&1
        isPass=1
    fi

    if [ $ProductType -eq 80 ];then
        declare -a memArr=(["0"]=49152  ["1"]=49152  ["2"]=32768  ["3"]=32768  ["4"]=32768 ["5"]=49152 ["6"]=49152 ["7"]=32768
               ["8"]=32768 ["12"]=32768 ["13"]=49152 ["14"]=81920 ["15"]=32768)
    elif [ "${SMRFlag}" = "1" ];then
        ##处理9000 RH服务器含有SMR盘
        declare -a memArr=(["0"]=49152   ["1"]=49152   ["2"]=32768   ["3"]=32768   ["4"]=32768   ["5"]=49152   ["6"]=49152   ["7"]=32768   ["8"]=32768
            ["12"]=32768   ["13"]=49152  ["14"]=49152  ["15"]=32768  ["16"]=49152  ["17"]=49152  ["21"]=32768  ["22"]=98304  ["23"]=81920  ["24"]=98304
            ["25"]=81920   ["26"]=81920  ["27"]=65536  ["28"]=98304  ["29"]=81920  ["31"]=114688 ["32"]=49152  ["33"]=114688 ["34"]=98304  ["35"]=98304
            ["36"]=81920   ["37"]=114688 ["38"]=98304  ["39"]=147456 ["40"]=65536  ["41"]=131072 ["42"]=147456 ["43"]=147456 ["44"]=131072 ["46"]=81920
            ["47"]=81920   ["48"]=81920)
    elif [ "${SMRFlag}" = "0" ];then
        ##处理9000 RH服务器没有SMR盘
        declare -a memArr=(["0"]=49152   ["1"]=49152   ["2"]=32768   ["3"]=32768   ["4"]=32768   ["5"]=49152   ["6"]=49152   ["7"]=32768   ["8"]=32768
            ["12"]=32768   ["13"]=49152  ["14"]=49152  ["15"]=32768  ["16"]=49152  ["17"]=49152  ["21"]=32768  ["22"]=98304  ["23"]=49152  ["24"]=49152
            ["25"]=32768   ["26"]=32768  ["27"]=65536  ["28"]=98304  ["29"]=81920  ["31"]=114688 ["32"]=49152  ["33"]=114688 ["34"]=65536  ["35"]=49152
            ["36"]=81920   ["37"]=114688 ["38"]=98304  ["39"]=147456 ["40"]=65536  ["41"]=131072 ["42"]=147456 ["43"]=147456 ["44"]=98304  ["46"]=49152
            ["47"]=32768   ["48"]=32768)
    else
        declare -a memArr=(["0"]=49152  ["1"]=49152  ["2"]=32768  ["3"]=32768 ["4"]=32768 ["5"]=49152 ["6"]=49152 ["7"]=32768
               ["8"]=32768 ["12"]=32768 ["13"]=49152 ["14"]=49152 ["15"]=32768)
    fi

    if [ "${memArr[$nodetype]}" != "" ];then
        if [ "$totalMemSize" -lt "${memArr[$nodetype]}" ]; then
            NodeType=$(MapNodeType "$nodetype")
            if [ "${SMRFlag}" == "1" ];then
                echo  "[ERR]INFO:${NodeType} including SMR disks, with a total capacity of ${totalMemSize}(MB) of physical memory less than ${memArr[$nodetype]}(MB)." >> ${resultFile} 2>&1
            else
                echo  "[ERR]INFO:${NodeType} do not include SMR disks, and the total capacity of physical memory ${totalMemSize}(MB) is less than ${memArr[$nodetype]}(MB)." >> ${resultFile} 2>&1
            fi
            isPass=1
        fi
    fi
    [ -f ${memresultFile} ] && rm -f ${memresultFile}
    echo "nodepool=${nodepool}" >> ${resultFile} 2>&1
    echo "totalmemsize=${totalMemSize}" >> ${resultFile} 2>&1
    echo "${curInspectFun}_Pass $isPass" >>${resultFile} 2>&1
    LOG "[$FUNCNAME]${curInspectFun}_Pass ${isPass}"
}

checkPyhMem


