#!/bin/bash
set +x

LOG_FILE="/var/log/inspect.log"
RepeatTimes=(1 2 3)

#ȫֱib˿Ϣ
IB_PORTS=""
#ȫֱǷNIC0
NIC0ExistFlag=0

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CommonFunc.sh
source $G_INSPECT_MMLPATH/CheckItems
G_REMOTE_TMP_FILE="/tmp/tmpfile"
CurInspectNum="111"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
fimename=""
>${RESULTFILE}
CHECK_PASSED=0
CHECK_FAILED=1
CHECK_REPAIR=4

isPass=0

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

function set_ifs
{
    IFS_OLD=$IFS
    IFS=$'\n'
}

function restore_ifs
{
    IFS=$IFS_OLD
}

#ȡù
function show_all_nic()
{
    ls /etc/sysconfig/network-scripts/ifcfg-*|grep -wv lo | sed 's/\/etc\/sysconfig\/network-scripts\/ifcfg-//'
}

function is_ib_nic()
{
    local nic=$1
    for time in ${RepeatTimes[*]}; do
        local containIB=`ethtool -i $nic|grep driver|awk -F: '{print $2}'|sed 's/^ *//g'`
        local iRet=$?
        if [ $iRet -eq 75 ]; then
            sleep 10
        else 
            break
        fi
    done
    
    if [ "$containIB" == "ipoib" -o "$containIB" == "ib_ipoib" ]; then
        return 0
    fi
    return 1
}

function is_bond_nic()
{
    local nic=$1
    for time in ${RepeatTimes[*]};do
        local containIB=`ethtool -i $nic|grep driver|awk -F: '{print $2}'|sed 's/^ *//g'`
        local iRet=$?
        if [ $iRet -eq 75 ]; then
            sleep 10
        else 
            break
        fi
    done
    IsVlan=$(echo $containIB |grep VLAN)
    if [ "$containIB" = "bonding" -o "$IsVlan" != "" ]; then
        return 0
    fi
    return 1
}

