Originally published January 7, 2018 @ 2:59 pm

Just a quick script that will measure bandwidth utilization in KB/s for all NICs on your server over the specified period of time. You can call the script with an optional time wait parameter in seconds.

Sample output:

root@ncc1701# /var/adm/bin/nic_traffic.sh 10s
Waiting...
bond0:  16099  KB/s
eth0:   0      KB/s
eth1:   0      KB/s
eth2:   0      KB/s
eth3:   0      KB/s
eth4:   0      KB/s
eth5:   16442  KB/s

And here’s the script (nic_traffic):
#!/bin/bash
if [[ ! -z "" ]] && [[ "" =~ ^-?[0-9]+$ ]]
then
	s=""
else
	s=5
fi
	
d="/sys/class/net"
nics=`/sbin/ifconfig -a | egrep -o -e "[a-z][a-z]*[0-9]*[ ]*(Link|: .*<)" | \
sed -e 's/://g' | awk '{print $1}' | grep -v "^lo$"`

for i in nic rx1 tx1 rx2 tx3
do
	unset array_${i}
done

i=0
for nic in ${nics}
do
	array_nic[$i]=${nic}
	array_rx1[$i]=$(cat ${d}/${nic}/statistics/rx_bytes)
	array_tx1[$i]=$(cat ${d}/${nic}/statistics/tx_bytes)
	(( i++ ))
done

echo "Waiting..."
secs=${s}
while [ ${secs} -gt 0 ]; do
   echo -ne "${secs}3[0K\r"
   sleep 1
   : $((secs--))
done

i=0
for nic in ${nics}
do
	array_rx2[$i]=$(cat ${d}/${nic}/statistics/rx_bytes)
	array_tx2[$i]=$(cat ${d}/${nic}/statistics/tx_bytes)
	(( i++ ))
done

i=0
for nic in `echo "${array_nic[@]}"`
do
	echo -n "${nic}: "
	printf '%s\n' "scale=0;(`echo ${array_rx2[$i]}`-`echo ${array_rx1[$i]}`)/(${s}*1024)"|bc -l | tr '\n' ' '
	echo "KB/s"
	(( i++ ))
done | column -t