一个半通用网卡流量监测脚本

!/bin/bash

Instruction:This script can be run under non-root user to check the network flow rate.

Uasage:check_netflow.sh p1 p2 p3

Parameter instruction:

p1-Network name,like eth0,eth1…;

p2-The sleep time(mesured by second) during every mesuring processes;

p3-Output file name.

define the varibles used in the script

v_netinterface=1  #网卡名  v_sleeptime=2 #采样间隔时间(单位:秒,规则:整数)
f_outfile=$3 #输出文件名

网卡输出变量

v_tx_lastnetbytes=1 #TX:上个采样周期网卡字节数
v_tx_nownetbytes=1 #TX:当前采样周期网卡字节数
v_tx_data=1 #TX:周期内传输字节数(单位:Byte)
v_tx_netrate=1 #TX:网卡速率(单位:Byte/s)

网卡输入变量

v_rx_lastnetbytes=1 #RX:上个采样周期网卡字节数
v_rx_nownetbytes=1 #RX:当前采样周期网卡字节数
v_rx_data=1 #RX:周期内传输字节数(单位:Byte)
v_rx_netrate=1 #RX:网卡速率(单位:Byte/s)

remind the Usage of the script

if [[ -z 1||z2 || -z $3 ]];then
echo “#Uasage:check_netflow.sh p1 p2 p3”
echo “#Parameter instruction:”
echo “# p1-Network name,like eth0,eth1…;”
echo “# p2-The sleep time(mesured by second) during every mesuring processes;”
echo “# p3-Output file name.”
echo “Please run the script in the correct way…”
exit
fi

判断输入的网卡名是否对

t_tmp1=ifconfig -a|grep ${v_netinterface}|wc -l
if [[ “${t_tmp1}” -gt “0” ]];then
echo ifconfig -a|grep ${v_netinterface}
else
echo “Please enter the right Ethernet Card Name…”
exit
fi

确认$3输入的输出文件名变量是否存在,并进行一次交互

if [ -f foutfile];thenecho{f_outfile}文件存在,任意键继续…”
read xx
else
echo “文件不存在,将创建,任意键继续…”
read xx
fi

check the OS platform

PLATFORM=/bin/uname
case $PLATFORM in
AIX)
echo “The OS platform is AIX”
;;
Linux)
echo “The OS platform is Linux”
;;
SunOS)
echo “The OS platform is SunOS”
;;
HP-UX)
echo “The OS platform is HP-UX”
;;
OS/390|z/OS)
echo “The OS platform is OS/390|Z/OS”
;;
esac

获取网卡输入输出字节数并计算输出至输出文件

echo “开始计算网卡vnetinterface[{v_sleeptime}秒/次]:”
while true
do
#Calculate the TX rate
v_tx_lastnetbytes=${v_tx_nownetbytes}
v_tx_nownetbytes=/bin/cat /proc/net/dev|grep "${v_netinterface}"|awk {'print $10'}

echo “DEBUG:1111==”${v_tx_nownetbytes}

v_tx_data=$(( ${v_tx_nownetbytes} - ${v_tx_lastnetbytes} ))
v_tx_netrate=$(( ${v_tx_data} / ${v_sleeptime} ))
echo -e `date`":tx_data=${v_tx_nownetbytes}Bytes:""TX rate is:${v_tx_netrate}Bytes/s\c">>${f_outfile}

sleep ${v_sleeptime}
echo -e ".\c"

    #Calculate the RX rate 
    v_rx_lastnetbytes=${v_rx_nownetbytes}
    v_rx_nownetbytes=`/bin/cat /proc/net/dev|grep "${v_netinterface}"|awk {'print $2'}`

echo “DEBUG:2222==”${v_rx_nownetbytes}

    v_rx_data=$(( ${v_rx_nownetbytes} - ${v_rx_lastnetbytes} ))
    v_rx_netrate=$(( ${v_rx_data} / ${v_sleeptime} ))
echo ":rx_data=${v_rx_nownetbytes}Bytes:""RX rate is:${v_rx_netrate}Bytes/s">>${f_outfile}

done

exit
exit

展开阅读全文

没有更多推荐了,返回首页