Last active
September 11, 2024 13:23
-
-
Save davidecavestro/ce54e7bf18b09812c03d109e83dbcc38 to your computer and use it in GitHub Desktop.
Use collectd-mod-ping on openwrt dumb AP to turn ON a led when internet connectivity is down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# see https://gist.github.com/davidecavestro/ce54e7bf18b09812c03d109e83dbcc38 | |
# originated from https://chatgpt.com/share/de263a5a-a3df-4dc3-944f-14b911624879 | |
# PLEASE NOTE that the LED_PATH variable should reflect the hardware specific led name | |
# Install necessary packages | |
opkg update | |
opkg install collectd collectd-mod-ping bc | |
uci set luci_statistics.collectd_network.enable="1" | |
uci set luci_statistics.collectd_ping.enable="1" | |
uci set luci_statistics.collectd_ping.Hosts="openwrt.org" | |
uci set luci_statistics.collectd_ping.AddressFamily="ipv4" | |
uci commit luci_statistics | |
/etc/init.d/luci_statistics restart | |
# Create the script to check ping and control the LED | |
mkdir -p /etc/custom-scripts | |
cat << 'EOF' > /etc/custom-scripts/check_collectd_ping.sh | |
#!/bin/sh | |
# LED control paths | |
LED_PATH="/sys/class/leds/red:led4" | |
LED_TRIGGER="$LED_PATH/trigger" | |
LED_BRIGHTNESS="$LED_PATH/brightness" | |
# Collectd RRD data path | |
RRD_FILE="/tmp/rrd/$(cat /proc/sys/kernel/hostname)/ping/ping_droprate-openwrt.org.rrd" | |
# Check if the RRD file exists | |
if [ ! -f "$RRD_FILE" ]; then | |
echo "RRD file not found: $RRD_FILE" | |
exit 1 | |
fi | |
# Get the latest ping drop rate value | |
PING_DROP_RATE=$(rrdtool fetch "$RRD_FILE" AVERAGE -s -30s -e now | head -n 3 | tail -n 1 | awk '{print $2}') | |
# Convert exponential notation to decimal notation | |
if echo "$PING_DROP_RATE" | grep -q "e"; then | |
PING_DROP_RATE=$(printf "%f" "$PING_DROP_RATE") | |
fi | |
# Check if the ping drop rate indicates a connectivity issue | |
if [ "$PING_DROP_RATE" = "nan" ] || [ "$(echo "$PING_DROP_RATE > 0" | bc)" -eq 1 ]; then | |
# Internet is not reachable, turn on the LED | |
echo "default-on" > $LED_TRIGGER | |
echo "1" > $LED_BRIGHTNESS | |
else | |
# Internet is reachable, turn off the LED | |
echo "none" > $LED_TRIGGER | |
echo "0" > $LED_BRIGHTNESS | |
fi | |
EOF | |
# Make the script executable | |
chmod +x /etc/custom-scripts/check_collectd_ping.sh | |
# Create the cron job | |
cat << 'EOF' >> /etc/crontabs/root | |
* * * * * /etc/custom-scripts/check_collectd_ping.sh | |
EOF | |
# Ensure persistence across upgrades | |
cat << 'EOF' >> /etc/sysupgrade.conf | |
/etc/custom-scripts/ | |
/etc/crontabs/ | |
EOF | |
# Restart services to apply changes | |
/etc/init.d/cron restart | |
/etc/init.d/collectd restart | |
echo "Setup complete. The LED will now indicate internet connectivity status." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment