I am in need of assistance. I'm pretty new as bash scripting though I have some programming knowledge which has helped me.
Lots of text incoming.
Basically what I need to here is to measure the bandwidth coming in to an Ethernet interface. I have used a script which almost did what I wanted which I have based my current one on. That script took a value from received bytes in /proc/net/dev each second and compared it to the last one and got the speed like that. It worked fine when there was possible to grab data each second but for the project I'm doing there are times where there are 2-5 second gaps between the received values so it doesn't work then. So, the idea to fix this was to first reset ifconfig so RX bytes in 0 (or close to) and then run a script that grabs values from /proc/net/dev and when it's done it takes the value it has then and divides it by the seconds the test was run, not as accurate but it works.
Though here's the problem. Our test is run for 20 minutes and it sends lots of data and we found out that the RX bytes counter is reset at 2^32 bytes. So the scripts idea is to check an "old" and one "new" value and if the new value is smaller than the old value then we can assume that the counter has been reset and the newest "old" value could be saved in a array and at the end all variables should be added up and divided by the time in seconds. There's also the idea that each time the counter has been reset we increase a variable i by 1 and at the end we see what i is and while not as accurate we take i*4 294 967 296 (or such) and add the current "new" value and divide the whole thing by the time in seconds.
Code:
# !/bin/bash
interface=$1
received_bytes=""
old_received_bytes=""
#Usage: bw.sh eth1
get_bytes()
{
line=$(cat /proc/net/dev | grep $interface | cut -d ':' -f 2 | awk '{print "received_bytes="$1, "transmitted_bytes="$9}')
eval $line
}
i=0;
while true;
do
get_bytes
old=$received_bytes
echo -n "old value "
echo $old
sleep 2;
get_bytes
new=$received_bytes
echo -n "new value "
echo $new
if [ $new -lt $old ]; then
echo "Value before reset is: " $old
let i=i+1
echo $i
else
echo "nothing to see here, all is normal";
fi
done
It's by no means pretty, but I guess the idea is fine right? Though I've read on about arrays it's still a bit confusing here. It's how I should
do to have the $old value saved in a variable called let's say $batch(i) where i is the number that gets increased. So they are called batch1, batch2 etc.
Many of the echo right now is just for control. After reading my ramblings do you get what I'm trying to do and any suggestions would be most
helpful. Why we even needed a script was to combat the resetting of the value at 2^32 bytes.
Because we stop the program manually when we see that test is over, right now we save the result to a file "bw.sh eth1 > FILE" and read the important manually. Not pretty but yeah.
Thanks for any input