📜 ⬆️ ⬇️

Cost Optimization Yota: Attempt # 3

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/account
pb_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/devices

Well then. Deal done, message delivered. Now you can show the code and tell how to prepare the whole thing.

Installation


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.

Source: https://habr.com/ru/post/228959/


All Articles