Server management and monitoring via Telegram bot

I am a developer in a company that deals with automation and remote control of industrial facilities. My responsibilities include developing and maintaining top-level software, administering the server and ensuring its smooth operation. Usually the software works stably, but sometimes there are failures (for reasons beyond the control of our software), which I sometimes find out about with a great delay. It was decided that a monitoring system was needed that would notify me and the management about problems so that they could be quickly resolved. In addition, I would like to be able to quickly fix the problem, even if there is no PC “at hand”.


The administered server is hosted by the customer and I have limited opportunities to connect to it (RDP and HTTP).

Figure 1 - Simplified network diagram.
Figure 1 – Simplified network diagram.

This most likely puts an end to standard monitoring systems. Therefore, I decided to write my own microsystem with the ability to manage the server. In Python, I wrote a Web service that performs certain actions at a given URL, and a Telegram bot that polls this Web service.

Figure 2. - Scheme of monitoring via a Telegram bot.
Figure 2. – Scheme of monitoring via a Telegram bot.

At the moment, the Web service is able to customize the user configuration:

  • execute queries to various databases;

  • issue information about:

  • CPU load;

  • loading RAM;

  • free disk space.

  • execute scripts and programs;

  • manage services.

Telegram bot is also configurable and can:

  • poll the server at regular intervals (to implement monitoring) and send changes to chats;

  • create a menu based on settings and access rights;

  • poll the server based on the selected menu item and generate a response;

  • request a password for additional authorization to perform critical operations (MD5 hash of users’ passwords is stored in the configuration, do not forget to clear messages after entering the password).

Monitoring supports:

  • three types of events:

    • norm (italic font);

    • warning (underlined font);

    • alarm (bold).

  • notifications based on database queries;

  • service notifications;

  • notifications about the load of server resources (processor, RAM, disks).

Figure 3 - Demonstration of the monitoring system.
Figure 3 – Demonstration of the monitoring system.

The system works so far only on Windows systems and is written in Python 2.7. It should work under Python 3.x too, you only need to remove the plug-in versions in the requiremets.txt files.

I will quickly outline how to start this system:

  1. Download the project https://github.com/NenashevSV/TeSeMoMa; 2.

  2. On the machine to be monitored:

    1. If not, then install a web server that supports WSGI and Python (they must be of the same bitness);

    2. We rewrite the WebWorker folder on the disk;

    3. We set up a web server for it;

    4. Open the command line, go to the WebWorker folder and execute:

      1. ‘pip install virtualenv’ – install the program for creating a virtual environment;

      2. ‘virtualenv env’ – create a virtual environment;

      3. ‘env / Scripts / activate.bat’ – activate the virtual environment;

      4. ‘pip install -r requirements.txt’ – install dependencies;

    5. We configure the settings.php file. (I think everything will be clear there)

    6. We carry out a check at http: //host/path_to_WSGI.py/settings_name/item_name/ {sub_name_name /…./} action_name For example http: // localhost / WebWorker / hardware / cpu to which you should receive something like this: “{” message “:” u0417 u0430 u0433 u0440 u0443 u0437 u043a u0430 u043f u0440 u043e u0446 u0435 u0441 u0441 u04043e u0441 u0430 u0432 u043d u043e u0440 u043c u0435 ( u043e u0442 0 u0434 u043e 85%) “,” type “: 0}”

  3. On the machine with which we will monitor (maybe the same machine):

    1. Copy the “Observer” folder to the disk. If not, install Python;

    2. Open the command line, go to the Observer folder and execute:

      1. ‘pip install virtualenv’ – install the program for creating a virtual environment;

      2. ‘virtualenv env’ – create a virtual environment;

      3. ‘env / Scripts / activate.bat’ – activate the virtual environment;

      4. ‘pip install -r requirements.txt’ – install dependencies;

    3. We register the bot via the Telegram bot @BotFather (write / start to it and then act according to the description);

    4. We configure the settings.php file. There are many comments that should be clear;

    5. Launch start.bat (for permanent work, I recommend registering it as a service, for example, using the nssm program https://pc.ru/articles/zapusk-prilozheniya-kak-sluzhbu-windows);

    6. To request a menu, send the bot a question mark “?”, For authorization, “pass your_password”.

That’s all, I will be glad if the project is useful to someone.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *