interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / ops / ipl-uptime-render.sh

108 lines · 5994 bytes Raw Blame History
1#!/usr/bin/env bash
2# infra/ops/ipl-uptime-render.sh: render $STATE/probes.tsv and $STATE/cert.txt into one static page.
3# Plain HTML, no script, no inline style (the interplane.io CSP is script-src 'none'; style-src 'self').
4# The page states what it is: an inside view from the Spark, not an outside check. It reports only
5# counts inside the measured window (first to last probe of the last 24 h); it makes no claim about
6# time between probes or outside the window, and prints no uptime percentage.
7# Triggered by aien-onchange on the state dir (infra/ops/aien-run-ipl-uptime-page.service).
8#
9# Usage: ipl-uptime-render.sh write $OUT atomically
10# ipl-uptime-render.sh --dry-run write the page to stdout instead
11# Env: IPL_UPTIME_STATE (default ~/.local/state/interplane-uptime)
12# IPL_UPTIME_OUT (default $STATE/www/index.html; deployed: /var/www/interplane-uptime/index.html)
13# IPL_UPTIME_NOW fixed "now" (UTC, YYYY-MM-DDTHH:MM:SSZ) for tests
14# IPL_UPTIME_STALE seconds after which the last probe is reported stale (default 900)
15# Host tools: GNU awk/date. Omega replacement: an Omega page generator once seeded text/date words exist.
16set -u
17STATE=${IPL_UPTIME_STATE:-$HOME/.local/state/interplane-uptime}
18OUT=${IPL_UPTIME_OUT:-$STATE/www/index.html}
19NOW=${IPL_UPTIME_NOW:-$(date -u +%FT%TZ)}
20STALE=${IPL_UPTIME_STALE:-900}
21dry=0
22case ${1:-} in --dry-run) dry=1;; '') ;; *) echo "usage: ipl-uptime-render.sh [--dry-run]" >&2; exit 2;; esac
23
24now_s=$(date -u -d "$NOW" +%s) || { echo "ipl-uptime-render: bad IPL_UPTIME_NOW" >&2; exit 2; }
25SINCE=$(date -u -d "@$((now_s - 86400))" +%FT%TZ)
26TSV=$STATE/probes.tsv
27[ -f "$TSV" ] || TSV=/dev/null
28
29cert_na=-; cert_checked=-; cert_iss=-; cert_days=
30if [ -s "$STATE/cert.txt" ]; then
31 IFS=$'\t' read -r cert_checked cert_na cert_iss <"$STATE/cert.txt"
32 if [ "$cert_na" != - ] && na_s=$(date -u -d "$cert_na" +%s 2>/dev/null); then
33 cert_days=$(( (na_s - now_s) / 86400 ))
34 fi
35fi
36
37render() {
38 gawk -F'\t' -v since="$SINCE" -v now="$NOW" -v now_s="$now_s" -v stale="$STALE" \
39 -v cna="$cert_na" -v cchk="$cert_checked" -v ciss="$cert_iss" -v cdays="$cert_days" '
40 function esc(s) { gsub(/&/, "\\&amp;", s); gsub(/</, "\\&lt;", s); gsub(/>/, "\\&gt;", s); gsub(/"/, "\\&quot;", s); return s }
41 function epoch(t, c) { c = t; gsub(/[-T:Z]/, " ", c); return mktime(c, 1) }
42 NF >= 4 && $1 >= since && $1 <= now {
43 p = $2
44 if (!(p in n)) { order[++np] = p }
45 n[p]++; total++
46 if (first == "" || $1 < first) first = $1
47 if ($1 > last) last = $1
48 if ($3 ~ /^[23][0-9][0-9]$/) {
49 ok[p]++; oks++
50 ms = $4 + 0; sum[p] += ms
51 if (!(p in mn) || ms < mn[p]) mn[p] = ms
52 if (ms > mx[p]) mx[p] = ms
53 } else {
54 fails++; fu[fails] = $1; fp[fails] = p; fs[fails] = $3; fm[fails] = $4
55 }
56 lastu[p] = $1; lasts[p] = $3
57 }
58 END {
59 print "<!doctype html>"
60 print "<html lang=\"en\"><head><meta charset=\"utf-8\">"
61 print "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
62 print "<meta name=\"robots\" content=\"noindex\">"
63 print "<title>interplane.io probes: inside view from the Spark</title></head><body>"
64 print "<h1>interplane.io probes</h1>"
65 print "<p><strong>Inside view from the Spark; not an outside check.</strong> The Spark asks for each page by its public name; the router turns the connection back to the Spark itself. A pass here does not show that the site is reachable from outside the house.</p>"
66 print "<p>Generated " now ". Each probe is one GET with a 15 s limit; OK means status 2xx or 3xx.</p>"
67 if (total == 0) {
68 print "<p>No probes in the 24 h before " now ". Nothing is claimed for that period.</p>"
69 } else {
70 wh = int((epoch(last) - epoch(first)) / 3600); wm = int(((epoch(last) - epoch(first)) % 3600) / 60)
71 print "<p>Measured window: " first " to " last " (" wh " h " wm " min), " total " probes, " oks " OK, " fails " failed. Nothing on this page covers the time between probes or outside this window.</p>"
72 age = now_s - epoch(last)
73 if (age > stale) print "<p><strong>Stale:</strong> the last probe is " int(age / 60) " min old; the prober may be stopped.</p>"
74 print "<table><caption>Per path, last 24 h</caption>"
75 print "<tr><th>path</th><th>probes</th><th>OK</th><th>failed</th><th>last probe</th><th>last status</th><th>latency of OK probes, ms (min / mean / max)</th></tr>"
76 for (i = 1; i <= np; i++) {
77 p = order[i]; k = ok[p] + 0
78 lat = (k > 0) ? mn[p] " / " int(sum[p] / k + 0.5) " / " mx[p] : "-"
79 print "<tr><td>" esc(p) "</td><td>" n[p] "</td><td>" k "</td><td>" n[p] - k "</td><td>" lastu[p] "</td><td>" esc(lasts[p]) "</td><td>" lat "</td></tr>"
80 }
81 print "</table>"
82 if (fails > 0) {
83 print "<h2>Failed probes (newest 30)</h2>"
84 print "<table><tr><th>utc</th><th>path</th><th>status (000 = no answer)</th><th>ms</th></tr>"
85 lo = fails - 29; if (lo < 1) lo = 1
86 for (i = fails; i >= lo; i--) print "<tr><td>" fu[i] "</td><td>" esc(fp[i]) "</td><td>" esc(fs[i]) "</td><td>" esc(fm[i]) "</td></tr>"
87 print "</table>"
88 }
89 }
90 print "<h2>Certificate</h2>"
91 if (cna == "-" || cna == "") {
92 print "<p>No certificate expiry recorded" (cchk != "-" ? " (last attempt " esc(cchk) ")" : "") ".</p>"
93 } else {
94 print "<p>The certificate served for interplane.io expires " esc(cna) (cdays != "" ? " (" cdays " days after this page was generated)" : "") ". Read with openssl s_client at " esc(cchk) "; issuer " esc(ciss) ".</p>"
95 }
96 print "</body></html>"
97 }' "$TSV"
98}
99
100if [ "$dry" = 1 ]; then render; exit; fi
101mkdir -p "$(dirname "$OUT")" 2>/dev/null
102[ -d "$(dirname "$OUT")" ] || { echo "ipl-uptime-render: output dir missing: $(dirname "$OUT")" >&2; exit 1; }
103tmp=$(mktemp "$(dirname "$OUT")/.index.XXXXXX") || exit 1
104if render >"$tmp"; then
105 chmod 644 "$tmp" && mv -f "$tmp" "$OUT"
106else
107 rm -f "$tmp"; exit 1
108fi