From 960272fb2f15d08fe6e53010e7f66995066008b3 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 4 Oct 2024 13:42:33 +0200 Subject: [PATCH] [roles/base/systemd-fail-mail] mail systemd unit errors via email to root --- .../files/default/systemd-unit-status-mail.sh | 19 ++++++++++ .../files/default/unit-status-mail.service | 15 ++++++++ roles/base/systemd/fail-mail/tasks/main.yml | 35 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 roles/base/systemd/fail-mail/files/default/systemd-unit-status-mail.sh create mode 100644 roles/base/systemd/fail-mail/files/default/unit-status-mail.service create mode 100644 roles/base/systemd/fail-mail/tasks/main.yml diff --git a/roles/base/systemd/fail-mail/files/default/systemd-unit-status-mail.sh b/roles/base/systemd/fail-mail/files/default/systemd-unit-status-mail.sh new file mode 100644 index 0000000..1994c9e --- /dev/null +++ b/roles/base/systemd/fail-mail/files/default/systemd-unit-status-mail.sh @@ -0,0 +1,19 @@ +#!/bin/bash +################################################ +### Managed by someone's ansible provisioner ### +################################################ +# Part of: https://git.somenet.org/root/pub/somesible.git +# 2017-2024 by someone +# + +UNITSTATUS=$(systemctl status -n1000000000 "$1") + +sendmail $2 < +To:<$2@$3> +Subject:systemd <$2@$3> ($1) + +$UNITSTATUS +EOF + +echo -e "Mail report sent to: $2 for unit: $1" diff --git a/roles/base/systemd/fail-mail/files/default/unit-status-mail.service b/roles/base/systemd/fail-mail/files/default/unit-status-mail.service new file mode 100644 index 0000000..88f588a --- /dev/null +++ b/roles/base/systemd/fail-mail/files/default/unit-status-mail.service @@ -0,0 +1,15 @@ +# +################################################ +### Managed by someone's ansible provisioner ### +################################################ +# Part of: https://git.somenet.org/root/pub/somesible.git +# 2017-2024 by someone +# + +[Unit] +Description=Unit Status Mailer Service +After=network.target + +[Service] +Type=oneshot +ExecStart=/bin/systemd-unit-status-mail.sh "%I" "%u" "%H" diff --git a/roles/base/systemd/fail-mail/tasks/main.yml b/roles/base/systemd/fail-mail/tasks/main.yml new file mode 100644 index 0000000..ab251d5 --- /dev/null +++ b/roles/base/systemd/fail-mail/tasks/main.yml @@ -0,0 +1,35 @@ +##################################### +### someone's ansible provisioner ### +##################################### +# Part of: https://git.somenet.org/root/pub/somesible.git +# 2017-2024 by someone +# +--- +- name: copy unit-status-mail unit file + copy: + src: "{{item}}" + dest: "/etc/systemd/system/unit-status-mail@.service" + with_first_found: + - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/unit-status-mail.service" + - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/unit-status-mail.service" + - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/unit-status-mail.service" + - "default/unit-status-mail.service" + + +- name: copy systemd-unit-status-mail shell script + copy: + src: "{{item}}" + dest: "/bin/systemd-unit-status-mail.sh" + mode: 0755 + owner: "root" + group: "root" + with_first_found: + - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/systemd-unit-status-mail.sh" + - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/systemd-unit-status-mail.sh" + - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/systemd-unit-status-mail.sh" + - "default/systemd-unit-status-mail.sh" + + +- name: reload systemd + systemd: + daemon_reload: yes -- 2.43.0