Monday, 11 November 2019

Monitor your mdadm RAID Arrays and get E-mail Alerts

You can be using a piece of software for some time and not know of one of it most useful functions. In my Desktop computer I use a software RAID1 to protect me against any data loss. RAID is where you join two or, more Hard Drives into a mirrored array. This basically means that if a drives fails, you don't lose all your data. If you also mirror the Operating system, it results in no or, little downtime.

I have two identically sized SSD (Solid State Drives) and two identically sized hard drives. The SSDs have two identically sized partitions for the Operating System and Boot. I‘d be gutted to be without my PC for too long following a failure.

This article is not about setting up or, configuring my chosen mdadm software raid but, how to get automated alerts if there is degradation. A warning before potential drive failure. I only have found out about getting email alerts recently and until now, manually ran cat /proc/mdstat from time to time.

md0 is my Boot and OS.
md1 is my /home.

media:~$ cat /proc/mdstat
Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10]
md1 : active raid1 sdb5[3] sdd5[2]
1952405312 blocks super 1.2 [2/2] [UU]

md0 : active raid1 sda1[0]
58091392 blocks super 1.2 [2/1] [UU]

unused devices:

Prerequisites

Before we get started:

  • You have to have an SMTP server that can send the emails from your workstation, to the recipient (i.e. your corporate exchange or Gmail).
  • You have the credentials for a user that is able to send Email on that server (i.e. a mailbox or a Gmail account).

There are a few simple SMTP servers out there. The ssmtp (Simple S.M.T.P) package works well. There are better setup articles but we can quickly set it up.

To install the ssmtp (Simple S.M.T.P) package, use the following command:

sudo apt-get install ssmtp

Using your favourite editor, edit the configuration file:

sudo nano /etc/ssmtp/ssmtp.conf

Edit the following lines. The number after the colon, is the port number used to connect to your email providers SMTP service.

root=username@gmail.com
mailhub=smtp.gmail.com:587
hostname=username@gmail.com
UseSTARTTLS=YES
AuthUser=username
AuthPass=password
FromLineOverride=yes

In order to make the default (root) “from” field be the server name, edit the /etc/ssmtp/revaliases file:

root:machine-name@some-domain.com:smtp.gmail.com

Test the ssmtp setup by sending an Email:

echo "Test message using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com

Configure mdadm

sudo nano /etc/mdadm/mdadm.conf

Just find and edit the following line:

MAILADDR username@gmail.com

Rather than just restart the service, it is best to restart the PC.

You can do a quick test that it is now working

sudo mdadm --monitor --scan --test -1

No comments: