Created
March 24, 2022 21:48
-
-
Save jontow/3eb14fc225af3595311226e66fc03cba to your computer and use it in GitHub Desktop.
Monitor network interface usage rate with minimal external tooling on linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Run like so: NET_IF=ens5 UNIT=K sh netifbw.sh | |
NET_IF="${NET_IF:-eth0}" | |
SLEEP_INTERVAL="${SLEEP_INTERVAL:-1}" | |
UNIT="${UNIT:-B}" | |
MODIFIER=1 | |
case "${UNIT}" in | |
B|[Bb]ytes) | |
MODIFIER=1 | |
;; | |
K|[Kk]ilo) | |
MODIFIER=1024 | |
;; | |
M|[Mm]ega) | |
MODIFIER=1048576 | |
;; | |
G|[Gg]iga) | |
MODIFIER=1073741824 | |
;; | |
*) | |
echo "unknown unit: ${UNIT}" && exit 1 | |
;; | |
esac | |
old_in_bytes=0 && old_out_bytes=0 | |
in_bytes=0 && out_bytes=0 | |
while true; do | |
proc_net_dev=$(grep "${NET_IF}:" /proc/net/dev) || ( echo "NET_IF ${NET_IF} not found" && exit 1 ) | |
old_in_bytes=${in_bytes} | |
old_out_bytes=${out_bytes} | |
in_bytes=$(echo ${proc_net_dev} | awk '{print $2}') | |
out_bytes=$(echo ${proc_net_dev} | awk '{print $10}') | |
in_rate=$(echo "(${in_bytes} - ${old_in_bytes}) / ${MODIFIER}" | bc) | |
out_rate=$(echo "(${out_bytes} - ${old_out_bytes}) / ${MODIFIER}" | bc) | |
echo "${NET_IF}: ${in_rate} in-${UNIT}bytes/${SLEEP_INTERVAL}sec ${out_rate} out-${UNIT}bytes/${SLEEP_INTERVAL}sec" | |
sleep ${SLEEP_INTERVAL:-1} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment