#!/bin/bash
set +x

#检查存储版本
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=$( grep "^T3000_front_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
T3000_front_card_2=$( grep "^T3000_front_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
T3000_back_card_1=$( grep "^T3000_back_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
T3000_back_card_2=$( grep "^T3000_back_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')

RHV3_front_card_1=$( grep "^RHV3_front_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
RHV3_front_card_2=$( grep "^RHV3_front_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
RHV3_back_card_1=$( grep "^RHV3_back_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
RHV3_back_card_2=$( grep "^RHV3_back_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')

RHV5_front_card_1=$( grep "^RHV5_front_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
RHV5_front_card_2=$( grep "^RHV5_front_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
RHV5_back_card_1=$( grep "^RHV5_back_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
RHV5_back_card_2=$( grep "^RHV5_back_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')

C72_front_card_1=$( grep "^C72_front_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
C72_front_card_2=$( grep "^C72_front_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
C72_back_card_1=$( grep "^C72_back_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
C72_back_card_2=$( grep "^C72_back_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')

TS1_front_card_1=$( grep "^TS1_front_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
TS1_front_card_2=$( grep "^TS1_front_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
TS1_back_card_1=$( grep "^TS1_back_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
TS1_back_card_2=$( grep "^TS1_back_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')

TS2_front_card_1=$( grep "^TS2_front_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
TS2_front_card_2=$( grep "^TS2_front_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
TS2_back_card_1=$( grep "^TS2_back_card_1" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')
TS2_back_card_2=$( grep "^TS2_back_card_2" /opt/huawei/snas/script/inspect_mml/quality_inspect.ini |awk -F"=" '{print $2}')

cm_db_file="/opt/huawei/snas/etc/cm_conf.db"
cm_conf_file="/opt/huawei/snas/etc/cm.ini"
source /opt/node_type_recognize.sh
source ${G_INSPECT_MMLPATH}/CommonFunc.sh
#TYPE:Front(back) portname: A0-0(A0-1) card:ib_ipoib(10000Mb/s)
function get_backport_type()
{
    local is10GE=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_driver=$(ethtool -i ${eth_back} |grep -w driver |awk '{print $2}')
        netcard_speed=$(ethtool ${eth_back} |grep Speed |awk -F":" '{print $2}'| tr -d " ")
        echo "TYPE:backend||portname:${eth_back}||driver/speed:${netcard_driver}/${netcard_speed}" >>${RESULTFILE} 2>&1
        #判断后端网口是否是10GE
        if [ "${netcard_speed}" == "${expect_speed}" ];then
            is10GE=1
            LOG "[FUNCNAME]:InspectNum ${CurInspectNum} InspectFun ${CurInspectFun}:card ${eth_back} speed is ${netcard_speed}."
        fi
    done

    echo "${is10GE}"
}

function get_frontport_GEtype()
{
    local isIBCard=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:frontend||portname:${eth_front}||driver/speed:${netcard_driver}/${netcard_speed}" >>${RESULTFILE} 2>&1
        #判断前端网口是否是IB卡
        if [ "${netcard_driver}" == "${expect_ibcard}" ];then
            isIBCard=1
            LOG "[FUNCNAME]:InspectNum ${CurInspectNum} InspectFun ${CurInspectFun}:card ${eth_front} type is ${netcard_driver}."
        fi
    done

    echo "${isIBCard}"
}

#TS前后端有两种组合形式
function get_network_type4front()
{
    card=$1
    case $card in
        "${TS1_front_card_1}"|"${TS1_front_card_2}")
            echo 1
            ;;
        "${TS2_front_card_1}"|"${TS2_front_card_2}")
            echo 2
            ;;
        *)
            #3表示非法
            echo 3
            ;;
    esac
}

function get_network_type4back()
{
    card=$1
    case $card in
        "${TS1_back_card_1}"|"${TS1_back_card_2}")
            echo 1
            ;;
        "${TS2_back_card_1}"|"${TS2_back_card_2}")
            echo 2
            ;;
        *)
            #3表示非法
            echo 3
            ;;
    esac
}

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=""
    local count=0
    local cardTypePre=0
    local cardTypeTmp=0
    local cardType=0
    local back_card_name=""
    local errResult=""
    if [ ! -f "${cm_conf_file}" ];then
        isPass=1
        echo "[ERR]INFO:The file(cm.ini) 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}' )
    get_cluster_lite_deploy 2>/dev/null
    isRetLite=$?
    if [ 0 -eq ${isRetLite} ];then
        echo "TYPE:backend||portname:not involve||driver/speed:not involve" >>${RESULTFILE} 2>&1
    else
        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}" )
    fi
    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${front_card_name}" == "X" ] || [ 1 -eq ${isRetLite} -a "X${back_card_name}" == "X" ];then
        isPass=1
        echo "[ERR]INFO:Get backend network card failed or get frontend network card failed from DB." >>${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
        echo "[ERR]INFO:The network of the frontend is IB and network of the backend is 10GE." >>${RESULTFILE} 2>&1
        LOG "[FUNCNAME]:InspectNum ${CurInspectNum} InspectFun ${CurInspectFun}:the front card is ib_ipoib,the back card speed is 10GE."
    fi

    # 针对已部署的局点，如果双后端或者双前端都部署在同一个网卡上，除非重新部署，否者没有办法解决，所以，针对这种，直接巡检通过吧。没必要纠结了。
    IsRHNode
    iRetRh=$?
    IsC72Node
    iRetC72=$?
    IsT3000Node
    iRetT3000=$?
    IsTSNode
    iRetTSNode=$?
    if [ 0 -eq ${iRetRh} ];then
        #RH v3类节点
        IsRHV3Node
        if [ 0 -eq $? ];then
            for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
            do
                if [ "X${eth_front}" != "X${RHV3_front_card_1}" -a "X${eth_front}" != "X${RHV3_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${RHV3_back_card_1}" -a "X${eth_back}" != "X${RHV3_back_card_2}" ];then
                    LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                    break
                fi
            done
        else
            #RH v5类节点
            for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
            do
                if [ "X${eth_front}" != "X${RHV5_front_card_1}" -a "X${eth_front}" != "X${RHV5_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${RHV5_back_card_1}" -a "X${eth_back}" != "X${RHV5_back_card_2}" ];then
                    LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                    break
                fi
            done
        fi
    elif [ 0 -eq ${iRetC72} ];then
        #RH C72节点
        for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
        do
            if [ "X${eth_front}" != "X${C72_front_card_1}" -a "X${eth_front}" != "X${C72_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${C72_back_card_1}" -a "X${eth_back}" != "X${C72_back_card_2}" ];then
                LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                break
            fi
        done
    elif [ 0 -eq ${iRetT3000} ];then
        #T3000类节点
        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
    elif [ 0 -eq ${iRetTSNode} ];then
        #TS类节点有两种组合
        for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
        do
            let count++
            cardTypeTmp=$( get_network_type4front $eth_front )
            if [ "${cardTypeTmp}" == "3" ] || [ "${count}" != "1" -a "${cardTypeTmp}" != "${cardTypePre}" ]; then
                LOG "the front card is invalid ${count} ${cardTypeTmp} ${cardTypePre}"
                LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                break
            fi
            cardTypePre="${cardTypeTmp}"
        done
        if [ "${isPass}" != "4" ]; then
            count=0
            cardType="${cardTypeTmp}"
            for eth_back in $(sort -V <<< "${back_card_name}" || echo ${back_card_name})
            do
                let count++
                cardTypeTmp=$( get_network_type4back $eth_back )
                if [ "X${eth_back}" == "X${TS1_back_card_1}" ]; then
                    continue
                fi
                if [ "${cardTypeTmp}" == "3" ] || [ "${cardTypeTmp}" != "${cardType}" ] || [ "${count}" != "1" -a "${cardTypeTmp}" != "${cardTypePre}" ]; then
                    LOG "the back card ${eth_back} is invalid ${cardTypeTmp} ${cardType} ${count} ${cardTypePre}"
                    LOG "[ERR]INFO:The frontend and backend ports are not used as recommaended."
                    break
                fi
                cardTypePre="${cardTypeTmp}"
            done
        fi
    else
        isPass=1
    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
            echo "[ERR]INFO:The frontend and backend cannot share the same network port(${eth_front})." >>${RESULTFILE} 2>&1
            isPass=1
            break
        fi

    done
    if [ -n "${errResult}" ]; then
        echo "${errResult}" >>${RESULTFILE} 2>&1
    fi
    checkNetPortCfg ${my_nid}
    iRet=$?
    isPass=$(check_pass $isPass $iRet)
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass}"
    return
}

CheckFrontAndBackPort

