Instead of alerting at 90% disk, management wants a warning when `/var` will fill within 24 hours. How do you do it in shell?
AdvancedScenarioScenarios: health checks, monitoring and alerts
Answer
Situation read → You need samples over time and a growth rate; a single reading cannot predict anything.
What I'd do →
- Every 10 minutes, append
epoch,used_bytes,size_bytesfor/varto a state file, keeping the last 12 hours. - Compute a linear trend with
awk(least squares over the window) and the time to full:awk -F, '{n++; sx+=$1; sy+=$2; sxy+=$1*$2; sxx+=$1*$1; size=$3; last=$2} END {d = n*sxx - sx*sx; if (n < 6 || d == 0) exit 3 slope = (n*sxy - sx*sy) / d if (slope <= 0) {print "OK not growing"; exit 0} hours = (size - last) / slope / 3600 printf "%s full in %.1f hours\n", (hours < 24 ? "CRIT" : "OK"), hours exit (hours < 24 ? 2 : 0)}' /var/lib/diskwatch/var.csv - Require enough samples and ignore negative slopes (cleanups).
Why → Predictive alerts give hours of warning, while a static threshold may fire at 3 AM with minutes left.
Interview tip → Mention that Prometheus does this natively with predict_linear(node_filesystem_avail_bytes[6h], 24*3600) < 0.
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