Breathe new life into your old home router
Viewing Router Data
Next, we want to create a simple Common Gateway Interface (CGI) Ash web page to view custom router data and pass the data to another device, such as a Raspberry Pi Node-RED server (Figure 7).
For this project, it's best to use dynamic data. If you don't have external data, then dynamic data from the router's system load data can be used (Figure 8). The command-line statement
cat /proc/loadavg
will show the router's one-, five-, and 15-minute load averages.
By adding some AWK code, you can extract each of the data points directly. For example, to get the first data value, enter the following code:
## Show router load averages cat /proc/loadavg 0.36 0.28 0.16 1/50 3307 ## get the first data point cat /proc/loadavg | awk '{print $1}' 0.36
Custom Router Web Page
OpenWRT runs the uHTTPd web server for its router configuration. This web server can also be used for custom CGI web pages that can use Ash scripting. You will find the OpenWRT custom CGI pages in the /www/cgi-bin
directory. Listing 4 shows an example CGI page, test.cgi
. This example shows the previous load average values along with some system summary information from the Linux monitoring tool vmstat
.
Listing 4
Router Web CGI Example
01 #!/bin/ash 02 # 03 # test.cgi - show system load averages and vmstat 04 # 05 06 echo "Content-type: text/html" 07 echo "" 08 echo "<!DOCTYPE html> 09 <html> 10 <head><title>Router Points</title> 11 </head> 12 <body> 13 <h1>Router CGI Page</h1><hr>" 14 15 # -- show router system load averages -- 16 echo "<h2> System Load Averages </h2>" 17 echo "1 minute load:" 18 cat /proc/loadavg |awk '{print $1}' 19 echo "<br>5 minute load:" 20 cat /proc/loadavg |awk '{print $2}' 21 echo "<br>15 minute load:" 22 cat /proc/loadavg |awk '{print $3}' 23 24 # -- show vmstat -- use <pre> formatting 25 echo "<h2> Vmstat Results </h2> <pre>" 26 vmstat 27 echo "</pre></body></html>"
CGI web pages use Ash/Bash echo
statements to output HTML code. It is important to start the page by echo
-ing out "Content-type: text/html"
with an added new line (lines 6-7). For this example, including HTML heading tags such as <h2>
in the echo
string improves the presentation (lines 16 and 25).
The output from Ash/Bash statements such as
cat /proc/loadavg |awk '{print $1}'
will be shown directly on the web page.
Using the HTML <pre>
tag provides a pre-formatted fixed format for the output from the vmstat
monitoring utility (lines 25-27).
After creating the CGI page, the final step involves setting the file's execution rights as follows:
chmod +x test.cgi
You can now access your custom web page at http://router_ip/cgi-bin/test.cgi. Figure 9 shows the test CGI web page.
Connecting to a Raspberry Pi
For our final project, we want to pass the data from the router to a Raspberry Pi. You could modify this project to pass data to a Home Assistant node or any PC on your home network.
The simplest protocol for passing data is to use TCP sockets. This approach doesn't require any added software to be loaded on either the router or on the Rasp Pi.
You can use the Bash nc
(or netcat) to both send and receive TCP messages. To create an nc
test, open two terminal windows: one on the router and the other on the Raspberry Pi (Figure 10).
To set up a listener on the Rasp Pi, define the Rasp Pi's IP address with a port (1234 in this example). The -l
option sets listening mode, and the -k
option will keep the connection open for multiple messages.
On the router sender side, an echo
message is piped to the nc
command with the Rasp Pi's IP address and port.
Next, you need to periodically send dynamic data out via TCP. Listing 5 shows an Ash script file that uses our earlier Ash/AWK code to get the router's load averages and then pipes the values to a TCP listener every two seconds.
Listing 5
Sending Router Data to a TCP Socket
#!/bin/ash # send_loadavg.sh - Send Router Load Averages via TCP # - send 1 min avg to port 1111 # - send 5 min avg to port 1115 while true do cat /proc/loadavg | awk '{printf $1}'| nc 192.168.1.11 1111 cat /proc/loadavg | awk '{printf $2}'| nc 192.168.1.11 1115 sleep 2 done
Node-RED [5] is a great visual programming environment that comes preinstalled on the Raspberry Pi Raspbian image. To get TCP messages from the script in Listing 5 into Node-RED, two tcp in
nodes can be configured with the required port numbers. To show the data graphically, two dashboard ui_gauge
nodes can be connected to the outputs of the tcp in
nodes. Figure 11 shows the Node-RED logic and web dashboard for the two router load average points.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.