diff options
Diffstat (limited to 'custom/unit-status-mail.nix')
| -rw-r--r-- | custom/unit-status-mail.nix | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/custom/unit-status-mail.nix b/custom/unit-status-mail.nix new file mode 100644 index 00000000..54e5cb39 --- /dev/null +++ b/custom/unit-status-mail.nix | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | { config, lib, pkgs, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | let | ||
| 6 | cfg = config.systemd.status-mail; | ||
| 7 | |||
| 8 | systemdCfg = foldr singleCfg {} cfg.onFailure; | ||
| 9 | singleCfg = unitName: attrs: attrs // (setAttrByPath ["systemd" "services" unitName "onFailure"] ["unit-status-mail@%n.service"]); | ||
| 10 | in { | ||
| 11 | options = { | ||
| 12 | systemd.status-mail = { | ||
| 13 | onFailure = mkOption { | ||
| 14 | default = []; | ||
| 15 | type = types.listOf types.str; | ||
| 16 | description = '' | ||
| 17 | Send status mail when these units fail | ||
| 18 | ''; | ||
| 19 | }; | ||
| 20 | |||
| 21 | recipient = mkOption { | ||
| 22 | default = "root"; | ||
| 23 | type = types.str; | ||
| 24 | description = '' | ||
| 25 | Recipient of status mails | ||
| 26 | ''; | ||
| 27 | }; | ||
| 28 | }; | ||
| 29 | }; | ||
| 30 | |||
| 31 | config = mkIf (cfg.onFailure != []) { | ||
| 32 | systemd.services."unit-status-mail@" = { | ||
| 33 | serviceConfig = { | ||
| 34 | Type = "oneshot"; | ||
| 35 | }; | ||
| 36 | scriptArgs = "%I \"Hostname: %H\" \"Machine-ID: %m\" \"Boot-ID: %b\""; | ||
| 37 | script = '' | ||
| 38 | #!${pkgs.stdenv.shell} | ||
| 39 | MAILTO="root" | ||
| 40 | MAILFROM="unit-status-mailer" | ||
| 41 | UNIT=$1 | ||
| 42 | |||
| 43 | EXTRA="" | ||
| 44 | for e in "''${@:2}"; do | ||
| 45 | EXTRA+="$e"$'\n' | ||
| 46 | done | ||
| 47 | |||
| 48 | UNITSTATUS=$(systemctl status $UNIT) | ||
| 49 | |||
| 50 | ${config.security.wrapperDir}/sendmail $MAILTO <<EOF | ||
| 51 | From:$MAILFROM | ||
| 52 | To:$MAILTO | ||
| 53 | Subject:Status of $UNIT | ||
| 54 | |||
| 55 | Status report for unit: $UNIT | ||
| 56 | $EXTRA | ||
| 57 | |||
| 58 | $UNITSTATUS | ||
| 59 | EOF | ||
| 60 | ''; | ||
| 61 | }; | ||
| 62 | } // systemdCfg; | ||
| 63 | } | ||
