Skip to content
Snippets Groups Projects
Commit 5dab0e1e authored by Jakob Moser's avatar Jakob Moser
Browse files

Add watchdog

parent ee9fdacd
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
TRAEFIK=https://traefik.fachschaft.cl.uni-heidelberg.de
BASE_DIR=$(dirname $(realpath $0))
LAST_KNOWN_DOWNTIME_FILE="${BASE_DIR}/last_known_downtime"
LAST_KNOWN_RESTART_FILE="${BASE_DIR}/last_known_restart"
# Return 0 (i.e. true) if the file $1 exists and was modified no more than $2 seconds ago.
# Return 1 (i.e. false) otherwise
younger_than () {
if [[ ! -f ${1} ]]
then
return 1
else
FILE_CHANGE=$(date -r ${1} +%s)
NOW=$(date +%s)
DIFFERENCE=$((${NOW}-${FILE_CHANGE}))
if [[ ${DIFFERENCE} -le ${2} ]]
then
return 0
else
return 1
fi
fi
}
# Return 0 (i.e. true) if Traefik is currently down.
# Return 1 (i.e. false) if it is currently reachable.
currently_down () {
curl ${TRAEFIK} > /dev/null 2> /dev/null
if [[ $? -eq 7 ]]
then
return 0
else
return 1
fi
}
# Return 0 (i.e. true) if Traefik was recently down, i.e. if the last
# modified timestamp of ${LAST_KNOWN_DOWNTIME_FILE} is not too far in the past.
# Return 1 (i.e. false) if it was not recently down.
recently_down () {
# If the last known downtime was before no more than 600 seconds
# (i.e. 10 minutes), we consider Traefik to have recently been down.
return $(younger_than ${LAST_KNOWN_DOWNTIME_FILE} 600)
}
restart () {
touch ${LAST_KNOWN_RESTART_FILE}
"${BASE_DIR}/restart.sh"
}
recently_restarted() {
# If the last known restart was before no more than 36000 seconds
# (i.e. 10 hours), we consider Traefik to have recently been restarted.
return $(younger_than ${LAST_KNOWN_RESTART_FILE} 36000)
}
if currently_down
then
if recently_down
then
if recently_restarted
then
exit 0
else
echo "${TRAEFIK} was unreachable in this and the previous check and hasn't been automatically restarted in a while."
echo "Restarting..."
restart
fi
else
echo "${TRAEFIK} was unreachable, but reachable in the previous check."
echo "Noting this down for the future..."
touch ${LAST_KNOWN_DOWNTIME_FILE}
fi
fi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment