统计多网卡流量(2)

面对众多的服务器,每台服务器的网卡名又不固定。所以需要写一个通用的形式来输出网卡的流量

#!/bin/bash
echo "==network_bandwidth"
#==================================network_card==========================================
network_card=$(awk '{if(NR>2)split($1,array,":");print array[1]}' /proc/net/dev  | awk '{if(length !=0) print $0}')

function in_out_old_func(){
for i in ${network_card[*]}
do
    ifconfig ${i} >/dev/null 2>&1
        if [ $? -eq 0 ];then
        echo ${i}_in_old=$(ifconfig $i | awk '$0~/RX bytes/{split($2,array,":");print array[2]}') >> in_out_old
        echo ${i}_out_old=$(ifconfig $i | awk '$0~/TX bytes/{split($2,array,":");print array[2]}') >> in_out_old
        fi
done
}

sleep 1

function in_out_new_func(){
for i in ${network_card[*]}
do
    ifconfig ${i} >/dev/null 2>&1
        if [ $? -eq 0 ];then
        echo ${i}_in_new=$(ifconfig $i | awk '$0~/RX bytes/{split($2,array,":");print array[2]}') >> in_out_new
        echo ${i}_out_new=$(ifconfig $i | awk '$0~/TX bytes/{split($2,array,":");print array[2]}') >> in_out_new
        fi
done
}

function operation_func(){
cat in_out_new | while read line_new
do
    network_card_1=$(echo ${line_new} | awk 'BEGIN{FS="_"}{print $1}')
    in_or_out_1=$(echo ${line_new} | awk 'BEGIN{FS="_"}{print $2}')
    new_value_1=$(echo ${line_new} | awk 'BEGIN{FS="="}{print $2}')
    if [ "${in_or_out_1}" == "in" ];then
        #echo ${network_card}
        old_value_in_1=`cat in_out_old | grep ${network_card_1} | grep "in" | awk 'BEGIN{FS="="}{print $2}'`
        new_minus_old_in_1=$(echo "scale=2;(${new_value_1}/1024.0 - ${old_value_in_1}/1024.0)" | bc)
        if [ $? -ne 0 ];then
            continue
        else
            echo "network_name=${network_card_1},in_bandwidth"=${new_minus_old_in_1} 
        fi
    elif [ "${in_or_out_1}" == "out" ];then
        old_value_out_1=`cat in_out_old | grep ${network_card_1} | grep "out" | awk 'BEGIN{FS="="}{print $2}'`
        new_minus_old_out_1=$(echo "scale=2;(${new_value_1}/1024.0 - ${old_value_out_1}/1024.0)" | bc)
        if [ $? -ne 0 ];then
            continue
        else
            echo "network_name=${network_card_1},out_bandwidth"=${new_minus_old_out_1} 
        fi
    elif [ "${in_or_out_1}" != "out|in" ];then
        network_card_2=$(echo ${line_new} | awk 'BEGIN{FS="_"}{print $1}')
        in_or_out_2=$(echo ${line_new} | awk 'BEGIN{FS="_"}{print $3}')
        new_value_2=$(echo ${line_new} | awk 'BEGIN{FS="="}{print $2}')
            if [ "${in_or_out_2}" == "in" ];then
                old_value_in_2=`cat in_out_old | grep ${network_card_2} | grep "in" | awk 'BEGIN{FS="="}{print $2}'`
                new_minus_old_in_2=$(echo "scale=2;(${new_value_2}/1024.0 - ${old_value_in_2}/1024.0)" | bc)
                if [ $? -ne 0 ];then
                    continue
                else
                    echo "network_name=${network_card_2},in_bandwidth"=${new_minus_old_in_2} 
                fi
            elif [ "${in_or_out_2}" == "out" ];then
                old_value_out_2=`cat in_out_old | grep ${network_card_2} | grep "out" | awk 'BEGIN{FS="="}{print $2}'`
                new_minus_old_out_2=$(echo "scale=2;(${new_value_2}/1024.0 - ${old_value_out_2}/1024.0)" | bc)
                if [ $? -ne 0 ];then
                    continue
                else
                    echo "network_name=${network_card_2},out_bandwidth"=${new_minus_old_in_2} 
                fi
            fi

    fi
done

}

main (){
    in_out_old_func
    in_out_new_func
    operation_func
    >in_out_new
    >in_out_old
}

    main

这个脚本的执行思路是把一秒前所有网卡的流量和一秒后所有网卡的流量放在不同的临时文件中,然后再循环其中的一个文件,用另外一个文件来匹配。

你可能感兴趣的:(统计多网卡流量(2))