我以前用脚本实现对实时网卡流量统计(http://angus717.blog.51cto.com/1593644/1186455),今天发现一个更好用的工具nicstat。
nicstat可以提供更加全面的网卡信息。
显示TCP流量统计
显示UDP流量统计
报告进出网卡的字节数
报告进出网卡的数据数
报告网卡利用率
报告NIC饱和度和其他信息
完整的安装过程(基于centos 6.3 x86_64,如果是ubuntu或者其linux可以参照官网文档)
安装make环境
yum install gcc gcc-c++ make automake autoconf -y
由于nicstat依赖32 bit glibc package,所以
yum install libgcc.i686 glibc.i686 glibc-devel.i686 -y
不安装glibc.i686 会报下面的错误
[root@localhost nicstat-1.92]# make gcc -O3 -m32 nicstat.c -o nicstat nicstat.c:99:1: warning: "DUPLEX_UNKNOWN" redefined In file included from nicstat.c:84: /usr/include/linux/ethtool.h:519:1: warning: this is the location of the previous definition /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.4.7/libgcc_s.so when searching for -lgcc_s /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.4.7/libgcc_s.so when searching for -lgcc_s /usr/bin/ld: cannot find -lgcc_s collect2: ld returned 1 exit status make: *** [nicstat] Error 1
不安装glibc-devel.i686 make会报下面错误
[root@andy nicstat-1.92]# make gcc -O3 -m32 nicstat.c -o nicstat In file included from /usr/include/features.h:385, from /usr/include/stdio.h:28, from nicstat.c:33: /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file or directory nicstat.c:99:1: warning: "DUPLEX_UNKNOWN" redefined In file included from nicstat.c:84: /usr/include/linux/ethtool.h:689:1: warning: this is the location of the previous definition make: *** [nicstat] Error 1 ======> yum install glibc-devel.i686
下载文件并安装
cd /usr/local/src wget http://nchc.dl.sourceforge.net/project/nicstat/nicstat-1.92.tar.gz tar xf nicstat-1.92.tar.gz cd nicstat-1.92 cp Makefile.Linux Makefile make
正常make完如下
[root@localhost nicstat-1.92]# make gcc -O3 -m32 nicstat.c -o nicstat mv nicstat `./nicstat.sh --bin-name`
nicstat使用
(一)
[root@localhost nicstat-1.92]# ./nicstat.sh Time Int rKB/s wKB/s rPk/s wPk/s rAvs wAvs %Util Sat 13:50:43 lo 0.01 0.01 0.06 0.06 94.88 94.88 0.00 0.00 13:50:43 eth0 16.48 0.59 12.25 7.37 1377.6 82.43 0.01 0.00
对于各项的解释
rKB/s : Kilobytes/second received. wKB/s : Kilobytes/second written. rPk/s : Packets/second received. wPk/s : Packets/second written. rAvs : Average size of packets received). wAvs : Average size of packets transmitted. %Util : Percentage utilization of the interface. Sat : This the number of errors/second seen for the interface. An indicator the interface may be approaching saturation. This statistic is combined from a number of kernel statistics. It is recommended to use the -x option to see more individual statistics when attempting to diagnose a network issue.
(二)
[root@localhost nicstat-1.92]# ./nicstat.sh -t 13:58:05 InKB OutKB InSeg OutSeg Reset AttF %ReTX InConn OutCon Drops TCP 0.00 0.00 10.50 6.50 0.00 0.00 0.000 0.00 0.02 0.00
用于查看tcp信息的
InKB : 每秒接收多少字节 OutKB : 每秒传输收多少字节 InSeg : 每秒接收多少数据段 OutSeg : 每秒传输多少数据段 Reset :TCP连接直接从ESTABLISHED状态或CLOSE-WAIT状态过渡到CLOSED状态的次数 AttF :TCP连接直接从SYN-SENT状态或SYN-RCVD状态过渡到CLOSED状态次数加上TCP连接直接从SYN-RCVD状态到LISTEN状态 %ReTX :TCP报文重传率 InConn :TCP连接直接从LISTEN状态过渡到SYN-RCVD状态次数 OutCon :TCP连接直接从CLOSED状态过渡到SYN-SENT状态次数 Drops :从TCP已建立连接队列和没有建立的连接队列丢弃数据包的次数
(三)
[root@localhost nicstat-1.92]# ./nicstat.sh -u 14:22:21 InDG OutDG InErr OutErr UDP 0.02 0.09 0.00 0.00
(四)
[root@localhost nicstat-1.92]# ./nicstat.sh -x 14:22:50 RdKB WrKB RdPkt WrPkt IErr OErr Coll NoCP Defer %Util lo 0.00 0.00 0.04 0.04 0.00 0.00 0.00 0.00 0.00 0.00 eth0 11.15 0.40 8.39 5.02 0.00 0.00 0.00 0.00 0.00 0.01
(五)
等同于执行-t -u -x
[root@localhost nicstat-1.92]# ./nicstat.sh -a 14:24:42 InKB OutKB InSeg OutSeg Reset AttF %ReTX InConn OutCon Drops TCP 0.00 0.00 7.75 4.81 0.00 0.00 0.000 0.00 0.01 0.00 14:24:42 InDG OutDG InErr OutErr UDP 0.02 0.08 0.00 0.00 14:24:42 RdKB WrKB RdPkt WrPkt IErr OErr Coll NoCP Defer %Util lo 0.00 0.00 0.04 0.04 0.00 0.00 0.00 0.00 0.00 0.00 eth0 10.94 0.40 8.24 4.93 0.00 0.00 0.00 0.00 0.00 0.01
:)便于操作:
echo "alias nicstat='bash /usr/loacl/src/nicstat-1.92/nicstat.sh'" > /etc/profile.d/nicstat.sh
附加安装脚本:
#/bin/bash #author junun #blog http://angus717.blog.51cto.com # SOFT_DIR=/usr/local/src function nicstat_ins { echo "===========will inst nicstat" ping -c 1 -t 1 www.baidu.com if [[ $? -gt 0 ]];then echo "nameserver 8.8.8.8\rnameserver 202.96.128.68" >> /etc/resolv.conf fi yum install gcc gcc-c++ make automake autoconf -y yum install libgcc.i686 glibc.i686 glibc-devel.i686 -y cd $SOFT_DIR wget http://nchc.dl.sourceforge.net/project/nicstat/nicstat-1.92.tar.gz tar xf nicstat-1.92.tar.gz cd nicstat-1.92 cp Makefile.Linux Makefile make [ $? -eq 0 ] || error "\033[31m there are something to make \033[0m" echo "alias nicstat='bash $SOFT_DIR/nicstat-1.92/nicstat.sh'" > /etc/profile.d/nicstat.sh } nicstat_ins