[How To] Switch on a led when internet connection is available

Fine, I simply shared the WIP of some experiments. Probably it's not the right place.
If you find my comments harmful for a How To, please let me know and I'll remove them.
I suppose no one really wants to turn this thread to a contest (no pun intended).

They are not harmful, and I am not the arbiter of good practice nor do I want to appear to be.

But your how-to advises people install collectd, which your how-to calls "necessary", to do simple pings:

and that is wasteful and unnecessary, especially in a potentially still very much space-restricted environment.

Now if you prefixed it with "if you're already running collectd", that's an entirely different story. You didn't write a "indicate ping on LED" script, you wrote a "indicate collectd data on LED" script.

Please also don't take this as confrontional criticism for confrontation's sake.

2 Likes

I have a simple way to change the color and behavior of the LED according to the connectivity status. For this purpose, I use a hotplug script.

My router is a Dynalink DL-WRX36, so I only have a single front LED that can change between red and blue.

I placed the script in the path /etc/hotplug.d/iface/ so that it can be triggered based on the WAN interface events.

#!/bin/sh
# /etc/hotplug.d/iface/25-led-control

if [ "$INTERFACE" = "wan" ]; then
        case "$ACTION" in
                ifup)
                        if uci show system | grep -q "system.@led.*.name='online'"; then
                                exit 0
                        else
                                uci del system.@led[0]
                                uci add system led
                                uci set system.@led[-1].name='online'
                                uci set system.@led[-1].sysfs='blue:system'
                                uci set system.@led[-1].trigger='default-on'
                                uci commit system
                                service led restart
                        fi
                        ;;
                ifdown)
                        if uci show system | grep -q "system.@led.*.name='offline'"; then
                                exit 0
                        else
                                uci del system.@led[0]
                                uci add system led
                                uci set system.@led[-1].name='offline'
                                uci set system.@led[-1].sysfs='red:system'
                                uci set system.@led[-1].trigger='timer'
                                uci set system.@led[-1].delayon='800'
                                uci set system.@led[-1].delayoff='200'
                                uci commit system
                                service led restart
                        fi
                        ;;
                ifup-failed)
                        if uci show system | grep -q "system.@led.*.name='offline'"; then
                                exit 0
                        else
                                uci del system.@led[0]
                                uci add system led
                                uci set system.@led[-1].name='offline'
                                uci set system.@led[-1].sysfs='red:system'
                                uci set system.@led[-1].trigger='timer'
                                uci set system.@led[-1].delayon='800'
                                uci set system.@led[-1].delayoff='200'
                                uci commit system
                                service led restart
                        fi
                        ;;
        esac
fi

FYI, you can directly turn off and on LEDs by echoing the values into /sys/class/leds/*. It's not necessary to go through UCI, a write cycle, and a service restart every time the WAN status changes. Which would potentially confuse other configured LEDs. That being said ...

... your script only works if there's one singular LED to be configured at any time -- the LED set up by the script itself.

As I said before, the Dynalink DL-WRX36 router only has one front LED, so there is no other LED that can be affected.

Also, through the method described by you, I can't find a way to customize other properties like "trigger", "delayon", "delayoff".

All LED properties are available and writable in userspace. The led "service" (i.e., /etc/init.d/led) does nothing else but parse the UCI configuration and echo the settings into the LEDs' properties in /sys/class/<led>/.

trigger is available, and writable, in /sys/class/leds/<led>/trigger

/sys/class/leds/<led>/delay_on and /sys/class/leds/<led>/delay_off appear after the trigger has been set to timer.

2 Likes

Without a doubt this has helped me better understand how to manage LEDs directly.

In fact, it helped me simplify the script a bit:

#!/bin/sh
# /etc/hotplug.d/iface/25-led-control
blue_brightness="/sys/class/leds/blue:system/brightness"
blue_trigger="/sys/class/leds/blue:system/trigger"
red_brightness="/sys/class/leds/red:system/brightness"
red_trigger="/sys/class/leds/red:system/trigger"
red_delay_on="/sys/class/leds/red:system/delay_on"
red_delay_off="/sys/class/leds/red:system/delay_off"

if [ "$INTERFACE" = "wan" ]; then
        case "$ACTION" in
                ifup)
                                echo "1" > "$blue_brightness"
                                echo "0" > "$red_brightness"
                                echo "default-on" > "$blue_trigger"
                        ;;
                ifdown)
                                echo "1" > "$red_brightness"
                                echo "0" > "$blue_brightness"
                                echo "timer" > "$red_trigger"
                                echo "500" > "$red_delay_on"
                                echo "500" > "$red_delay_off"
                        ;;
                ifup-failed)
                                # the LED flashes between blue and red
                                echo "1" > "$red_brightness"
                                echo "1" > "$blue_brightness"
                                echo "timer" > "$red_trigger"
                                echo "100" > "$red_delay_on"
                                echo "100" > "$red_delay_off"
                        ;;
        esac
fi
1 Like

I must admit after the first sysupgrade I completely agree. I should have read overkill as cumbersome to maintain over upgrades :slight_smile: