1
0
Fork 0

script-debit: rewrite

This commit is contained in:
Chl 2019-10-14 03:44:20 +02:00
parent ed26706790
commit 7e971a5c76
2 changed files with 39 additions and 30 deletions

View file

@ -1,30 +0,0 @@
#!/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
INTERFACE="eth0"
DELAI="10"
OLD_VOLUME_IN=0
OLD_VOLUME_OUT=0
VOLUME_IN=0
VOLUME_OUT=0
while true ; do
VOLUME_IN=$( cat "$FICHIER_PROC" | grep "$INTERFACE:" | sed 's/^[[:space:]]*eth0:\([[:digit:]]\+\)[[:space:]]\+.*/\1/' )
VOLUME_OUT=$( cat "$FICHIER_PROC" | grep "$INTERFACE:" | sed 's/^[[:space:]]*eth0:\([[:digit:]]\+[[:space:]]\+\)\{8\}\([[:digit:]]\+\)[[:space:]]\+.*/\2/' )
if [ "$OLD_VOLUME_IN" -eq "0" ] ; then
echo "Please wait..."
else
printf "%s: %10d o/s reception, %10d o/s emission\n" "$INTERFACE" $(( ( $VOLUME_IN - $OLD_VOLUME_IN ) / $DELAI )) $(( ( $VOLUME_OUT - $OLD_VOLUME_OUT ) / $DELAI ))
fi
OLD_VOLUME_IN=$VOLUME_IN
OLD_VOLUME_OUT=$VOLUME_OUT
sleep "$DELAI"
done

39
script_debit2.sh Executable file
View file

@ -0,0 +1,39 @@
#!/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