Last active
July 3, 2017 02:46
-
-
Save porjo/3a2be2604d49fc5b5856e68ba9dcd1a7 to your computer and use it in GitHub Desktop.
Simple interface byte counter script for 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/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