#һIB
function check_nic()
{
    local errResult=""
    local result=""
    local bOk=0
    local nic=$1
    for time in ${RepeatTimes[*]};do
        local nic_info=`ethtool $nic`
        local iRet=$?
        if [ $iRet -eq 75 ]; then
            sleep 10
        else 
            break
        fi
    done
     
    #link status
    local status=`echo "$nic_info"|grep "Link detected"|awk '{print $3}'`
    if [ "$status" = "yes" ]; then
        status="Normal"
    else
        return $bOk
    fi
 
    #߼⴦
    is_bond_nic $nic
    if [ $? -eq 0 ]; then
           duplex="--"
           speed="--"
           rx_flow="--"
           tx_flow="--"
           flow_Usage="--"
           echo "NetName:$nic||Status:$status||Duplex:$duplex||Speed:$speed||rx_flow:$rx_flow||tx_flow:$tx_flow||flow_Usage:$flow_Usage" >> ${RESULTFILE} 2>&1
           return $bOk
    fi
    
    local speed=`echo "$nic_info"|grep "Speed"|awk '{print $2}'` 
    if [ "$speed" = "" ]; then
        echo "[ERR]Error:No speed info of $nic!" >> ${RESULTFILE} 2>&1
        isPass=1
        speed="--"
        bOK=1
    fi

    local duplex=`echo "$nic_info"|grep "Duplex"|awk '{print $2}'`   
    if [ "$duplex" = "" ]; then
        isPass=1
        echo "[ERR]Error:No duplex info of $nic!" >> ${RESULTFILE} 2>&1
        duplex="--"
        bOK=1
    fi

    if [ "$speed" != "1000Mb/s"  ] && [ "$speed" != "10000Mb/s" ] && [ "$speed" != "25000Mb/s" ] && [ "$speed" != "40000Mb/s" ];then
        bOK=1
        isPass=1
        echo "[ERR]Error:$nic Speed(${speed}) not 1000Mb/s or 10000Mb/s or 25000Mb/s or 40000Mb/s!" >> ${RESULTFILE} 2>&1
    fi
    
    if [ "$speed" = "1000Mb/s" ] && [ "$duplex" != "Full" ];then
        bOK=1
        isPass=1
        echo "[ERR]Error:$nic Duplex($duplex) not Full!" >> ${RESULTFILE} 2>&1
    fi
    
    ##################
    #ȡ
    fimename="RT_FLOW"
    sar -n DEV 1 2 > $G_REMOTE_TMP_FILE$fimename
    rx_flow=`cat $G_REMOTE_TMP_FILE$fimename | grep $nic | awk NR==2 | awk '{print $5}'`
    tx_flow=`cat $G_REMOTE_TMP_FILE$fimename | grep $nic | awk NR==2 | awk '{print $6}'`

    Now_Flow=`echo $rx_flow + $tx_flow|bc`    
    LOG "[$LINENO]nic:$nic,rx_flow:$rx_flow,tx_flow:$tx_flow,Now_Flow:$Now_Flow"

    flow_Usage=0
    if [ "$speed" != "--" ] ;then 
        #xxxxMb/s תΪΪ KB(*1024/8) ,ֵΪ80%
        tmp_speed=`echo $speed | awk '{print $1+0}'`
        tmp_speed1=`expr $tmp_speed \* 128`
        tmp_speed11=`echo $tmp_speed1 \* 0.8|bc`
        
        if [ "$tmp_speed1" != "0" ];then
            flow_Usage=`echo "scale=2;$Now_Flow*100/$tmp_speed1"|bc`               
        else
            #Ϣʹ[ERR]ʶ
            bOK=1
            isPass=1
            echo "[ERR]Error:$nic get Now_Flow fail!" >> ${RESULTFILE} 2>&1
        fi
		
        LOG "[$LINENO]tmp_speed:$tmp_speed,tmp_speed1:$tmp_speed1,tmp_speed11:$tmp_speed11,Now_Flow:$Now_Flow,flow_Usage:$flow_Usage"
    fi    
	
    first=$(echo ${flow_Usage:0:1})
    if [ "$first" == '.' ];then
    	flow_Usage=0$flow_Usage
    fi

    echo "NetName:$nic||Status:$status||Duplex:$duplex||Speed:$speed||rx_flow:${rx_flow}kB/s||tx_flow:${tx_flow}kB/s||flow_Usage:${flow_Usage}%" >> ${RESULTFILE} 2>&1
    
    return $bOK
}
isZeroDomain()
{
    local domainID=$(grep SubDomain /opt/huawei/snas/etc/cm.ini|awk -F "=" '{print $2}')
    return $domainID
}
function check_all_nic()
{
    local bOK=0
    local localomnic=""
    local localomnic_v6=""
    local nics=`show_all_nic`
    set_ifs
    for nic in $nics; 
    do    
        #0NIC0״̬
        if [[ "$nic" =~ "NIC" ]]; then
            isZeroDomain
            if [ $? -ne 0 ]; then
                LOG "[$LINENO] The $nic isn't in subdomian of zero." 
                continue;
            fi
            #Ƿǹ
            omnic=$(cat /opt/huawei/snas/etc/cm.ini |grep omCard |awk -F= '{print $2}'|tr -d ' ')
            if [[ "${omnic}" =~ "bond" ]];then
                LOG "[$LINENO] The $omnic is match bond. nic is ${nic}"
                local bBondcard=0
                for bondcard in $(cat /etc/sysconfig/network-scripts/ifcfg-${omnic} | grep BONDING_SLAVE | awk -F= '{print $2}'|tr -d ' '|tr -d "'")
                do
                    if [ "X${nic}" = "X${bondcard}" ];then
                        bBondcard=1
                        break;
                    fi
                done
                localomnic=$(cat /opt/huawei/snas/etc/cm.ini |grep -w LocalOMCard |awk -F= '{print $2}'|tr -d ' ')
                localomnic_v6=$(cat /opt/huawei/snas/etc/cm.ini |grep -w LocalOMCard_v6 |awk -F= '{print $2}'|tr -d ' ')
                
                if [ "${nic}" != "${localomnic}" -a "${nic}" != "${localomnic_v6}" -a ${bBondcard} -ne 1 ];then
                    LOG "[$LINENO] The $nic isn't omCard and localOmCard(${localomnic}, ${localomnic_v6})" 
                    continue;
                fi      
            elif [ "$nic" != "$omnic" ];then
                LOG "[$LINENO] The $nic isn't omCard."
                continue;
            fi
        fi
    
        #IB,жIB ʱ浽IB_PORTS
        is_ib_nic $nic
        if [ $? -eq 0 ]; then
            IB_PORTS="$nic $IB_PORTS"
            continue
        fi
        
        #IBڵڴ˼
        check_nic $nic
        if [ $? -ne 0 ]; then
            bOK=1
        fi
    done
    restore_ifs
    return $bOK
}

