linux流量监控shell脚本

网上找了很多,感觉写的最好是这个
复制代码
[root@localhost ~]# vi count_net.sh
复制代码
#!/bin/bash
#
R1=`cat /sys/class/net/eth0/statistics/rx_bytes`
T1=`cat /sys/class/net/eth0/statistics/tx_bytes`

sleep 1
R2=`cat /sys/class/net/eth0/statistics/rx_bytes`
T2=`cat /sys/class/net/eth0/statistics/tx_bytes`

TBPS=`expr $T2 – $T1`
RBPS=`expr $R2 – $R1`

TKBPS=`expr $TBPS / 1024`
RKBPS=`expr $RBPS / 1024`

echo “上传速率 eth0: $TKBPS kb/s 下载速率 eth0: $RKBPS kb/s at $(date +%Y%m%d%H:%M:%S)” >>/var/log/filecopy/count_net/network_10.0.0.1_$(date +%Y%m%d).log

添加到计划任务里面,每5分钟一次
复制代码
*/5 * * * * /backup/bin/count_net.sh
设置自动回传(要添加免密钥验证)
复制代码
[root@localhost bin]# vi count_net_bak.sh
复制代码
scp /var/log/filecopy/count_net/network_211.xxx.xxx.xxx_$(date +%Y%m%d).log root@221.123.166.201:/backup/count_net

==================================
复制代码
#!/bin/bash
#
TX=0;
RX=0;

MAX_TX=0;
MAX_RX=0;

while read line
do
a=`echo $line | grep “eth0” |awk ‘{print $3}’`

if [ $a -ge 0 ]
then
TX=$a
if [ $TX -ge $MAX_TX ]
then
MAX_TX=$TX
fi
fi

b=`echo $line | grep “eth0” |awk ‘{print $7}’`

if [ $b -ge 0 ]
then
RX=$b
if [ $RX -ge $MAX_RX ]
then
MAX_RX=$RX
fi 31.fi
done < /var/log/filecopy/count_net/network_10.0.0.1_$(date +%Y%m%d).log echo "最高上传速度为 $MAX_TX kb/s at $(date +%Y%m%d)">>/var/log/filecopy/count_net/tongji.log

echo “最高下载速度为 $MAX_RX kb/s at $(date +%Y%m%d)”>>/var/log/filecopy/count_net/tongji.log

上一篇: