Issue
I'm having trouble calculating bandwidth usage on a network card. I am doing this on FreeBSD and I am only able to use netstat without the possibility of installing additional modules.
I just do not know how to calculate it. At this moment I came up with an idea to execute in a script the command
netstat -i -b -n -I INTERFACE write to file columns 8 and 11 that is Ibytes + Obytes
Then do this command again and read the same columns
And here I have a problem what to do with it? Is there any mathematical formula to calculate bandwidth consumption?
Solution
Processing netstat
output to get the Ibytes
sum and Obytes
sum of a given interface with awk:
netstat -i -b -n -I IFACE |
awk 'BEGIN { getline } { i += $8; o += $11 } END { print i, o }'
Now a simple monitoring script:
#!/bin/sh
iface=$1
seconds=$2
while :
do
read curr_Ibytes curr_Obytes < <(
netstat -i -b -n -I "$iface" |
awk 'BEGIN { getline } { i += $8; o += $11 } END { print i, o }'
)
if [ -n "$prev_Ibytes" ]
then
printf 'in: %d B/s\tout: %d B/s\n' \
$(( (curr_Ibytes - prev_Ibytes) / seconds )) \
$(( (curr_Obytes - prev_Obytes) / seconds ))
fi
prev_Ibytes=$curr_Ibytes
prev_Obytes=$curr_Obytes
sleep "$seconds"
done
Example:
./netmon.sh em0 5
in: 520 B/s out: 3040 B/s
in: 325 B/s out: 1648 B/s
in: 95 B/s out: 130 B/s
in: 1380 B/s out: 23629 B/s
in: 232 B/s out: 146 B/s
...
Answered By - Fravadona Answer Checked By - Marilyn (WPSolving Volunteer)