>_ IzzyLab

Hands-on task

This task is step 16 of the course “Monitoring and metrics”

Disk Alert

medium~10 min

Write /usr/local/bin/disk-alert that computes the total size of the contents of the directory /var/data and, if it is greater than 50 MB, appends the line ALERT disk high to /var/log/alerts.log. Run it.

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

What you'll practise:
  • threshold alerting
  • du
  • bash scripts
  • numeric comparison
  • appending to a log

Topic breakdown

The classic alerting pattern: measure a value, compare against a threshold, and on exceedance record the event. Here the value is the total directory size, computed by du (it's important to get a single total number in the right units, not a line-by-line list). The comparison against the threshold is done with a numeric test ([ -gt ] or (( ))), not a string one. The event is appended to the log with the >> operator so as not to overwrite the history of past firings.

What's checked

  1. Script is executable
  2. Alert recorded
  3. Script appends an alert above the threshold
  4. No alert below the threshold

What it looks like