UNIX Get An Alert When Disk Is Full

To get an alert when a disk is full on a UNIX system, you can set up a disk space monitoring script that periodically checks the disk usage and sends an email alert when the disk usage exceeds a certain threshold. Here’s an example script that you can modify to suit your needs:

#!/bin/bash

# Set the email address to receive the alert
EMAIL="your.email@example.com"

# Set the threshold for the disk usage in percent
THRESHOLD=90

# Get the disk usage for the root file system in percent
DISK_USAGE=$(df / | awk '{print $5}' | tail -n 1 | cut -d'%' -f1)

# Check if the disk usage is greater than or equal to the threshold
if [ $DISK_USAGE -ge $THRESHOLD ]; then
# Send an email alert
echo "Disk usage is at $DISK_USAGE%." | mail -s "Disk full alert" $EMAIL
fi

Here’s how the script works:

  1. The script sets the email address to receive the alert and the threshold for the disk usage in percent.
  2. The script uses the df command to get the disk usage for the root file system (/) in percent. The output is piped to awk to extract the percentage value, then piped to tail to get the last line (which corresponds to the root file system), and finally piped to cut to extract the percentage value only.
  3. The script checks if the disk usage is greater than or equal to the threshold.
  4. If the disk usage is greater than or equal to the threshold, the script sends an email alert using the mail command.
  5. You can set up a cron job to run the script periodically (e.g. every hour or every day) to monitor the disk usage and send alerts as needed.

Note that this is just a simple example script, and you may want to modify it to suit your specific requirements, such as monitoring multiple disks, using a different threshold, or sending alerts using a different method (e.g. a Slack notification or a text message).

Leave a Comment