linux下也有命令可以实现。
- iftop
- iftraf–Linux Interface Traffic Monitor
- nload–monitors network traffic and bandwidth usage
- sar
一个强大的工具(好像这些工具都蛮强的),参数很多,有时间man一下。 -n参数很有用,他有6个不同的开关:DEV | EDEV | NFS | NFSD | SOCK | ALL 。DEV显示网络接口信息,EDEV显示关于网络错误的统计数据,NFS统计活动的NFS客户端的信息,NFSD统计NFS服务器的信息,SOCK显示套 接字信息,ALL显示所有5个开关。它们可以单独或者一起使用。我们现在要用的就是-n DEV了。 输入命令:sar –n DEV 1 4
命令后面 1 4 意思是:每一秒钟取一次值,取四次。 IFACE:LAN接口
rxpck/s:每秒钟接收的数据包
txpck/s:每秒钟发送的数据包
rxbyt/s:每秒钟接收的字节数
txbyt/s:每秒钟发送的字节数
rxcmp/s:每秒钟接收的压缩数据包
txcmp/s:每秒钟发送的压缩数据包
rxmcst/s:每秒钟接收的多播数据包
- ifstat
- watch more /proc/net/dev
IFACE:LAN接口 rxerr/s:每秒钟接收的坏数据包 txerr/s:每秒钟发送的坏数据包 coll/s:每秒冲突数 rxdrop/s:因为缓冲充满,每秒钟丢弃的已接收数据包数 txdrop/s:因为缓冲充满,每秒钟丢弃的已发送数据包数 txcarr/s:发送数据包时,每秒载波错误数 rxfram/s:每秒接收数据包的帧对齐错误数 rxfifo/s:接收的数据包每秒FIFO过速的错误数 txfifo/s:发送的数据包每秒FIFO过速的错误数 下面几个更简单的方法,虽然可以看到流量的统计信息,但是太简单,而且也不直观。
- watch ifconfig 这个就不用多说了吧 :)
脚本一:
- #!/bin/bash
-
eth=eth0
-
RXpre=$(ifconfig ${eth} | grep bytes | awk '{print $2}'| awk -F":" '{print $2}')
-
TXpre=$(ifconfig ${eth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')
-
sleep 1
-
RXnext=$(ifconfig ${eth} | grep bytes | awk '{print $2}'| awk -F":" '{print $2}')
-
TXnext=$(ifconfig ${eth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')
-
-
echo RX ----- TX
-
echo "$(((${RXnext}-${RXpre})/1024))KB/s $(((${TXnext}-${TXpre})/1024))KB/s"
脚本二:
- #!/bin/bash
-
-
# test network width
-
-
function usage
-
-
{
-
-
echo "Usage: $0 ethX Y "
-
-
echo " e.g. $0 eth0 2″
-
-
exit 65
-
-
}
-
if [ $# -lt 2 ];then
-
usage
-
fi
-
typeset in in_old dif_in
-
typeset out out_old dif_out
-
typeset timer
-
typeset eth
-
-
eth=$1
-
timer=$2
-
-
in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
-
out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
-
-
while true
-
do
-
sleep ${timer}
-
in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
-
out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
-
dif_in=$(((in-in_old)/timer))
-
dif_out=$(((out-out_old)/timer))
-
echo "IN: ${dif_in} Byte/s OUT: ${dif_out} Byte/s"
-
in_old=${in}
-
out_old=${out}
-
done
-
exit 0
脚本三:
- #!/bin/bash
-
echo -n "请输入需要查询的网卡的接口:"
-
read eth
-
echo "你要查询的网卡接口为"$eth
-
echo -n "输入需要等到的时间(秒):"
-
read sec
-
echo "你计算的是"$sec"秒内的平均流量,请等待."
-
infirst=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
-
outfirst=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
-
sumfirst=$(($infirst+$outfirst))
-
sleep $sec"s"
-
inend=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
-
outend=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
-
sumend=$(($inend+$outend))
-
sum=$(($sumend-$sumfirst))
-
echo $sec"秒内总流量为:"$sum"bytes"
-
aver=$(($sum/$sec))
-
echo "平均流量为:"$aver"bytes/sec"
脚本四:
- #!/bin/bash
-
e0_in_old=$(ifconfig eth0 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
-
e0_out_old=$(ifconfig eth0 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |tail -n1)>e0_total_old=$(($e0_in_old + $e0_out_old))
-
e1_in_old=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
-
e1_out_old=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |tail -n1)
-
e1_total_old=$(($e1_in_old + $e1_out_old))
-
while true
-
do
-
sleep 1
-
e0_in_new=$(ifconfig eth0 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
-
e0_out_new=$(ifconfig eth0 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |tail -n1)
-
e0_total_new=$(($e0_in_new + $e0_out_new))
-
e1_in_new=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
-
e1_out_new=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |tail -n1)
-
e1_total_new=$(($e1_in_new + $e1_out_new))
-
e0_in=$((($e0_in_new - $e0_in_old) * 8))
-
e0_out=$((($e0_out_new - $e0_out_old) * 8))
-
e0_total=$((($e0_total_new - $e0_total_old) * 8))
-
e1_in=$((($e1_in_new - $e1_in_old) * 8))
-
e1_out=$((($e1_out_new - $e1_out_old) * 8))
-
e1_total=$((($e1_total_new - $e1_total_old) * 8))
-
echo -e "eth0 IN: $e0_in \t b/s OUT: $e0_out \t b/s TOTAL: $e0_total \t b/s"
-
echo -e "eth1 IN: $e1_in \t b/s OUT: $e1_out \t b/s TOTAL: $e1_total \t b/s\n"
-
e0_in_old=$e0_in_new
-
e0_out_old=$e0_out_new
-
e0_total_old=$e0_total_new
-
e1_in_old=$e1_in_new
-
e1_out_old=$e1_out_new
-
e1_total_old=$e1_total_new
-
done
-
-
exit 0
脚本五:
- #!/bin/bash
-
# osdba 2008.10.22 monitor the interface's network traffic.
-
if [ $# -ne 3 ];then
-
echo Useage : $0 interface interval count
-
echo Example: $0 eth0 2 10
-
exit
-
fi
-
eth=$1
-
count=$3
-
interval=$2
-
inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
-
if [ -z "$inbytesfirst" ];then
-
echo The network interface $eth is not exits!
-
exit 1;
-
fi
-
outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
-
inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
-
outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
-
sleep $interval"s"
-
i=0
-
while [ "$i" -lt "$count" ]
-
do
-
inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
-
outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
-
inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
-
outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
-
bytesin=$(($inbytesend-$inbytesfirst))
-
bytesout=$(($outbytesend-$outbytesfirst))
-
packetsin=$(($inpacketsend-$inpacketsfirst))
-
packetsout=$(($outpacketsend-$outpacketsfirst))
-
if [ "$bytesin" -lt "0" ];then
-
bytesin=$((4294967295-$inbytesfirst+$inbytesend))
-
#echo bytesin $bytesin $inbytesfirst $inbytesend
-
fi
-
if [ "$bytesout" -lt "0" ];then
-
bytesout=$((4294967295-$outbytesfirst+$outbytesend))
-
#echo bytesout $bytesout $outbytesfirst $outbytesend
-
fi
-
if [ "$packetsin" -lt "0" ];then
-
packetsin=$((4294967295-$inpacketsfirst+$inpacketsend))
-
#echo packetsin $packetsin $inpacketsfirst $inpacketsend
-
fi
-
if [ "$packetsout" -lt "0" ];then
-
packetsout=$((4294967295-$outpacketsfirst+$outpacketsend))
-
#echo packetsout $packetsout $outpacketsfirst $outpacketsend
-
fi
-
bytesin=$(($bytesin/$interval))
-
bytesout=$(($bytesout/$interval))
-
packetsin=$(($packetsin/$interval))
-
packetsout=$(($packetsout/$interval))
-
sumbytesin=$(($sumbytesin+$bytesin))
-
sumbytesout=$(($sumbytesout+$bytesout))
-
sumpacketsin=$(($sumpacketsin+$packetsin))
-
sumpacketsout=$(($sumpacketsout+$packetsout))
-
if [ $(($i%20)) -eq 0 ];then
-
echo " ifname | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s"
-
echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
-
fi
-
echo $eth $bytesin $bytesout $packetsin $packetsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'
-
inbytesfirst=$inbytesend
-
outbytesfirst=$outbytesend
-
inpacketsfirst=$inpacketsend
-
outpacketsfirst=$outpacketsend
-
-
i=$(($i+1))
-
sleep $interval"s"
-
done
-
sumbytesin=$(($sumbytesin/$i))
-
sumbytesout=$(($sumbytesout/$i))
-
sumpacketsin=$(($sumpacketsin/$i))
-
sumpacketsout=$(($sumpacketsout/$i))
-
echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
-
echo Average $sumbytesin $sumbytesout $sumpacketsin $sumpacketsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'
脚本六:
- #!/bin/bash
-
# test network width
-
function usage
-
{
-
echo "Usage: $0?? "
-
echo "?????? e.g. $0 eth0 2"
-
exit 65
-
}
-
if [ $# -lt 2 ];then
-
usage
-
fi
-
typeset in in_old dif_in
-
typeset out out_old dif_out
-
typeset timer
-
typeset eth
-
eth=$1
-
timer=$2
-
in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
-
out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
-
while true
-
do
-
sleep ${timer}
-
in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
-
out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
-
dif_in=$(((in-in_old)/timer))
-
dif_out=$(((out-out_old)/timer))
-
echo "IN: ${dif_in} Byte/s OUT: ${dif_out} Byte/s"
-
in_old=${in}
-
out_old=${out}
-
done
-
exit 0
阅读(4276) | 评论(0) | 转发(0) |