Using LEDs to Indicate Ubuntu Updates
I have an installation of Rundeck on my Raspberry Pi 4 cluster. It will periodically use SSH pubkey auth to login to each other server in the cluster and turn on GPIO pins, which are connected to breadboards with other components nearby. If you don’t use Rundeck, you can use these scripts with crontab as well, or tie them directly to the existing upgrade check cron schedule. Then, pending an update, LEDs will illuminate, showing me which nodes I need to update.
#!/bin/bash # Check for available updates sudo apt update > /dev/null num_updates=$(apt list --upgradable 2>/dev/null | grep -c '\-') # If updates are available, turn on the LED if [ $num_updates -gt 0 ]; then echo 1 > /sys/class/gpio/gpioXX/value # Replace XX with the GPIO number of the LED else echo 0 > /sys/class/gpio/gpioXX/value fi
To use this script, follow these steps:
Connect an LED to one of the GPIO pins on your Raspberry Pi 4. You can find a pinout diagram online or by running the gpio readall command in the terminal.
Note the GPIO number of the pin you connected the LED to.
Open a terminal on your Raspberry Pi and create a new bash script if you don’t use Rundeck. If you do, make this a script to execute on each node.
If there are any available updates for your Raspberry Pi, the LED connected to the GPIO pin will turn on. If there are no updates available, the LED will turn off.
If you’re feeling particularly fancy, here’s one that turn on a different color LED for security updates and another for normal updates. Now you can see which servers in your rack…err, desktop home cluster, have updates. (I’d use this at work, no hesitation).
#!/bin/bash # Check for available updates sudo apt update > /dev/null normal_updates=$(apt list --upgradable 2>/dev/null | grep -c '^\S*[[:space:]]\S*[[:space:]]\S*') security_updates=$(apt list --upgradable 2>/dev/null | grep -c '\[security') # If security updates are available, turn on the red LED if [ $security_updates -gt 0 ]; then echo 0 > /sys/class/gpio/gpioXX/value # Replace XX with the GPIO number of the red LED # If normal updates are available, turn on the green LED elif [ $normal_updates -gt 0 ]; then echo 1 > /sys/class/gpio/gpioYY/value # Replace YY with the GPIO number of the green LED # If no updates are available, turn off both LEDs else echo 0 > /sys/class/gpio/gpioXX/value echo 0 > /sys/class/gpio/gpioYY/value fi
Replace XX
with the GPIO number of the red LED and YY
with the GPIO number of the green LED. You can find the GPIO numbers by running the gpio readall
command in the terminal.
This script first checks for security updates and, if there are any available, turns on the red LED. If there are no security updates available, it checks for normal updates and, if there are any available, turns on the green LED. If there are no updates available, both LEDs are turned off.
Remember to run this script with sudo privileges, and to make it executable with the chmod +x
command. You can also add it to the root user's crontab to have it run automatically at a set interval.
Why do I use Rundeck for this? Well, that's simple. When I add a new node to the cluster, I add it in the same way physically, and then I simply clone the SD card and add a new node to my Rundeck definitions. I can repeat this process so easily and I don't even need to log into the Pi when I do it.
If you want to use this in a server room, but have rackmount servers with no GPIO pins, use CircuitPython and some Pis to give your servers some. As we all know, I love using USB ports to power low-voltage things like the temperature sensor projects I’ve done in the past. This time, we can use it to add GPIO and scale this to a server room.
Thanks for the USB cable end, Apple. It gets more use here than it ever did with my iPod.