diff --git a/files/all/etc/init.d/wifi-sentinel b/files/all/etc/init.d/wifi-sentinel new file mode 100755 index 0000000..e1bab96 --- /dev/null +++ b/files/all/etc/init.d/wifi-sentinel @@ -0,0 +1,34 @@ +#!/bin/sh /etc/rc.common +# Copyright (C) 2014 OpenWrt.org + +NAME=wifi-sentinel +START=95 + +SENTINEL_BIN=/usr/bin/${NAME}.sh + +USE_PROCD=1 + +start_instance() { + local cfg="$1" + local packets interval network + + config_get packets "$cfg" "packets" + config_get interval "$cfg" "interval" + config_get network "$cfg" "network" + + procd_open_instance + + procd_set_param command "${SENTINEL_BIN}" + procd_set_param respawn + procd_set_param env PACKETS=$packets INTERVAL=$interval NETWORK=$network + + procd_add_reload_interface_trigger $interface + procd_add_reload_trigger "${NAME}" + + procd_close_instance +} + +start_service() { + config_load ${NAME} + config_foreach start_instance ${NAME} +} diff --git a/files/all/usr/sbin/wifi-sentinel.sh b/files/all/usr/sbin/wifi-sentinel.sh new file mode 100755 index 0000000..88beb2b --- /dev/null +++ b/files/all/usr/sbin/wifi-sentinel.sh @@ -0,0 +1,59 @@ +#!/bin/sh +. /lib/functions/network.sh + +PACKETS='3' +INTERVAL='10' +STATUS='unknown' +NETWORK='management' +PORT='1' + +lightsoff() +{ + if [ "$STATUS" != 'down' ]; then + /usr/bin/logger -t wifi-sentinel -p daemon.alert Shutting down wireless interfaces due to lack of connectivity to the gateway. + /sbin/wifi down + STATUS='down' + fi +} + +lightson() +{ + if [ "$STATUS" != 'up' ]; then + /usr/bin/logger -t wifi-sentinel -p daemon.notice Bringing up wireless interfaces. Gateway connectivity restored. + /sbin/wifi up + STATUS='up' + fi +} + +port_check() +{ + LINK=`swconfig dev switch0 port 1 show | awk '/link/ {print $3}'` + if [ "$LINK" = 'link:up' ]; then + return 0 + else + return 1 + fi +} + +ping_check() +{ + local target + network_get_gateway target $NETWORK + RET=`ping -c $PACKETS $target 2> /dev/null | awk '/packets received/ {print $4}'` + if [ "$RET" -eq "$PACKETS" ]; then + return 0 + else + return 1 + fi +} + +/usr/bin/logger -t wifi-sentinel -p daemon.info "Starting Wi-Fi Sentinel on the $NETWORK network with $PACKETS checks every $INTERVAL seconds." + +while true ; do + if port_check && ping_check; then + lightson + else + lightsoff + fi + sleep $INTERVAL +done