| 1 |
#!/bin/bash |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
# |
| 4 |
# netload |
| 5 |
# written by Jan Engelhardt, 2002-2007 |
| 6 |
# |
| 7 |
# NOTE: This script uses /proc/net/dev. Note that TX counters are NOT updated |
| 8 |
# for vmnet-bridge traffic! |
| 9 |
COMPAT_PROC="" # FreeBSD:/compat/linux |
| 10 |
|
| 11 |
if [ -z "$1" ]; then |
| 12 |
echo "Usage: $0 <device> [interval]"; |
| 13 |
exit 1; |
| 14 |
fi |
| 15 |
|
| 16 |
if [ -z "$2" ]; then |
| 17 |
sleeper=sleep; |
| 18 |
wait=1; |
| 19 |
elif [ "$2" -ge 50000 ]; then |
| 20 |
sleeper=usleep; |
| 21 |
wait="$2"; |
| 22 |
else |
| 23 |
sleeper=sleep; |
| 24 |
wait="$2"; |
| 25 |
fi; |
| 26 |
|
| 27 |
while :; do |
| 28 |
cat "$COMPAT_PROC/proc/net/dev" | grep " $1" | cut -f 2 -d:; |
| 29 |
"$sleeper" $wait; |
| 30 |
done | perl -pe '$|=1;s/.*://' | while read rxcnt rxpkt c d e f g h txcnt txpkt; do |
| 31 |
if [ "$all" != "" ]; then |
| 32 |
now="`cat "$COMPAT_PROC/proc/uptime" | cut -f 1 -d " " | sed s/"\."//g`"; |
| 33 |
[ $[$now-$oldtm] -eq 0 ] && continue; |
| 34 |
rxbytes=$[($rxcnt-$oldrx)*100/($now-$oldtm)]; |
| 35 |
rxint=$[$rxbytes/1024]; |
| 36 |
rxfrac=$[$rxbytes-($rxbytes/1024*1024)]; |
| 37 |
|
| 38 |
txbytes=$[($txcnt-$oldtx)*100/($now-$oldtm)]; |
| 39 |
txint=$[$txbytes/1024]; |
| 40 |
txfrac=$[$txbytes-($txbytes/1024*1024)]; |
| 41 |
|
| 42 |
printf "\r\e[2K"" IN: %9d.%03d KB/s OUT: %9d.%03d KB/s" \ |
| 43 |
$rxint $rxfrac $txint $txfrac; |
| 44 |
fi; |
| 45 |
all=$[$rxcnt+$txcnt]; |
| 46 |
oldrx=$rxcnt; |
| 47 |
oldtx=$txcnt; |
| 48 |
oldtm="`cat "$COMPAT_PROC/proc/uptime" | cut -f 1 -d " " | sed s/"\."//g`"; |
| 49 |
done; |