diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-07-30 19:09:34 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-07-30 19:09:34 +0200 |
| commit | c0e55961e32b9221bd2f4deca7449f58163adc44 (patch) | |
| tree | c950b4e9c9aa1248aad168eb3582bf63cbc02904 | |
| parent | 72b4151390f8f60e4ed4b89a5d246565c49ccb9b (diff) | |
| download | nixos-c0e55961e32b9221bd2f4deca7449f58163adc44.tar nixos-c0e55961e32b9221bd2f4deca7449f58163adc44.tar.gz nixos-c0e55961e32b9221bd2f4deca7449f58163adc44.tar.bz2 nixos-c0e55961e32b9221bd2f4deca7449f58163adc44.tar.xz nixos-c0e55961e32b9221bd2f4deca7449f58163adc44.zip | |
Refactor status mails
| -rw-r--r-- | bragi.nix | 5 | ||||
| -rw-r--r-- | custom/unit-status-mail.nix | 63 | ||||
| -rw-r--r-- | ymir.nix | 33 |
3 files changed, 71 insertions, 30 deletions
| @@ -10,6 +10,7 @@ in rec { | |||
| 10 | ./bragi/hw.nix | 10 | ./bragi/hw.nix |
| 11 | ./custom/zsh.nix | 11 | ./custom/zsh.nix |
| 12 | ./users.nix | 12 | ./users.nix |
| 13 | ./custom/unit-status-mail.nix | ||
| 13 | ]; | 14 | ]; |
| 14 | 15 | ||
| 15 | boot.loader.grub.enable = true; | 16 | boot.loader.grub.enable = true; |
| @@ -424,4 +425,8 @@ in rec { | |||
| 424 | git -C /etc/nixos pull | 425 | git -C /etc/nixos pull |
| 425 | git -C /etc/nixos submodule update | 426 | git -C /etc/nixos submodule update |
| 426 | ''; | 427 | ''; |
| 428 | |||
| 429 | systemd.status-mail = { | ||
| 430 | onFailure = [ "nixos-upgrade" ]; | ||
| 431 | }; | ||
| 427 | } | 432 | } |
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 | } | ||
| @@ -32,6 +32,7 @@ in rec { | |||
| 32 | ./custom/tinc/def.nix | 32 | ./custom/tinc/def.nix |
| 33 | ./custom/ymir-nginx.nix | 33 | ./custom/ymir-nginx.nix |
| 34 | ./custom/uucp.nix | 34 | ./custom/uucp.nix |
| 35 | ./custom/unit-status-mail.nix | ||
| 35 | ]; | 36 | ]; |
| 36 | 37 | ||
| 37 | boot.loader.grub = { | 38 | boot.loader.grub = { |
| @@ -898,36 +899,8 @@ in rec { | |||
| 898 | git -C /etc/nixos pull | 899 | git -C /etc/nixos pull |
| 899 | git -C /etc/nixos submodule update | 900 | git -C /etc/nixos submodule update |
| 900 | ''; | 901 | ''; |
| 901 | systemd.services."nixos-upgrade".onFailure = ["unit-status-mail@%n.service"]; | ||
| 902 | 902 | ||
| 903 | systemd.services."unit-status-mail@" = { | 903 | systemd.status-mail = { |
| 904 | serviceConfig = { | 904 | onFailure = [ "nixos-upgrade" ]; |
| 905 | Type = "oneshot"; | ||
| 906 | }; | ||
| 907 | scriptArgs = "%I \"Hostname: %H\" \"Machine-ID: %m\" \"Boot-ID: %b\""; | ||
| 908 | script = '' | ||
| 909 | #!${pkgs.stdenv.shell} | ||
| 910 | MAILTO="root" | ||
| 911 | MAILFROM="unit-status-mailer" | ||
| 912 | UNIT=$1 | ||
| 913 | |||
| 914 | EXTRA="" | ||
| 915 | for e in "''${@:2}"; do | ||
| 916 | EXTRA+="$e"$'\n' | ||
| 917 | done | ||
| 918 | |||
| 919 | UNITSTATUS=$(systemctl status $UNIT) | ||
| 920 | |||
| 921 | ${config.security.wrapperDir}/sendmail $MAILTO <<EOF | ||
| 922 | From:$MAILFROM | ||
| 923 | To:$MAILTO | ||
| 924 | Subject:Status of $UNIT | ||
| 925 | |||
| 926 | Status report for unit: $UNIT | ||
| 927 | $EXTRA | ||
| 928 | |||
| 929 | $UNITSTATUS | ||
| 930 | EOF | ||
| 931 | ''; | ||
| 932 | }; | 905 | }; |
| 933 | } | 906 | } |
