Files
box/bin/watchdog-alert-check.sh
T

71 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# watchdog-alert-check.sh - Check for new failed browser relaunches.
#
# Scans /home/super/Projects/NetVM/chromebox-watchdog.log for
# "relaunch FAILED" lines newer than the watermark.
#
# Watermark: /home/super/Projects/NetVM/watchdog-alert-watermark.txt
# Holds the last reported log line (or is empty/missing on first run).
#
# Output contract:
# - No new failures: print nothing, exit 0.
# - New failures: print each new "relaunch FAILED" line, update the
# watermark to the newest line, exit 1.
#
# --no-advance: peek-only read. New failures are printed (same output and
# exit codes as above) but the watermark is NOT advanced. The web
# surface (via `box-ctl watchdog-alerts --no-advance`) should always
# pass this flag so UI polling never churns the watermark out from
# under the CLI. CLI runs without the flag keep advance-on-read.
#
# Self-contained: safe to call from cron or from the box CLI.
set -u
NO_ADVANCE=0
for arg in "$@"; do
case "$arg" in
--no-advance) NO_ADVANCE=1 ;;
*) echo "watchdog-alert-check.sh: unknown argument: $arg" >&2; exit 2 ;;
esac
done
LOG="/home/super/Projects/NetVM/chromebox-watchdog.log"
WATERMARK="/home/super/Projects/NetVM/watchdog-alert-watermark.txt"
# No log file -> nothing to report.
[ -f "$LOG" ] || exit 0
# Gather all FAILED lines in chronological order.
mapfile -t failed < <(grep "relaunch FAILED" "$LOG" 2>/dev/null || true)
[ "${#failed[@]}" -gt 0 ] || exit 0
# Read the watermark (last reported line). Missing/empty = report everything.
watermark=""
[ -f "$WATERMARK" ] && watermark="$(cat "$WATERMARK")"
# Collect lines strictly newer than the watermark.
new_lines=()
if [ -z "$watermark" ]; then
new_lines=("${failed[@]}")
else
seen_watermark=0
for line in "${failed[@]}"; do
if [ "$seen_watermark" -eq 1 ]; then
new_lines+=("$line")
elif [ "$line" = "$watermark" ]; then
seen_watermark=1
fi
done
fi
[ "${#new_lines[@]}" -gt 0 ] || exit 0
# Report new failures and advance the watermark to the newest line
# (skipped in --no-advance peek mode).
printf '%s\n' "${new_lines[@]}"
if [ "$NO_ADVANCE" -eq 0 ]; then
printf '%s\n' "${failed[-1]}" > "$WATERMARK"
fi
exit 1