From 15e4775679e924b347e1eda6744c0a146d720387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sch=C3=A4ferdiek?= Date: Tue, 21 Mar 2023 18:48:46 +0100 Subject: [PATCH] Notifier allows to override global configuration files on a per-command base by checking for existence of all required configuration values as environment values. --- README.md | 15 ++++++++++++++ _man/src/notifier.1.md | 2 ++ usr/local/bin/notifier | 23 +++++++++++++++------ usr/share/man/man1/check_updates.1 | 4 ++-- usr/share/man/man1/disk_space_alert.1 | 4 ++-- usr/share/man/man1/docker_check.1 | 4 ++-- usr/share/man/man1/docker_compose_update.1 | 4 ++-- usr/share/man/man1/dynv6.1 | 4 ++-- usr/share/man/man1/memory_usage_alert.1 | 4 ++-- usr/share/man/man1/notifier.1 | 11 +++++++--- usr/share/man/man1/smart_tests_long.1 | 4 ++-- usr/share/man/man1/system-helpers.1 | 4 ++-- usr/share/man/man1/systemd_check.1 | 4 ++-- usr/share/man/man1/systemd_failure_notify.1 | 4 ++-- 14 files changed, 62 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 5f3ffd8..e7444c2 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,21 @@ NOTIFIER_MAIL_ENABLED=true NOTIFIER_GOTIFY_ENABLED=false ``` +**Important:** You can also change notifier behavior on a per-command basis, e.g. disable mail or gotify by providing ALL notifier variables as environment variables where the command is issued, for example in a systemd .service file. + +```shell +# ... + +[Service] +# ... +Environment="NOTIFIER_GOTIFY_ENABLED=false" +Environment="NOTIFIER_MAIL_ENABLED=true" +Environment="NOTIFIER_MAIL_ADDRESS=another-mail-different-from-your-global-config@domain.tld" + +# ... +``` + + Otherwise, services and scripts don't depend on each other, but have hints that they can be combined (out-commented line in service files for `systemd_failure_notify@.service` for example). Examples are given inside `usr/share/doc/system-helpers` and *man pages* are also available. diff --git a/_man/src/notifier.1.md b/_man/src/notifier.1.md index 6ad15c8..c797335 100644 --- a/_man/src/notifier.1.md +++ b/_man/src/notifier.1.md @@ -15,6 +15,8 @@ It's part of **system-helpers**. By default, **notifier** tries to read from *$HOME/.notifier.conf* and */etc/notifier.conf* for configuration. It will exit with a non-zero exit code if it cannot find a proper configuration file. +If all of the below variables are set and not empty as environment variables, then **notifier** skips sourcing config files. This allows a per-command configuration without relying on the global configs. + The following configuration values can be set, defaults are shown as well:\ - **NOTIFIER_MAIL_ENABLED="true"** which enables or disables notification via mail to *NOTIFIER_MAIL_ADDRESS*. diff --git a/usr/local/bin/notifier b/usr/local/bin/notifier index ccea046..fa6a4b6 100755 --- a/usr/local/bin/notifier +++ b/usr/local/bin/notifier @@ -29,13 +29,9 @@ if [ -z "$NOTIFIER_CONTENT" ]; then exit 1; fi -NOTIFIER_MAIL_ENABLED="true"; -NOTIFIER_GOTIFY_ENABLED="false"; -NOTIFIER_MAIL_ADDRESS=$3; - argsMailProvided="false" -if [[ -n "${NOTIFIER_MAIL_ADDRESS}" ]]; then +if [[ -n "$3" ]]; then argsMailProvided="true" fi @@ -88,6 +84,17 @@ source_config() { local configFallback=$1; local configGlobalFallback=$2; + checkMailEnabled=${NOTIFIER_MAIL_ENABLED:-1} + checkGotifyEnabled=${NOTIFIER_GOTIFY_ENABLED:-1} + checkMailAddress=${NOTIFIER_MAIL_ADDRESS:-1} + + if [[ ! -v "$checkMailEnabled" && ! -v "$checkGotifyEnabled" && ! -v "$checkMailAddress" ]]; then + echo "Using environment variables for configuration instead of configuration files"; + return; + else + echo "Using configuration files"; + fi + if [[ -f "$configFallback" ]]; then apply_config "$configFallback"; return; @@ -106,12 +113,16 @@ if [[ "${argsMailProvided}" == "true" ]]; then echo "Overwriting mail with argument value" fi +echo "Using NOTIFIER_MAIL_ENABLED=${NOTIFIER_MAIL_ENABLED}" +echo "Using NOTIFIER_GOTIFY_ENABLED=${NOTIFIER_GOTIFY_ENABLED}" +echo "Using NOTIFIER_MAIL_ADDRESS=${NOTIFIER_MAIL_ADDRESS}" + SUBJECT="${NOTIFIER_SUBJECT}" MESSAGE="${NOTIFIER_CONTENT}" if [[ "${NOTIFIER_MAIL_ENABLED}" == "true" ]]; then echo "$MESSAGE"|mailx -Ssendwait -s "$SUBJECT" "$NOTIFIER_MAIL_ADDRESS"; - echo "Sent notifiction via mail" + echo "Sent notifiction via mail to $NOTIFIER_MAIL_ADDRESS" else echo "Sending notifictions via mail is disabled" fi diff --git a/usr/share/man/man1/check_updates.1 b/usr/share/man/man1/check_updates.1 index 5359458..5f03ea9 100644 --- a/usr/share/man/man1/check_updates.1 +++ b/usr/share/man/man1/check_updates.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -76,4 +76,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/disk_space_alert.1 b/usr/share/man/man1/disk_space_alert.1 index 2896b30..b1aabf7 100644 --- a/usr/share/man/man1/disk_space_alert.1 +++ b/usr/share/man/man1/disk_space_alert.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -73,4 +73,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/docker_check.1 b/usr/share/man/man1/docker_check.1 index 583dc1b..96a9367 100644 --- a/usr/share/man/man1/docker_check.1 +++ b/usr/share/man/man1/docker_check.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -80,4 +80,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/docker_compose_update.1 b/usr/share/man/man1/docker_compose_update.1 index c54c73f..c40cf7c 100644 --- a/usr/share/man/man1/docker_compose_update.1 +++ b/usr/share/man/man1/docker_compose_update.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -98,4 +98,4 @@ Success \f[B]1\f[R] Error .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/dynv6.1 b/usr/share/man/man1/dynv6.1 index 8c5ca39..0e6473f 100644 --- a/usr/share/man/man1/dynv6.1 +++ b/usr/share/man/man1/dynv6.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -85,4 +85,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/memory_usage_alert.1 b/usr/share/man/man1/memory_usage_alert.1 index 4946365..1b810a8 100644 --- a/usr/share/man/man1/memory_usage_alert.1 +++ b/usr/share/man/man1/memory_usage_alert.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -64,4 +64,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/notifier.1 b/usr/share/man/man1/notifier.1 index 51e2f9d..8ffc91e 100644 --- a/usr/share/man/man1/notifier.1 +++ b/usr/share/man/man1/notifier.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -39,6 +39,11 @@ configuration. It will exit with a non-zero exit code if it cannot find a proper configuration file. .PP +If all of the below variables are set and not empty as environment +variables, then \f[B]notifier\f[R] skips sourcing config files. +This allows a per-command configuration without relying on the global +configs. +.PP The following configuration values can be set, defaults are shown as well: .PD 0 @@ -50,7 +55,7 @@ notification via mail to \f[I]NOTIFIER_MAIL_ADDRESS\f[R]. .IP \[bu] 2 \f[B]NOTIFIER_MAIL_ADDRESS=\[lq]\[lq]\f[R] which requires \f[I]mail.rc\f[R] to be configured and `\f[I]mailx\f[R]' command has be -available\[ac] +available¸ .IP \[bu] 2 \f[B]NOTIFIER_GOTIFY_ENABLED=\[lq]false\[rq]\f[R] which `gotify' command has be available and needs to be properly configured by having a @@ -77,4 +82,4 @@ Error .PP \f[B]system-helpers\f[R] .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/smart_tests_long.1 b/usr/share/man/man1/smart_tests_long.1 index 31d9eaf..25c9c27 100644 --- a/usr/share/man/man1/smart_tests_long.1 +++ b/usr/share/man/man1/smart_tests_long.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -68,4 +68,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/system-helpers.1 b/usr/share/man/man1/system-helpers.1 index 113ad21..3f207ea 100644 --- a/usr/share/man/man1/system-helpers.1 +++ b/usr/share/man/man1/system-helpers.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -58,4 +58,4 @@ See \f[I]/usr/share/doc/system-helpers/system-helpers\f[R] for example configurations which should be copied inside \f[B]$HOME\f[R] or \f[B]/etc\f[R] depending on the helper script. .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/systemd_check.1 b/usr/share/man/man1/systemd_check.1 index b5cb934..54cfdd5 100644 --- a/usr/share/man/man1/systemd_check.1 +++ b/usr/share/man/man1/systemd_check.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -71,4 +71,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek. diff --git a/usr/share/man/man1/systemd_failure_notify.1 b/usr/share/man/man1/systemd_failure_notify.1 index ec70309..ce86461 100644 --- a/usr/share/man/man1/systemd_failure_notify.1 +++ b/usr/share/man/man1/systemd_failure_notify.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 2.19.2 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -71,4 +71,4 @@ Error .PP \f[B]notifier\f[R] - used as notification application .SH AUTHORS -Alexander Sch\[:a]ferdiek. +Alexander Schäferdiek.