Commit 93495b69 authored by Zach van Rijn's avatar Zach van Rijn
Browse files

Enable uptime and freshness checks.

parent 572a1857
*.pass
*.fail
*.beat
heartbeat
#!/bin/sh -e
DESTFILE=/var/www/site/new/mirrors.html;
DESTFILE=/var/www/status.html;
#---------------------------------------------------------------
......@@ -98,12 +98,122 @@ EOF
);
#---------------------------------------------------------------
##
# Convert integer seconds into "0d 00:00:00" format.
#
sec2str ()
{
T=$1;
D=$((T/60/60/24));
H=$((T/60/60%24));
M=$((T/60%60));
S=$((T%60));
printf '%dd %02d:%02d:%02d' $D $H $M $S;
}
#---------------------------------------------------------------
##
# We define freshness as one of three scenarios. (1) Under 24h
# is "current" due to heartbeat being daily cron. (2) Any value
# converted to the human-friendly string. (3) No heartbeat found
# is "never", which occurs in the case of mirrors not set up.
#
# The 'heartbeat' file is downloaded once from mirrormaster each
# time this script is run, so we do not do that in this loop.
#
do_fresh ()
{
# some mirrors may be partial
if ! curl -s -m 2 -o ${1}.beat ${2}/adelie/heartbeat; then
curl -s -m 2 -o ${1}.beat ${2}/heartbeat;
where=${2}/heartbeat;
else
where=${2}/adelie/heartbeat;
fi
# 200 or die
case $(curl -s -o /dev/null -I -w "%{http_code}" ${where}) in
200) : ;;
*) rm ${1}.beat; ;;
esac
status=;
timer=;
# we actually have 200
if test -e ${1}.beat; then
# file is empty (unknown condition)
if ! test -s ${1}.beat; then
status=dark;
timer=unknown;
else
# either we are in the 24-hour enveloper or not
delta=$(($(cat heartbeat) - $(cat ${1}.beat)));
if test ${delta} -eq 0; then
status=primary;
timer=current;
else
status=warning;
timer=$(sec2str ${delta});
fi
fi
else
status=danger;
timer=never;
fi
printf '<span class="badge badge-pill badge-%s">%s</span>' "${status}" "${timer}";
}
#---------------------------------------------------------------
##
# We define uptime and downtime as "time since last pass/fail"
# via HTTP.
#
do_uptime ()
{
# record last success or failure
if curl -m 2 -i -o /dev/null ${2}; then
test -e ${1}.pass || touch ${1}.pass;
rm ${1}.fail;
else
test -e ${1}.fail || touch ${1}.fail;
rm ${1}.pass;
fi
status=;
testme=;
if test -e ${1}.fail; then
status=danger;
testme=${1}.fail;
elif test -e ${1}.pass; then
status=success;
testme=${1}.pass;
else
exit 1; # unexpected state
fi
delta=$(($(date +%s) - $(date +%s -r ${testme})))
timer=$(sec2str ${delta});
printf '<span class="badge badge-pill badge-%s">%s</span>' "${status}" "${timer}";
}
#---------------------------------------------------------------
##
# Get time of last mirrormaster update.
#
wget -q http://distfiles.adelielinux.org/heartbeat -O heartbeat;
wget -q http://mirrormaster.adelielinux.org/adelie/heartbeat -O heartbeat;
##
# Populate templates. The logic here is as follows:
......@@ -143,8 +253,8 @@ while read type flag state country name speed sponsor http https rsync ftp; do
-e 's@__NAME__@'"${name}"'@g' \
-e 's@__SPEED__@'"${speed}"'@g' \
-e 's@__SPONSOR__@'"${sponsor}"'@g' \
-e 's@__FRESH__@'"00:00:00"'@g' \
-e 's@__UPTIME__@'"00:00:00"'@g' \
-e 's@__FRESH__@'"$(do_fresh ${name} ${http})"'@g' \
-e 's@__UPTIME__@'"$(do_uptime ${name} ${http})"'@g' \
;
if test ${http} != _; then
......@@ -194,5 +304,4 @@ printf >> ${TEMPFILE} "</tbody>\n";
#---------------------------------------------------------------
mv ${TEMPFILE} ${DESTFILE};
chown z:z ${DESTFILE};
chmod 644 ${DESTFILE};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment