Shell Script to Check Disk Space and Send Email Alert

Written by: Bobbin Zachariah   |   Last updated: November 29, 2023

In this article, we'll create a shell script that checks disk space and sends an email alert. The script loops through each disk and checks disk usage using df command. We provide a threshold value (in % ) and schedule to send an email when it crosses that mark.

Prerequisites

  • A Unix-like operating system.
  • The system should be configured to send emails using the mail command.
  • A log file to append each critical message.

Shell Script to Check Disk space

This shell script monitors disk usage and send an email notification when it exceeds 90%. Additionally, it logs the disk usage information to a log file.

Output:

$ ./disk_space_check.sh
Critical logs are appended to /opt/disk-usage/report.txt
$ cat /opt/disk-usage/report.txt
2023-11-28 02:03:57 - Disk utilization of /dev/sdc on linuxopsys is 9.1G/236M (Used/Avail) and is critical.
$

Script:

#!/bin/bash

# Set email parameters
recipient="[email protected]"
subject="Disk Space Report"
logfile="/opt/disk-usage/report.txt"
critical_threshold=90

# Get the hostname
hostname=$(hostname)

# Get a list of all available disks
disks=$(lsblk -o NAME -nl | grep -vE '^Filesystem|tmpfs|cdrom|loop|udev')

is_threshold=0
email_txt="/tmp/email.txt"
# Iterate through each disk
echo "All available disks:" > $email_txt
for disk in $disks; do
	disk_info=$(df -h /dev/$disk | tail -n 1)
	echo $disk_info | awk '{print $1}' >> $email_txt
done
    
for disk in $disks; do
	# Get disk usage information
	disk_info=$(df -h /dev/$disk | tail -n 1)

	# Extract usage and available value
	used=$(echo $disk_info | awk '{print $3}')
	avail=$(echo $disk_info | awk '{print $4}')

	current_usage=$(echo $disk_info | awk '{print $5}' | tr -d '%')

	# Check if disk usage exceeds the critical threshold
	if (( $(echo "${current_usage} >= ${critical_threshold}" | bc -l) )); then
		is_threshold=1
		# Create the email message
		message="Disk utilization of /dev/$disk on $hostname is $used/$avail (Used/Avail) and is critical."
		echo $message >> $email_txt

		# Append the log file with date and time
		echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> $logfile
        echo "Critical logs are appended to $logfile"
	fi
done

if [ "${is_threshold}" == "1" ] ; then
    # Send email
    if ! [ -x "$(command -v mail)" ]; then
        echo "mail command not found, please install and configure mail."
        exit 1
    fi
    mail -s "$subject" "$recipient" < $email_txt
fi

Note:

  • Make sure to replace "[email protected]" with your actual email address.
  • Create the log file in your required location.

Understanding the main portions of the scripts:

First defining parameters such as email, log file, threshold value, and hostname:

recipient="[email protected]"
subject="Disk Space Report"
logfile="/opt/disk-usage/report.txt"
critical_threshold=90
hostname=$(hostname)

Getting the available disks information:

disks=$(lsblk -o NAME -nl | grep -vE '^Filesystem|tmpfs|cdrom|loop|udev')

This line uses lsblk to list all available disks on the system. The grep command is used to filter out certain types of devices (Filesystem, tmpfs, cdrom, loop, udev) that should not be included in the list.

Getting the disk information:

for disk in $disks; do
disk_info=$(df -h /dev/$disk | tail -n 1)

For each disk, this line uses the df command to retrieve disk usage information for the specific disk. tail -n 1 is used to extract the last line of output, which contains the relevant information.

And finally for automation and Schedule - add a cron job to automate the script's execution. Adjust the timing based on your requirements.

0 0 * * * /path/to/disk_space_check.sh

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment