How make script python read bandwidth

Hello i'm install python like this
opkg install python-light

i want by using python read bandwidth every 15 minute
if today we are usage 7GB or more change this file

path: /etc/config/sqm
======================= Default Value ==================================

config queue 'eth1'
option qdisc 'fq_codel'
option script 'simple.qos'
option qdisc_advanced '0'
option linklayer 'none'
option enabled '1'
option interface 'eth0'
option debug_logging '0'
option verbosity '5'
option download '1840'
option upload '1840'

config queue
option debug_logging '0'
option verbosity '5'
option qdisc 'fq_codel'
option script 'simple.qos'
option linklayer 'none'
option enabled '1'
option interface 'br-lan'
option download '1840'
option upload '1840'
option qdisc_advanced '0'

config queue
option debug_logging '0'
option verbosity '5'
option qdisc 'fq_codel'
option script 'simple.qos'
option linklayer 'none'
option enabled '1'
option interface 'wlan0'
option download '1840'
option upload '1840'
option qdisc_advanced '0'

=========================================================

path: /etc/config/sqm
if we are using 7GB or more change to this info

====================== Change Value ==================================

config queue 'eth1'
option qdisc 'fq_codel'
option script 'simple.qos'
option qdisc_advanced '0'
option linklayer 'none'
option enabled '1'
option interface 'eth0'
option debug_logging '0'
option verbosity '5'
option download '920'
option upload '920'

config queue
option debug_logging '0'
option verbosity '5'
option qdisc 'fq_codel'
option script 'simple.qos'
option linklayer 'none'
option enabled '1'
option interface 'br-lan'
option download '920'
option upload '920'
option qdisc_advanced '0'

config queue
option debug_logging '0'
option verbosity '5'
option qdisc 'fq_codel'
option script 'simple.qos'
option linklayer 'none'
option enabled '1'
option interface 'wlan0'
option download '920'
option upload '920'
option qdisc_advanced '0'

=========================================================

and next day back to default value
path: /etc/config/sqm
just change 920 to 1840

i just want moniter Bandwidth every day if usage 7GB or more change SQM from 1840 to 920 and next day back to 1840
if we can use this tools here https://openwrt.org/docs/guide-user/services/network_monitoring/bwmon
for calc bandwidth and script python read data from tools bandwidth every 15 Minute if this Day use 7GB make sqm limite

way i want do this ? because my ISP give me Bandwidth just 250 GB Every Month
and i limit my connection by add 1840 in SQM it's work very good but i want more limit if my familie usage more than 7GB by day
just change 1840 to 920 and next day back to 1840
note: i want this script run automatically every time run router
sorry my English not good
thank you

Why python? Bash suffices, IMHO. The only problem for me is to determine current traffic.
You should decide, whether to run script: at start, or by cron.

1 Like

i think script Bandwidth store data in database or xml if use python this easy
if you can do this with Bash I hope this :slight_smile:
Because Bash not use Ram like python
Now how use Bash or Python or any tools
i use script Yamon for moniter Bandwidth it's nice script
How can use Bash for read data from Script Yamon Every Hour
If we are use 7GB or more Bash change this file
path: /etc/config/sqm
and next day back to default value
if any one can use bash for read information from Yamon
like this image



this image from script Yamon if we can use another script calc bandwidth not problem
now how can use Bash or python to get this info ?
thank you

It would be relatively easy directly from bash to read values from the YAMon data files. You could also get the values (for all interfaces) from /proc/net/dev.

