网卡流量监控脚本

#!/bin/bash
while [ "1" ]
do
eth=$1
RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
sleep 1
RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
#clear
echo  -e  "\t RX `date +%k:%M:%S` TX"
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))
 
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 \t $RX   $TX "
done

 使用方法如下,RX为入流量,TX为出流量

# ./ttt.sh eth1
RX 19:33:09 TX
eth1 35.6152KB/s 5.69769MB/s
RX 19:33:10 TX
eth1 40.1328KB/s 6.60828MB/s
RX 19:33:11 TX
eth1 2.53724MB/s 8.39092MB/s
RX 19:33:12 TX
eth1 168.514KB/s 5.5667MB/s
RX 19:33:13 TX
eth1 4.6224MB/s 7.22006MB/s
RX 19:33:14 TX
eth1 240.126KB/s 6.34822MB/s
RX 19:33:15 TX
eth1 3.37922MB/s 7.15415MB/s
RX 19:33:16 TX
eth1 1.54262MB/s 6.35194MB/s
RX 19:33:17 TX
eth1 39.1971MB/s 2.31175MB/s

posted @ 2015-11-20 19:33  数据盟  阅读(282)  评论(0编辑  收藏  举报