Hi, Habr!
Summer has arrived and many people are leaving the city. Someone on vacation, and someone for the whole summer (if the work allows). But one of the main problems in the country (for all people, one way or another connected with IT) is the lack of a normal wired fast Internet. But this is partially solved due to the existence of LTE networks.
In my region there are only two major LTE providers: Megaphone and Yota. Megaphone is much cheaper, but it has one extremely unpleasant feature: a limit of 20GB of traffic per month, even at the maximum rate.
')
Therefore, the choice of the operator, in my opinion, is obvious. But still paying for 20 megabits twice as much as for 100 at home is a dubious pleasure. But at the same time, unlike other operators, Yota allows you to change the current tariff in your personal account at any time for free with recalculation of paid time. Need speed - unscrew the slider to the maximum. Not? Then you can slow down and pay less. Well, how can you resist and not automate this process?
On Habré there were already articles (
one ,
two ), describing the attempts of automata automatics. However, due to some peculiarities, their creations did not suit me and I had to write my own, significantly different bicycle. However, due to some peculiarities, their creations did not suit me and I had to write my own, significantly different bicycle.
And we will begin, perhaps, with the analysis of the reasons why other people's bicycles did not suit me. Particularly impatient / financially interested can go directly to the section describing the implementation.
Formulation of the problem
The linx56 option didn’t
work for me at all, because phantomjs works as it does, which requires quite a good iron (which Raspberry PI is not) to work with. And even if I put it on my main computer and leave it to work around the clock, scoring a schedule on which to turn off the Internet to cron, I will
get an electricity bill more than I save anyway, I won’t get a normal result, because the Internet is used rather unpredictably (and not only by me, but also by family members, for whom the main thing is to “just work”).
The second option from
bambrman significantly wins from the first one because instead of phantomjs it uses a much less specious curl. But otherwise everything is the same.
As a result, it was decided to write another bike, which was based not on the schedule (or human hands), but on the current speed consumed by all devices in the local network.
Implementation
I used Raspberry Pi and a Python script that connects SSH to the main home router every TPN (TP-LINK WDR4300 with OpenWrt on board) and with the help of ifconfig it tightens the statistics of the desired interface.
After that, he considers the average speed for the interval between the last and the previous measurement and sends it to the module, which is responsible for switching the tariffs in accordance with the current speed and settings of the config. To make it clearer, here is a part of it:
modes: { 0: 320, 290: 512, 495: 768, 700: 1.0, 900: 2.1, 1900: 3.1, 2900: 4.1, 3900: 6.5, 6500: 8.0, 8000: 10.0, 10000: 15.0, 14800: 20.0 }
As you can see, the speed in kilobits acts as the key of the dictionary, and the iota tariff is the value.
According to the results of measuring the current speed, we have, say, 1530 kilobits. Rounded down to the value of the nearest key in the config and we get 900. Accordingly, the tariff should be used at a speed of 2.1 megabits. In case this tariff is already active, nothing happens. But if you need to raise the tariff, then there is one feature.
The problem is that if the Internet was not used for a long time (and the tariff of 320 kbit / s was activated), and then someone suddenly turned on YouTube, then switching from 320 kbit / s to 3.1 Mb / s may take a lot of time (not to mention the forever lagging LC Yota). On the other hand, to jump every time to the maximum rate, when someone decided to load a couple of pages and a few pictures is also a bad idea. In general, I did not come up with anything better than adding to the config
speed_increase_step: 1
This parameter indicates the size of the upgrade step between the tariffs. That is, if you set speed_increase_step = 5, an upgrade from 320 kbps will immediately happen to 3.1 megabits, and then 15.0 and 20.0. And if the possible speed will not be used completely, it will be compensated by the downgrade mechanism.
Downgrade
If the active tariff is not fully used for a long time (the current speed does not exceed the threshold required for the current tariff), the script waits for n minutes and switches to a tariff that satisfies the current requests of the local network (based on the average speed). This is the number of minutes specified in the hold_high_speed_time parameter in minutes.
hold_high_speed_time: 3
In fact, the main functionality ends here. True, there is still something that, perhaps, someone may be interested in:
Notifications about tariff change via PushBullet
It was written, like everything else, primarily for yourself. But to be honest, to be honest, it was not useful, because it generates too much spam. But maybe someone will like it;)
This case is included in the same place, in the config file:
pb_enabled: true pb_api: v13WA7HYfwj99gUz8rwvK2m7eLN1uheVxZujAgP8gn2su pb_devices: [ ujAgP8gn2sasFDsgsWhxs, ujAgP8gn2mkasnuH2Gtga ]
As you can see, everything is pretty simple here. pb_enabled is true / false. Appointment is obvious.
pb_api - your key from
www.pushbullet.com/accountpb_devices - a list of device identifiers to which notifications are sent. You can take it here:
api.pushbullet.com/v2/devices . At the entrance you will be asked to log in: just enter the api key in the login field. Full description of the method here:
docs.pushbullet.com/v2/devicesWell then.
Deal done, message delivered. Now you can
show the code and tell how to prepare the whole thing.
Installation
- First of all, go and install on your OpenWrt router. Well, or something similar with SSH, authorization by keys and ifconfig utility.
- After that, you need to find something like Raspberry Pi. That, on what it is possible to start the third python and where there is an ssh-client with authorization on keys.
- Configure key authorization to connect to the router. We are convinced that the command of the form (with your data, of course) "ssh 192.168.1.1 -p 22 -l root ifconfig" works without additional questions
- We put Python 3 and pip. Initially, everything was written and tested on 3.2.3. Compatibility with earlier versions of the tests was not
- We clone a project repository with GitHub: https://github.com/wutiarn/yota-speed-controller
- Making cd in the repository folder
- Perform pip3 install -r requirements.txt. Please note that pip may have a different name (pip-3.2, for example)
- Edit config.yaml. Descriptions of most of its parameters are at the top. The rest, as it seems to me, need no introduction. Well, except that the interface. By default, eth0.2 is listed there. Usually, eth0.2 is the wan-interface in openwrt, but it’s better to check. Also worth noting is the use_ssh option. If you set it to false, then it’s perfectly logical that the local interface will be monitored. Well, this is in case someone has a full-fledged computer as a router. Well, or someone will run the whole thing right on the router
- After that, run main.py. It can be either directly (after giving it the right to execute it), or by passing it to the python interpreter
- If everything works, then install supervisor (usually available in standard repositories)
- Moving the yota.conf file to the /etc/supervisor/conf.d/ folder along with correcting the paths to main.py and, if you wish, to the file to which the log will be written
- And restart the supervisor with the supervisorctl reload command. The daemon should start automatically. Look at last logs. They should have something like:
2014-07-06 14:16:01,805 [INFO] yota | Initializing 2014-07-06 14:16:11,497 [INFO] yota.web | CURRENT TARIFF: 512 2014-07-06 14:16:11,712 [INFO] yota | Initialised 2014-07-06 14:16:11,718 [INFO] yota | Starting 2014-07-06 14:16:16,016 [DEBUG] yota.current_speed_provider | Current speed: RX: 5 TX: 9. Summary: 14, Time: 3 2014-07-06 14:16:17,262 [INFO] yota.speed_control | Downgrade started: 2014-07-06 14:19:16.022307 2014-07-06 14:16:18,315 [DEBUG] yota.current_speed_provider | Current speed: RX: 7 TX: 19. Summary: 26, Time: 2 2014-07-06 14:16:20,575 [DEBUG] yota.current_speed_provider | Current speed: RX: 12 TX: 20. Summary: 32, Time: 2
That's all. I understand that this is some abuse of the generosity of Yota (although, given the tariffs, is it generous?), But nevertheless I dare to hope that no one is going to change anything. In the end, there are not so many people capable of deploying this. And if you consider that few of them use Yota, then so and in general is a trifle.