summaryrefslogtreecommitdiff
path: root/statuspage/lib/common.sh
diff options
context:
space:
mode:
Diffstat (limited to 'statuspage/lib/common.sh')
-rw-r--r--statuspage/lib/common.sh49
1 files changed, 49 insertions, 0 deletions
diff --git a/statuspage/lib/common.sh b/statuspage/lib/common.sh
new file mode 100644
index 0000000..d063e07
--- /dev/null
+++ b/statuspage/lib/common.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+# lib/common.sh
+# Shared helpers for statuspage check scripts. Source this, don't execute it.
+#
+# . "$(dirname "$0")/../lib/common.sh"
+
+# json_line SECTION LABEL VALUE STATUS
+# Emits one JSONL row on stdout. Escapes double quotes and backslashes in
+# LABEL/VALUE so a stray '"' in e.g. `zpool status` output can't break the
+# line for the renderer's parser.
+json_line() {
+ section=$1
+ label=$2
+ value=$3
+ status=$4
+
+ # Order matters: backslashes first, then quotes, then newlines (zpool
+ # status output can be multi-line when a pool is degraded) so a JSONL
+ # line always stays exactly one line.
+ label=$(printf '%s' "$label" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr '\n' '\1' | sed 's/\x01/\\n/g')
+ value=$(printf '%s' "$value" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr '\n' '\1' | sed 's/\x01/\\n/g')
+
+ printf '{"section": "%s", "label": "%s", "value": "%s", "status": "%s"}\n' \
+ "$section" "$label" "$value" "$status"
+}
+
+# svc_status NAME
+# Prints "ok" or "down" depending on `service NAME status`.
+svc_status() {
+ service "$1" status >/dev/null 2>&1 && echo "ok" || echo "down"
+}
+
+# wg_status IFACE
+# Prints "ok" or "down" depending on whether wg reports handshakes for IFACE.
+wg_status() {
+ wg show "$1" latest-handshakes >/dev/null 2>&1 && echo "ok" || echo "down"
+}
+
+# port_check IP PORT
+# Prints "ok" or "down" depending on TCP connect success.
+port_check() {
+ nc -z -w2 "$1" "$2" >/dev/null 2>&1 && echo "ok" || echo "down"
+}
+
+# jail_status NAME
+# Prints "ok" or "down" depending on whether the jail is running.
+jail_status() {
+ jls -j "$1" >/dev/null 2>&1 && echo "ok" || echo "down"
+}