#!/usr/bin/env bash
# journal_check.sh — count journal errors in the last check interval.
set -euo pipefail

ERRORS=0

if command -v journalctl >/dev/null 2>&1; then
  ERRORS=$(journalctl -p err --since "-5min" --no-pager 2>/dev/null | wc -l | tr -d ' ' || echo 0)
fi

if [ "$ERRORS" -gt 0 ]; then
  echo "{\"health\":\"degraded\",\"signalKind\":\"journal-errors\",\"evidence\":\"$ERRORS error entries in last 5min\",\"metrics\":{\"journal_errors\":$ERRORS}}"
else
  echo '{"health":"healthy","metrics":{"journal_errors":0}}'
fi
