Compare commits
3 commits
7a9023bed5
...
a7def32a75
Author | SHA1 | Date | |
---|---|---|---|
a7def32a75 | |||
d6300e6e19 | |||
c1afcb7768 |
3 changed files with 59 additions and 13 deletions
|
@ -39,6 +39,7 @@
|
|||
"extranix"
|
||||
"forgejo-multiuser-nix-runners"
|
||||
"openbao"
|
||||
"systemd-notify"
|
||||
])
|
||||
++ [
|
||||
"${sources.agenix}/modules/age.nix"
|
||||
|
@ -52,7 +53,6 @@
|
|||
"services/forgejo-nix-runners"
|
||||
"services/nginx-sni"
|
||||
"services/reaction"
|
||||
"services/systemd-notify"
|
||||
"services/victorialogs"
|
||||
"services/victoriametrics"
|
||||
]
|
||||
|
|
|
@ -54,19 +54,16 @@ in
|
|||
};
|
||||
|
||||
services.systemd-notify = {
|
||||
enable = true;
|
||||
command = builtins.toString (
|
||||
pkgs.writeShellScript "sendmail" ''
|
||||
${pkgs.msmtp}/bin/sendmail -i -t <<ERRMAIL
|
||||
To: admins+monitoring@dgnum.eu, ${emails}
|
||||
Subject: [$HOSTNAME] Systemd failure: $1
|
||||
Content-Transfer-Encoding: 8bit
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
mail = pkgs.writeShellScript "sendmail" ''
|
||||
${pkgs.msmtp}/bin/sendmail -i -t <<ERRMAIL
|
||||
To: admins+monitoring@dgnum.eu, ${emails}
|
||||
Subject: [$HOSTNAME] Systemd failure: $1
|
||||
Content-Transfer-Encoding: 8bit
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
|
||||
$(systemctl status --full "$1")
|
||||
ERRMAIL
|
||||
''
|
||||
);
|
||||
$(systemctl status --full "$1")
|
||||
ERRMAIL
|
||||
'';
|
||||
};
|
||||
age-secrets.sources = [ ./. ];
|
||||
};
|
||||
|
|
49
modules/nixos/systemd-notify.nix
Normal file
49
modules/nixos/systemd-notify.nix
Normal file
|
@ -0,0 +1,49 @@
|
|||
# SPDX-FileCopyrightText: 2024 Tom Hubrecht <tom.hubrecht@dgnum.eu>
|
||||
#
|
||||
# SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
{ config, lib, ... }:
|
||||
|
||||
let
|
||||
inherit (lib)
|
||||
getExe
|
||||
mapAttrs'
|
||||
mapAttrsToList
|
||||
mkOption
|
||||
mkForce
|
||||
nameValuePair
|
||||
;
|
||||
inherit (lib.types) attrsOf package submodule;
|
||||
|
||||
cfg = config.services.systemd-notify;
|
||||
in
|
||||
|
||||
{
|
||||
options.services.systemd-notify = mkOption {
|
||||
type = attrsOf package;
|
||||
description = ''
|
||||
Commands to execute when a systemd unit fails.
|
||||
Attrs keys will be the unit name and attrs value is the command that
|
||||
will be run with the name of the failed unit as an argument.
|
||||
'';
|
||||
default = { };
|
||||
};
|
||||
|
||||
options.systemd.services = mkOption {
|
||||
type = attrsOf (submodule {
|
||||
config.onFailure = mapAttrsToList (name: _: "${name}@%n.service") cfg;
|
||||
});
|
||||
};
|
||||
|
||||
config.systemd.services = mapAttrs' (
|
||||
name: script:
|
||||
nameValuePair "${name}@" {
|
||||
description = "Run ${name} script on service failures.";
|
||||
onFailure = mkForce [ ]; # Avoid recursive failures
|
||||
serviceConfig = {
|
||||
ExecStart = "${getExe script} %i";
|
||||
Type = "oneshot";
|
||||
};
|
||||
}
|
||||
) cfg;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue