>_ IzzyLab

Hands-on task

This task is step 12 of the course “Running a server”

Disk alert script

medium~10 min

Write an executable script /usr/local/bin/disk-alert that takes two arguments: a path and a threshold in percent. If disk usage for that path is above the threshold — exit 1, otherwise exit 0.

The script is easiest to write in the Editor tab: /usr/local/bin shows up there in the file tree; run and test it in the terminal.

What you'll practise:
  • bash scripts
  • exit codes
  • df
  • positional arguments
  • chmod

Topic breakdown

A script communicates with the outside world not only through output but also through its return code: a process returns a number to the system, where 0 traditionally means success and non-zero signals a problem, and it's convenient to build automatic alerts on top of it. The task is to turn a measurable metric (disk usage, given by the df family) into a binary verdict "above the threshold / not" and express it via the appropriate exit. Along the way you practice parsing positional arguments ($1, $2), numeric comparison of integers in the [ ... ] test, and setting the execute bit via chmod.

What's checked

  1. /usr/local/bin/disk-alert is executable
  2. with threshold 99% exit code is 0
  3. with threshold 0% exit code is 1
  4. the threshold is compared with the real disk usage
  5. usage is taken for the path from the first argument

What it looks like