Interview Prep/Shell Scripting/Scenarios: health checks, monitoring and alerts

An endpoint returns HTTP 200 even when the app is degraded; the JSON body has `"status": "degraded"`. How do you check it in a script?

BeginnerScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → The status code is not enough; parse the body.

What I'd do →

body=$(curl -fsS --max-time 5 http://127.0.0.1:8080/health) || { echo "CRIT unreachable"; exit 2; }
status=$(jq -r '.status // "unknown"' <<<"$body")
case $status in
  ok)       echo "OK";              exit 0 ;;
  degraded) echo "WARN degraded: $(jq -c '.checks' <<<"$body")"; exit 1 ;;
  *)        echo "CRIT status=$status"; exit 2 ;;
esac
  1. curl -f turns HTTP 4xx and 5xx into a non-zero exit.
  2. The // "unknown" default handles a missing field.

Why → Health semantics live in the body for many apps; ignoring it gives false greens.

Interview tip → Mention timeouts on every probe; a hanging health check blocks the next one.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts