Remote monitoring and management of Linux / OpenWrt / Lede-based devices through port 80, continued
- Tutorial
The last time I wrote about how I implemented device monitoring, now we will focus on management. In discussions with "techies" on the part of the Customer, I often encounter a limited perception of the capabilities of such small devices (with low memory resources and performance), many people think that "the maximum that we need is to send a reboot, for something more serious - we will send a team" .
But practice shows that this is not entirely true.
Here is a short list of common common tasks:
- Network diagnostics and troubleshooting. Behind the ethernet port of your router, another piece of hardware usually has its own internal IP address. Sometimes, it can (need) "ping". Or tunnel management - if a router that doesn’t suddenly go up on a router running through a 3G modem, but we see the router itself.
- System service. Firmware upgrade, service script upgrade.
- Balancing act. This could be called "perversions", but the concept of "balancer" as, I quote, "the ability of a circus artist to maintain balance in an unstable position of the body" is more suitable. Similar situations arise due to the limited budget of the customer. Below are a couple of examples, but because they have no direct relation to the subject of the narrative, put them in notes
And we must not forget that due to the mass of circumstances uncertain in advance, control should be carried out in non-standard conditions, when we cannot connect to the router directly via ip: the port and are forced to just wait for activity from it. If we ignore it, then the dialogue between the server and the router can be represented like this:
- Router : hi. I am such a router, are there any tasks for me?
- Server : such and such a router I registered you that you are alive. Here's the task: show me the result of the ifconfig command?
- Router : hi. I’m such a router, the last time you asked me to show the result of ifconfig, here it is. Are there any tasks for me?
- Server : such and such a router I registered you that you are alive. There are no tasks for you.
The most interesting question: how can a remote router send a certain amount of information? In the last part, I described that on the router due to limited resources there is only a "stripped down" wget that works only through GET and nothing else, there is no ftp client or curl. More precisely, we need a universal way, regardless of the features of the image assembly. I settled on using wget. More precisely, how I “stopped” - I just had no choice :)
Let's move on to implementation. Let's say your customer wants from zabbix to reboot the router easily and naturally, with a “click of the mouse." Today we will begin the description of implementation with zabbiksa.
In the menu "Administration" -> "Scripts" add a new script. We call it “Reboot”, as a command we set it to “php /usr/share/zabbix/reboot.php {HOST.HOST}”

Next: Menu “Monitoring” -> “Recent data” -> “Right-click on the node network ". This is how the menu will look after adding a script.

Accordingly, we put the reboot.php script in the / usr / share / zabbix directory (you may have a different one, I use the zabbixa root directory).
Reboot.php file
set_charset("utf8");
// "Отправляем" команду reboot за счет изменения поля task таблицы users. В поле task можно отправлять любую команду.
$sql_users=$conn->prepare("UPDATE users SET task='reboot' WHERE id=? AND status='active';");
$sql_users->bind_param('s', $user);
$sql_users->execute();
$sql_users->close();
?>Actually everything. The question “how to get the result of the execution of a command from the device side” remains open. Consider the problem using the ifconfig command as an example. This command can be sent to the device:
message=`ifconfig`; wget "http://xn--80abgfbdwanb2akugdrd3a2e5gsbj.xn--p1ai/a.php?u=user&p=password!&m=$message" -O /tmp/out.txt, where:
message = `ifconfig` - we assign the $ message variable to the result of the output of the ifconfig
wget command “ xn - 80abgfbdwanb2akugdrd3a2e5gsbj.xn - p1ai / a.php - our a.php script that registers routers and receives messages from them
u = user & p = password! & m = $ message - credentials and the value of the request variable m - assigns the contents of the variable $ message
-O /tmp/out.txt - we do not need output to the file /tmp/out.txt, but if you do not specify this parameter, wget does not work
Well, I’ve touched the future: I haven’t figured out how to reflect the results (for example, the result of the command) that come to the server using standard zabbix tools.
I remind you that all sources can be taken from the Git repository