What is it for
When the number of mobile phones in the family becomes more than 3, the question of monitoring balance and alerts is brewing when the balance approaches a critical point. There are a lot of tools for monitoring the balance, but why make a fuss when there is an indispensable Nagios at hand (it so happens that at home I have my own monitoring server, its main task is to monitor the status of the servers of the organizations I administer during off-hours). This solution is also suitable for monitoring the balance of a corporate personal account.
Small retreat
To monitor, we will be using http / https requests to the servers of mobile operators. For this we need php with the curl library connected. Nagios will be used for monitoring, and Mrtg for plotting. In this version, work with two mobile operators, MTS and Beeline, is implemented. Oddly enough, Beeline turned out to have the least of problems: we make a request for authorization, then we make a request for a balance, then to parse the HTML output. With MTS, it turned out to be more interesting, for each region they use their domain name “Internet Assistant”, which adds a couple of unplanned actions to the algorithm. The problem was solved by calculating the region using the telephone number prefix, this was helped by the
MTT Internet resource, which provides access to the database of telecom operator prefixes. After receiving the region, we get the domain name "Internet Assistant" by parsing the
java-script from the MTS website, then authorizing and parsing the HTML output. It is also possible to fasten a megaphone here, but not yet before it.
So, from theory to practice
I will not focus on the configuration of Nagios, I think the inhabitants of the Habr will not have any problems with this, and move, so to speak, to the most delicious. We download the latest version of the scripts
from here , unpack. we get 3 files, mobile-api.php, nagios-check-mobile-balance.php, mrtg-mobile-balance.php. From the title, the purpose of the files, the plugin for Nagios, the script for MRTG and the file with functions for obtaining balance are understandable (I tried to uncomment it in as much detail as possible).
Copy files in places and assign rights
cp ./mobile-balance-api.php /usr/local/lib/ cp ./nagios-check-mobile-balance.php /usr/lib/nagios/plugins/check_mobile_balance.php cp ./mrtg-check-mobile-balance.php /usr/lib/mrtg2/check_mobile_balance.php chmod +x /usr/lib/nagios/plugins/check_mobile_balance.php chmod +x /usr/lib/mrtg2/check_mobile_balance.php
')
We check the performance
First, you need to fix the constants in /usr/local/lib/mobile-balance-api.php
DEFINE("CACHE_PATH", "/var/cache/balance");
Then you need to fix the path in the constant "MOBILE_API" in the file /usr/lib/nagios/plugins/check_mobile_balance.php
DEFINE("MOBILE_API", "/usr/local/lib/mobile-balance-api.php");
If you do not have a password to access the "Internet Assistant", then do the following:
- For MTS users: from your mobile phone (or in the MTS-Connect program) dial: * 111 * 25 # or call 111 5 (when in the home network) and follow the instructions of the autoinformer.
- For Beeline users: call the toll-free number * 110 * 9 # from your phone and get an SMS with the password to the system.
Test run script
/usr/lib/nagios/plugins/check_mobile_balance.php -w 20 -c 10 -n 9128141111 -p 123456
Answer
BALANCE OK- +79128141111 = 55 rub
If the password is incorrect, or the MTS host is down (this is very often the case, Beeline is better) the script will return the 0th balance.
Customize Nagios
Edit the file /etc/nagios/objects/commands.cfg, add a new command "check_mobile_balance"
define command{
command_name check_mobile_balance
command_line $USER1$/check_mobile_balance.php -w 30 -c 10 -n $ARG1$ -p $ARG2$
}
-w 30 is a warning threshold in rubles
-c 10 is the threshold critical / critical in rubles
Then edit /etc/nagios/objects/localhost.cfg add checks
define service{
use local-service
host_name localhost
service_description Balance +79128141111
check_command check_mobile_balance!9128141111!123456
}
Where 9128141111 is the phone number, 123456 is the password for the mobile assistant.
Then we re-read the Nagios settings:
service nagios reload
and go to the web interface to enjoy the result:

Charts
For charting, I prefer to use MRTG. This is a lightweight, easy-to-use application. We mean that MRTG is installed, configured and working for you. Check the constant “MOBILE_API” in the file /usr/lib/nagios/plugins/check_mobile_balance.php. Rules / etc / mrtg / mrtg.cfg add the following lines:
Target[mb]: `/usr/lib/mrtg2/check_mobile_balance.php 9128141111 123456`
MaxBytes[mb]: 500
MaxBytes2[mb]: 75
Title[mb]: Mobile balance +79218141111
Options[mb]: unknaszero,gauge,growright,nopercent,transparent,noborder,pngdate,noinfo
AbsMax[mb]: 500
YLegend[mb]: Rub
ShortLegend[mb]: R
Legend1[mb]: Rub
Legend2[mb]: Rub
LegendI[mb]: Rub
LegendO[mb]: Rub
PageTop[mb]: Mobile balance +79218141111
Testing, env
LANG=C mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log > /dev/null 2>&1
We look what happened.

