Chinaunix首页 | 论坛 | 博客
  • 博客访问: 304884
  • 博文数量: 81
  • 博客积分: 3813
  • 博客等级: 中校
  • 技术积分: 945
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-24 18:14
文章分类

全部博文(81)

文章存档

2013年(1)

2012年(2)

2011年(54)

2010年(15)

2009年(9)

分类: LINUX

2011-02-22 14:52:59

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 这个就不用多说了吧 :)
    •  


脚本一:
  1. #!/bin/bash
  2. eth=eth0
  3. RXpre=$(ifconfig ${eth} | grep bytes | awk '{print $2}'| awk -F":" '{print $2}')
  4. TXpre=$(ifconfig ${eth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')
  5. sleep 1
  6. RXnext=$(ifconfig ${eth} | grep bytes | awk '{print $2}'| awk -F":" '{print $2}')
  7. TXnext=$(ifconfig ${eth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')
  8. echo RX ----- TX
  9. echo "$(((${RXnext}-${RXpre})/1024))KB/s $(((${TXnext}-${TXpre})/1024))KB/s"
脚本二:
  1. #!/bin/bash
  2. # test network width
  3. function usage
  4. {
  5. echo "Usage: $0 ethX Y "
  6. echo " e.g. $0 eth0 2″
  7. exit 65
  8. }
  9. if [ $# -lt 2 ];then
  10. usage
  11. fi
  12. typeset in in_old dif_in
  13. typeset out out_old dif_out
  14. typeset timer
  15. typeset eth
  16. eth=$1
  17. timer=$2
  18. in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
  19. out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
  20. while true
  21. do
  22. sleep ${timer}
  23. in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
  24. out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
  25. dif_in=$(((in-in_old)/timer))
  26. dif_out=$(((out-out_old)/timer))
  27. echo "IN: ${dif_in} Byte/s OUT: ${dif_out} Byte/s"
  28. in_old=${in}
  29. out_old=${out}
  30. done
  31. exit 0
脚本三:
  1. #!/bin/bash
  2. echo -n "请输入需要查询的网卡的接口:"
  3. read eth
  4. echo "你要查询的网卡接口为"$eth
  5. echo -n "输入需要等到的时间(秒):"
  6. read sec
  7. echo "你计算的是"$sec"秒内的平均流量,请等待."
  8. infirst=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
  9. outfirst=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
  10. sumfirst=$(($infirst+$outfirst))
  11. sleep $sec"s"
  12. inend=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
  13. outend=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
  14. sumend=$(($inend+$outend))
  15. sum=$(($sumend-$sumfirst))
  16. echo $sec"秒内总流量为:"$sum"bytes"
  17. aver=$(($sum/$sec))
  18. echo "平均流量为:"$aver"bytes/sec"
脚本四:
  1. #!/bin/bash
  2. e0_in_old=$(ifconfig eth0 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
  3. 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))
  4. e1_in_old=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
  5. e1_out_old=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |tail -n1)
  6. e1_total_old=$(($e1_in_old + $e1_out_old))
  7. while true
  8. do
  9. sleep 1
  10. e0_in_new=$(ifconfig eth0 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
  11. e0_out_new=$(ifconfig eth0 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |tail -n1)
  12. e0_total_new=$(($e0_in_new + $e0_out_new))
  13. e1_in_new=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |head -n1)
  14. e1_out_new=$(ifconfig eth1 |grep bytes |awk '{print $2" "$6}' |egrep -o '[0-9]+' |tail -n1)
  15. e1_total_new=$(($e1_in_new + $e1_out_new))
  16. e0_in=$((($e0_in_new - $e0_in_old) * 8))
  17. e0_out=$((($e0_out_new - $e0_out_old) * 8))
  18. e0_total=$((($e0_total_new - $e0_total_old) * 8))
  19. e1_in=$((($e1_in_new - $e1_in_old) * 8))
  20. e1_out=$((($e1_out_new - $e1_out_old) * 8))
  21. e1_total=$((($e1_total_new - $e1_total_old) * 8))
  22. echo -e "eth0 IN: $e0_in \t b/s OUT: $e0_out \t b/s TOTAL: $e0_total \t b/s"
  23. echo -e "eth1 IN: $e1_in \t b/s OUT: $e1_out \t b/s TOTAL: $e1_total \t b/s\n"
  24. e0_in_old=$e0_in_new
  25. e0_out_old=$e0_out_new
  26. e0_total_old=$e0_total_new
  27. e1_in_old=$e1_in_new
  28. e1_out_old=$e1_out_new
  29. e1_total_old=$e1_total_new
  30. done
  31. exit 0
脚本五:
  1. #!/bin/bash
  2. # osdba 2008.10.22 monitor the interface's network traffic.
  3. if [ $# -ne 3 ];then
  4. echo Useage : $0 interface interval count
  5. echo Example: $0 eth0 2 10
  6. exit
  7. fi
  8. eth=$1
  9. count=$3
  10. interval=$2
  11. inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
  12. if [ -z "$inbytesfirst" ];then
  13. echo The network interface $eth is not exits!
  14. exit 1;
  15. fi
  16. outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
  17. inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
  18. outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
  19. sleep $interval"s"
  20. i=0
  21. while [ "$i" -lt "$count" ]
  22. do
  23. inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
  24. outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
  25. inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
  26. outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
  27. bytesin=$(($inbytesend-$inbytesfirst))
  28. bytesout=$(($outbytesend-$outbytesfirst))
  29. packetsin=$(($inpacketsend-$inpacketsfirst))
  30. packetsout=$(($outpacketsend-$outpacketsfirst))
  31. if [ "$bytesin" -lt "0" ];then
  32. bytesin=$((4294967295-$inbytesfirst+$inbytesend))
  33. #echo bytesin $bytesin $inbytesfirst $inbytesend
  34. fi
  35. if [ "$bytesout" -lt "0" ];then
  36. bytesout=$((4294967295-$outbytesfirst+$outbytesend))
  37. #echo bytesout $bytesout $outbytesfirst $outbytesend
  38. fi
  39. if [ "$packetsin" -lt "0" ];then
  40. packetsin=$((4294967295-$inpacketsfirst+$inpacketsend))
  41. #echo packetsin $packetsin $inpacketsfirst $inpacketsend
  42. fi
  43. if [ "$packetsout" -lt "0" ];then
  44. packetsout=$((4294967295-$outpacketsfirst+$outpacketsend))
  45. #echo packetsout $packetsout $outpacketsfirst $outpacketsend
  46. fi
  47. bytesin=$(($bytesin/$interval))
  48. bytesout=$(($bytesout/$interval))
  49. packetsin=$(($packetsin/$interval))
  50. packetsout=$(($packetsout/$interval))
  51. sumbytesin=$(($sumbytesin+$bytesin))
  52. sumbytesout=$(($sumbytesout+$bytesout))
  53. sumpacketsin=$(($sumpacketsin+$packetsin))
  54. sumpacketsout=$(($sumpacketsout+$packetsout))
  55. if [ $(($i%20)) -eq 0 ];then
  56. echo " ifname | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s"
  57. echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
  58. fi
  59. 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)}'
  60. inbytesfirst=$inbytesend
  61. outbytesfirst=$outbytesend
  62. inpacketsfirst=$inpacketsend
  63. outpacketsfirst=$outpacketsend
  64. i=$(($i+1))
  65. sleep $interval"s"
  66. done
  67. sumbytesin=$(($sumbytesin/$i))
  68. sumbytesout=$(($sumbytesout/$i))
  69. sumpacketsin=$(($sumpacketsin/$i))
  70. sumpacketsout=$(($sumpacketsout/$i))
  71. echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
  72. 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)}'
脚本六:
  1. #!/bin/bash
  2. # test network width
  3. function usage
  4. {
  5. echo "Usage: $0?? "
  6. echo "?????? e.g. $0 eth0 2"
  7. exit 65
  8. }
  9. if [ $# -lt 2 ];then
  10. usage
  11. fi
  12. typeset in in_old dif_in
  13. typeset out out_old dif_out
  14. typeset timer
  15. typeset eth
  16. eth=$1
  17. timer=$2
  18. in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
  19. out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
  20. while true
  21. do
  22. sleep ${timer}
  23. in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
  24. out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
  25. dif_in=$(((in-in_old)/timer))
  26. dif_out=$(((out-out_old)/timer))
  27. echo "IN: ${dif_in} Byte/s OUT: ${dif_out} Byte/s"
  28. in_old=${in}
  29. out_old=${out}
  30. done
  31. exit 0






阅读(4051) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~