#ҪúIB_PORTS
function check_all_ib()
{
    #ofed4.171822ûibstat
    local ofedVer=$(GetOFEDVersion)
    if [ "${ofedVer}" == "4.17" ];then
        return
    fi
    local ib_info=`ibstat 2>/dev/null`
    local ret=$?
    if [ $ret -ne 0 ]; then
        #isPass=1
        echo "[ERR]Error:Failed to get ib info!" >> ${RESULTFILE} 2>&1
        return $ret
    fi
    
    #ibĻȡ״̬
    for ib_port in $IB_PORTS; do
    
        #0NIC0״̬
        if [ "$NIC0ExistFlag" == 0 ] && [ "$ib_port" == "NIC0" ] ; then
            LOG "[$LINENO] NIC0ExistFlag=$NIC0ExistFlag,ib_port=$ib_port"
            continue;
        fi
    
        mac=`cat /sys/class/net/$ib_port/address  | tr -d ":"`
        guid=${mac:24:40}
        Status=`ibstat |grep  -B6 "Port GUID: 0x$guid" |grep "Physical state"|awk '{print $3}' 2>/dev/null`
        IBSpeed=`ibstat |grep  -B6 "Port GUID: 0x$guid" |grep "Rate" | awk '{print $2}' 2>/dev/null`
        
        LOG "[$LINENO]:ib_port:$ib_port,Status:$Status,IBSpeed:$IBSpeed"
       
        if [ "$Status" = "LinkUp" ];then
            Status="Normal"
        else
            isPass=1
            echo "[ERR]Error:IB $ib_port Status($Status) is not Normal!" >> ${RESULTFILE} 2>&1
            Status="Fault"
            ret=1
        fi

        Duplex="--"   
        
        ##################
        set_ifs
        #ȡ
        fimename="RT_FLOW"
        sar -n DEV 1 2 > $G_REMOTE_TMP_FILE$fimename
        rx_flow=`cat $G_REMOTE_TMP_FILE$fimename | grep $ib_port | awk NR==2 | awk '{print $5}'`
        tx_flow=`cat $G_REMOTE_TMP_FILE$fimename | grep $ib_port | awk NR==2 | awk '{print $6}'`

        Now_Flow=`echo $rx_flow + $tx_flow|bc`    
        LOG "[$LINENO]ib_port:$ib_port,rx_flow:$rx_flow,tx_flow:$tx_flow,Now_Flow:$Now_Flow"
        restore_ifs
 
        flow_Usage=0
        if [ "$IBSpeed" != "--" ] ;then 
            #xxxxGb/s תΪΪ KB(*1024*1024/8) ,ֵΪ80%
            tmp_speed1=`expr $IBSpeed \* 131072`
            tmp_speed11=`echo $tmp_speed1 \* 0.8|bc`
            
            if [ "$tmp_speed1" != "0" ];then
                flow_Usage=`echo "scale=2;$Now_Flow*100/$tmp_speed1"|bc` 
            else
                 bOK=1
                 #Ϣʹ[ERR]ʶ  
                 isPass=1 
                 echo "[ERR]Error:IB $ib_port get Now_Flow fail!" >> ${RESULTFILE} 2>&1
            fi        
            
            LOG "[$LINENO]tmp_speed:$tmp_speed,tmp_speed1:$tmp_speed1,tmp_speed11:$tmp_speed11,Now_Flow:$Now_Flow,flow_Usage:$flow_Usage"
        fi    
		
        first=$(echo ${flow_Usage:0:1})
        if [ "$first" == '.' ];then
            flow_Usage=0$flow_Usage
        fi
		

        echo "NetName:$ib_port||Status:$Status||Duplex:$Duplex||Speed:${IBSpeed}Gb/s||rx_flow:${rx_flow}kB/s||tx_flow:${tx_flow}kB/s||flow_Usage:${flow_Usage}%" >> ${RESULTFILE} 2>&1
    done
}

check_all_nic
dmidecode -t 4 | grep -q "Family: Atom"
if [ $? -ne 0 ]; then
    check_all_ib
fi

if [ 0 -eq ${isPass} ]; then 
    echo "${CurInspectFun}_Pass ${CHECK_PASSED}" >>${RESULTFILE} 2>&1
else
    echo "${CurInspectFun}_Pass ${CHECK_REPAIR}" >>${RESULTFILE} 2>&1
fi

[ -f "$G_REMOTE_TMP_FILE$fimename" ] && rm -f "$G_REMOTE_TMP_FILE$fimename"
#Ƿسɹ[ERR]ʶǷ
exit 0

