`
junun
  • 浏览: 1755 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

使用nicstat查看网卡状态

阅读更多

我以前用脚本实现对实时网卡流量统

#!/bin/bash
NAME=nic_flow
PIDFILE=/var/tmp/$NAME.pid
LOGFILE=/var/tmp/$NAME.log

function start(){
    if [ -f "$PIDFILE" ]; then
        echo $PIDFILE
        exit 2
    fi
    for (( ; ; ));do
        eth=$1
        [ -z "${eth:-}" ] && eth=eth0
        time_range=$2
        [ -z "${time_range:-}" ] && time_range=1      
        ifconfig  | awk '{print $1}' | grep "eth" > /dev/null 
        [ $? -gt 0 ] && echo "wrong NIC device" && exit 10
        RX_1=$(cat /proc/net/dev | grep "$eth" | awk -F'[ |:]+'  '{print $4}')
        TX_1=$(cat /proc/net/dev | grep "$eth" | awk '{print $9}')
        sleep $time_range
        RX_2=$(cat /proc/net/dev | grep "$eth" | awk -F'[ |:]+'  '{print $4}')
        TX_2=$(cat /proc/net/dev | grep "$eth" | awk '{print $9}')
        clear
        echo  -e "`date +%k:%M:%S`      RX        TX" | tee -a $LOGFILE 
        RX=$((${RX_2}-${RX_1}))
        TX=$((${TX_2}-${TX_1}))
        if [[ $RX -lt 1024 ]];then
            RX="${RX}B/s"
        elif [[ $RX -gt 1048576 ]];then
            RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
        else
            RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
        fi
        if [[ $TX -lt 1024 ]];then
            TX="${TX}B/s"

        elif [[ $TX -gt 1048576 ]];then
            TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
        else
            TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
        fi 
        echo -e " $eth       $RX        $TX " | tee -a $LOGFILE
    done& 
    echo $! > $PIDFILE
} 
function stop(){
    [ -f $PIDFILE ] && kill `cat $PIDFILE` && rm -rf $PIDFILE
}

case "$1" in
  start)
    start $2 $3
    ;;
  stop)
    stop
    ;;
  *)
    echo $"Usage:$0  {start|stop,NIC device(eth*), time_range(s)}"
    exit 2
esac

 今天发现一个更好用的工具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

下载文件并安装

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

 

 

安装错误及解决方法

[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

[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

=======>yum install libgcc.i686

 

安装脚本:

#/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

 具体使用可以看http://angus717.blog.51cto.com/1593644/1208937,以后可能不在维护51的博客了。

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics