Hi!
Just wanted to share my setup to monitor my traffic from my fritzbox (7490) in pimatic and with my smart mirror.
Since my provider is now restricting my traffic I wanted to have an eye on it.
I use a simple shell script to get the total bytes (current month):
You’ll need to adjust the config values to your needs.
Also pimatic needs a variable $traffic
#!/bin/bash
#########################################################
#
# modified to be used to collect the total traffic (actual month) from your internet connection
# Use case was the newly released 300GB limit/month from my internet provider
# Januar 2015 - georg dot online at posteo dot de
#
# original from
# June 2013 - framp at linux-tips-and-tricks dot de
#
#########################################################
## Edit this ##
FRITZBOX="192.168.0.1"
PASSWORD="FRITZ USER"
PIMATIC_USER="USER"
PIMATIC_PASS="PASS"
PIMATIC="PIMATIC IP:CUSTOMPORT"
## script ##
challengeRsp=$(curl --header "Accept: application/xml" \
--header "Content-Type: text/plain" \
"http://$FRITZBOX/login_sid.lua" 2>/dev/null)
challenge=$(echo $challengeRsp | sed "s/^.*<Challenge>//" | sed "s/<\/Challenge>.*$//")
if [[ -z $challenge ]]; then
echo "No challenge found"
exit 0
fi
challenge_bf="$challenge-$PASSWORD"
challenge_bf=$(echo -n $challenge_bf | iconv -f ISO8859-1 -t UTF-16LE | md5sum -b)
challenge_bf=$(echo $challenge_bf | sed "s/ .*$//")
response_bf="$challenge-$challenge_bf"
url="http://$FRITZBOX/login_sid.lua"
sidRsp=$(curl --header "Accept: text/html,application/xhtml+xml,application/xml" \
--header "Content-Type: application/x-www-form-urlencoded" \
-d "response=$response_bf" \
$url 2>/dev/null)
sid=$(echo $sidRsp | sed "s/^.*<SID>//" | sed "s/<\/SID>.*$//")
regex="^0+$"
if [[ $sid =~ $regex ]]; then
echo "Invalid password"
exit 0
fi
IFS=' '
stats=$(curl --header "Accept: application/xml" \
--header "Content-Type: text/plain" \
"http://$FRITZBOX/internet/inetstat_counter.lua?sid=$sid" 2>/dev/null)
stats=$(echo $stats | grep "inetstat:" | sed "s/inetstat:status\///" | sed 's/[["]//g' | sed 's/]//' | sed 's/ = / /' | sed 's/,//' | sed 's/\// /' | sed 's/^ //')
IFS=$'\n'
regex="([a-zA-Z]+) ([a-zA-Z]+) ([0-9]+)"
for line in $stats; do
if [[ $line =~ $regex ]]; then
date=${BASH_REMATCH[1]}
type=${BASH_REMATCH[2]}
value=${BASH_REMATCH[3]}
if [[ "$date" == "ThisMonth" ]]; then
case "$type" in
"BytesReceivedLow")
bytesreclow=$value
;;
"BytesReceivedHigh")
bytesrechigh=$value
;;
"BytesSentLow")
bytessentlow=$value
;;
"BytesSentHigh")
bytessenthigh=$value
;;
esac
fi
fi
done
# Get all possible output
#
# for line in $stats; do
# if [[ $line =~ $regex ]]; then
# date=${BASH_REMATCH[1]}
# type=${BASH_REMATCH[2]}
# value=${BASH_REMATCH[3]}
# echo $date $type $value
# fi
# done
# We need to convert the output to 64bit - to have the actual traffic
bytesIn=$(($bytesrechigh*4294967296+$bytesreclow))
bytesOut=$(($bytessenthigh*4294967296+$bytessentlow))
# Get totals..
bytes=$((bytesIn+$bytesOut))
# And output it (used for my mirror)
echo $bytes
# send result to pimatic
curl --insecure -X PATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": '"${bytes}"'}' --user "${PIMATIC_USER}:${PIMATIC_PASS}" https://$PIMATIC/api/variables/traffic >/dev/null 2>&1
With a variable device: (please note that this will look ugly if you have less than 1GB traffic - you can use a custom script to preprocess the value before parsing it…)
{
"id": "traffic-variable",
"name": "Traffic",
"class": "VariablesDevice",
"variables": [
{
"name": "Traffic",
"expression": "($traffic/1000/1000/1000)", # convert bytes to GB
"type": "number",
"unit": "GB"
}
]
},
Some pictures:
smart mirror:
http://fs1.directupload.net/images/150128/t6qa3njs.jpg
http://fs2.directupload.net/images/150128/595qm535.png