So you’ve just setup your shiny new Linux server with RAID Level 1 (mirroring) to ensure that even if you have a hard drive crash, your system will continue to operate. Great idea, but how will you know if one of your hard drives is hemorrhaging data? That’s where this handy script comes in. You RAID status can be checked with the /proc/mdstat file. First, you need to take a snapshot of your healthy /proc/mdstat file.
# cat /proc/mdstat > /root/mdstat
This will save the file in root’s home directory. You can save the file anywhere, but make sure user write permissions are disabled. Here’s the bash script to monitor your RAID array.
#! /bin/bash
# emails root if there is a problem with the raid array
TO=”root”
CC=”youraccount@gmail.com”
SUBJECT=”raid array problem on linux server”
HEALTHYFILE=/root/mdstat
MDSTAT=/proc/mdstat
if ! diff $HEALTHYFILE $MDSTAT &>/dev/null; then
cat /proc/mdstat | mail -s “$SUBJECT” -c “$CC” $TO
fi
Save the script, set it to be executable and add it to your /etc/crontab. I run it once a day. If you are running Fedora, you can just drop it in your /etc/cron.daily folder and you’re good to go. If one of the drives in your RAID array goes down, you’ll receive an email with the contents of /proc/mdstat.