Install Raspberry Pi production monitor timer
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
|
||||
FAILED_UNIT="${1:-qsfera-rpi-production-check.service}"
|
||||
JOURNAL_LINES="${QSFERA_ALERT_JOURNAL_LINES:-80}"
|
||||
HOSTNAME_VALUE="$(hostname -f 2>/dev/null || hostname)"
|
||||
TIMESTAMP_UTC="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
SUMMARY="QSfera Raspberry Pi production check failed on ${HOSTNAME_VALUE}: ${FAILED_UNIT} at ${TIMESTAMP_UTC}"
|
||||
|
||||
if command -v journalctl >/dev/null 2>&1; then
|
||||
JOURNAL_OUTPUT="$(journalctl -u "$FAILED_UNIT" -n "$JOURNAL_LINES" --no-pager 2>/dev/null || true)"
|
||||
else
|
||||
JOURNAL_OUTPUT=""
|
||||
fi
|
||||
|
||||
{
|
||||
echo "$SUMMARY"
|
||||
if [ -n "$JOURNAL_OUTPUT" ]; then
|
||||
echo
|
||||
echo "$JOURNAL_OUTPUT"
|
||||
fi
|
||||
} | if command -v systemd-cat >/dev/null 2>&1; then
|
||||
systemd-cat -t qsfera-rpi-production-alert -p err
|
||||
else
|
||||
cat >&2
|
||||
fi
|
||||
|
||||
if [ -n "${QSFERA_ALERT_WEBHOOK_URL:-}" ]; then
|
||||
if ! command -v python3 >/dev/null 2>&1; then
|
||||
echo "ERROR: python3 is required to format the alert webhook payload." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
echo "ERROR: curl is required to send the alert webhook." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
python3 - "$SUMMARY" "$FAILED_UNIT" "$HOSTNAME_VALUE" "$TIMESTAMP_UTC" "$JOURNAL_OUTPUT" <<'PY' |
|
||||
import json
|
||||
import sys
|
||||
|
||||
summary, failed_unit, hostname, timestamp_utc, journal = sys.argv[1:]
|
||||
print(json.dumps({
|
||||
"summary": summary,
|
||||
"failedUnit": failed_unit,
|
||||
"host": hostname,
|
||||
"timestampUtc": timestamp_utc,
|
||||
"journal": journal,
|
||||
}, ensure_ascii=False))
|
||||
PY
|
||||
curl -fsS \
|
||||
-X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
--data-binary @- \
|
||||
"$QSFERA_ALERT_WEBHOOK_URL" >/dev/null
|
||||
fi
|
||||
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
|
||||
SERVER_DIR="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)"
|
||||
SYSTEMD_DIR="${SYSTEMD_DIR:-/etc/systemd/system}"
|
||||
ENV_DIR="${ENV_DIR:-/etc/qsfera}"
|
||||
ENV_FILE="${ENV_FILE:-$ENV_DIR/rpi-production-check.env}"
|
||||
RUN_NOW="${RUN_NOW:-true}"
|
||||
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
SUDO=""
|
||||
else
|
||||
SUDO="${SUDO:-sudo}"
|
||||
fi
|
||||
|
||||
run_privileged() {
|
||||
if [ -n "$SUDO" ]; then
|
||||
"$SUDO" "$@"
|
||||
else
|
||||
"$@"
|
||||
fi
|
||||
}
|
||||
|
||||
install_unit() {
|
||||
source_path="$1"
|
||||
target_path="$2"
|
||||
run_privileged install -m 0644 "$source_path" "$target_path"
|
||||
}
|
||||
|
||||
tmp_env="$(mktemp)"
|
||||
cleanup() {
|
||||
rm -f "$tmp_env"
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
run_privileged install -d -m 0755 "$SYSTEMD_DIR" "$ENV_DIR"
|
||||
|
||||
if [ ! -f "$ENV_FILE" ]; then
|
||||
{
|
||||
echo "QSFERA_SERVER_DIR=$SERVER_DIR"
|
||||
grep -v '^QSFERA_SERVER_DIR=' "$SERVER_DIR/systemd/rpi-production-check.env.example"
|
||||
} >"$tmp_env"
|
||||
run_privileged install -m 0640 "$tmp_env" "$ENV_FILE"
|
||||
echo "Created $ENV_FILE"
|
||||
else
|
||||
echo "Keeping existing $ENV_FILE"
|
||||
fi
|
||||
|
||||
install_unit "$SERVER_DIR/systemd/qsfera-rpi-production-check.service" "$SYSTEMD_DIR/qsfera-rpi-production-check.service"
|
||||
install_unit "$SERVER_DIR/systemd/qsfera-rpi-production-check.timer" "$SYSTEMD_DIR/qsfera-rpi-production-check.timer"
|
||||
install_unit "$SERVER_DIR/systemd/qsfera-rpi-production-check-alert@.service" "$SYSTEMD_DIR/qsfera-rpi-production-check-alert@.service"
|
||||
|
||||
run_privileged systemctl daemon-reload
|
||||
run_privileged systemctl enable --now qsfera-rpi-production-check.timer
|
||||
|
||||
if [ "$RUN_NOW" = "true" ]; then
|
||||
run_privileged systemctl start qsfera-rpi-production-check.service
|
||||
fi
|
||||
|
||||
run_privileged systemctl --no-pager status qsfera-rpi-production-check.timer
|
||||
run_privileged systemctl --no-pager show qsfera-rpi-production-check.service -p Result -p ExecMainStatus -p ActiveState
|
||||
Reference in New Issue
Block a user