Hey everyone-
This is my first post to these forums (well, aside from the intro post
)! Looking forward to exchanging ideas with all of you!
I have a script that I am using to try to measure network throughput over the ethernet ports. This is at work in order to test the performance of our onboard ethernet ports (planar) versus our pci-e (and daughter) ethernet pluggable cards.
The script is very long (and calls other scripts), so I will try to only show the parts that seem to be producing this weird little bug:
Code:
#Robert de Bock's "A shell script to measure network
#throughput on Linux machines" served as the basis for
#this part of the script. Thank you for your help Robert!
#the $interface here is eth0 by default, but it is set from prompt args
printTXbytes(){
/sbin/ifconfig "$interface" | grep "TX bytes" | cut -d: -f3 | awk '{ print $1 }'
}
printRXbytes(){
/sbin/ifconfig "$interface" | grep "RX bytes" | cut -d: -f2 | awk '{ print $1 }'
}
bytestohumanreadable(){
multiplier="0"
number="$1"
while [ "$number" -ge 1024 ] ; do
multiplier=$(($multiplier+1))
number=$(($number/1024))
done
case "$multiplier" in
1)
echo "$number KB"
;;
2)
echo "$number MB"
;;
3)
echo "$number GB"
;;
4)
echo "$number TB"
;;
*)
echo "$1 B"
;;
esac
}
#below, usleep is also set by prompt args- default it is 1000000 usecs.
#counter is set by prompt arguments also, default is 60 iterations.
printResults (){
startTime=$(date +%s.%N)
while [ "$counter" -ge 0 ] ; do
counter=$(($counter - 1))
if [ "$rxbytes" ] ; then
oldrxbytes="$rxbytes"
oldtxbytes="$txbytes"
fi
rxbytes=$(printrxbytes)
txbytes=$(printtxbytes)
rxpackets=$(printrxpackets)
txpackets=$(printtxpackets)
ipaddr=$(printIP)
cpuLoad=$(printCPULoad)
memTotal=$(printTotalMem)
byteFilterOne=$((memTotal*1024))
memTotal=$byteFilterOne
memUsed=$(printUsedMem)
byteFilterTwo=$((memUsed*1024))
memUsed=$byteFilterTwo
if [ "$oldrxbytes" -a "$rxbytes" -a "$oldtxbytes" -a "$txbytes" ] ; then
echo "================================================================="
echo "Monitoring $interface @ $ipaddr every $usleep usec(s)."
echo ""
echo "$cpuLoad"
echo ""
echo "Total Memory ~ $(bytestohumanreadable $memTotal) Used Memory ~ $(bytestohumanreadable $memUsed)"
echo "================================================================="
echo ""
echo "RXbytes = $(bytestohumanreadable $(($rxbytes - $oldrxbytes))) TXbytes = $(bytestohumanreadable $(($txbytes - $oldtxbytes)))"
timeStamp=$(date +%M:%S)
echo "$timeStamp $(($rxbytes - $oldrxbytes))" >> rxgrph.dat
echo "$timeStamp $(($txbytes - $oldtxbytes))" >> txgrph.dat
echo ""
echo "RXpackets = $rxpackets TXpackets = $txpackets"
echo ""
echo "--------------------------------------------------------------"
echo "RXbytes total = $(bytestohumanreadable $rxbytes) TXbytes total = $(bytestohumanreadable $txbytes)"
echo "--------------------------------------------------------------"
fi
usleep "$usleep"
clear
done
What we're concerned with is what is getting put into the rxgrph.dat and txgrph.dat files. It seems that no matter what, there is usually at least 1 entry (no more than 3) into the .dat file that is double the amount of all of the other entries, causing a strange anomalous spike when it is graphed. A snapshot of a portion of the rxgrph.dat file:
Code:
29:36 9519552
29:37 9519616
29:38 19039552
29:39 9519680
29:40 9519552
First I started tinkering with the rate of the loop versus the rate that ifconfig is capable of refreshing at- which near as I can tell is 1 second. Also, the timestamps in the rxgrph.dat file seem to indicate that the sampling is indeed being done every second, but still the doubled number shows up. I had no success there, and now I'm thinking it could be caused by the way I have scripted something.
I'm still stuck on the idea that I'm catching ifconfig twice before it refreshes, hence the doubled number, but I can't really find any good info on the workings behind it to form some new ideas or workarounds. Suggestions on how to fix this anyone?
(let me know if I need to post more of the script)
Thanks in advance!