ansible/roles/router/files/monitor.sh

113 lines
2.2 KiB
Bash
Raw Normal View History

2020-07-03 07:27:38 +00:00
#!/usr/bin/env vbash
2020-06-23 00:16:47 +00:00
WAN=eth0
HOSTS=(
'1.1.1.1'
'1.0.0.1'
'8.8.8.8'
)
OUTLET_IP=192.168.60.14
PING_TIMEOUT=5
SOFT_WAIT_TIME=30
DHCP_WAIT_TIME=30
MODEM_RESET_WAIT_TIME=60
PING="/bin/ping -c 1 -W ${PING_TIMEOUT} -w ${PING_TIMEOUT}"
RESET_MODEM_SCRIPT="/config/scripts/tplink_smartplug.py"
DEBUG=true
2020-09-11 06:18:57 +00:00
DEBUG_FILE=/var/log/cron-monitor.log
2020-07-03 07:27:38 +00:00
DISABLE_SOFT_RESTART=false
2020-06-23 00:16:47 +00:00
function debug() {
if [[ ${DEBUG} ]]; then
2020-09-11 06:18:57 +00:00
echo $@ > ${DEBUG_FILE}
2020-06-23 00:16:47 +00:00
fi
}
2020-07-24 00:08:04 +00:00
function interfaceDown() {
interface=$1
ip link set dev ${interface} down
}
function interfaceUp() {
interface=$1
2020-09-11 06:18:57 +00:00
ip link set dev ${interface} up
2020-07-24 00:08:04 +00:00
}
2020-06-23 00:16:47 +00:00
function soft_restart() {
if [[ ${DISABLE_SOFT_RESTART} ]] ; then
return
fi
debug "Releasing DHCP IP lease on ${WAN}"
release dhcp interface ${WAN}
debug "Disabling ${WAN} interface"
2020-07-24 00:08:04 +00:00
interfaceDown ${WAN}
2020-06-23 00:16:47 +00:00
debug "Waiting ${SOFT_WAIT_TIME}"
sleep ${SOFT_WAIT_TIME}
debug "Enabling ${WAN} interface"
2020-07-24 00:08:04 +00:00
interfaceUp ${WAN}
2020-06-23 00:16:47 +00:00
debug "Renewing DHCP IP lease on ${WAN}"
renew dhcp interface ${WAN}
2020-07-03 07:27:38 +00:00
debug "Waiting for ip lease from dhcp (${DHCP_WAIT_TIME}s)"
sleep ${DHCP_WAIT_TIME}
2020-06-23 00:16:47 +00:00
}
function hard_restart() {
2020-07-03 07:27:38 +00:00
debug "Releasing DHCP IP lease on ${WAN}"
release dhcp interface ${WAN}
debug "Disabling ${WAN} interface"
2020-07-24 00:08:04 +00:00
interfaceDown ${WAN}
2020-07-03 07:27:38 +00:00
2020-06-23 00:16:47 +00:00
debug "Turning off router outlet"
${RESET_MODEM_SCRIPT} -t ${OUTLET_IP} -c off >> /dev/null 2>&1
debug "Waiting ${SOFT_WAIT_TIME}"
sleep ${SOFT_WAIT_TIME}
2020-07-03 07:27:38 +00:00
debug "Turning on router outlet"
2020-06-23 00:16:47 +00:00
${RESET_MODEM_SCRIPT} -t ${OUTLET_IP} -c on >> /dev/null 2>&1
2020-07-03 07:27:38 +00:00
debug "Waiting for modem to bootup ${MODEM_RESET_WAIT_TIME}"
sleep ${MODEM_RESET_WAIT_TIME}
debug "Enabling ${WAN} interface"
2020-07-24 00:08:04 +00:00
interfaceUp ${WAN}
2020-07-03 07:27:38 +00:00
debug "Renewing DHCP IP lease on ${WAN}"
renew dhcp interface ${WAN}
2020-06-23 00:16:47 +00:00
}
function ping() {
for host in "${HOSTS[@]}"; do
${PING} ${host} >> /dev/null 2>&1
if [[ $? == 0 ]] ; then
debug "Ping successful, exiting"
exit 0
else
debug "Ping failed on ${host}"
fi
done
}
function main() {
ping
soft_restart
ping
hard_restart
ping
soft_restart
}
main