To receive automatic disk alerts by email we’ll make use of three common Linux utilities. We will use badblocks to look for bad sectors on our disk(s), crontab to schedule the task and either GNU’s mailutils or mailx to send the results via email.

Most modern disks will have S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology) which in most cases will retire bad sectors in the event that they are encountered. We can use the badblocks utility to find these sectors in advance by performing a read/write test on every sector of the disk. We will be performing a non-destructive read/write test which will make a backup of the original content of a sector before testing with a single random pattern and then restoring the content from the backup. It is highly recommended that all data be backed up prior to testing. As an alternative, the default read-only test can be used without passing any arguments.

If your system does not have the mail command, install it with apt-get on Debian/Ubuntu or with yum on RHEL/CentOS. For the sake of simplicity, we won’t be configuring this utility. There’s a good chance that mail sent from your server will end up in the spam filter. Send a test email and unmark as spam to avoid missing these alerts.

sudo apt-get install mailutils
yum install mailx

We’re going to put all of our commands into a bash script and schedule it to execute on a monthly basis using crontab. We’ll put this file in our user’s home directory.

cd ~
touch diskalerts.sh
nano diskalerts.sh

The following script is for one disk seen as /dev/sda. Repeat or modify as necessary for additional physical disks. View your disk’s name using fdisk -l command. In the case of a software RAID array, use the underlying physical disks and not the RAID device.

Copy the following commands into diskalerts.sh. The first line of our script will write the output of our badblocks scan to the file badblocks.txt. If bad sectors are found, those sectors will appear line by line in this file. If no bad sectors are found, an empty file will be created. The following if statement checks to see if badblocks.txt has anything in it. If it does, the file will be sent via email to user@address.com with the subject “Disk Warning: Bad Sectors” and then the file will be deleted. If the file is empty, it will simply be deleted.

#!/bin/bash
sudo badblocks -nsv /dev/sda > badblocks.txt
if [ -s badblocks.txt ]
then
echo -e "The following bad sectors were found on /dev/sda:\n Reported on: "$(date –u)"\n" | cat - badblocks.txt | mail –s "Disk Warning: Bad Sectors" user@address.com
rm badblocks.txt
else
rm badblocks.txt
fi

We’ll schedule the execution of this script by adding it to the root crontab. If you are on RHEL/CentOS as root you can do the following command without sudo.

sudo crontab -e

Add the following line to execute diskmail.sh on a monthly basis. If you are using RHEL/CentOS, replace user with root.

@monthly sh /home/user/diskalerts.sh

About Canadian Web Hosting

Since 1998, Canadian Web Hosting has been providing on-demand hosting solutions that include Shared Hosting, Virtual Private Servers (VPS), Cloud Hosting, Dedicated Servers, and Infrastructure as a Service (IaaS) for Canadian companies of all sizes. Canadian Web Hosting is SSAE 16 Type II SOC 2 certified, ensuring that their processes and business practices are thoroughly audited against industry standards. Canadian Web Hosting guarantees a 100% network uptime, and a total money-back guarantee that backs everything they do. Customers can get help by calling 1-888-821-7888 to get 24/7 support. For more information, visit them at www.canadianwebhosting.com, or get the latest news by following them on Twitter at @cawebhosting.