Interview Prep/Shell Scripting/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?

IntermediateScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → It must be fast, never hang, report every check (not stop at the first failure), and return a machine-readable result.

What I'd do →

  1. One function per check, each calling record STATUS CHECK TARGET DETAIL that appends to an array and tracks the worst status.
  2. Disk from df -P -x tmpfs -x devtmpfs, memory from MemAvailable in /proc/meminfo, services with systemctl is-active, HTTP with curl -sS -o /dev/null --max-time 5 -w '%{http_code}'.
  3. Warning and critical thresholds from a config file, overridable by environment.
  4. Exit with Nagios-style codes (0 OK, 1 WARNING, 2 CRITICAL, 3 UNKNOWN), so monitoring systems and systemd can consume it.
  5. Do not use set -e to abort on the first failed check; capture each result instead.
  6. Alert per failing check with a dedupe key, and send a "resolved" when it recovers.

Why → A health check that dies on the first problem hides the second one, and one that hangs is worse than none.

Interview tip → The hands-on lab builds exactly this, with tests. Walk through its exit codes.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts