#!/bin/sh
# 流量統計工具
# 可帶一個參數:網卡名,如eth0或eth1等
# 輸出格式:統計時間,入流量(Kbps),入流量(Mbps),出流量(Kbps),出流量(Mbps)
# Please edit the followings
EthXname=eth0 # Interface name
StatFreq=2 # Seconds
if test $# -eq 1; then
EthXname=$1
fi
echo "Destination: $EthXname"
# Don't change
influx_kbps=0
outflux_kbps=0
influx_mbps=0
outflux_mbps=0
unsigned_long_max=4294967295
# 檢查是否存在EthXname
Ethname=`cat /proc/net/dev|grep $EthXname|awk -F"[: ]+" '{ printf("%s", $2); }'`
if test "$EthXname" != "$Ethname"; then
echo "Please set EthXname first before running"
echo "Usage: flux.sh ethX"
echo "Example: flux.sh eth0"
exit 1
# 進一步檢查是否存在EthXname
netstat -ie|grep $EthXname> /dev/null 2>&1
if test $? -ne 0; then
# 初始化
influx1_byte=`cat /proc/net/dev|grep $EthXname|awk -F"[: ]+" '{ printf("%d", $3); }'`
outflux1_byte=`cat /proc/net/dev|grep $EthXname|awk -F"[: ]+" '{ printf("%d", $11); }'`
echo "Date,IN-Kbps,IN-Mbps,OUT-Kbps,OUT-Mbps"
while test 2 -gt 1;
do
sleep $StatFreq
#influx2_byte=`cat /proc/net/dev|grep $EthXname|awk -F"[: ]+" '{ printf("%d", $3); }'`
#outflux2_byte=`cat /proc/net/dev|grep $EthXname|awk -F"[: ]+" '{ printf("%d", $11); }'`
inout_bytes=`awk -F"[: ]+" /$EthXname/'{ printf("%s %s", $3, $11) }' /proc/net/dev`
inout_bytes_array=($inout_bytes)
influx2_byte=${inout_bytes_array[0]}
outflux2_byte=${inout_bytes_array[1]}
dd=`date +'%Y-%m-%d/%H:%M:%S'`
if test $influx2_byte -ge $influx1_byte; then
let influx_byte=$influx2_byte-$influx1_byte
else
let influx_byte=$unsigned_long_max-$influx1_byte
let influx_byte=$influx_byte+$influx2_byte
if test $outflux2_byte -ge $outflux1_byte; then
let outflux_byte=$outflux2_byte-$outflux1_byte
let outflux_byte=$unsigned_long_max-$outflux1_byte
let outflux_byte=$outflux_byte+$outflux2_byte
let influx_byte=$influx_byte/$StatFreq
let outflux_byte=$outflux_byte/$StatFreq
# TO bps
let influx_bps=$influx_byte*8
let outflux_bps=$outflux_byte*8
# To kbps
let influx_kbps=$influx_bps/1024
let outflux_kbps=$outflux_bps/1024
# To mbps
let influx_mbps=$influx_kbps/1024
let outflux_mbps=$outflux_kbps/1024
# SHOW on screen
# COLUMN: Date,IN-Kbps,IN-Mbps,OUT-Kbps,OUT-Mbps
echo "$dd,${influx_kbps}Kbps,${influx_mbps}Mbps,${outflux_kbps}Kbps,${outflux_mbps}Mbps"
let influx1_byte=influx2_byte
let outflux1_byte=outflux2_byte
done