161 lines
4.9 KiB
Bash
Executable file
161 lines
4.9 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# check_monitoring_pages.sh — verify public monitoring HTML pages via FQDN
|
|
#
|
|
# Outside-in: curl https://<fqdn>/<path>/ and require a real HTML monitoring page.
|
|
# Catches: merchant JSON code 21 (Caddy handle missing), WP 404, empty stubs, deploy skip.
|
|
#
|
|
# Env (same knobs as host-agent HTML layout):
|
|
# MON_HOSTS space-separated FQDNs (default from TALER_DOMAIN)
|
|
# HTML_URL_OK path for OK page (default /monitoring/)
|
|
# HTML_URL_ERR optional err path; checked if MONPAGES_CHECK_ERR=1
|
|
# MONITORING_PAGE_URLS optional full URL list (overrides host+path construction)
|
|
# MONPAGES_EXTRA_URLS additional absolute URLs (space-separated)
|
|
# TIMEOUT curl timeout (from lib.sh)
|
|
#
|
|
set -euo pipefail
|
|
ROOT=$(cd "$(dirname "$0")" && pwd)
|
|
# shellcheck source=lib.sh
|
|
source "$ROOT/lib.sh"
|
|
|
|
set_area monpages
|
|
section "monpages · public monitoring HTML via FQDN (outside-in)"
|
|
|
|
# Default hosts from domain (mirror run-host-report.sh)
|
|
if [ -z "${MON_HOSTS:-}" ]; then
|
|
case "${TALER_DOMAIN:-}" in
|
|
hacktivism.ch|koopa)
|
|
MON_HOSTS="bank.hacktivism.ch exchange.hacktivism.ch taler.hacktivism.ch"
|
|
;;
|
|
stage.lefrancpaysan.ch|stage.*lefrancpaysan*)
|
|
MON_HOSTS="stage.bank.lefrancpaysan.ch stage.exchange.lefrancpaysan.ch stage.monnaie.lefrancpaysan.ch"
|
|
;;
|
|
lefrancpaysan.ch)
|
|
MON_HOSTS="bank.lefrancpaysan.ch exchange.lefrancpaysan.ch monnaie.lefrancpaysan.ch"
|
|
;;
|
|
*)
|
|
MON_HOSTS="${TALER_DOMAIN:-}"
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
HTML_URL_OK="${HTML_URL_OK:-/monitoring/}"
|
|
HTML_URL_ERR="${HTML_URL_ERR:-/monitoring_err/}"
|
|
# ensure leading + trailing slash
|
|
case "$HTML_URL_OK" in
|
|
/*) ;;
|
|
*) HTML_URL_OK="/$HTML_URL_OK" ;;
|
|
esac
|
|
case "$HTML_URL_OK" in
|
|
*/) ;;
|
|
*) HTML_URL_OK="${HTML_URL_OK}/" ;;
|
|
esac
|
|
|
|
build_urls() {
|
|
local h p
|
|
if [ -n "${MONITORING_PAGE_URLS:-}" ]; then
|
|
# shellcheck disable=SC2086
|
|
printf '%s\n' $MONITORING_PAGE_URLS
|
|
else
|
|
for h in $MON_HOSTS; do
|
|
[ -z "$h" ] && continue
|
|
printf 'https://%s%s\n' "$h" "$HTML_URL_OK"
|
|
if [ "${MONPAGES_CHECK_ERR:-0}" = "1" ]; then
|
|
p="$HTML_URL_ERR"
|
|
case "$p" in /*) ;; *) p="/$p" ;; esac
|
|
case "$p" in */) ;; *) p="${p}/" ;; esac
|
|
printf 'https://%s%s\n' "$h" "$p"
|
|
fi
|
|
done
|
|
fi
|
|
if [ -n "${MONPAGES_EXTRA_URLS:-}" ]; then
|
|
# shellcheck disable=SC2086
|
|
printf '%s\n' $MONPAGES_EXTRA_URLS
|
|
fi
|
|
}
|
|
|
|
# True if body looks like our monitoring HTML (not merchant API / WP 404).
|
|
is_monitoring_html() {
|
|
local f="$1"
|
|
if grep -qE '"code"[[:space:]]*:[[:space:]]*21' "$f" 2>/dev/null; then
|
|
return 1
|
|
fi
|
|
if grep -qiE 'There is no endpoint defined for the URL' "$f" 2>/dev/null; then
|
|
return 1
|
|
fi
|
|
if grep -qiE 'wp-content|wordpress' "$f" 2>/dev/null \
|
|
&& grep -qiE '404|not found|page introuvable' "$f" 2>/dev/null; then
|
|
return 1
|
|
fi
|
|
if grep -qE 'sticky-bar|version-link|taler-monitoring|class="sticky' "$f" 2>/dev/null; then
|
|
return 0
|
|
fi
|
|
if head -c 4096 "$f" | grep -qiE '<!DOCTYPE html|<html' \
|
|
&& grep -qiE 'monitoring|mon ·|host-agent' "$f" 2>/dev/null; then
|
|
return 0
|
|
fi
|
|
if head -c 512 "$f" | grep -qiE '<!DOCTYPE html|<html'; then
|
|
return 0
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
check_one() {
|
|
local url="$1"
|
|
local body code hint
|
|
body=$(mktemp)
|
|
code=$(curl -skS -L --max-redirs 5 -m "${TIMEOUT}" -o "$body" -w '%{http_code}' "$url" 2>/dev/null || echo 000)
|
|
if [ "$code" != "200" ]; then
|
|
fail "public mon page" "$url -> HTTP $code (want 200 HTML)"
|
|
rm -f "$body"
|
|
return 1
|
|
fi
|
|
if grep -qE '"code"[[:space:]]*:[[:space:]]*21' "$body" 2>/dev/null; then
|
|
fail "public mon page missing" \
|
|
"$url -> merchant/API JSON code 21 (Caddy handle not live or not published — sudo apply-monitoring-live.sh)"
|
|
rm -f "$body"
|
|
return 1
|
|
fi
|
|
if is_monitoring_html "$body"; then
|
|
if grep -qE 'sticky-bar|version-link' "$body" 2>/dev/null; then
|
|
ok "public mon page" "$url -> HTTP 200 monitoring HTML"
|
|
else
|
|
warn "public mon page" "$url -> HTTP 200 HTML but weak markers (stub/bootstrap?)"
|
|
fi
|
|
rm -f "$body"
|
|
return 0
|
|
fi
|
|
hint=$(head -c 120 "$body" | tr '\n' ' ' | tr -cd '[:print:] ')
|
|
fail "public mon page not monitoring HTML" "$url -> HTTP 200 body!=suite (${hint}...)"
|
|
rm -f "$body"
|
|
return 1
|
|
}
|
|
|
|
URLS=()
|
|
while IFS= read -r line; do
|
|
[ -n "$line" ] || continue
|
|
# dedupe
|
|
skip=0
|
|
for e in "${URLS[@]+"${URLS[@]}"}"; do
|
|
[ "$e" = "$line" ] && skip=1 && break
|
|
done
|
|
[ "$skip" = "1" ] && continue
|
|
URLS+=("$line")
|
|
done < <(build_urls)
|
|
|
|
if [ "${#URLS[@]}" -eq 0 ]; then
|
|
fail "monpages" "no URLs to check (set MON_HOSTS or MONITORING_PAGE_URLS)"
|
|
exit 1
|
|
fi
|
|
|
|
echo " checking ${#URLS[@]} URL(s) via FQDN..."
|
|
ec=0
|
|
for u in "${URLS[@]}"; do
|
|
check_one "$u" || ec=1
|
|
done
|
|
|
|
if [ "$ec" -ne 0 ]; then
|
|
echo " hint: HTML may exist only under ~/monitoring-sites-staging; public needs"
|
|
echo " writable DEPLOY_WWW_ROOT + Caddy handles (sudo ~/koopa-caddy/apply-monitoring-live.sh)"
|
|
fi
|
|
|
|
exit "$ec"
|