Tracking Internet Usage

Hi,
Im on a limited data plan and wish to track the Datavolume i used with a python script on my homeserver
In the openwrt LuCI the information i want to track already shows up as RX and TX of the wwan0 interface, of course i could read that out with an automated browser instance, but that would be a giant waste of resources.
There has to be a simpler way to pull that data, is there some file i can read out over ssh, or is there already some special protocol implemented for it?

INTERFACE="wwan0"
TX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)
RX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)

this is how u can get that information
then choose how to send it to where u want

Great, thank you <3

on router:

while true; do
    echo "<html><body>TX $(cat /sys/class/net/wan/statistics/tx_bytes)</body></html>" > /www/tx.html
    echo "<html><body>RX $(cat /sys/class/net/wan/statistics/rx_bytes)</body></html>" > /www/rx.html
    sleep 2
done

access using http://router.IP/tx.html and rx.html, no logon required.

hey thats even better than setting up ssh, i wouldnt want some automated script running with root access using my cert for that, so i would have to deal with setting up another user otherwise

thought about it a bit and doesnt rewriting the file all the time degrade the storage? its not saved into ram is it? and routers tend to have shitty flash storage thats not designed to be constantly overwritten

I was thinking the same thing and just tested the following addition:

mkdir -p /tmp/www
touch /tmp/www/rx.html
ln -s /tmp/www/rx.html /www/rx.html
touch /tmp/www/tx.html
ln -s /tmp/www/tx.html /www/tx.html

while true; do
    echo "<html><body>TX $(cat /sys/class/net/wan/statistics/tx_bytes)</body></html>" > /tmp/www/tx.html
    echo "<html><body>RX $(cat /sys/class/net/wan/statistics/rx_bytes)</body></html>" > /tmp/www/rx.html
    sleep 2
done

What spence wrote, use soft links.

Pretty sure you don't need the touch, it's OK if the soft link target doesn't exist.

To eliminate the router side processing you can modify what @tanish256 provided and fetch the raw data on the router via ssh.

On your homeserver, fetch the data from the router and assign it to a couple variables.

WAN_RX=$(ssh root@192.168.1.1 cat /sys/class/net/wan/statistics/rx_bytes)
WAN_TX=$(ssh root@192.168.1.1 cat /sys/class/net/wan/statistics/tx_bytes)

Adjust to your scripting environment and needs.

thats just what i did after finding out /tmp is generally in ram on openwrt

echo cant write a file into a folder that doesnt exist yet, and the /tmp/www folder would be deleted at every startup, but then again you could put the .html files directly into /tmp as well

yes i know, that was my first idea, but i want to run it in a script, but i dont want to have my ssh key unlocked by the script and i dont really want the script to have root privileges (on the router) either, so i would need to set up another user on the router etc.

Oops, I missed the following line in my copy/paste.
mkdir -p /tmp/www

I updated the earlier reply.

I made a comment about the touch, not the mkdir (which didn't exist at the time, but now does ,) ).

Correct.

i never use touch, just tested it and it cant create files into nonexistent directories either
i had mkdir in my script instead, so i thought touch would act as an alternative to that

Another thing to consider is vnstat. It does the heavy lifting of collecting the data at 5 minute intervals, combining older samples so as to not take too much storage space and displaying the results at various timescales. It works best if you use removable media like a usb thumbdrive to store the data file (assuming the router has a usb socket).

What are you trying to do?

I use:

  • luci-app-statistics on my router for various data
  • luci-app-vnstat2 on my router for per-interface traffic
  • luci-app-nlbwmon on my router for per-client traffic
  • luci-app-statistics on my additional access points to monitor AP health (interface traffic, wireless quality, and system load) on the statistics instance on the router

Pretty basic stuff.

I never used it, but according to the documentation, luci-app-statistics can output its data to InfluxDB, which can be ingested by Grafana.

So maybe that's how to get an external server (running Grafana) to create alarms based on how traffic on the OpenWRT router increases.