Many of these steps were borrowed from other blogs around the place. I've used this technique many times and it has saved me at least once from a dramatic harddrive failure.

This is what we are going to do:

  1. Setup Postfix to use gmail as a mail relay
  2. Setup smartmontools to monitor your disks and do regular self-tests

Using Postfix as a relay with gmail

Get yourself a gmail address dediciated for machine monitoring.

Install postfix, mailutils and the sasl magic:
sudo apt-get install postfix mailutils libsasl2-modules

Postfix configuration

  • Choose 'Internet site'
  • Accept the default System mail name (your hostname)

Choose the defaults for the rest of the options – if you’re not running IPv6 then choose IPv4 on the last screen.

Now, postfix is up and running, but we have to reconfigure it.

Log into your gmail account and generate an app password.

Create a password file for it with:
sudo vim /etc/postfix/sasl/sasl_passwd

Put the following line in the file:
[smtp.gmail.com]:587 [email protected]:apppassword

Make sure that root is the only person who can see it:
sudo chmod 600 /etc/postfix/sasl/sasl_passwd

Create the hash db file for Postfix by running the postmap command:
sudo postmap /etc/postfix/sasl/sasl_passwd

Edit your postfix mail configuration file with:
sudo vim /etc/postfix/main.cf

modify the relayhost line to read:

relayhost = [smtp.gmail.com]:587

Then add to the end of the file:

smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Restart postfix with:
sudo service postfix restart

Setup smartmontools

Install smartmontools:
sudo apt-get install smartmontools

Check your hardrive name (if you are using one drive it should be /dev/sda):

lsblk

Should get something like:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0 461.4G  0 part /
└─sda3   8:3    0   3.9G  0 part [SWAP]

Then check the status of the SMART compatibility of the drive:
smartctl -i /dev/sda

You should get something like:

smartctl 6.5 2016-01-24 r4214 [x86_64-linux-4.4.0-116-generic] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Device Model:     WDC WD5000LPLX-00ZNTT0
Serial Number:    WD-WXU1AB7JFUDZ
LU WWN Device Id: 5 0014ee 608430033
Firmware Version: 01.01A01
User Capacity:    500,107,862,016 bytes [500 GB]
Sector Sizes:     512 bytes logical, 4096 bytes physical
Rotation Rate:    7200 rpm
Device is:        Not in smartctl database [for details use: -P showall]
ATA Version is:   ACS-2, ACS-3 T13/2161-D revision 3b
SATA Version is:  SATA 3.1, 6.0 Gb/s (current: 6.0 Gb/s)
Local Time is:    Mon Mar 19 13:22:01 2018 AEDT
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

Note here that SMART support is available and enabled but the device is not in the smartctl database.

To enable SMART on sda1:
sudo smartctl -s on /dev/sda

smartctl 6.5 2016-01-24 r4214 [x86_64-linux-4.4.0-116-generic] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF ENABLE/DISABLE COMMANDS SECTION ===
SMART Enabled.

Want to run a test now? Have a look at the options with:
sudo smartctl --help

Next, we want to set this up to run every morning.

Enable it to run as a daemon:
sudo vim /etc/default/smartmontools
Uncomment the line that says ‘start_smartd=yes’

Edit the smartd.conf file:
sudo vim /etc/smartd.conf

Comment out the line that looks like:

DEVICESCAN -d removable -n standby -m root -M exec /usr/share/smartmontools/smartd-runner

Replace it with something like:

DEVICESCAN -H -l error -l selftest -f -s (O/../.././01|L/../.././04|C/../.././06) -m [email protected] -M test -M daily

This will now:
a. Run self tests every morning of every day on your disks
b. Send you an email if there is a problem
c. Send you an email at startup of the smartmontools daemon to test if the email communication is working
d. Mail you daily reminders about a failed disk

Now, start the smartmontools daemon with:
sudo service smartmontools start