2021-12-19 01:06:50 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
host = config.networking.fqdnOrHostName;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cfg = config.services.smartd;
|
2021-12-19 01:06:50 +00:00
|
|
|
opt = options.services.smartd;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
nm = cfg.notifications.mail;
|
2024-07-01 15:47:52 +00:00
|
|
|
ns = cfg.notifications.systembus-notify;
|
2020-04-24 23:36:52 +00:00
|
|
|
nw = cfg.notifications.wall;
|
|
|
|
nx = cfg.notifications.x11;
|
|
|
|
|
|
|
|
smartdNotify = pkgs.writeScript "smartd-notify.sh" ''
|
|
|
|
#! ${pkgs.runtimeShell}
|
|
|
|
${optionalString nm.enable ''
|
|
|
|
{
|
|
|
|
${pkgs.coreutils}/bin/cat << EOF
|
2020-08-20 17:08:02 +00:00
|
|
|
From: smartd on ${host} <${nm.sender}>
|
2023-11-16 04:20:00 +00:00
|
|
|
To: ${nm.recipient}
|
2020-09-25 04:45:31 +00:00
|
|
|
Subject: $SMARTD_SUBJECT
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
$SMARTD_FULLMESSAGE
|
|
|
|
EOF
|
|
|
|
|
|
|
|
${pkgs.smartmontools}/sbin/smartctl -a -d "$SMARTD_DEVICETYPE" "$SMARTD_DEVICE"
|
|
|
|
} | ${nm.mailer} -i "${nm.recipient}"
|
|
|
|
''}
|
2024-07-01 15:47:52 +00:00
|
|
|
${optionalString ns.enable ''
|
|
|
|
${pkgs.dbus}/bin/dbus-send --system \
|
|
|
|
/ net.nuetzlich.SystemNotifications.Notify \
|
|
|
|
"string:Problem detected with disk: $SMARTD_DEVICESTRING" \
|
|
|
|
"string:Warning message from smartd is: $SMARTD_MESSAGE"
|
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
${optionalString nw.enable ''
|
|
|
|
{
|
|
|
|
${pkgs.coreutils}/bin/cat << EOF
|
|
|
|
Problem detected with disk: $SMARTD_DEVICESTRING
|
|
|
|
Warning message from smartd is:
|
|
|
|
|
|
|
|
$SMARTD_MESSAGE
|
|
|
|
EOF
|
2020-11-24 20:58:05 +00:00
|
|
|
} | ${pkgs.util-linux}/bin/wall 2>/dev/null
|
2020-04-24 23:36:52 +00:00
|
|
|
''}
|
|
|
|
${optionalString nx.enable ''
|
|
|
|
export DISPLAY=${nx.display}
|
|
|
|
{
|
|
|
|
${pkgs.coreutils}/bin/cat << EOF
|
|
|
|
Problem detected with disk: $SMARTD_DEVICESTRING
|
|
|
|
Warning message from smartd is:
|
|
|
|
|
|
|
|
$SMARTD_FULLMESSAGE
|
|
|
|
EOF
|
|
|
|
} | ${pkgs.xorg.xmessage}/bin/xmessage -file - 2>/dev/null &
|
|
|
|
''}
|
|
|
|
'';
|
|
|
|
|
|
|
|
notifyOpts = optionalString (nm.enable || nw.enable || nx.enable)
|
|
|
|
("-m <nomailer> -M exec ${smartdNotify} " + optionalString cfg.notifications.test "-M test ");
|
|
|
|
|
|
|
|
smartdConf = pkgs.writeText "smartd.conf" ''
|
|
|
|
# Autogenerated smartd startup config file
|
|
|
|
DEFAULT ${notifyOpts}${cfg.defaults.monitored}
|
|
|
|
|
|
|
|
${concatMapStringsSep "\n" (d: "${d.device} ${d.options}") cfg.devices}
|
|
|
|
|
|
|
|
${optionalString cfg.autodetect
|
|
|
|
"DEVICESCAN ${notifyOpts}${cfg.defaults.autodetected}"}
|
|
|
|
'';
|
|
|
|
|
|
|
|
smartdDeviceOpts = { ... }: {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
device = mkOption {
|
|
|
|
example = "/dev/sda";
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Location of the device.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
options = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "-d sat";
|
|
|
|
type = types.separatedString " ";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Options that determine how smartd monitors the device.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.smartd = {
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "smartd daemon from `smartmontools` package";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
autodetect = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whenever smartd should monitor all devices connected to the
|
|
|
|
machine at the time it's being started (the default).
|
|
|
|
|
|
|
|
Set to false to monitor the devices listed in
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`services.smartd.devices` only.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
|
|
|
default = [];
|
|
|
|
type = types.listOf types.str;
|
|
|
|
example = ["-A /var/log/smartd/" "--interval=3600"];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
Extra command-line options passed to the `smartd`
|
2020-04-24 23:36:52 +00:00
|
|
|
daemon on startup.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
(See `man 8 smartd`.)
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
notifications = {
|
|
|
|
|
|
|
|
mail = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = config.services.mail.sendmailSetuidWrapper != null;
|
2021-12-06 16:07:01 +00:00
|
|
|
defaultText = literalExpression "config.services.mail.sendmailSetuidWrapper != null";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whenever to send e-mail notifications.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
sender = mkOption {
|
|
|
|
default = "root";
|
|
|
|
example = "example@domain.tld";
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-08-20 17:08:02 +00:00
|
|
|
Sender of the notification messages.
|
2022-09-09 14:08:57 +00:00
|
|
|
Acts as the value of `email` in the emails' `From: ...` field.
|
2020-08-20 17:08:02 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
recipient = mkOption {
|
|
|
|
default = "root";
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Recipient of the notification messages.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mailer = mkOption {
|
|
|
|
default = "/run/wrappers/bin/sendmail";
|
|
|
|
type = types.path;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Sendmail-compatible binary to be used to send the messages.
|
|
|
|
|
|
|
|
You should probably enable
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`services.postfix` or some other MTA for
|
2020-04-24 23:36:52 +00:00
|
|
|
this to work.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
systembus-notify = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Whenever to send systembus-notify notifications.
|
|
|
|
|
|
|
|
WARNING: enabling this option (while convenient) should *not* be done on a
|
|
|
|
machine where you do not trust the other users as it allows any other
|
|
|
|
local user to DoS your session by spamming notifications.
|
|
|
|
|
|
|
|
To actually see the notifications in your GUI session, you need to have
|
|
|
|
`systembus-notify` running as your user, which this
|
|
|
|
option handles by enabling {option}`services.systembus-notify`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
wall = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whenever to send wall notifications to all users.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
x11 = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = config.services.xserver.enable;
|
2021-12-06 16:07:01 +00:00
|
|
|
defaultText = literalExpression "config.services.xserver.enable";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whenever to send X11 xmessage notifications.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
display = mkOption {
|
|
|
|
default = ":${toString config.services.xserver.display}";
|
2021-12-06 16:07:01 +00:00
|
|
|
defaultText = literalExpression ''":''${toString config.services.xserver.display}"'';
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "DISPLAY to send X11 notifications to.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
test = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whenever to send a test notification on startup.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
defaults = {
|
|
|
|
monitored = mkOption {
|
|
|
|
default = "-a";
|
|
|
|
type = types.separatedString " ";
|
|
|
|
example = "-a -o on -s (S/../.././02|L/../../7/04)";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Common default options for explicitly monitored (listed in
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`services.smartd.devices`) devices.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
The default value turns on monitoring of all the things (see
|
2022-08-12 12:06:08 +00:00
|
|
|
`man 5 smartd.conf`).
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
The example also turns on SMART Automatic Offline Testing on
|
|
|
|
startup, and schedules short self-tests daily, and long
|
|
|
|
self-tests weekly.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
autodetected = mkOption {
|
|
|
|
default = cfg.defaults.monitored;
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression "config.${opt.defaults.monitored}";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.separatedString " ";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
Like {option}`services.smartd.defaults.monitored`, but for the
|
2020-04-24 23:36:52 +00:00
|
|
|
autodetected devices.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
devices = mkOption {
|
|
|
|
default = [];
|
|
|
|
example = [ { device = "/dev/sda"; } { device = "/dev/sdb"; options = "-d sat"; } ];
|
|
|
|
type = with types; listOf (submodule smartdDeviceOpts);
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "List of devices to monitor.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
assertions = [ {
|
|
|
|
assertion = cfg.autodetect || cfg.devices != [];
|
|
|
|
message = "smartd can't run with both disabled autodetect and an empty list of devices to monitor.";
|
|
|
|
} ];
|
|
|
|
|
|
|
|
systemd.services.smartd = {
|
|
|
|
description = "S.M.A.R.T. Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig.ExecStart = "${pkgs.smartmontools}/sbin/smartd ${lib.concatStringsSep " " cfg.extraOptions} --no-fork --configfile=${smartdConf}";
|
|
|
|
};
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
services.systembus-notify.enable = mkDefault ns.enable;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|