HowTo: Poor mans data counter
In the absence of a reliable data counter, some people might have an eye on their bandwidth consumption nevertheless due to their providers plans. Using an Android app for this purpose seems to be not quite the adequate solution.
I whipped up a script that can be registered as a service and just counts bytes by interface. Note it starts counting from the most recent boot, not the moment the script has been started.
When started interactively on a terminal, it shows the current stats when Enter is pressed. As a service it just logs the data into a file. If the script is then just started on a terminal to peek into the values, it doesn't interfere with the service, as both use the same files.
To avoid cluttering the system, all files including the script itself should be put into /home/nemo/netcount. This way it's easy to get rid of it if the Jolla builtin counter works as intended.
To register it as a service, create this file (netcount.service), copy it to /etc/systemd/system and issue a sudo systemctl enable netcount.service
:
[Unit]
Description=Count bytes passing the data connection interface
Before=default.target
[Service]
Type=forking
ExecStart=/home/nemo/netcount/netcount.sh
[Install]
WantedBy=default.target
If you don't want to run it as a service, just chmod 755 netcount.sh
the script and start it on a terminal.
#!/bin/bash
# Script for logging interface traffic - marsch, 20140718
# Note: /bin/bash necessary for read timeout parameter
IFACE=rmnet0
DIR=/home/nemo/netcount
INTERVAL=5
LOCK=/tmp/netcount
# Data counter starting at reboot
RX_STAT=/sys/class/net/$IFACE/statistics/rx_bytes
TX_STAT=/sys/class/net/$IFACE/statistics/tx_bytes
# Persistent data storage
RX_FILE=$DIR/rxbytes
TX_FILE=$DIR/txbytes
# Data logged before reboot
RX_TMP=$DIR/rxtmp
TX_TMP=$DIR/txtmp
# Functions
val() {
echo `head -1 $1`;
}
show() {
echo $1 | awk '{ sum=$1 ; hum[1024**3]="GB";hum[1024**2]="MB";hum[1024]="KB"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}'
}
update() {
echo $(( $2 + $3 )) > $1
}
# Create new files when starting from scratch
if [ ! `ls -1 $RX_FILE* &>/dev/null ` ] && [ ! `ls -1 $TX_FILE* &>/dev/null` ]; then
DATE=`date +%Y%m%d.%H:%M`
RX_FILE=${RX_FILE}_${DATE}
echo 0 > $RX_FILE
TX_FILE=${TX_FILE}_${DATE}
echo 0 > $TX_FILE
echo "New persistence files created at `date`"
else # Use available data
RX_FILE=`ls -1 $RX_FILE* | head -1`
TX_FILE=`ls -1 $TX_FILE* | head -1`
echo "Files found, using $RX_FILE and $TX_FILE."
fi
# Look for lockfile and add up old value if reboot
# has been detected (assumes /tmp is deleted at boot)
if [ ! -f $LOCK ]; then
echo "$LOCK not existing, assuming reboot or first invocation."
touch $LOCK
if [ -f $RX_TMP ] && [ -f $TX_TMP ]; then
echo "Summing up data captured before reboot."
update $RX_FILE `val $RX_TMP` `val $RX_FILE`
update $TX_FILE `val $TX_TMP` `val $TX_FILE`
fi
fi
# Read persistent data
RX_PERSIST=`val $RX_FILE`
TX_PERSIST=`val $TX_FILE`
# Start logging loop
while true; do
RX_CUR=`val $RX_STAT`
TX_CUR=`val $TX_STAT`
RX_TOTAL=$(( $RX_PERSIST + $RX_CUR ))
TX_TOTAL=$(( $TX_PERSIST + $TX_CUR ))
# Check if shell is interactive
if [ "${-#*i}" != "$-" ]; then
sleep $INTERVAL
else
read -t $INTERVAL STROKE
if [ $? -eq 0 ] && [ x$STROKE = x"" ]; then
tput cuu1
echo -n -e "\033[1;32m$IFACE \033[m"
echo -e "\033[0;32m| Received `show $RX_TOTAL`, Sent `show $TX_TOTAL`, Total `show $(( $RX_TOTAL + $TX_TOTAL ))`\033[m"
fi
fi
echo $RX_CUR > $RX_TMP
echo $TX_CUR > $TX_TMP
done
Don't blame me if it doesn't work for you. It's provided as is, do whatever you want with it. All rites reversed.
EDIT: Starting to count from the point where new data files are created would be easy in fact. Just remember the counter value at startup and write the difference in the tempfile instead of the absolute value. But as Jolla is resetting/rebooting by itself a couple of times a day currently when running on battery, I'd say this is a rather futile exercise.
Well, thank you, is something in the pure spirit of Sailfish.
magullo ( 2014-07-20 23:01:16 +0200 )editAbsolutely great. Thanks a lot.
Mced ( 2016-11-20 19:07:05 +0200 )edit