The slightly more difficult bits include:

  • How often would you like to check the total? (likely a cron entry)
  • What changes would you want to make to SQM?
  • Things would get a little messy if your router reboots though (as those values get reset to zero, if I'm not mistaken).
  • Depending on your router, you may also have to watch for integer overflow... (been there; had to deal with that)

Al

Oops, just read the original message not just the one above my response. I agree that python (while likely cool) is probably overkill

Activating and deactivating the changes in sqm could be accomplished with a single sed -i call or you could simply swap file names

Running every 15min... no problem, an easy cron job

  • e.g., */15 * * * * [root] <path_to_totalling_script>
    (the [root] is optional depending on firmware... If I recall correctly, OpenWrt does not need it)

Resetting at midnight, another cron job

  • e.g., 0 0 * * * [root] <path_to_reset_script>

Getting the totals... perhaps a little more work but not much

  • currentUp=$(cat "/proc/net/dev" | grep 'br-lan'| awk '{ print $2 }')
  • currentDown=$(cat "/proc/net/dev" | grep 'br-lan'| awk '{ print $10 }')
  • echo "up=$currentUp down=$currentDown"
    I leave persisting those values between cron jobs & dealing with a reboot as an exercise for you.

I would not add this directly to YAMon but with those cron jobs above, the two could easily run in parallel.

My two cents.

Al

@al_c

Would it be possible to make a package for YAMon? It would make it a lot more convenient.

I find the band width of Advanced Tomato great, and it would be great to have something like this in OpenWrt. YAMon has the functionality, but needs better integration with OpenWrt and LuCI (and then some Javascript can be used to give a modern and dynamic look.

I know nothing about how to make a package... can you point me in the right direction.

What sort of integration would you like into OpenWrt and LuCI? I may not have much control over that though...

There's lotsa JavaScript already in play... what sorts of changes would you like?
In case you were not aware, you can override the default CSS values (via custom.css in /opt/YAMon4/www/css). I also recently added a Dark Mode on the Settings tab.
image

I haven't started looking into making packages, so I don't know either. But it's on my future to-do list. There is a developer guide in OpenWrt wiki.

I understand that YAMon was developed to run on different firmwares, and that's why it's kind of stand-alone in the way it runs, but the ideal scenario would be that it's integrated in terms of installation, running and more importantly using. Integration in using is probably the most important; ideally the user would prefer to access the info from within LuCI.

There are several bandwidth monitoring packages for OpenWrt already, most of them with GUI, integrated in LuCI, but none of them have a much functionality as YAMon.

As for the front end, I didn't know that there was CSS, but anyway my uneducated guess is that the interface was done by a developer not a designer. I hate to be critical, but I think some sleek improvement in the GUI would give the functionality justice.

As for the JavaScript when I used YAMon, it didn't feel the interface was very reactive. Is it possibly that some of the code is server-side JavaScript?

One more thing, it would be great if using a USB is optional. If the user wasn't to just live with the RAM, why not?

The photos below show real time and historical monitoring graphs respectively from AdvancedTomato. Simple yet elegant. It's also interactive as you can change the scale and the time frame and the change is applied smoothly. (I hope it's OK to include photos from other projects here--it's just an excellent example).


It's hard for me to believe it but I started working on YAMon almost exactly 7 years ago.

Unlike the other built-in and pre-existing monitoring interfaces, I did not want a real-time graph. Instead, wanted to answer the question "which devices used how much data when". And, I want to be able to aggregate it by device owner.

I started by adapting pre-existing (and largely abandoned) usage scripts and then ended up completely rewriting things. Over the years, many many users suggested new features and new ways to present the data. And yes, asked me to support a variety of firmware platforms.

The (bash) data collection scripts were significantly updated in v3 and then again more recently in v4.
The web reports, on the other hand, have been largely unchanged since about v2 and could definitely do with a refresh. The JS and CSS code 'lives' at my web site (https://usage-monitoring.com) because that allows me to fix things in on the fly and you do not have to touch your router. That could change, of course, but would involve your router having to phone-home to auto update or for you to manually intervene. The latter is not desirable and I've simply not made time for the former because (IMHO) the current situation works well (unless my ISP goes down, like it did for a couple hours yesterday... sigh)

With respect to optional USB... the question then becomes where do you locally retain the historical data? In short, USB is the simplest solution. However, you can actually change the data paths to point to any accessible location (e.g., a samba share, etc.). You do not want to store the data in /tmp because that will be lost when your router reboots.

Last but no least, a couple of years ago I did start a prototype of a YAMon variant that stored data to an external database (i.e., hosted at usage-monitoring.com)... data storage seemed to work well and it greatly reduced the amount of code on the server. However, I did not want to mix usage data from two different users into the same database but my ISP does not allow me to programmatically create them... so if I continued to host the data, I would've been stuck manually setting things up for each new user (not feasible). I did not want to go the route of locally created databases because then I'd also have to support installation & creation of the databases, linking the reports, etc., etc. (10x not feasible)... so I was stuck and then got distracted with another shiny object.

YAMon5 might store to a database... and then the reports would have to be completely re-written...

Al

1 Like

BTW - If you have suggestions for improvements to the YAMon scripts and/or reports, I'm all ears. All of the code is available in GitHub... https://github.com/al-caughey/

No worries if you can't help with the code, create an issue and add screenshots, etc.

1 Like