#!/bin/bash
set +x

LOG_FILE="/var/log/inspect.log"
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
CurInspectNum="149"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

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

netcard_oversize_array="mlx5_core|oversize_pkts cxgb4|RxFramesTooLong ixgbe|rx_long_length_errors igb|rx_long_length_errors bnx2|rx_oversize_packets"

my_nid=$(cat /opt/huawei/snas/etc/cm.ini  | grep "^NID=" | awk -F= '{print $2}')
netport_num=0

GetMTU ()
{
    bussiness_type=$1
    if [ "front" = "${bussiness_type}" ];then
        card_name=$(sqlite3 /opt/huawei/snas/etc/cm_conf.db "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=2 and NID=${my_nid}")
        is_succeed=$?
        if [ $is_succeed -ne 0 ];then
            card_name=$(sqlite3 /opt/huawei/snas/etc/cm_conf.db "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=2 and NID=${my_nid}")
        fi
    else
        card_name=$(sqlite3 /opt/huawei/snas/etc/cm_conf.db "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=3 and NID=${my_nid}")
        is_succeed=$?
        if [ $is_succeed -ne 0 ];then
            card_name=$(sqlite3 /opt/huawei/snas/etc/cm_conf.db "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=3 and NID=${my_nid}")
        fi
    fi
    for eth_tmp in $(sort -u <<< "$card_name" || echo $card_name);do
        netcard_driver=$(ethtool -i $eth_tmp |grep -w driver |awk '{print $2}')
        #判断ib网卡，ib网口不需要查询对比mtu
        if [ "$netcard_driver" = "ib_ipoib" ];then
            continue
        fi
        #判断网口是否为bond
        echo $eth_tmp |grep bond >>${RESULTFILE} 2>&1
        is_bond=$?
        if [ $is_bond -eq 0 ];then
            echo $eth_tmp |grep '\.' >>${RESULTFILE} 2>&1
            is_vlan=$?
            if [ $is_vlan -eq 0 ];then
                eth_corr=$(echo '$eth_tmp' |grep '\.'|awk -F'.' '{print $1}')
            else
                eth_corr=$eth_tmp
            fi
            if [ -f /proc/net/bonding/$eth_corr ];then
                member_list=$(cat /proc/net/bonding/$eth_corr |grep -w Slave|grep -w Interface|awk '{print $3}')
                for eth in $(sort -u <<< "$member_list" || echo $member_list);do
                    if [ -f /etc/sysconfig/network-scripts/ifcfg-$eth ];then
                        mtu_str=$(cat /etc/sysconfig/network-scripts/ifcfg-$eth |grep MTU)
                        echo "/etc/sysconfig/network-scripts/ifcfg-$eth $mtu_str" >>${RESULTFILE} 2>&1
                        LOG [$LINENO] "/etc/sysconfig/network-scripts/ifcfg-$eth $mtu_str return $?"
                    fi
                    echo "$eth type: ${bussiness_type}" >>${RESULTFILE} 2>&1
                    mtu_str=$(ifconfig $eth |grep -w "mtu" |awk '{print $NF}')
                    echo "${eth} mtu: ${mtu_str}" >>${RESULTFILE} 2>&1
                    LOG [$LINENO] "$eth $mtu_str return $?"
                    BitErr_RX=$(ifconfig $eth |grep errors |grep -w "RX" |awk '{print $3}')
                    BitErr_TX=$(ifconfig $eth |grep errors |grep -w "TX" |awk '{print $3}')
                    BitErr=$[$BitErr_RX+$BitErr_TX]
                    echo "$eth BitErr: $BitErr" >>${RESULTFILE} 2>&1
                    LOG [$LINENO] "$eth BitErr: $BitErr return $?"
                    netcard_driver=$(ethtool -i $eth |grep -w driver |awk '{print $2}')
                    JumboFrame=$(echo ${netcard_oversize_array} |awk -F"${netcard_driver}" '{print $2}' |awk '{print $1}' |awk -F'|' '{print $2}')
                    JumboFrame_num=""
                    if [ ! -z $JumboFrame ];then
                        JumboFrame_num=$(ethtool -S $eth |grep -w $JumboFrame |awk '{print $NF}')
                    fi
                    echo "$eth $netcard_driver JumboFrameErr: $JumboFrame_num" >>${RESULTFILE} 2>&1
                    LOG [$LINENO] "$eth $netcard_driver JumboFrameErr: $JumboFrame_num return $?"
                    netport_num=$((netport_num+1))
                done
            fi
        else
            if [ -f /etc/sysconfig/network-scripts/ifcfg-$eth_tmp ];then
                mtu_str=$(cat /etc/sysconfig/network-scripts/ifcfg-$eth_tmp |grep -w MTU)
                echo "/etc/sysconfig/network-scripts/ifcfg-$eth_tmp $mtu_str" >>${RESULTFILE} 2>&1
                LOG [$LINENO] "/etc/sysconfig/network-scripts/ifcfg-$eth_tmp $mtu_str return $?"
            fi
            echo "${eth_tmp} type: ${bussiness_type}" >>${RESULTFILE} 2>&1
            mtu_str=$(ifconfig $eth_tmp |grep -w "mtu" |awk '{print $NF}')
            echo "${eth_tmp} mtu: ${mtu_str}" >>${RESULTFILE} 2>&1
            LOG [$LINENO] "$eth_tmp $mtu_str return $?"
            BitErr_RX=$(ifconfig $eth_tmp |grep errors |grep -w "RX" |awk '{print $3}')
            BitErr_TX=$(ifconfig $eth_tmp |grep errors |grep -w "TX" |awk '{print $3}')
            BitErr=$[$BitErr_RX+$BitErr_TX]
            echo "$eth_tmp BitErr: $BitErr" >>${RESULTFILE} 2>&1
            LOG [$LINENO] "$eth_tmp BitErr: $BitErr return $?"
            JumboFrame=$(echo ${netcard_oversize_array} |awk -F"${netcard_driver}" '{print $2}' |awk '{print $1}' |awk -F'|' '{print $2}')
            JumboFrame_num=""
            if [ ! -z $JumboFrame ];then
                JumboFrame_num=$(ethtool -S $eth_tmp |grep -w $JumboFrame |awk '{print $NF}')
            fi
            echo "$eth_tmp $netcard_driver JumboFrameErr: $JumboFrame_num" >>${RESULTFILE} 2>&1
            LOG [$LINENO] "$eth_tmp $netcard_driver JumboFrameErr: $JumboFrame_num return $?"
            netport_num=$((netport_num+1))
        fi
    done
}

GetMTU "manage"
GetMTU "front"

MTU_Value=$(cat /opt/huawei/snas/etc/cm.ini |grep front_mtu_value |awk -F'=' '{print $2}')
echo "front_mtu_value: $MTU_Value" >>${RESULTFILE} 2>&1
LOG [$LINENO] "front_mtu_value: $MTU_Value"
MTU_Value=$(cat /opt/huawei/snas/etc/cm.ini |grep manage_mtu_value |awk -F'=' '{print $2}')
echo "manage_mtu_value: $MTU_Value" >>${RESULTFILE} 2>&1
LOG [$LINENO] "manage_mtu_value: $MTU_Value"

echo "netport_num: $netport_num" >>${RESULTFILE} 2>&1
LOG [$LINENO] "netport_num: $netport_num"
exit 0