#!/bin/bash

LOG ()
{
    local time_now=`date "+%Y/%m/%d %H:%M:%S"`
    echo "[$time_now] $@" >> /var/log/dep.log
}
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 -V <<< "$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
    is_bond=$?
    if [ $is_bond -eq 0 ];then
        echo $eth_tmp |grep '\.'
        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 -V <<< "$member_list" || echo $member_list);do 
                if [ -f /etc/sysconfig/network/ifcfg-$eth ];then
                    mtu_str=$(cat /etc/sysconfig/network/ifcfg-$eth |grep MTU)
                    echo "/etc/sysconfig/network/ifcfg-$eth $mtu_str"
                    LOG [GetNetportMTU][$LINENO] "/etc/sysconfig/network/ifcfg-$eth $mtu_str return $?"
                fi
                echo "$eth type: ${bussiness_type}"
                mtu_str=$(ifconfig $eth |sed -n 's/^.*\(MTU:[0-9]\+\).*$/\1/p' |awk -F':' '{print $2}')
                echo "${eth} mtu: ${mtu_str}"
                LOG [GetNetportMTU][$LINENO] "$eth $mtu_str return $?"
                BitErr_RX=$(ifconfig $eth |grep errors |grep RX |awk '{print $3}'|awk -F':' '{print $2}')
                BitErr_TX=$(ifconfig $eth |grep errors |grep TX |awk '{print $3}'|awk -F':' '{print $2}')
                BitErr=$[$BitErr_RX+$BitErr_TX]
                echo "$eth BitErr: $BitErr"
                LOG [GetNetportMTU][$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"
                LOG [GetNetportMTU][$LINENO] "$eth $netcard_driver JumboFrameErr: $JumboFrame_num return $?"
                netport_num=$((netport_num+1))
            done
        fi
    else
        if [ -f /etc/sysconfig/network/ifcfg-$eth_tmp ];then
            mtu_str=$(cat /etc/sysconfig/network/ifcfg-$eth_tmp |grep -w MTU)
            echo "/etc/sysconfig/network/ifcfg-$eth_tmp $mtu_str"
            LOG [GetNetportMTU][$LINENO] "/etc/sysconfig/network/ifcfg-$eth_tmp $mtu_str return $?"
        fi
        echo "${eth_tmp} type: ${bussiness_type}"
        mtu_str=$(ifconfig $eth_tmp |sed -n 's/^.*\(MTU:[0-9]\+\).*$/\1/p' |awk -F':' '{print $2}')
        echo "${eth_tmp} mtu: ${mtu_str}"
        LOG [GetNetportMTU][$LINENO] "$eth_tmp $mtu_str return $?"
        BitErr_RX=$(ifconfig $eth_tmp |grep errors |grep RX |awk '{print $3}'|awk -F':' '{print $2}')
        BitErr_TX=$(ifconfig $eth_tmp |grep errors |grep TX |awk '{print $3}'|awk -F':' '{print $2}')
        BitErr=$[$BitErr_RX+$BitErr_TX]
        echo "$eth_tmp BitErr: $BitErr"
        LOG [GetNetportMTU][$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"
            LOG [GetNetportMTU][$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"
LOG [GetNetportMTU][$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"
LOG [GetNetportMTU][$LINENO] "manage_mtu_value: $MTU_Value"

echo "netport_num: $netport_num"
LOG [GetNetportMTU][$LINENO] "netport_num: $netport_num"
exit 0