summaryrefslogtreecommitdiff
path: root/statuspage/render.sh
blob: 301b85d9182569dd4664cb4d437417100e05c451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/sh
# statuspage/render.sh
# Orchestrator + renderer for the dandokmang status console.
#
# Runs every executable script in checks/, collects their JSONL rows, groups
# them by section, and writes a static HTML status page. A check script
# that fails or emits garbage is reported as a down/warn row instead of
# aborting the whole render. See README.md for the JSONL contract that
# check scripts must follow.
set -u

SELF_DIR=$(cd "$(dirname "$0")" && pwd)
CHECKS_DIR="$SELF_DIR/checks"
OUT=${STATUSPAGE_OUT:-/usr/local/www/status/index.html}
HOST=$(hostname)
NOW=$(date "+%Y-%m-%d %H:%M:%S %Z")

. "$SELF_DIR/lib/common.sh"

JSONL=$(mktemp)
AWKFILE=$(mktemp)
trap 'rm -f "$JSONL" "$AWKFILE"' EXIT INT TERM

# valid_jsonl OUTPUT
# Cheap structural check: every non-empty line must look like one of our
# flat {"section":.., "label":.., "value":.., "status":..} objects. This
# isn't a general JSON validator - it doesn't need to be, since the only
# producer of this format is json_line() in lib/common.sh.
valid_jsonl() {
    bad=$(printf '%s\n' "$1" | grep -v '^$' | \
        grep -Evc '^\{.*"section"[[:space:]]*:.*"label"[[:space:]]*:.*"value"[[:space:]]*:.*"status"[[:space:]]*:.*\}$')
    [ "$bad" -eq 0 ]
}

for script in "$CHECKS_DIR"/*.sh; do
    [ -e "$script" ] || continue
    [ -x "$script" ] || continue
    name=$(basename "$script")

    output=$("$script" 2>/dev/null)
    rc=$?

    if [ "$rc" -ne 0 ]; then
        json_line "checks" "$name" "check exited with status $rc" "down"
    elif [ -z "$output" ]; then
        json_line "checks" "$name" "no output" "warn"
    elif ! valid_jsonl "$output"; then
        json_line "checks" "$name" "malformed JSONL output" "warn"
    else
        printf '%s\n' "$output"
    fi
done >"$JSONL"

# Render with awk: group rows by section (fixed order hardware/host
# services/jails, then any other section in first-seen order - e.g.
# "checks" for broken-script rows), preserving row order within a section.
cat >"$AWKFILE" <<'AWKEOF'
function jsonval(line, key,    pat, start, i, c, nc, res) {
    pat = "\"" key "\"[ \t]*:[ \t]*\""
    if (!match(line, pat)) return ""
    start = RSTART + RLENGTH
    res = ""
    for (i = start; i <= length(line); i++) {
        c = substr(line, i, 1)
        if (c == "\\") {
            nc = substr(line, i + 1, 1)
            if (nc == "n") res = res "\n"
            else res = res nc
            i++
            continue
        }
        if (c == "\"") break
        res = res c
    }
    return res
}

function htmlesc(s) {
    gsub(/&/, "\\&amp;", s)
    gsub(/</, "\\&lt;", s)
    gsub(/>/, "\\&gt;", s)
    return s
}

function sqclass(status) {
    if (status == "ok") return "green"
    if (status == "warn") return "yellow"
    if (status == "info") return "gray"
    return "red"
}

BEGIN {
    order[1] = "hardware"; order[2] = "host services"; order[3] = "jails"
    norder = 3
    for (i = 1; i <= norder; i++) known[order[i]] = 1
}

{
    if ($0 == "") next
    section = jsonval($0, "section")
    label   = jsonval($0, "label")
    value   = jsonval($0, "value")
    status  = jsonval($0, "status")
    if (section == "") next

    if (!(section in seen)) {
        seen[section] = 1
        if (!(section in known)) { norder++; order[norder] = section }
    }
    n = ++count[section]
    L[section, n] = label
    V[section, n] = value
    S[section, n] = status
}

END {
    print "<!DOCTYPE html>"
    print "<html>"
    print "<head>"
    print "<title>" htmlesc(host) " status</title>"
    print "<meta http-equiv=\"refresh\" content=\"30\">"
    print "<style>"
    print "body { background:#15111d; color:#e4dcee; font-family: monospace; padding: 2em; }"
    print "h1 { color: #e0c3ff; margin-bottom: 0; display: inline-block; border-bottom: 2px solid #d9a441; padding-bottom: 6px; }"
    print "h2 { color: #c084fc; border-bottom: 1px solid #3a2b4a; padding-bottom: 4px; }"
    print ".cols { column-width: 380px; column-gap: 2.5em; }"
    print ".section { break-inside: avoid; -webkit-column-break-inside: avoid; display: inline-block; width: 100%; vertical-align: top; margin-bottom: 1.5em; }"
    print "table { border-collapse: collapse; width: 100%; }"
    print "td { padding: 6px 10px; border-bottom: 1px solid #241c30; }"
    print ".sq { display:inline-block; width:12px; height:12px; border-radius:2px; margin-right:6px; vertical-align:middle; }"
    print ".green { background:#3c3; }"
    print ".yellow { background:#cc3; }"
    print ".red { background:#c33; }"
    print ".gray { background:#666; }"
    print ".dim { color:#9c8aad; font-size: 0.85em; }"
    print ".val { color:#d9a441; }"
    print "</style>"
    print "</head>"
    print "<body>"
    print "<h1>" htmlesc(host) "</h1>"
    print "<p class=\"dim\">updated " htmlesc(now) "</p>"

    print "<div class=\"cols\">"
    for (i = 1; i <= norder; i++) {
        sect = order[i]
        if (!(sect in count)) continue
        print "<div class=\"section\">"
        print "<h2>" htmlesc(sect) "</h2>"
        print "<table>"
        for (j = 1; j <= count[sect]; j++) {
            valcell = (V[sect, j] == "") ? "" : "<td class=\"val\">" htmlesc(V[sect, j]) "</td>"
            print "<tr><td><span class=\"sq " sqclass(S[sect, j]) "\"></span>" htmlesc(L[sect, j]) "</td>" valcell "</tr>"
        }
        print "</table>"
        print "</div>"
    }
    print "</div>"

    print "</body>"
    print "</html>"
}
AWKEOF

awk -v host="$HOST" -v now="$NOW" -f "$AWKFILE" "$JSONL" >"$OUT"