add unitstatus

with some TODOs
This commit is contained in:
Motiejus Jakštys 2023-07-20 06:58:47 +03:00
parent 9b090ff8ae
commit 1bfd201028
4 changed files with 87 additions and 47 deletions

View File

@ -101,6 +101,13 @@ in {
};
};
};
unitstatus = {
enable = true;
email = "motiejus+alerts@jakstys.lt";
# see TODO in base/unitstatus/default.nix
#units = ["zfs-scrub"];
};
};
};
@ -587,36 +594,6 @@ in {
};
};
# https://northernlightlabs.se/2014-07-05/systemd-status-mail-on-unit-failure.html
"unit-status-mail@" = let
script = pkgs.writeShellScript "unit-status-mail" ''
set -e
MAILTO="motiejus+alerts@jakstys.lt"
UNIT=$1
EXTRA=""
for e in "''${@:2}"; do
EXTRA+="$e"$'\n'
done
UNITSTATUS=$(${pkgs.systemd}/bin/systemctl status "$UNIT")
${pkgs.postfix}/bin/sendmail $MAILTO <<EOF
Subject:Status mail for unit: $UNIT
Status report for unit: $UNIT
$EXTRA
$UNITSTATUS
EOF
echo -e "Status mail sent to: $MAILTO for unit: $UNIT"
'';
in {
description = "Send an email on unit failure";
serviceConfig = {
Type = "simple";
ExecStart = ''${script} "%I" "Hostname: %H" "Machine ID: %m" "Boot ID: %b" '';
};
};
zfs-scrub.unitConfig.OnFailure = "unit-status-mail@zfs-scrub.service";
nixos-upgrade.unitConfig.OnFailure = "unit-status-mail@nixos-upgrade.service";
}

View File

@ -6,9 +6,10 @@
...
}: {
imports = [
./sshd
./initrd
./snapshot
./sshd
./unitstatus
./zfsborg
];

View File

@ -0,0 +1,64 @@
{
config,
lib,
pkgs,
...
}: {
# TODO:
# - accept unit names:
# - assert they exist
# - add 'systemd.<unit>.unitConfig.OnFailure' to point to this one.
# - assert postfix is configured
options.mj.base.unitstatus = with lib.types; {
enable = lib.mkEnableOption "alert by email on unit failure";
email = lib.mkOption {type = str;};
#units = lib.mkOption {type = lisOf str;};
};
config =
lib.mkIf config.mj.base.unitstatus.enable {
systemd.services."unit-status-mail@" = let
# https://northernlightlabs.se/2014-07-05/systemd-status-mail-on-unit-failure.html
script = pkgs.writeShellScript "unit-status-mail" ''
set -e
MAILTO="${config.mj.base.unitstatus.email}"
UNIT=$1
EXTRA=""
for e in "''${@:2}"; do
EXTRA+="$e"$'\n'
done
UNITSTATUS=$(${pkgs.systemd}/bin/systemctl status "$UNIT")
${pkgs.postfix}/bin/sendmail $MAILTO <<EOF
Subject:Status mail for unit: $UNIT
Status report for unit: $UNIT
$EXTRA
$UNITSTATUS
EOF
echo -e "Status mail sent to: $MAILTO for unit: $UNIT"
'';
in {
description = "Send an email on unit failure";
serviceConfig = {
Type = "simple";
ExecStart = ''${script} "%I" "Hostname: %H" "Machine ID: %m" "Boot ID: %b" '';
};
};
#};
# See TODO above.
#// {
# systemd.services =
# lib.listToAttrs
# (map (
# unit: {
# name = unit;
# value = {
# unitConfig = {OnFailure = "unit-status-mail@${unit}.service";};
# };
# }
# )
# config.mj.base.unitstatus.units);
};
}

View File

@ -2,7 +2,6 @@
config,
lib,
pkgs,
myData,
...
}: let
mountLatest = mountpoint: zfs_name: ''
@ -16,27 +15,26 @@
exec ${pkgs.util-linux}/bin/umount ${mountpoint}/.snapshot-latest
'';
in {
options.mj.base.zfsborg = {
options.mj.base.zfsborg = with lib.types; {
enable = lib.mkEnableOption "backup zfs snapshots with borg";
repo = with lib.types; lib.mkOption {type = str;};
passwdPath = with lib.types; lib.mkOption {type = str;};
repo = lib.mkOption {type = str;};
passwdPath = lib.mkOption {type = str;};
mountpoints = lib.mkOption {
default = {};
type = with lib.types;
attrsOf (submodule (
{...}: {
options = {
paths = lib.mkOption {type = listOf path;};
patterns = lib.mkOption {
type = listOf str;
default = [];
};
backup_at = lib.mkOption {type = str;};
type = attrsOf (submodule (
{...}: {
options = {
paths = lib.mkOption {type = listOf path;};
patterns = lib.mkOption {
type = listOf str;
default = [];
};
}
));
backup_at = lib.mkOption {type = str;};
};
}
));
};
};