天天看點

使用nicstat全面檢視你的網卡資訊

我以前用腳本實作對實時網卡流量統計(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      

繼續閱讀