Sunday, May 24, 2009

Network Usage from console Max / Cur / Avg.

Here is a small shell script which I have used to monitor the Maximum, Current and average usage of a network interface.

#!/bin/bash
IFACE=bond0
oldbyt=(0 0);
cnt=1;
ORXB=0
OTXB=0
MAXRXB=0
MAXTXB=0
while : ; do
byte=($(/sbin/ifconfig $IFACE | \
sed -e 's/:/ /g'|awk '$2 ~ /bytes/ {print $3,$8}'));
if [ ${oldbyt[0]} -ne 0 ] && [ ${oldbyt[1]} -ne 0 ]; then
RXB="$(((${byte[0]}-${oldbyt[0]})/1024))"
TXB="$(((${byte[1]}-${oldbyt[1]})/1024))"
ORXB=$(($RXB+$ORXB))
OTXB=$((TXB+$OTXB))
[ $MAXRXB -lt $RXB ] && MAXRXB=$RXB
[ $MAXTXB -lt $TXB ] && MAXTXB=$TXB
echo -e "\fCUR RX : $RXB\tTX : $TXB\nAVG RX : $(($ORXB/$cnt))\tTX : $(($OTXB/$cnt))\nMAX RX : $MAXRXB \tTX : $MAXTXB"
cnt=$(($cnt+1))
fi
oldbyt=(${byte[@]});
sleep 1
done


I know I can do the same thing using some stock utilities like ntop or iftop, but this was a production environment and I don't want to install any thing unless it is absolute necessity.

No comments:

Post a Comment