Shell脚本实现的单机流量统计功能
在网上看到这个单机流量的脚本,挺不错的。
#!/bin/sh
usage(){
echo“Usage:$0[-iINTERFACE][-sINTERVAL][-cCOUNT]”
echo
echo“-iINTERFACE”
echo“ Theinterfacetomonitor,defaultiseth0.”
echo“-sINTERVAL”
echo“ Thetimetowaitinsecondsbetweenmeasurements,defaultis3seconds.”
echo“-cCOUNT”
echo“ Thenumberoftimestomeasure,defaultis10times.”
exit3
}
readargs(){
while["$#"-gt0];do
case“$1″in
-i)
if["$2"];then
interface=”$2″
shift;shift
else
echo“Missingavaluefor$1.”
echo
shift
usage
fi
;;
-s)
if["$2"];then
sleep=”$2″
shift;shift
else
echo“Missingavaluefor$1.”
echo
shift
usage
fi
;;
-c)
if["$2"];then
counter=”$2″
shift;shift
else
echo“Missingavaluefor$1.”
echo
shift
usage
fi
;;
*)
echo“Unknownoption$1.”
echo
shift
usage
;;
esac
done
}
checkargs(){
if[!"$interface"];then
interface=”eth0″
fi
if[!"$sleep"];then
sleep=”3″
fi
if[!"$counter"];then
counter=”10″
fi
}
printrxbytes(){
/sbin/ifconfig“$interface”|grep“RXbytes”|cut-d:-f2|awk‘{print$1}'
}
printtxbytes(){
/sbin/ifconfig“$interface”|grep“TXbytes”|cut-d:-f3|awk‘{print$1}'
}
bytestohumanreadable(){
multiplier=”0″
number=”$1″
while["$number"-ge1024];do
multiplier=$(($multiplier+1))
number=$(($number/1024))
done
case“$multiplier”in
1)
echo“$numberKb”
;;
2)
echo“$numberMb”
;;
3)
echo“$numberGb”
;;
4)
echo“$numberTb”
;;
*)
echo“$1b”
;;
esac
}
printresults(){
while["$counter"-ge0];do
counter=$(($counter–1))
if["$rxbytes"];then
oldrxbytes=”$rxbytes”
oldtxbytes=”$txbytes”
fi
rxbytes=$(printrxbytes)
txbytes=$(printtxbytes)
if["$oldrxbytes"-a"$rxbytes"-a"$oldtxbytes"-a"$txbytes"];then
echo“$(/bin/date+%Y%m%d-%H%M%S)RXbytes=$(bytestohumanreadable$(($rxbytes–$oldrxbytes)))TXbytes=$(bytestohumanreadable$(($txbytes–$oldtxbytes)))”
else
echo“Monitoring$interfaceevery$sleepseconds.(RXbytetotal=$(bytestohumanreadable$rxbytes)TXbytestotal=$(bytestohumanreadable$txbytes))”
fi
sleep“$sleep”
done
}
readargs“$@”
checkargs
printresults
测试如下:
每三秒的流量,总输出999行,可以输出到文件里,其中:前面为时间,RxPackets是接收数据包,即下载,TxPackets是发送数据包,即上传.
[root@host]#sht.sh-c999 Monitoringeth0every3seconds.(RXbytetotal=6TbTXbytestotal=5Tb) 20101105-201539RXbytes=126KbTXbytes=658Kb 20101105-201542RXbytes=87KbTXbytes=487Kb 20101105-201545RXbytes=159KbTXbytes=668Kb 20101105-201548RXbytes=107KbTXbytes=725Kb 20101105-201551RXbytes=110KbTXbytes=704Kb 20101105-201554RXbytes=90KbTXbytes=726Kb 20101105-201558RXbytes=100KbTXbytes=850Kb 20101105-201601RXbytes=102KbTXbytes=703Kb 20101105-201604RXbytes=168KbTXbytes=693Kb 20101105-201607RXbytes=105KbTXbytes=730Kb 20101105-201610RXbytes=133KbTXbytes=711Kb 20101105-201613RXbytes=431KbTXbytes=703Kb 20101105-201616RXbytes=84KbTXbytes=527Kb 20101105-201619RXbytes=239KbTXbytes=825Kb 20101105-201622RXbytes=117KbTXbytes=801Kb 20101105-201625RXbytes=99KbTXbytes=913Kb 20101105-201628RXbytes=89KbTXbytes=322Kb 20101105-201631RXbytes=63KbTXbytes=73Kb 20101105-201634RXbytes=84KbTXbytes=191Kb 20101105-201637RXbytes=174KbTXbytes=481Kb 20101105-201640RXbytes=120KbTXbytes=383Kb 20101105-201643RXbytes=94KbTXbytes=496Kb 20101105-201646RXbytes=108KbTXbytes=340Kb 20101105-201649RXbytes=91KbTXbytes=639Kb 20101105-201652RXbytes=106KbTXbytes=629Kb 20101105-201655RXbytes=125KbTXbytes=496Kb 20101105-201658RXbytes=90KbTXbytes=537Kb 20101105-201701RXbytes=114KbTXbytes=641Kb