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

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 →

  1. Every 10 minutes, append epoch,used_bytes,size_bytes for /var to a state file, keeping the last 12 hours.
  2. 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
    
  3. 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.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts