shell腳本實現Linux流量監控

流量監控腳本

#!/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)" >>/usr/monitor/network/network_$(date +%Y%m%d).log
 
 
當天最高上傳及下載流量統計
 
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
fi
done < /usr/monitor/network/network_$(date +%Y%m%d).log
 
echo "最高上傳速度爲 $MAX_TX kb/s at $(date +%Y%m%d)">>/usr/monitor/network/tongji.log
 
echo "最高下載速度爲 $MAX_RX kb/s at $(date +%Y%m%d)">>/usr/monitor/network/tongji.log
 
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章