Skip to content

Instantly share code, notes, and snippets.

@porjo
Last active July 3, 2017 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porjo/3a2be2604d49fc5b5856e68ba9dcd1a7 to your computer and use it in GitHub Desktop.
Save porjo/3a2be2604d49fc5b5856e68ba9dcd1a7 to your computer and use it in GitHub Desktop.
Simple interface byte counter script for Linux
#!/bin/ash
#
# Track bytes in/out on selected interface
#
# Run this script once a month/week/day to calculate how much data usage occuring
# on given interface
#
STOR="bwmon.store"
STAT_DIR="/sys/class/net/$1/statistics/"
[ $# -ne 1 ] && echo "Usage: $0 <interface>" && exit 1
for X in tx rx; do
B=`cat ${STAT_DIR}/${X}_bytes`
OLD_B=`grep ${1}_${X} $STOR | awk '{print $2}'`
[ -z "$OLD_B}" ] && OLD_B="0"
DIFFB=$(( B - OLD_B ))
echo "New ${X}: $B"
echo "Old ${X}: $OLD_B"
echo "Diff ${X}: $DIFFB"
grep "${1}_${X}" $STOR > /dev/null
if [ $? -ne 0 ]; then
echo ${1}_${X} $B >> $STOR
else
sed -i "s/^${1}_${X}.*$/${1}_${X} $B/" $STOR
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment