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
curl -fturns HTTP 4xx and 5xx into a non-zero exit.- 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.
More on Scenarios: health checks, monitoring and alerts
You are asked for a host health-check script covering disk, memory, systemd services and an HTTP endpoint. How do you design it?IntermediateYour disk alert fires and clears every few minutes around 80%, spamming Slack. How do you fix the alerting?AdvancedA server reports `No space left on device` but `df -h` shows 40% free. What would you add to the health checks?BeginnerAlert when any certificate under `/etc/letsencrypt/live` expires within 21 days.IntermediateA developer wrote a Bash loop that restarts the app whenever its process disappears. What do you suggest instead?IntermediateYou suspect a memory leak in a service. How do you collect evidence with a script before involving developers?Advanced