fruchti
801a472e99
Previously, the exit code of an ExitStartPre script might have wound up in the e-mail subject.
82 lines
2.6 KiB
Nix
82 lines
2.6 KiB
Nix
{ pkgs, lib, config, ... }:
|
|
let
|
|
fromAddress = config.email.fromAddress;
|
|
fromIdentity = config.email.fromIdentity;
|
|
toAddress = config.email.adminEmail;
|
|
cfg = config.services.statusEmail;
|
|
in
|
|
{
|
|
options.services.statusEmail = {
|
|
enable = lib.mkEnableOption "Send systemd status e-mails";
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
email.enable = true;
|
|
systemd.services."status-email@" = let
|
|
sendStatusEmail = pkgs.writeScript "send-status-email" ''
|
|
#!${pkgs.bash}/bin/bash
|
|
|
|
from="${fromIdentity}"
|
|
to="${toAddress}"
|
|
service="$1"
|
|
full_status="$(systemctl status --full --lines 200 "$service")"
|
|
exit_code="$(echo "$full_status" | head -n5 | tail -1 | sed -e 's/.*ExecStart=.*status=\(.*\))$/\1/g')"
|
|
# state="$(systemctl is-failed "$service")"
|
|
|
|
fail_priority=1
|
|
fail_subject="Unit \"$service\" failure report (exit code $exit_code)"
|
|
success_priority=3
|
|
success_subject="Unit \"$service\" report (success)"
|
|
|
|
shift
|
|
while [ $# -gt 0 ] ; do
|
|
case "$1" in
|
|
'-s'|'--fail-subject')
|
|
fail_subject="$2"
|
|
shift 2
|
|
;;
|
|
'-p'|'--fail-priority')
|
|
fail_priority="$2"
|
|
shift 2
|
|
;;
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ "$exit_code" != "0/SUCCESS" ] ; then
|
|
subject="$fail_subject"
|
|
priority="$fail_priority"
|
|
else
|
|
subject="$success_subject"
|
|
priority="$success_priority"
|
|
fi
|
|
|
|
echo "Sending e-mail \"$subject\" to \"$to\"."
|
|
|
|
${pkgs.system-sendmail}/bin/sendmail -t -X - <<ERRMAIL
|
|
To: $to
|
|
From: $from
|
|
Subject: $subject
|
|
Content-Transfer-Encoding: 8bit
|
|
Content-Type: text/plain; charset=UTF-8
|
|
X-Priority: $priority
|
|
|
|
$full_status
|
|
|
|
ERRMAIL
|
|
'';
|
|
in
|
|
{
|
|
unitConfig = {
|
|
Description = "Send a status e-mail for %I";
|
|
};
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
ExecStart = "${sendStatusEmail} %i";
|
|
};
|
|
};
|
|
};
|
|
}
|