Receive Email when ZFS Pool is Degraded | Austin Butler

Receive Email when ZFS Pool is Degraded

A script that runs hourly via systemd timers, checking to see if your ZFS ZPool is in any status other than online and then emailing you via Gmail if it is

Friday, May 23, 2014

What this how-to will provide is a script that runs hourly via systemd timers, checking to see if your ZFS ZPool is in any status other than online and then emailing you via Gmail if it is. These specific instructions are written for Arch Linux but can likely be adapted to other distributions easily.

Installation

ZFS Mail Script

sudo vim /root/zfsmail.sh

#!/bin/bash
ZPOOLSTATUS=$(zpool status | grep 'state:' | cut -c 9-)
if [ "$ZPOOLSTATUS" != ONLINE ];
then
  TO="[email protected]"
  SUBJECT="ALERT: ZPool $ZPOOLSTATUS"
  BODY="Your precious ZPool is $ZPOOLSTATUS. Get in there and fix it!"
  echo "$BODY" | /usr/bin/mail -s "$SUBJECT" "$TO"
else
  exit 0
fi

Install MSMTP

Rather than set up a full-blown MTA, we'll use MSMTP (Mini SMTP) to simply forward email to Gmail's SMTP servers for sending. I use and recommend Pacaur for an AUR helper, and it also can install non-AUR packages.

pacaur -S msmtp msmtp-mta

Configure MSMTP

Edit the from, user, and password fields.

sudo vim /root/.msmtprc

# Accounts will inherit settings from this section
defaults
auth             on
tls              on
tls_trust_file   /usr/share/ca-certificates/mozilla/Thawte_Premium_Server_CA.crt

# Gmail address
account        gmail
host           smtp.gmail.com
port           587
from           [email protected]
user           [email protected]
password       enterpassword
tls_trust_file /etc/ssl/certs/ca-certificates.crt

#Default account
account default : gmail

Hourly Timer

This will create a systemd timer that runs every hour. Output will be logged to the journal and, since Arch already uses systemd, no installation of cron is required.

sudo vim /etc/systemd/system/timer-hourly.timer

[Unit]
Description=Hourly Timer

[Timer]
OnBootSec=5min
OnCalendar=hourly
Unit=timer-hourly.target

[Install]
WantedBy=basic.target

Hourly Timer Target

sudo vim /etc/systemd/system/timer-hourly.target

[Unit]
Description=Hourly Timer Target
StopWhenUnneeded=yes

Hourly Timer Directory

Service files placed in this directory will be run automatically every hour.

sudo mkdir /etc/systemd/system/timer-hourly.target.wants

ZFS Mail Service

This is the actual systemd service that will execute the bash script.

sudo vim /etc/systemd/system/timer-hourly.target.wants/zfsmail.service

[Unit]
Description=ZFS Mail

[Service]
Type=forking
ExecStart=/root/zfsmail.sh

Enable Hourly Timer

Finally, enable the hourly timer.

sudo systemctl enable timer-hourly.timer

Verification

The very last step is to verify that it is working, which we can do by checking the journal.

Viewing Status

sudo journalctl _PID=1 -n

Successful Run

May 23 17:00:04 austin-nas systemd[1]: Starting ZFS Mail...
May 23 17:00:04 austin-nas systemd[1]: Started ZFS Mail.
May 23 17:00:04 austin-nas systemd[1]: Starting Hourly Timer Target.
May 23 17:00:04 austin-nas systemd[1]: Reached target Hourly Timer Target.
May 23 17:00:04 austin-nas systemd[1]: Service timer-hourly.target is not needed anymore. Stopping.
May 23 17:00:04 austin-nas systemd[1]: Stopping Hourly Timer Target.
May 23 17:00:04 austin-nas systemd[1]: Stopped target Hourly Timer Target.

Conclusion

That's it! I'm sure there are a million way to do the same thing, but if there's something clearly better please comment and let me know!