1
0
Fork 0
scripts-admin-quickndirty-p.../script_debit2.sh
2019-10-14 03:45:02 +02:00

39 lines
1 KiB
Bash
Executable file

#!/bin/sh
# Ce script donne la volumétrie d'une interface réseau à intervalle régulier
# (se base sur /proc/net/dev)
FICHIER_PROC=/proc/net/dev
DELAI="1"
# Try to get a pretty formatter
FORMATTER="cat -"
if which numfmt >/dev/null 2>&1; then
FORMATTER="numfmt --field 2-3 --to si --padding 7"
fi
# Factorize the parsing
getdata() {
cat "$1" | sed -e '1d;2d' -e 's/^[[:space:]]*//' -e 's/[[:space:]]\+/ /g' | cut -f 1,2,10 -d " "
}
# First grab
OLDDATA="$( getdata "$FICHIER_PROC" )"
while sleep "$DELAI" ; do
NEWDATA="$( getdata "$FICHIER_PROC" )"
# header
printf "\n% 15s\t% 6s\t% 6s\n" " " "IN" "OUT"
echo "$NEWDATA" | while read INTERFACE VOLUME_IN VOLUME_OUT; do
# Again, scope issues with read and pipes so we make a one-time loop :)
echo "$OLDDATA" | grep "$INTERFACE" | while read DUMP OLD_VOLUME_IN OLD_VOLUME_OUT; do
VOLUME_IN="$(( $VOLUME_IN - $OLD_VOLUME_IN ))"
VOLUME_OUT="$(( $VOLUME_OUT - $OLD_VOLUME_OUT ))"
printf "% 15s\t%d\t%d\n" "$INTERFACE" "$VOLUME_IN" "$VOLUME_OUT" | $FORMATTER
done
done
OLDDATA="$NEWDATA"
done