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 →
- One function per check, each calling
record STATUS CHECK TARGET DETAILthat appends to an array and tracks the worst status. - Disk from
df -P -x tmpfs -x devtmpfs, memory fromMemAvailablein/proc/meminfo, services withsystemctl is-active, HTTP withcurl -sS -o /dev/null --max-time 5 -w '%{http_code}'. - Warning and critical thresholds from a config file, overridable by environment.
- Exit with Nagios-style codes (0 OK, 1 WARNING, 2 CRITICAL, 3 UNKNOWN), so monitoring systems and systemd can consume it.
- Do not use
set -eto abort on the first failed check; capture each result instead. - 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.
More on Scenarios: health checks, monitoring and alerts
Your 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?AdvancedHow do you know a scheduled job actually ran, not just that it did not report failure?Intermediate