#!/usr/bin/env bash
# oom_check.sh — detect OOM kills since last boot.
set -euo pipefail

OOM_COUNT=0

if command -v dmesg >/dev/null 2>&1; then
  OOM_COUNT=$(dmesg 2>/dev/null | grep -ci 'out of memory\|oom-killer\|Killed process' || echo 0)
elif command -v journalctl >/dev/null 2>&1; then
  OOM_COUNT=$(journalctl -k --no-pager 2>/dev/null | grep -ci 'out of memory\|oom-killer\|Killed process' || echo 0)
fi

if [ "$OOM_COUNT" -gt 0 ]; then
  echo "{\"health\":\"degraded\",\"signalKind\":\"oom-kills\",\"evidence\":\"$OOM_COUNT OOM events detected since boot\",\"metrics\":{\"oom_count\":$OOM_COUNT}}"
else
  echo '{"health":"healthy","metrics":{"oom_count":0}}'
fi
