#!/bin/bash
set +x

#检查集群节点个数
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source ${G_INSPECT_MMLPATH}/CheckItems
CurInspectNum="351"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="${G_TMP_INSPECT_PATH}tmpResult${CurInspectFun}"
>${RESULTFILE}

source /opt/node_type_recognize.sh
ofedConfigPath="/opt/driver/ib-driver"
LOG_FILE="/var/log/inspect.log"
RepeatTimes=(1 2 3)

EXPANDERTOOL228812="/opt/hardware_firmware/rh/ses/RH228812HDD/expanderTool"
EXPANDERTOOL228825="/opt/hardware_firmware/rh/ses/RH228825HDD/expanderTool"
EXPANDERTOOL528836="/opt/huawei/snas/bin/g3Xflash"
SANS_FILE="/opt/huawei/snas/etc/snas.ini"
#获取当前节点上各个固件版本的ipmitool命令
CMD_GET_BMC_VER="/usr/bin/ipmitool raw 0x30 0x90 0x08 0x00 0x01 0x00 0x0e"
CMD_GET_MAINBOARD_BIOS_VER="/usr/bin/ipmitool raw 0x30 0x90 0x08 0x00 0x06 0x00 0x0e"
CMD_GET_MAINBOARD_CPLD_VER="/usr/bin/ipmitool raw 0x30 0x90 0x08 0x00 0x02 0x00 0x0e"
CheckVersion=$(grep "^ProductVersion" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
Product_Version=$( grep -w "ExternalVersion" /opt/huawei/deploy/package/version |awk -F"=" '{print $2}' )
source /opt/node_type_recognize.sh

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

#RH结果是从第二个字段开始取
function RHhex2ascii()
{
    local idx=0
    local INPUT=$1
    local OUTPUT=""
    for i in $INPUT ; do
        idx=$((idx+1))
        if [ $idx -gt 1 ];then
            OUTPUT=$OUTPUT`echo -e "\\x$i"`
        fi
    done
    echo "$OUTPUT"
}

# bmc版本格式都是V1.40或者1.2.56.2或者00.3.43之类的，前面带V的是NVdimm的版本
function curVer_ge_desVer() 
{
    curbmcVer="${1}"
    expbmcVer="${2}"

    curbmcVer=$( echo "$curbmcVer" | sed "s/V//g" | sed "s/\.//g" | grep -o [0-9]* )
    expbmcVer=$( echo "$expbmcVer" | sed "s/V//g" | sed "s/\.//g" | grep -o [0-9]* )

    if [ X"${curbmcVer}" == X"" -o  X"${expbmcVer}" == X"" ]; then
        echo "0"
        return
    fi

    if [ ${curbmcVer} -ge ${expbmcVer} ]; then
        echo "1"
        return
    fi

    echo "0"
    return
}

#需Web界面升级的固件
function getBMC()
{
    local Des_V3_BMC_Version=$(grep "^Des_V3_BMC_Version" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_V5_BMC_Version=$(grep "^Des_V5_BMC_Version" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_TS_BMC_Version=$(grep "^Des_TS_BMC_Version" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_TS_BMC_Version_1711=$(grep "^Des_TS_BMC_1711_Version" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local boardVer=$(ipmitool raw 0x30 0x93 0xdb 0x07 0x00 0x65 0x00 0x00 | awk '{print $4}')
    local version_bmc=""
    local isPass="${1}"
    #获取BMC固件版本
    raw_version=$($CMD_GET_BMC_VER)
    raw_version_test=$( echo ${raw_version} | grep -Ev ^[0-9,a-f,' ']+$ )
    if [ -z "${raw_version}" ] || [ ! -z "${raw_version_test}" ] ; then
        version_bmc="NULL"
    else
        version_bmc=$( RHhex2ascii "$raw_version" )
    fi

    IsRHV3Node
    isRetRH3=$?
    IsRHV5Node
    isRetRH5=$?
    IsTSNode
    isRetTS=$?
    if [ "$isRetRH3" == "0" ]; then
        curVerGeDesVer=$( curVer_ge_desVer "${version_bmc}" "${Des_V3_BMC_Version}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:BMC version ${version_bmc} is lower than the standard version ${Des_V3_BMC_Version}" >>${RESULTFILE} 2>&1
        fi
    elif [ "$isRetRH5" == "0" ]; then
        curVerGeDesVer=$( curVer_ge_desVer "${version_bmc}" "${Des_V5_BMC_Version}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:BMC version ${version_bmc} is lower than the standard version ${Des_V5_BMC_Version}" >>${RESULTFILE} 2>&1
        fi
    elif [ "$isRetTS" == "0" ]; then
        if [ "X${boardVer}" != "X20" ]; then
            curVerGeDesVer=$( curVer_ge_desVer "${version_bmc}" "${Des_TS_BMC_Version}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:BMC version ${version_bmc} is lower than the standard version ${Des_TS_BMC_Version}" >>${RESULTFILE} 2>&1
            fi
        else
            curVerGeDesVer=$( curVer_ge_desVer "${version_bmc}" "${Des_TS_BMC_Version_1711}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:BMC version ${version_bmc} is lower than the standard version ${Des_TS_BMC_Version_1711}" >>${RESULTFILE} 2>&1
            fi
        fi
    fi

    echo "BMC_Version:${version_bmc}"
    return ${isPass}
}

function getBIOS()
{
    local Des_V3_2288_BIOS_Ver=$(grep "^Des_V3_2288_BIOS_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_V3_5288_BIOS_Ver=$(grep "^Des_V3_5288_BIOS_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_V5_BIOS_Ver=$(grep "^Des_V5_BIOS_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_TS_BIOS_Ver=$(grep "^Des_TS_BIOS_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local version_bios=""
    local isPass="${1}"

    #获取BIOS固件版本
    raw_version=$($CMD_GET_MAINBOARD_BIOS_VER)
    raw_version_test=$( echo ${raw_version} |grep -Ev ^[0-9,a-f,' ']+$ )
    if [ -z "${raw_version}" ] || [ ! -z "${raw_version_test}" ] ; then
        version_bios="NULL"
    else
        version_bios=$( RHhex2ascii "${raw_version}" )
        version_bios=$( echo ${version_bios} | awk -F ")" '{print $2}' )
    fi

    IsRHV3Node
    isRetRH3=$?
    IsRHV5Node
    isRetRH5=$?
    IsTSNode
    isRetTS=$?
    if [ "$isRetRH3" == "0" ]; then
        IsRH2288Node
        if [ "$?" == "0" ]; then
            curVerGeDesVer=$( curVer_ge_desVer "${version_bios}" "${Des_V3_2288_BIOS_Ver}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:BIOS version ${version_bios} is lower than the standard version ${Des_V3_2288_BIOS_Ver}" >>${RESULTFILE} 2>&1
            fi
        else
            curVerGeDesVer=$( curVer_ge_desVer "${version_bios}" "${Des_V3_5288_BIOS_Ver}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:BIOS version ${version_bios} is lower than the standard version ${Des_V3_5288_BIOS_Ver}" >>${RESULTFILE} 2>&1
            fi
        fi
    elif [ "$isRetRH5" == "0" ]; then
        curVerGeDesVer=$( curVer_ge_desVer "${version_bios}" "${Des_V5_BIOS_Ver}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:BIOS version ${version_bios} is lower than the standard version ${Des_V5_BIOS_Ver}" >>${RESULTFILE} 2>&1
        fi
    elif [ "$isRetTS" == "0" ]; then
        curVerGeDesVer=$( curVer_ge_desVer "${version_bios}" "${Des_TS_BIOS_Ver}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:BIOS version ${version_bios} is lower than the standard version ${Des_TS_BIOS_Ver}" >>${RESULTFILE} 2>&1
        fi
    fi

    echo "BIOS_Version:${version_bios}"
    return ${isPass}
}

#1.12   3.11        1.15    3.15
function getCPLD4RH()
{
    local Des_2288V3_MBCPLD_Ver=$(grep "^Des_2288V3_MBCPLD_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_5288V3_MBCPLD_Ver=$(grep "^Des_5288V3_MBCPLD_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_2288V5_MBCPLD_Ver=$(grep "^Des_2288V5_MBCPLD_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_5288V5_MBCPLD_Ver=$(grep "^Des_5288V5_MBCPLD_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local CPLD_Version=""
    local isPass="${1}"

    #获取主板CPLD固件版本
    raw_version=$($CMD_GET_MAINBOARD_CPLD_VER)
    raw_version_test=$( echo ${raw_version} | grep -Ev ^[0-9,a-f,' ']+$ )
    if [ -z "${raw_version}" ] || [ ! -z "${raw_version_test}" ] ; then
        version_mb_cpld="NULL"
    else
        version_mb_cpld=$( RHhex2ascii "${raw_version}" )
        version_mb_cpld=$( echo ${version_mb_cpld} | awk -F ")" '{print $2}')
    fi

    IsRH2288Node
    if [ "$?" == "0" ]; then
        # 背板CPLD不再获取版本，因为采购渠道不统一，可以外购
        IsRHV3Node
        if [ "$?" = "0" ]; then
            curVerGeDesVer=$( curVer_ge_desVer "${version_mb_cpld}" "${Des_2288V3_MBCPLD_Ver}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:MainBoard CPLD version ${version_mb_cpld} is lower than the standard version ${Des_2288V3_MBCPLD_Ver}" >>${RESULTFILE} 2>&1
            fi
        else
            curVerGeDesVer=$( curVer_ge_desVer "${version_mb_cpld}" "${Des_2288V5_MBCPLD_Ver}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:MainBoard CPLD version ${version_mb_cpld} is lower than the standard version ${Des_2288V5_MBCPLD_Ver}" >>${RESULTFILE} 2>&1
            fi
        fi
        CPLD_Version="CPLD_Version:MB_CPLD=${version_mb_cpld}"
        echo "CPLD_Version:${CPLD_Version}"
    else
        IsRHV3Node
        if [ "$?" = "0" ]; then
            curVerGeDesVer=$( curVer_ge_desVer "${version_mb_cpld}" "${Des_5288V3_MBCPLD_Ver}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:MainBoard CPLD version ${version_mb_cpld} is lower than the standard version ${Des_5288V3_MBCPLD_Ver}" >>${RESULTFILE} 2>&1
            fi
        else
            curVerGeDesVer=$( curVer_ge_desVer "${version_mb_cpld}" "${Des_5288V5_MBCPLD_Ver}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:MainBoard CPLD version ${version_mb_cpld} is lower than the standard version ${Des_5288V5_MBCPLD_Ver}" >>${RESULTFILE} 2>&1
            fi
        fi

        CPLD_Version="CPLD_Version:MB_CPLD=${version_mb_cpld}"
        echo "CPLD_Version:${CPLD_Version}"
    fi
    return ${isPass}
}

function getCPLD4TS()
{
    local Des_2280TS_MBCPLD_Ver=$(grep "^Des_2280TS_MBCPLD_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_5280TS_MBCPLD_Ver=$(grep "^Des_5280TS_MBCPLD_Ver" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local CPLD_Version=""
    local isPass="${1}"

    #获取主板CPLD固件版本  同时去除背板CPLD固件版本的获取
    raw_version=$($CMD_GET_MAINBOARD_CPLD_VER)
    raw_version_test=$( echo ${raw_version} | grep -Ev ^[0-9,a-f,' ']+$ )
    if [ -z "${raw_version}" ] || [ ! -z "${raw_version_test}" ] ; then
        version_mb_cpld="NULL"
    else
        version_mb_cpld=$( RHhex2ascii "${raw_version}" )
        version_mb_cpld=$( echo ${version_mb_cpld} | awk -F ")" '{print $2}')
    fi

    IsTS5280Node
    if [ "$?" == "0" ]; then
        curVerGeDesVer=$( curVer_ge_desVer "${version_mb_cpld}" "${Des_5280TS_MBCPLD_Ver}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:MainBoard CPLD version ${version_mb_cpld} is lower than the standard version ${Des_5280TS_MBCPLD_Ver}" >>${RESULTFILE} 2>&1
        fi
        CPLD_Version="CPLD_Version:MB_CPLD=${version_mb_cpld}"
        echo "CPLD_Version:${CPLD_Version}"
    else
        curVerGeDesVer=$( curVer_ge_desVer "${version_mb_cpld}" "${Des_2280TS_MBCPLD_Ver}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:MainBoard CPLD version ${version_mb_cpld} is lower than the standard version ${Des_2280TS_MBCPLD_Ver}" >>${RESULTFILE} 2>&1
        fi
        CPLD_Version="CPLD_Version:MB_CPLD=${version_mb_cpld}"
        echo "CPLD_Version:${CPLD_Version}"
    fi

    return ${isPass}
}

#126,129 FirmWare：00.15.01.11 Platform version:00.11
function getSES()
{
    local Des_2288_SesVer=$(grep "^Des_2288_SesVer" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_5288_FirmVer=$(grep "^Des_5288_FirmVer" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_5288_PlatVer=$(grep "^Des_5288_PlatVer" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local command_tool=""
    local isPass="${1}"

    IsRH2288Node
    if [ "$?" == "0" ]; then
        devname=$(lsscsi -g |grep "Expander" |awk '{print $NF}');
        devicetype=$( grep -w "nodeType" ${SANS_FILE} | awk -F"=" '{print $2}' | sed 's/ //g')
        #12:P12E Node, 21:P12X Node, 27:F12B Node, 40:F12B-H Node.  13:P25E Node, 22:P25X Node, 28:F25B Node, 30:S25A Node, 31:S25B Node, 41:S25X Node
        if [[ "12" == "${devicetype}" || "21" == "${devicetype}" || "27" == "${devicetype}" || "40" == "${devicetype}" ]]; then
            command_tool="${EXPANDERTOOL228812}"
        elif [[ "13" == "${devicetype}" || "22" == "${devicetype}" || "28" == "${devicetype}" || "30" == "${devicetype}" || "31" == "${devicetype}" || "41" == "${devicetype}" ]]; then
            command_tool="${EXPANDERTOOL228825}"
        fi
        if [ "X" == "X${command_tool}" ]; then
            echo "SES_Version:NULL"
            isPass=1
            LOG "[${LINENO}] TOOL ${command_tool} is not exist"
            echo "[ERR]INFO:SES tool is not exist,can not get SES version." >>${RESULTFILE} 2>&1
            return ${isPass}
        fi

        sesversion=$( ${command_tool} query ${devname} | grep "Image Version" | awk -F":" '{print $2}'| sed 's/ //g' )
        curVerGeDesVer=$( curVer_ge_desVer "${sesversion}" "${Des_2288_SesVer}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:SES version ${sesversion} is lower than the standard version ${Des_2288_SesVer}" >>${RESULTFILE} 2>&1
            LOG "[${LINENO}] SES version ${sesversion} is lower than the standard version ${Des_2288_SesVer}"
        fi
        echo "SES_Version:${sesversion}"

    else
        local isPMCboard="$(lsscsi |grep enclosu |grep LSI |grep 12G)" #为空就是 PMC背板
        if [ -z "${isPMCboard}" ]; then
            source /opt/huawei/snas/upd/upd_fw/rhfw/ses_utility.sh
            sesversion=$(GetsesFwVer)
            echo "SES_Version:${sesversion}"
            LOG "[${LINENO}] SES Firmware version : PMC backBoard ${sesversion}."
            return ${isPass}
        fi
        command_tool="${EXPANDERTOOL528836}"
        if [ ! -f "${command_tool}" ]; then
            isPass=1
            echo "SES_Version:NULL"
            echo "[ERR]INFO: The tool(/opt/product/snas/bin/g3Xflash) does not exist,can not get SESVersion information." >>${RESULTFILE} 2>&1
            return ${isPass}
        fi
        SES_Firmware=$( ${command_tool} -i 500605b0000273bf get ver | grep "Firmware" | awk -F":" '{print $2}'| sed 's/ //g' )
        curVerGeDesVer=$( curVer_ge_desVer "${SES_Firmware}" "${Des_5288_FirmVer}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:SES Firmware version ${SES_Firmware} is not match the standard version ${Des_5288_FirmVer}" >>${RESULTFILE} 2>&1
        fi

        SES_Platform=$( ${command_tool} -i 500605b0000273bf get ver 3 | grep "Platform Version" |awk -F":" '{print $2}' | sed 's/\t//g' )
        curVerGeDesVer=$( curVer_ge_desVer "${SES_Platform}" "${Des_5288_PlatVer}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:SES Firmware version ${SES_Platform} is not match the standard version ${Des_5288_PlatVer}" >>${RESULTFILE} 2>&1
            LOG "[${LINENO}] SES Firmware version ${SES_Platform} is not match the standard version ${Des_5288_PlatVer}"
        fi
        echo "SES_Version:SES_Firmware=${SES_Firmware},SES_Platform=${SES_Platform}"

    fi
    return ${isPass}
}

function getNVDIMM()
{
    local Des_8G_NVDIMM_Version=$(grep "^Des_8G_NVDIMM_Version" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_16G_NVDIMM_Version=$(grep "^Des_16G_NVDIMM_Version" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local command_tool="/usr/local/nvdimm/nvdimm"
    local isPass="${1}"
    NVDIMM_Version=$( ${command_tool} getfwver | grep "FW Version" |awk -F":" '{print $2}' )

    IsRHV3Node
    if [ "$?" == "0" ]; then
        curVerGeDesVer=$( curVer_ge_desVer "${NVDIMM_Version}" "${Des_8G_NVDIMM_Version}")
        if [ ${curVerGeDesVer} -eq 0 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:NVDIMM version ${NVDIMM_Version} is lower than the standard version ${Des_8G_NVDIMM_Version}" >>${RESULTFILE} 2>&1
            LOG "[${LINENO}] NVDIMM version ${NVDIMM_Version} is lower than the standard version ${Des_8G_NVDIMM_Version}"
        fi
    else
        nvdimmSpeed=$(dmidecode -t17 | grep "Synchronous Non-Volatile" -C 5 | grep "Speed:" | awk -F " " '{print$2}')
        curVerGeDesVer=$( curVer_ge_desVer "${NVDIMM_Version}" "${Des_16G_NVDIMM_Version}")
        # 如果是镁光的版本则默认通过
        if [ ${curVerGeDesVer} -eq 0 -a X"${nvdimmSpeed}" != X"2933" ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:NVDIMM version ${NVDIMM_Version} is lower than the standard version ${Des_16G_NVDIMM_Version}" >>${RESULTFILE} 2>&1
            LOG "[${LINENO}] NVDIMM version ${NVDIMM_Version} is lower than the standard version ${Des_16G_NVDIMM_Version}"
        fi
    fi

    echo "NVDIMM_Version:${NVDIMM_Version}"
    return ${isPass}
}

function getSAS_3008()
{
    local Des_SAS_FW=$(grep "^Des_SAS_FW" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_SAS_NVDATA=$(grep "^Des_SAS_NVDATA" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_SAS_x86_BIOS=$(grep "^Des_SAS_x86_BIOS" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_SAS_EFI_BIOS=$(grep "^Des_SAS_EFI_BIOS" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local sas3flashRltInfo=/tmp/_sas3flashRltInfo_f
    local isPass="${1}"

    /opt/driver/lsisas-mpt3sas-driver/sas3flash -list >${sas3flashRltInfo}
    SAS_FW=$( grep "Firmware Version" ${sas3flashRltInfo} | awk -F":" '{print $2}' |sed 's/ //g' )
    if [ "X" == "X${SAS_FW}" ]; then
        SAS_FW="NULL"
    fi
    SAS_NVDATA=$( grep "NVDATA Version (Default)" ${sas3flashRltInfo} | awk -F":" '{print $2}' |sed 's/ //g' )
    if [ "X" == "X${SAS_NVDATA}" ]; then
        SAS_NVDATA="NULL"
    fi
    SAS_x86_BIOS=$( grep "BIOS Version" ${sas3flashRltInfo} | awk -F":" '{print $2}' |sed 's/ //g' )
    if [ "X" == "X${SAS_x86_BIOS}" ]; then
        SAS_x86_BIOS="NULL"
    fi
    SAS_EFI_BIOS=$( grep "UEFI BSD Version" ${sas3flashRltInfo} | awk -F":" '{print $2}' | sed 's/ //g' )
    if [ "X" == "X${SAS_EFI_BIOS}" ]; then
        SAS_EFI_BIOS="NULL"
    fi

    rm -f ${sas3flashRltInfo}
    if [ "X${Des_SAS_FW}" != "X${SAS_FW}" ]; then
        isPass=$(check_pass ${isPass} 4)
        echo "[ERR]INFO:SAS FW version ${SAS_FW} is not match the standard version ${Des_SAS_FW}" >>${RESULTFILE} 2>&1
        LOG "[${LINENO}] SAS FW version ${SAS_FW} is not match the standard version ${Des_SAS_FW}"
    fi
    if [ "X${Des_SAS_NVDATA}" != "X${SAS_NVDATA}" ]; then
        isPass=$(check_pass ${isPass} 4)
        echo "[ERR]INFO:SAS NVDATA version ${SAS_NVDATA} is not match the standard version ${Des_SAS_NVDATA}" >>${RESULTFILE} 2>&1
        LOG "[${LINENO}] SAS NVDATA version ${SAS_NVDATA} is not match the standard version ${Des_SAS_NVDATA}"
    fi
    if [ "X${Des_SAS_x86_BIOS}" != "X${SAS_x86_BIOS}" ]; then
        isPass=$(check_pass ${isPass} 4)
        echo "[ERR]INFO:SAS x86 BIOS version ${SAS_x86_BIOS} is not match the standard version ${Des_SAS_x86_BIOS}" >>${RESULTFILE} 2>&1
        LOG "[${LINENO}] SAS x86 BIOS version ${SAS_x86_BIOS} is not match the standard version ${Des_SAS_x86_BIOS}"
    fi
    if [ "X${Des_SAS_EFI_BIOS}" != "X${SAS_EFI_BIOS}" ]; then
        isPass=$(check_pass ${isPass} 4)
        echo "[ERR]INFO:SAS EFI BIOS version ${SAS_EFI_BIOS} is not match the standard version ${Des_SAS_EFI_BIOS}" >>${RESULTFILE} 2>&1
        LOG "[${LINENO}] SAS EFI BIOS version ${SAS_EFI_BIOS} is not match the standard version ${Des_SAS_EFI_BIOS}"
    fi
    echo "SAS_Version:SAS_FW=${SAS_FW},SAS_NVDATA=${SAS_NVDATA},SAS_x86_BIOS=${SAS_x86_BIOS},SAS_EFI_BIOS=${SAS_EFI_BIOS}"
    return ${isPass}

}

function getSAS_3416()
{
    local Des_SAS_FW=$(grep "^Des_SAS_3416_FW" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local Des_SAS_NVDATA=$(grep "^Des_SAS_3416_NVDATA" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
    local isPass="${1}"

    SAS_FW=$(cat /sys/class/scsi_host/host*/version_fw)
    if [ "X" == "X${SAS_FW}" ]; then
        SAS_FW="NULL"
    fi
    SAS_NVDATA=$(cat /sys/class/scsi_host/host*/version_nvdata_persistent)
    if [ "X" == "X${SAS_NVDATA}" ]; then
        SAS_NVDATA="NULL"
    fi

    if [ "X${Des_SAS_FW}" != "X${SAS_FW}" ]; then
        isPass=$(check_pass ${isPass} 4)
        echo "[ERR]INFO:SAS FW version ${SAS_FW} is not match the standard version ${Des_SAS_FW}" >>${RESULTFILE} 2>&1
        LOG "[${LINENO}] SAS FW version ${SAS_FW} is not match the standard version ${Des_SAS_FW}"
    fi
    if [ "X${Des_SAS_NVDATA}" != "X${SAS_NVDATA}" ]; then
        isPass=$(check_pass ${isPass} 4)
        echo "[ERR]INFO:SAS NVDATA version ${SAS_NVDATA} is not match the standard version ${Des_SAS_NVDATA}" >>${RESULTFILE} 2>&1
        LOG "[${LINENO}] SAS NVDATA version ${SAS_NVDATA} is not match the standard version ${Des_SAS_NVDATA}"
    fi

    echo "SAS_Version:SAS_FW=${SAS_FW},SAS_NVDATA=${SAS_NVDATA}"
    return ${isPass}
}

function getSAS()
{
    local isPass="${1}"

    is3008SasCard
    if [ $? -eq 0 ];then
        getSAS_3008 "${isPass}"
        isPass=$?
    else
        getSAS_3416 "${isPass}"
        isPass=$?
    fi

    return $isPass
}

#获取OFED版本
function GetOFEDVersion()
{
    local ofedVer="NULL"
    local tmpInfo=""
    local iRet=0
    tmpInfo=$(ofed_info -s)
    iRet=$?
    if [ $? -ne 0 ]; then
        LOG "[$LINENO]get ofed info fail,iRet=${iRet},tmpInfo=${tmpInfo}"
        echo ${ofedVer}
        return 0
    fi

    if [[ "${tmpInfo}" =~ "MLNX_OFED_LINUX-3.2" ]]; then
        ofedVer="3.2"
    elif [[ "${tmpInfo}" =~ "MLNX_OFED_LINUX-4.2" ]]; then
        ofedVer="4.2"
    elif [[ "${tmpInfo}" =~ "OFED-internal-4.4" ]]; then
        ofedVer="4.4"
    elif [[ "${tmpInfo}" =~ "OFA-LINUX-4.17" ]]; then
        ofedVer="4.17"
    else
        ofedVer="NULL"
        LOG "[$LINENO]no match ofed info,tmpInfo=${tmpInfo}"
    fi

    echo ${ofedVer}
    return 0
}

function CheckRoceIBFirmware()
{
    local mstInfo="$1"
    local idx=""
    local netType=""
    local netName=""
    local curfwVersion=""
    local tmpCardtype=""
    local flintInfo=""
    local businfo=""
    local tmpPSID=""
    local tmpinfo=""
    local linkinfo=""
    local iret=0
    local Result=""
    local isPass=0

    for idx in ${ROCE_IB_LIST[@]}; do
        #获取网口名字，businfo
        tmpinfo=$(echo "${mstInfo}" | grep -A1 "${idx}" | grep "bus:dev.fn" | awk '{print $1}' | awk -F'=' '{print $2}')
        businfo=$(echo "${tmpinfo}" | awk -F':' '{print $2 ":" $3}')
        linkinfo=$(find /sys/class/net -maxdepth 1 -type l -exec readlink -e {} \; | grep "${tmpinfo}" | head -1)
        netName=$(basename ${linkinfo})

        curfwVersion=$( ethtool -i $netName | grep "firmware-version" | awk '{print $2}' )
        tmpPSID=$( ethtool -i $netName | grep "firmware-version" | awk '{print $3}' | awk -F "[()]" '{print $2}' )

        #获取网卡类型，PSID为HUA0010110034的网卡做特殊处理，看实际工作是在10g,还是25g，其他类型的依据配置文件获取
        if [ "${tmpPSID}" == "HUA0010110034" ]; then
            tmpinfo=$(ethtool "${netName}" 2>&1)
            iret=$?
            tmpCardtype=$(echo "${tmpinfo}" | grep "Speed:" | awk '{gsub("Mb/s","",$2);printf "%d",$2/1000}')
            if [ $? -eq 0 ] && [ "${tmpCardtype}" == "10" -o "${tmpCardtype}" == "25" ]; then
                netType="${tmpCardtype}g RoCE(V3)"
            else
                echo "[ERR]INFO:BDF (${businfo}) NetName (${netName}). Get network adapter type failed." >>${RESULTFILE} 2>&1
                isPass=1
                LOG "[${LINENO}] cannot recognize the type of card,idx=[${idx}],PSID:${tmpPSID},tmpCardtype=[${tmpCardtype}]，iret=${iret}"
                continue
            fi
        elif [ "${tmpPSID}" == "HUA0000000001" ]; then
            #此卡为外购卡没有配套关系，不需要检查
            Result="${tmpPSID} No need to check because there is no mapping relationship;${Result[@]}"
            LOG "[${LINENO}] skip it,idx=[${idx}],PSID:${tmpPSID}"
            continue
        elif [ "${tmpPSID}" == "HUA0000000023" ];then
            # mlx的RoCE卡-25g RoCE(V5)，已投到4.4使用，配置文件未更新，巡检规避
            netType=$(cat ${ofedConfigPath}/ofed*.conf |grep ";${tmpPSID};"| grep -v '^#' | head -1 |awk -F';' '{print $1}')
            Result="Type=${netType},Version=${curfwVersion};${Result[@]}"
            LOG "[${LINENO}]tmpPSID:${tmpPSID},Type:${netType},BDF:${businfo},NetName:${netName},OFED_Version:${OFED_VER},Firmware_Version:${curfwVersion},iret=[${iret}]."
            continue
        else
            netType=$(grep ";${tmpPSID};" "${ofedConfigPath}/ofed${OFED_VER}.conf" 2>&1 | grep -v '^#' | head -1 |awk -F';' '{print $1}')
            if [ "${netType}X" == "X" ]; then
                netType="null"
                Result="Type=${netType},Version=${curfwVersion};${Result[@]}"
                LOG "[${LINENO}]tmpPSID:${tmpPSID}(Failed to obtain the PSID),Type:${netType},BDF:${businfo},NetName:${netName},OFED_Version:${OFED_VER},Firmware_Version:${curfwVersion},iret=[${iret}]."
                continue
            fi
        fi

        #拼接字符串(网卡类型;网卡PSID;网卡固件版本;OFED版本)
        tmpinfo="${netType};${tmpPSID};${curfwVersion};${OFED_VER}"
        #在配套表格中查询
        cat "${ofedConfigPath}/ofed${OFED_VER}.conf" | grep -v "^#" | grep "${tmpinfo}" >/dev/null
        iret=$?
        #判断是否与配置文件中固件配套关系匹配
        if [ ${iret} -eq 0 ]; then
            Result="Type=${netType},Version=${curfwVersion};${Result[@]}"
        else
            local Des_fwVersion=`cat "${ofedConfigPath}/ofed${OFED_VER}.conf" | grep ${tmpPSID} | grep ${OFED_VER} | grep "${netType}"| awk -F';' '{print $3}'`
            curVerGeDesVer=$( curVer_ge_desVer "${curfwVersion}" "${Des_fwVersion}")
            if [ ${curVerGeDesVer} -eq 0 ]; then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:NET version ${curfwVersion} is not match the standard version ${Des_fwVersion}." >>${RESULTFILE} 2>&1
            fi
            Result="Type=${netType},Version=${curfwVersion};${Result[@]}"
        fi
        LOG "[${LINENO}]tmpPSID:${tmpPSID},Type:${netType},BDF:${businfo},NetName:${netName},OFED_Version:${OFED_VER},Firmware_Version:${curfwVersion},iret=[${iret}]."
    done

    echo "Firmware_Version:${Result}"
    return ${isPass}
}

#ARMofed4.17目前不检查Firmware_Version,只检查ofed版本和网卡配套关系(ofed4.17-->Hi1822#driver: hinic)
function Checkofed_417()
{
    local ethName=$(ls /etc/sysconfig/network-scripts/ifcfg-* | grep -wv lo | sed 's/\/etc\/sysconfig\/network-scripts\/ifcfg-//' | grep -v '\.')
    local ethInfo=""
    local curfwVersion=""
    local businfo=""
    local iRet=0
    local errInfo=""
    local driver=""
    local netType=""
    local Result=""
    local isLite=1
    get_cluster_lite_deploy
    isLite=$?

    for item in ${ethName};do
        if [[ "${item}" =~ "NIC" ]]; then
            LOG "[$LINENO]no check nic,item=${item}"
            continue
        fi
        #单节点lite的后端不插网线,不检查
        if [ "${isLite}" = "0" ]; then
            local backup_port=$(get_lite_backup_netport)
            if [[ "${backup_port}" =~ "${item}" ]]; then
                LOG "[$LINENO]no check lite_backup_netport,item=${item}"
                continue
            fi
        fi
        netName="${item}"
        ethInfo=$(ethtool -i ${item} 2>&1)
        iRet=$?
        if [ ${iRet} -ne 0 ]; then
            errInfo="INFO:Get network adapter info from command(${ethtool -i ${item}}) failed."
            LOG "[${LINENO}]INFO:NetName (${netName}). get network adapter info failed.iRet=${iRet}"
            break
        fi


        businfo=$(echo "${ethInfo}" | grep -w "bus-info" | awk -F':' '{print $3 ":" $4}')
        curfwVersion=$(echo "${ethInfo}" | grep -w "firmware-version" | awk -F':' '{sub("^[ \t]*","",$2);print $2}')
        driver=$(echo "${ethInfo}" | grep -w "driver" | awk -F':' '{sub("^[ \t]*","",$2);print $2}')

        if [ "${driver}X" = "hinicX" ] || [ "${driver}X" = "bondingX" ]; then
            bondInfo=$( cat /proc/net/bonding/${item} )
            if [ $? -eq 0 ];then
                nicbond=$( cat ${bondInfo} | grep NIC | wc -l )
                if [ ${nicbond} -gt 0 ]; then
                    LOG "[$LINENO]no check nic match bond,item=${item}"
                    continue
                fi
            fi
            ethInfo=$(ethtool "${netName}" 2>&1)
            iRet=$?
            tmpCardtype=$(echo "${ethInfo}" | grep "Speed:" | awk '{gsub("Mb/s","",$2);printf "%d",$2/1000}')
            if [ ${iRet} -eq 0 ] && [ "${tmpCardtype}" == "10" -o "${tmpCardtype}" == "25" -o "${tmpCardtype}" == "20" -o "${tmpCardtype}" == "50" ]; then
                netType="${tmpCardtype}g RoCE(1822)"
            else
                errInfo="INFO:BDF (${businfo}) NetName (${netName}). Get network adapter type failed."
                isPass=1
                LOG "[${LINENO}]INFO:BDF (${businfo}) NetName (${netName}). get network adapter type failed. iRet=$iRet"
                break
            fi
        else
            errInfo="INFO:BDF (${businfo}) NetName(${netName}),The OFED (${OFED_VER}) version does not match the network adapter."
            LOG "[${LINENO}]INFO:NetName (${netName}). driver=${driver},businfo=${businfo}"
            break
        fi
        Result="Type=${netType},Version=${curfwVersion};${Result[@]}"
    done

    if [ "${errInfo}X" != "X" ]; then
        isPass=1
        echo "[ERR]${errInfo}" >>${RESULTFILE}
    else
        echo "Firmware_Version:${Result}"
    fi

    return ${isPass}
}

function getIBandRoce()
{
    local ofedVersion=""
    local mstinfo=""
    local iret=0
    local IsRoceFlag=0
    local IsIBFlag=0
    local RoceorIB_Version=""
    local isPass=${1}

    #获取OFED版本号
    OFED_VER="$(GetOFEDVersion)"
    ofedVersion="${OFED_VER}"
    if [ "${ofedVersion}" = "NULL" ]; then
        echo "[ERR]INFO:Get ofed version failed." >>${RESULTFILE}
        isPass=1
        LOG "[$LINENO]get ofed version error"
        echo "RoceorIB_Version:NULL"
        return ${isPass}
    fi

    #arm里面ofed4.4按照V5逻辑从配置文件ofed4.4.conf中获取信息; ofed4.17做特殊处理
    if [ "${OFED_VER}" = "4.17" ]; then
        RoceorIB_Version=$(Checkofed_417)
        if [ "$?" != 0 ]; then
            isPass=1
        fi
        echo "RoceorIB_Version:${RoceorIB_Version}"
        return ${isPass}
    fi

    mst start >/dev/null
    iret=$?
    if [ ${iret} -ne 0 ]; then
        echo "[ERR]INFO:Execute command (mst start) failed!" >>${RESULTFILE}
        isPass=1
        LOG "[${LINENO}]mst start exec failed,iret=$iret."
        echo "RoceorIB_Version:${RoceorIB_Version}"
        return ${isPass}
    fi

    #列出当前系统设备上的ROCE与IB卡对应的MST设备
    mstinfo=$(mst status)
    iret=$?
    if [ ${iret} -ne 0 ]; then
        echo "[ERR]INFO:Execute command (mst status) failed!" >>${RESULTFILE}
        isPass=1
        LOG "[${LINENO}]mst status exec failed,iret=$iret."
        echo "RoceorIB_Version:${RoceorIB_Version}"
        return ${isPass}
    fi

    ROCE_IB_LIST=$(echo "${mstinfo}" | grep -E "mt4117_pciconf|mt4099_pci_|mt4115_pciconf|mt4119_pciconf" | awk '{print $1}')
    if [ -z "${ROCE_IB_LIST}" ] ; then
        echo "RoceorIB_Version:no IB/RoCE network adapter exists"
        LOG "[${LINENO}]No check is required because no IB/RoCE network adapter exists."
        return ${isPass}
    fi

    #检查配置文件是否存在
    if [ ! -f "${ofedConfigPath}/ofed${OFED_VER}.conf" ]; then
        echo "[ERR]INFO:Get network adapter information failed." >>${RESULTFILE}
        isPass=1
        LOG "[${LINENO}]Get Congfig file(${ofedConfigPath}/ofed${OFED_VER}.conf) failed"
        echo "RoceorIB_Version:${RoceorIB_Version}"
        return ${isPass}
    fi

    RoceorIB_Version=$(CheckRoceIBFirmware "${mstinfo}")
    if [ "$?" != 0 ]; then
        isPass=1
    fi
    echo "RoceorIB_Version:${RoceorIB_Version}"
    return ${isPass}
}

function CheckHardwareVersion4RH()
{
    local isPass=0
    local PRODUCT_NAME=$(dmidecode -t 1 | grep "Product Name:" | awk -F: '{print $2}' | sed 's/^\s*//;s/\s*$//')
    
    IBandRock_Version=$( getIBandRoce ${isPass} )
    isPass=$?

    BMC_Version=$( getBMC ${isPass} )
    isPass=$?

    NVDIMM_Version=$( getNVDIMM ${isPass} )
    isPass=$?

    SES_Version=$( getSES  ${isPass} )
    isPass=$?

    SAS_Version=$( getSAS ${isPass} )
    isPass=$?

    BIOS_Version=$( getBIOS ${isPass} )
    isPass=$?

    CPLD_Version=$( getCPLD4RH ${isPass} )
    isPass=$?

    if [ "${PRODUCT_NAME}" == "DP4200" -o "${PRODUCT_NAME}" == "DP2200" ]; then
        isPass=0
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},DP node need not to check hardware version"
    fi

    echo "${SES_Version}||${SAS_Version}||${IBandRock_Version}||${BMC_Version}||${BIOS_Version}||${CPLD_Version}||${NVDIMM_Version}" >>${RESULTFILE} 2>&1
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},Only check 7.1.1.SPC600 version"
}

function CheckHardwareVersion4TS()
{
    local isPass=0

    IBandRoce_Version=$( getIBandRoce ${isPass} )
    isPass=$?

    BMC_Version=$( getBMC ${isPass} )
    isPass=$?

    # 不再检测BBU、IOB、iob_cpld固件版本
    BIOS_Version=$( getBIOS ${isPass} )
    isPass=$?

    CPLD_Version=$( getCPLD4TS ${isPass} )
    isPass=$?

    #注意：TS和RH混合场景需保持输出的参数一致
    SES_Version="SES_Version:--"
    SAS_Version="SAS_Version:--"
    NVDIMM_Version="NVDIMM_Version:--"

    echo "${SES_Version}||${SAS_Version}||${IBandRoce_Version}||${BMC_Version}||${BIOS_Version}||${CPLD_Version}||${NVDIMM_Version}" >>${RESULTFILE} 2>&1
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},Only check 7.1.0 version"
}


function CheckHardwareVersion()
{
    declare -A equalVersion=(
        ["V100R006C30SPC505"]="7.0.2"
        ["V100R006C30SPC510"]="7.0.2"
        ["V100R006C30SPC700"]="7.1.0"
        ["V100R006C30SPC900"]="7.1.1.SPC1"
        ["V100R006C30SPC970"]="7.1.1.SPC600"
    )
    #检查当前集群版本号
    if [ "X${CheckVersion}" != "X${Product_Version}" ] && [ "X${equalVersion[${Product_Version}]}" != "X${CheckVersion}" ]; then
        isPass=0
        echo "INFO:Only check ${CheckVersion} version,Other versions of nodes are not checked." >>${RESULTFILE} 2>&1 #所有节点版本一致
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},Only check ${CheckVersion} version,current version ${Product_Version}."
        exit 0
    fi

    #只检查RH和TS节点
    IsRHNode
    iRetRH=$?
    IsTSNode
    iRetTs=$?
    if [ "${iRetRH}" == "0" ]; then
        CheckHardwareVersion4RH
    elif [ "${iRetTs}" == "0" ]; then
        CheckHardwareVersion4TS
    else
        isPass=0
        echo "SES_Version:not involve||SAS_Version:--||IBandRoce_Version:--||BMC_Version:--||BIOS_Version:--||CPLD_Version:--||NVDIMM_Version:--" >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},Only check RH or TS Node"
    fi
}

CheckHardwareVersion
exit 0


