From a8b32e039c5421dde3ec5306ba9c872ae1bf5697 Mon Sep 17 00:00:00 2001 From: mlise Date: Mon, 22 Jul 2019 15:59:50 +0200 Subject: [PATCH 1/4] Don't send alerts too offen --- templates/systemd.email.j2 | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/templates/systemd.email.j2 b/templates/systemd.email.j2 index 3f54cbf..cb65dd6 100644 --- a/templates/systemd.email.j2 +++ b/templates/systemd.email.j2 @@ -1,13 +1,20 @@ #!/bin/bash -file=$(mktemp) +ERRORMAIL=$(mktemp) +LOCKFILE="/var/lock/$2" +set -eux -echo "TO: $1" >> $file -echo "From: admin@temelio.com" >> $file -echo "Subject: $HOSTNAME - $2" >> $file -echo "Content-Transfer-Encoding: 8bit" >> $file -echo "Content-Type: text/plain; charset=UTF-8" >> $file -echo "$(systemctl status --full "$2")" >> $file -echo "------------------------- logs tail -n 100 ------------------------" >> $file -echo "$(journalctl -n 100 -u "$2")" >> $file -cat $file |/usr/sbin/sendmail -t; -rm $file; +if lockfile -r0 -l900 $LOCKFD; then + echo "TO: $1" >> $ERRORMAIL + echo "From: $1" >> $ERRORMAIL + echo "Subject: $HOSTNAME - $2" >> $ERRORMAIL + echo "Content-Transfer-Encoding: 8bit" >> $ERRORMAIL + echo "Content-Type: text/plain; charset=UTF-8" >> $ERRORMAIL + echo "$(systemctl status --full "$2")" >> $ERRORMAIL + echo "------------------------- logs tail -n 100 ------------------------" >> $ERRORMAIL + echo "$(journalctl -n 100 -u "$2")" >> $ERRORMAIL + cat $ERRORMAIL |/usr/sbin/sendmail -t; + rm $ERRORMAIL; +else + echo 1>&2 "Fatal error: another process has been keeping the lock for too long or the lock file is inaccessible" + exit 3 +fi From d13cb61ce6c1a9797961c4f14dd6c448e7bb69bd Mon Sep 17 00:00:00 2001 From: mlise Date: Mon, 22 Jul 2019 16:43:34 +0200 Subject: [PATCH 2/4] update molecule role --- .travis.yml | 2 +- meta/main.yml | 2 +- molecule/default/requirements.yml | 2 +- requirements.txt | 8 ++++---- templates/systemd.email.j2 | 3 ++- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9cf434d..9f9c5c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ # Travis configuration -dist: 'trusty' +dist: 'xenial' sudo: 'required' services: diff --git a/meta/main.yml b/meta/main.yml index 34dfbbe..9c26f84 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -20,5 +20,5 @@ galaxy_info: - 'system' dependencies: - src: 'Temelio.ssmtp' - version: '0.1.0' + version: '0.1.1' when: 'ssmtp_use_ansible_galaxy_dependencies or _use_role_dependencies' diff --git a/molecule/default/requirements.yml b/molecule/default/requirements.yml index a50445d..ec44814 100644 --- a/molecule/default/requirements.yml +++ b/molecule/default/requirements.yml @@ -1,4 +1,4 @@ --- - src: 'Temelio.ssmtp' - version: '0.1.0' + version: '0.1.1' diff --git a/requirements.txt b/requirements.txt index 4cccad0..4648dc5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ -docker==4.0.1 -molecule==2.20.1 +docker==4.0.2 +molecule==2.20.2 pytest==4.5.0 six==1.11.0 -tox==3.12.1 -testinfra==1.19.0 +tox==3.13.2 +testinfra==3.0.5 diff --git a/templates/systemd.email.j2 b/templates/systemd.email.j2 index cb65dd6..6867728 100644 --- a/templates/systemd.email.j2 +++ b/templates/systemd.email.j2 @@ -1,11 +1,12 @@ #!/bin/bash ERRORMAIL=$(mktemp) LOCKFILE="/var/lock/$2" +FROMAIL="{{ notify_fromEmail }}" set -eux if lockfile -r0 -l900 $LOCKFD; then echo "TO: $1" >> $ERRORMAIL - echo "From: $1" >> $ERRORMAIL + echo "From: $FROMAIL" >> $ERRORMAIL echo "Subject: $HOSTNAME - $2" >> $ERRORMAIL echo "Content-Transfer-Encoding: 8bit" >> $ERRORMAIL echo "Content-Type: text/plain; charset=UTF-8" >> $ERRORMAIL From 007569c00231bf83ca522e38784baf3135518186 Mon Sep 17 00:00:00 2001 From: mlise Date: Thu, 25 Jul 2019 08:55:14 +0200 Subject: [PATCH 3/4] use hostname -f instead of hostname in alert email --- templates/systemd.email.j2 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/systemd.email.j2 b/templates/systemd.email.j2 index 6867728..13454dd 100644 --- a/templates/systemd.email.j2 +++ b/templates/systemd.email.j2 @@ -2,12 +2,13 @@ ERRORMAIL=$(mktemp) LOCKFILE="/var/lock/$2" FROMAIL="{{ notify_fromEmail }}" +SUBJECT=$(hostname -f) set -eux -if lockfile -r0 -l900 $LOCKFD; then +if lockfile -r0 -l900 $LOCKFFILE; then echo "TO: $1" >> $ERRORMAIL echo "From: $FROMAIL" >> $ERRORMAIL - echo "Subject: $HOSTNAME - $2" >> $ERRORMAIL + echo "Subject: $SUBJECT - $2" >> $ERRORMAIL echo "Content-Transfer-Encoding: 8bit" >> $ERRORMAIL echo "Content-Type: text/plain; charset=UTF-8" >> $ERRORMAIL echo "$(systemctl status --full "$2")" >> $ERRORMAIL From f388cd89875e0d929228942d8f136de3e45ff288 Mon Sep 17 00:00:00 2001 From: mlise Date: Thu, 25 Jul 2019 09:39:57 +0200 Subject: [PATCH 4/4] Install procmail (needed by lockfile) --- tasks/install.yml | 6 ++++++ tasks/main.yml | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 tasks/install.yml diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..ffe1fcd --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,6 @@ +--- + +- name: 'INSTALL | install procmail' + apt: + name: 'procmail' + state: 'present' diff --git a/tasks/main.yml b/tasks/main.yml index 6ec0ea9..516757a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -8,6 +8,12 @@ - 'role::systemd-notify' - 'role::systemd-notify::init' +- name: 'INSTALL | Install package with lockfile' + become: True + include_tasks: "{{ role_path }}/tasks/install.yml" + tags: + - 'role::systemd-notify' + - 'role::systemd-notify::install' - name: 'CONFIG | Manage config tasks' become: 'True'