I think it makes no sense to list scripts, for there are many lines. Just in case, I will duplicate the link to them
here .
Total
In fact, the script is a universal solution, and they can monitor not only the balance of a mobile phone, but also, for example, rating on rutracker or karma on Habré.
Monitoring karma on Habré
To monitor karma on Habré, you must explicitly register the user “habrahabr.carma.yozhiks” (the user
yozhiks was not chosen by chance, in order to pay tribute for an invite) in the $ manual_detect variable in the /usr/local/lib/mobile-balance-api.php eg:
$manual_detect = array ( "9128141111" => array("url" => "https://ihelper.ural.mts.ru/SelfCarePda/Security.mvc/LogOn", "operator" => "mts"), "9061971111" => array("url" => "https://uslugi.beeline.ru/loginPage.do|https://uslugi.beeline.ru/vip/prepaid/refreshedPrepaidBalance.jsp", "operator" => "beeline"), "habrahabr.carma.yozhiks" => array("url" => "http://yozhiks.habrahabr.ru", "operator" => "habrahabr.carma") );
and add the operator “habrahabr.carma” to the $ operators variable:
$operators = array( "mts" => array("name" => ' " "', "result" => "<br/>: <strong><strong>|</strong> .< /strong>"), "beeline" => array("name" => ' "-"', "result" => '<td class="tabred">|& nbsp;'), "habrahabr.carma" => array("name" => ' ', "result" => '< span class="mark"><span>|</span></span>') );
check:
/usr/lib/nagios/plugins/nagios-check-mobile-balance.php -w 30 -c 10 -n habrahabr.carma.yozhiks -p 1
Answer:
BALANCE OK- +7habrahabr.carma.yozhiks = 58 rub
Of course, for such monitoring it is necessary to slightly change the script, but in general it turned out quite well.
Thanks for attention.
PS Today, when testing the script, problems were identified with the MTT site. Problems can be avoided by manually writing the number settings to the $ manual_detect variable by pattern.
PPS Nano MRTG HowTo for Windows
Downloading:
windows.php.net/download - php 5.3.5
www.activestate.com/store/activeperl/download - perl 5.12.3
oss.oetiker.ch/mrtg/pub - mrtg 2.17
Install / Unpack:
perl c:\perl
mrtg c:\mrtg
php5 c:\php
mobile-balance-api.php mrtg-check-mobile-balance.php c:\mrtg\bin\
We enable error output in php.ini
display_errors = On
and
error_reporting = E_ALL & ~E_NOTICE
for every fireman.
Rule constants:
mobile-balance-api.php
DEFINE ("CACHE_PATH", "c: \ Tmp");
mrtg-check-mobile-balance.php
DEFINE ("MOBILE_API", "c: \ mrtg \ bin \ mobile-balance-api.php");
Checking:
c:\php\php c:\mrtg\bin\mrtg-check-mobile-balance.php 9128141111 123456
If there are errors, we look understand.
Customize MRTG:
mkdir c:\mrtg\www
Editing / creating c: \ mrtg \ mrtg.cfg
EnableIPv6: no
WorkDir: c:\mrtg\www
Target[mb]: `c:\php\php c:\mrtg\bin\mrtg-check-mobile-balance.php 9128141111 123456`
MaxBytes[mb]: 500
MaxBytes2[mb]: 75
Title[mb]: Mobile balance +79218141111
Options[mb]: unknaszero,gauge,growright,nopercent,transparent,noborder,pngdate,noinfo
AbsMax[mb]: 500
YLegend[mb]: Rub
ShortLegend[mb]: R
Legend1[mb]: Rub
Legend2[mb]: Rub
LegendI[mb]: Rub
LegendO[mb]: Rub
PageTop[mb]: Mobile balance +79218141111
Checking:
c:\perl\bin\perl c:\mrtg\bin\mrtg c:\mrtg\bin\mrtg.cfg
The mb * .png and mb.html files should appear in the c: \ mrtg \ www directory. The
c:\perl\bin\perl c:\mrtg\bin\mrtg c:\mrtg\bin\mrtg.cfg
in the “Task Scheduler”. Everything.
PPPS Plot my karma and ranking on MRTG. Blue - rating, green - karma.
