Introduction
This post builds on part 2: NAS – Part 2: Software and services. It’s a detection script to see if your RAID is failing. In the past I’ve had my fair share of failed RAID configurations.
I do know the package mdadm can send alerts, however this small script which can be extended to detect specific changes in RAID/system configuration without using the built in reporting.
Implementation
First let’s start by installing mailutils. This package is needed
sudo apt-get install mailutils |
Next up is the ‘ssmtp’ package. This package will allow you to send a mail.
sudo apt-get install ssmtp |
Create the ssmtp directory (if it doesn’t exists).
sudo mkdir /etc/ssmtp/ |
And create an ssmtp.conf file.
sudo nano /etc/ssmtp/ssmtp.conf |
This ssmtp.conf requires a username(author) and password(authpass). Also a mail hub (smtp, example: mailhub=smtp.gmail.com:587)
AuthUser=<your-email-adres> AuthPass=<password> FromLineOverride=YES mailhub=<smtp-mailserver> UseSTARTTLS=YES |
To test your configuration you can try to send a test mail. Just change ’email@mail.com’ to your email adress.
echo "This is a test" | mail -s "Test" email@mail.com |
If everything works you are ready to create your cron job script. (I will create this script in my user directory, however you can create this wherever you want.)
cd ~ nano health-mdstat.sh |
The underscore of ‘cat /proc/mdstat’ is used by mdadm to notify you of any failing RAID disks. So I’ll be checking for this character.
#!/bin/bash SUBJECT="---RAID IN DEGRADED STATE---" EMAIL="<target email>" FROM="<from email>" EMAILMESSAGE="/tmp/cron-email" cat /proc/mdstat > /tmp/cron-email if grep -q "_" "$EMAILMESSAGE"; then mail -aFrom:$FROM -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE fi |
Let’s assign execute rights to our script.
sudo chmod +x ./health-mdstat.sh |
That’s it! Now assign this to a cron job. I assigned my cron job to run daily.
Also happy scripting (when extending this script).