ansible/roles/router/files/monitor.sh

120 lines
2.3 KiB
Bash
Raw Normal View History

2021-09-01 22:18:49 +00:00
#!/usr/bin/env bash
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
2021-01-01 03:00:12 +00:00
SOFT_WAIT_TIME=60
DHCP_WAIT_TIME=60
2020-06-23 00:16:47 +00:00
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
2021-09-01 22:18:49 +00:00
if [ 'vyattacfg' != "$(id -ng)" ]; then
exec sg vyattacfg -c "$0 $@"
exit 0
fi
cw=/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper
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
2021-09-01 22:18:49 +00:00
$cw ip link set dev ${interface} down
2020-07-24 00:08:04 +00:00
}
function interfaceUp() {
interface=$1
2021-09-01 22:18:49 +00:00
$cw 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}"
2021-09-01 22:18:49 +00:00
$cw release dhcp interface ${WAN}
2020-06-23 00:16:47 +00:00
debug "Disabling ${WAN} interface"
2021-09-01 22:18:49 +00:00
$cw 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}"
2021-09-01 22:18:49 +00:00
$cw 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}"
2021-09-01 22:18:49 +00:00
$cw release dhcp interface ${WAN}
2020-07-03 07:27:38 +00:00
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}"
2021-09-01 22:18:49 +00:00
$cw 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