 #!/bin/bash

#检查存储版本
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source ${G_INSPECT_MMLPATH}/CheckItems
CurInspectNum="352"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}
T3000_front_card_1="A0-1"
T3000_front_card_2="B3-1"
T3000_back_card_1="A0-0"
T3000_back_card_2="B3-0"
RH_front_card_1="SLOT4-1"
RH_front_card_2="SLOT5-1"
RH_back_card_1="SLOT4-0"
RH_back_card_2="SLOT5-0"
cm_db_file="/opt/huawei/snas/etc/cm_conf.db"
cm_conf_file="/opt/huawei/snas/etc/cm.ini"
#TYPE:Front(back) portname: A0-0(A0-1) card:ib_ipoib(10000Mb/s) 
function isRHNODE()
{
    local name=''
    name=$(dmidecode -t 1 | grep "Product Name:" | awk '{print $3}')
    if [ "$name" = "RH2288" -o "$name" = "5288" -o "$name" = "RH2288H" ]; then
        return 0
    else
        return 1
    fi
}

function get_backport_type()
{
    local isPass=0
    local expect_speed="10000Mb/s"
    local back_card_name=$1
    for eth_back in $(sort -V <<< "$back_card_name" || echo $back_card_name)
    do
        
        netcard_speed=$(ethtool ${eth_back} |grep Speed |awk -F":" '{print $2}'| tr -d " ")
        echo "TYPE:back||portname:${eth_back}||driver/speed:${netcard_speed}" >>${RESULTFILE} 2>&1
        #判断后端网口是否是10GE
        if [ "${netcard_speed}" == "${expect_speed}" ];then
            isPass=1
            LOG "[FUNCNAME]:InspectNum ${CurInspectNum} InspectFun ${CurInspectFun}:card ${eth_back} speed is ${netcard_speed}."
        fi
    done   
    
    echo "${isPass}"
}

function get_frontport_GEtype()
{
    local isPass=0
    local expect_ibcard="ib_ipoib"
    local front_card_name=$1
    for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
    do
        netcard_driver=$(ethtool -i $eth_front |grep -w driver |awk '{print $2}')
        echo "TYPE:front||portname:${eth_front}||driver/speed:${netcard_driver}" >>${RESULTFILE} 2>&1
        #判断前端网口是否是IB卡
        if [ "${netcard_driver}" == "${expect_ibcard}" ];then
            isPass=1
            LOG "[FUNCNAME]:InspectNum ${CurInspectNum} InspectFun ${CurInspectFun}:card ${eth_front} type is ${netcard_driver}."
        fi
    done   
    
    echo "${isPass}" 
}

function checkNetPortCfg()
{
    local ret=0
    local mynid=$1
    local flag=1
    isMaster
    flag=$?
    if [ ${flag} -eq 0 ];then
        cat /opt/huawei/deploy/etc/deployconfig.xml|sed 's/></>\n</g' | grep backCard | sort -u |grep -q "\." >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            ret=$(check_pass ${ret} 1)
            echo "[ERR]INFO:Please check the file deployconfig.xml(backCard value)" >> ${RESULTFILE} 2>&1
            LOG "[ERR]INFO:Please check the file deployconfig.xml(backCard value)"
        fi
    fi
    local backIpNum=$(/usr/bin/sqlite3 "${cm_db_file}"  "select IP_ADDR from CM_NODE_IP_T where SUBNET_ID=1 and NID=${mynid}" |wc -l)
    if [ ${backIpNum} -ne 2 ]; then
        ret=$(check_pass ${ret} 4)
        echo "[ERR]INFO:Please check the number of backend IP addresses." >> ${RESULTFILE} 2>&1
    fi
    return ${ret}
}
function CheckFrontAndBackPort()
{
    local isPass=0
    local iRet=""
    if [ ! -f "${cm_conf_file}" ];then
        isPass=1
        echo "INFO:The file ${cm_conf_file} does not exist." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass}"
        return
    fi
    my_nid=$( cat ${cm_conf_file}  | grep "^NID=" | awk -F= '{print $2}' )
    
    back_card_name=$( /usr/bin/sqlite3 "${cm_db_file}"  "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=1 and NID=${my_nid}" )
    front_card_name=$( /usr/bin/sqlite3 "${cm_db_file}" "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=2 and NID=${my_nid}" )
    if [ "X${back_card_name}" == "X" -o "X${front_card_name}" == "X" ];then
        isPass=1
        echo "INFO:Get back_ip network card fiald or Get front network card fiald,Can not check the network." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass}"
        return
    fi
    back_card_type=$( get_backport_type "${back_card_name}")
    front_card_type=$( get_frontport_GEtype "${front_card_name}")
  
    if [ "${back_card_type}" -eq 1 -a "${front_card_type}" -eq 1 -a "${isPass}" != "1" ];then
        isPass=4
        LOG "[FUNCNAME]:InspectNum ${CurInspectNum} InspectFun ${CurInspectFun}:the front card is ib_ipoib,the back card speed is 10GE."
    fi
    
    isRHNODE
    iRet=$?
    if [ 0 -eq ${iRet} ];then
        #RH类节点
        for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
        do
            if [ "X${eth_front}" != "X${RH_front_card_1}" -a "X${eth_front}" != "X${RH_front_card_2}" ];then
                LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                break
            fi   
        done
        for eth_back in $(sort -V <<< "${back_card_name}" || echo ${back_card_name})
        do
            if [ "X${eth_back}" != "X${RH_back_card_1}" -a "X${eth_back}" != "X${RH_back_card_2}" ];then
                LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                break
            fi
        done
    else
        #T000类节点
        for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
        do
            if [ "X${eth_front}" != "X${T3000_front_card_1}" -a "X${eth_front}" != "X${T3000_front_card_2}" ];then
                LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                break
            fi   
        done
        for eth_back in $(sort -V <<< "${back_card_name}" || echo ${back_card_name})
        do
            if [ "X${eth_back}" != "X${T3000_back_card_1}" -a "X${eth_back}" != "X${T3000_back_card_2}" ];then
                LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                break
            fi
        done
    fi 
    
    #检查是否存在前后端公用一个网口
    for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
    do
        card_same=$( echo "${back_card_name}" | grep -w "${eth_front}" )
        if [ "X${card_same}" != "X" ];then
            isPass=1
            break
        fi
        
    done
    checkNetPortCfg ${my_nid}
    iRet=$?
    isPass=$(check_pass $isPass $iRet)
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass}"
    return
}

CheckFrontAndBackPort

