ZFS: Difference between revisions

imported>Cyounkins
ZED needs ZFS to be be built with mail features, added test pool for email testing
imported>Cyounkins
Under ZED, moved msmtp doc into separate page
Line 514: Line 514:
== Mail notification for ZFS Event Daemon ==
== Mail notification for ZFS Event Daemon ==


ZFS Event Daemon (zed) monitors events generated by the ZFS kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed.
ZFS Event Daemon (zed) monitors events generated by the ZFS kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed. [https://search.nixos.org/options?query=services.zfs.zed zed options]


The <code>zfs</code> package must be built with mail features. The following override is needed as <code>zfs</code> is implicitly used in partition mounting:
The <code>zfs</code> package must be built with mail features. The following override is needed as <code>zfs</code> is implicitly used in partition mounting:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  nixpkgs.config.packageOverrides = pkgs: {
nixpkgs.config.packageOverrides = pkgs: {
    zfsStable = pkgs.zfsStable.override { enableMail = true; };
  zfsStable = pkgs.zfsStable.override { enableMail = true; };
  };
};
</syntaxhighlight>
</syntaxhighlight>


We need to configure a mail transfer agent, the program that sends email:
A mail sender like [[msmtp]] or [[postfix]] is required.
<syntaxhighlight lang="nix">
{
  programs.msmtp = {
    enable = true;
    setSendmail = true;
    defaults = {
      aliases = "/etc/aliases";
      port = 465;
      tls_trust_file = "/etc/ssl/certs/ca-certificates.crt";
      tls = "on";
      auth = "login";
      tls_starttls = "off";
    };
    accounts = {
      default = {
        host = "mail.example.com";
        passwordeval = "cat /etc/emailpass.txt";
        user = "user@example.com";
        from = "user@example.com";
      };
    };
  };
}
</syntaxhighlight>


Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account.
A minimal, testable ZED configuration example:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="nix">
tee -a /etc/aliases <<EOF
services.zfs.zed.enableMail = true;
root: user@example.com
services.zfs.zed.settings = {
EOF
  ZED_EMAIL_ADDR = [ "root" ];
  ZED_NOTIFY_VERBOSE = true;
};
</syntaxhighlight>
</syntaxhighlight>


Finally, override default zed settings with a custom one:
Above, <code>ZED_EMAIL_ADDR</code> is set to <code>root</code>, which most people will have an alias for in their mailer. You can change it to directly mail you: <code>ZED_EMAIL_ADDR = [ "you@example.com" ];</code>
<syntaxhighlight lang="nix">
{
  services.zfs.zed.settings = {
    ZED_DEBUG_LOG = "/tmp/zed.debug.log";
    ZED_EMAIL_ADDR = [ "root" ];
    ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";
    ZED_EMAIL_OPTS = "@ADDRESS@";


    ZED_NOTIFY_INTERVAL_SECS = 3600;
ZED pulls in <code>mailutils</code> and runs <code>mail</code> by default, but you can override it with <code>ZED_EMAIL_PROG</code>. If using msmtp, you may need <code>ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";</code>.
    ZED_NOTIFY_VERBOSE = true;


    ZED_USE_ENCLOSURE_LEDS = true;
You can customize the mail command with <code>ZED_EMAIL_OPTS</code>. For example, if your upstream mail server requires a certain FROM address: <code>ZED_EMAIL_OPTS = "-r 'noreply@example.com' -s '@SUBJECT@' @ADDRESS@";</code>
    ZED_SCRUB_AFTER_RESILVER = true;
  };
  # this will error if zfs package override above is not used
  services.zfs.zed.enableMail = true;
}
</syntaxhighlight>


You can now test this by performing a scrub on an existing pool, or on a small test pool:
After a <code>sudo nixos-rebuild switch</code>, you can test ZED notifications by performing a scrub on an existing pool, or on a small test pool:
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ cd /tmp
$ cd /tmp
Line 584: Line 548:
$ sudo zpool create test /tmp/sparse_file
$ sudo zpool create test /tmp/sparse_file
$ sudo zpool scrub test
$ sudo zpool scrub test
// Check for email
// Check for email when scrub is complete
// Cleanup
$ sudo zpool export test
$ sudo zpool export test
$ rm sparse_file
$ rm sparse_file