From dac955544bf2b9ff3456c07d657c16acce6eeecc Mon Sep 17 00:00:00 2001 From: Matthew Oleksowicz Date: Wed, 20 Aug 2025 03:42:21 -0400 Subject: [PATCH] Implement notification channel template reuse and advanced configuration variables. --- README.md | 27 +- default.config | 4 +- dockcheck.sh | 2 +- notify_templates/notify_DSM.sh | 17 +- notify_templates/notify_HA.sh | 31 ++- notify_templates/notify_apprise.sh | 33 ++- notify_templates/notify_discord.sh | 27 +- notify_templates/notify_file.sh | 12 +- notify_templates/notify_gotify.sh | 30 ++- notify_templates/notify_matrix.sh | 35 ++- notify_templates/notify_ntfy.sh | 33 ++- notify_templates/notify_pushbullet.sh | 30 ++- notify_templates/notify_pushover.sh | 33 ++- notify_templates/notify_slack.sh | 30 ++- notify_templates/notify_smtp.sh | 33 ++- notify_templates/notify_telegram.sh | 33 ++- notify_templates/notify_v2.sh | 345 ++++++++++++++++---------- 17 files changed, 511 insertions(+), 244 deletions(-) diff --git a/README.md b/README.md index 4499857..9c8298c 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,16 @@ ___ ## :bell: Changelog +- **v0.7.1**: + - Added support for multiple notifications using the same template + - Added support for notification output format + - Added support for file output + - Added optional configuration variables per channel to (replace with any channel name): + - \_TEMPLATE : Specify a template + - \_SKIPSNOOZE : Skip snooze + - \_CONTAINERSONLY : Only notify for docker container related updates + - \_ALLOWEMPTY : Always send notifications, even when empty + - \_OUTPUT : Define output format - **v0.7.0**: - Bugfix: snooze dockcheck.sh-self-notification and some config clarification. - Added authentication support to Ntfy.sh. @@ -194,7 +204,22 @@ If an update becomes available for an item that is not snoozed, notifications wi - [Slack](https://api.slack.com/tutorials/tracks/posting-messages-with-curl) - Slack curl api Further additions are welcome - suggestions or PRs! -Initiated and first contributed by [yoyoma2](https://github.com/yoyoma2). +Initiated and first contributed by [yoyoma2](https://github.com/yoyoma2). + +#### Notification channel configuration: +All required environment variables for each channel type are provided in the default.config file as comments and must be uncommented and modified for your requirements. +For advanced users, additional functionality is available via custom configurations and environment variables. +Use cases (in all cases, replace with the name of the channel specified in the NOTIFY_CHANNELS environment variable): +- To bypass the snooze feature, even when enabled, add the variable `\_SKIPSNOOZE` to `dockcheck.config` and set it equal to `true`. +- To configure the channel to only send Docker container update notifications, add the variable `\_CONTAINERSONLY` to `dockcheck.config` and set it equal to `true`. +- To send notifications even when there are no updates available, add the variable `\_ALLOWEMPTY` to `dockcheck.config` and set it equal to `true`. +- To use another notification output format, add the variable `\_OUTPUT` to `dockcheck.config` and set it equal to `csv`, `json`, or `text`. If unset or set to an invalid value, defaults to `text`. +- To send multiple notifications using the same notification template: + - Strings in the `NOTIFY_CHANNELS` list are now treated as unique names and do not necessarily refer to the notification template that will be called, though they do by default. + - Add another notification channel to `NOTIFY_CHANNELS` in `dockcheck.config`. It can have any name as long as it is unique and contains no spaces or special characters. + - Add the variable `\_TEMPLATE` to `dockcheck.config` where `` is the name of the channel added above and set the value to an available notification template script (`slack`, `apprise`, `gotify`, etc.) + - Add all other environment variables required for the chosen template to function with `` in upper case as the prefix rather than the template name. + - For example, if `` is `mynotification` and the template configured is `slack`, you would need to set `MYNOTIFICATION_CHANNEL_ID` and `MYNOTIFICATION_ACCESS_TOKEN`. ### :date: Release notes addon There's a function to use a lookup-file to add release note URL's to the notification message. diff --git a/default.config b/default.config index e4f0507..d5d21aa 100644 --- a/default.config +++ b/default.config @@ -32,7 +32,7 @@ ## All commented values are examples only. Modify as needed. ## ## Uncomment the line below and specify the notification channels you wish to enable in a space separated string -# NOTIFY_CHANNELS="apprise discord DSM file generic HA gotify matrix ntfy pushbullet pushover slack smtp telegram" +# NOTIFY_CHANNELS="apprise discord DSM file generic HA gotify matrix ntfy pushbullet pushover slack smtp telegram file" # ## Uncomment the line below and specify the number of seconds to delay notifications to enable snooze # SNOOZE_SECONDS=86400 @@ -87,4 +87,6 @@ # TELEGRAM_CHAT_ID="mychatid" # TELEGRAM_TOKEN="token-value" # TELEGRAM_TOPIC_ID="0" +# +# FILE_PATH="${ScriptWorkDir}/updates_available.txt" diff --git a/dockcheck.sh b/dockcheck.sh index 05c239c..d46f4da 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash VERSION="v0.7.1" -# ChangeNotes: Call send_notification even when no updates are available to write to file (if enabled) and clean up snooze +# ChangeNotes: Add support for multiple notifications of the same type, output formatting, and file output Github="https://github.com/mag37/dockcheck" RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh" diff --git a/notify_templates/notify_DSM.sh b/notify_templates/notify_DSM.sh index 3afe0ab..b98281b 100644 --- a/notify_templates/notify_DSM.sh +++ b/notify_templates/notify_DSM.sh @@ -1,5 +1,5 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_DSM_VERSION="v0.4" +NOTIFY_DSM_VERSION="v0.5" # INFO: ssmtp is deprecated - consider to use msmtp instead. # # mSMTP/sSMTP has to be installed and configured manually. @@ -23,15 +23,26 @@ else fi trigger_DSM_notification() { + if [[ -n "$1" ]]; then + DSM_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$DSM_channel") + else + DSM_channel="DSM" + UpperChannel="DSM" + fi + +DSMSendmailToVar="${UpperChannel}_SENDMAILTO" +DSMSubjectTagVar="${UpperChannel}_SUBJECTTAG" + CfgFile="/usr/syno/etc/synosmtp.conf" # User variables: # Automatically sends to your usual destination for synology DSM notification emails. # You can also manually override by assigning something else to DSM_SENDMAILTO in dockcheck.config. -SendMailTo=${DSM_SENDMAILTO:-$(grep 'eventmail1' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p')} +SendMailTo=${!DSMSendmailToVar:-$(grep 'eventmail1' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p')} # e.g. DSM_SENDMAILTO="me@mydomain.com" -SubjectTag=${DSM_SUBJECTTAG:-$(grep 'eventsubjectprefix' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p')} +SubjectTag=${!DSMSubjectTagVar:-$(grep 'eventsubjectprefix' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p')} # e.g. DSM_SUBJECTTAG="Email Subject Prefix" SenderName=$(grep 'smtp_from_name' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p') SenderMail=$(grep 'smtp_from_mail' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p') diff --git a/notify_templates/notify_HA.sh b/notify_templates/notify_HA.sh index dda74be..26586b4 100755 --- a/notify_templates/notify_HA.sh +++ b/notify_templates/notify_HA.sh @@ -1,5 +1,5 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_HA_VERSION="v0.1" +NOTIFY_HA_VERSION="v0.2" # # This is an integration that makes it possible to send notifications via Home Assistant (https://www.home-assistant.io/integrations/notify/) # You need to generate a long-lived access token in Home Sssistant to be used here (https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token) @@ -7,15 +7,28 @@ NOTIFY_HA_VERSION="v0.1" # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set HA_ENTITY, HA_URL and HA_TOKEN in your dockcheck.config file. -if [[ -z "${HA_ENTITY:-}" ]] || [[ -z "${HA_URL:-}" ]] || [[ -z "${HA_TOKEN:-}" ]]; then - printf "Home Assistant notification channel enabled, but required configuration variables are missing. Home assistant notifications will not be sent.\n" - - remove_channel HA -fi - trigger_HA_notification() { - AccessToken="${HA_TOKEN}" - Url="${HA_URL}/api/services/notify/${HA_ENTITY}" + if [[ -n "$1" ]]; then + HA_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$HA_channel") + else + HA_channel="HA" + UpperChannel="HA" + fi + + HAEntityVar="${UpperChannel}_ENTITY" + HAUrlVar="${UpperChannel}_URL" + HATokenVar="${UpperChannel}_TOKEN" + + if [[ -z "${!HAEntityVar:-}" ]] || [[ -z "${!HAUrlVar:-}" ]] || [[ -z "${!HATokenVar:-}" ]]; then + printf "The ${HA_channel} notification channel is enabled, but required configuration variables are missing. Home assistant notifications will not be sent.\n" + + remove_channel HA + return 1 + fi + + AccessToken="${!HATokenVar}" + Url="${!HAUrlVar}/api/services/notify/${!HAEntityVar}" JsonData=$( "$jqbin" -n \ --arg body "$MessageBody" \ '{"title": "dockcheck update", "message": $body}' ) diff --git a/notify_templates/notify_apprise.sh b/notify_templates/notify_apprise.sh index 1fa94de..579b3c9 100644 --- a/notify_templates/notify_apprise.sh +++ b/notify_templates/notify_apprise.sh @@ -1,5 +1,5 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_APPRISE_VERSION="v0.3" +NOTIFY_APPRISE_VERSION="v0.4" # # Required receiving services must already be set up. # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. @@ -7,17 +7,28 @@ NOTIFY_APPRISE_VERSION="v0.3" # Do not modify this file directly within the "notify_templates" subdirectory. Set APPRISE_PAYLOAD in your dockcheck.config file. # If API, set APPRISE_URL instead. -if [[ -z "${APPRISE_PAYLOAD:-}" ]] && [[ -z "${APPRISE_URL:-}" ]]; then - printf "Apprise notification channel enabled, but required configuration variables are missing. Apprise notifications will not be sent.\n" - - remove_channel apprise -fi - trigger_apprise_notification() { + if [[ -n "$1" ]]; then + apprise_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$apprise_channel") + else + apprise_channel="apprise" + UpperChannel="APPRISE" + fi - if [[ -n "${APPRISE_PAYLOAD:-}" ]]; then + ApprisePayloadVar="${UpperChannel}_PAYLOAD" + AppriseUrlVar="${UpperChannel}_URL" + + if [[ -z "${!ApprisePayloadVar:-}" ]] && [[ -z "${!AppriseUrlVar:-}" ]]; then + printf "The ${apprise_channel} notification channel is enabled, but required configuration variables are missing. Apprise notifications will not be sent.\n" + + remove_channel apprise + return 1 + fi + + if [[ -n "${!ApprisePayloadVar:-}" ]]; then apprise -vv -t "$MessageTitle" -b "$MessageBody" \ - ${APPRISE_PAYLOAD} + ${!ApprisePayloadVar} if [[ $? -gt 0 ]]; then NotifyError=true @@ -29,8 +40,8 @@ trigger_apprise_notification() { # pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b # tgram://{bot_token}/{chat_id}/' - if [[ -n "${APPRISE_URL:-}" ]]; then - AppriseURL="${APPRISE_URL}" + if [[ -n "${!AppriseUrlVar:-}" ]]; then + AppriseURL="${!AppriseUrlVar}" curl -S -o /dev/null ${CurlArgs} -X POST -F "title=$MessageTitle" -F "body=$MessageBody" -F "tags=all" $AppriseURL # e.g. APPRISE_URL=http://apprise.mydomain.tld:1234/notify/apprise if [[ $? -gt 0 ]]; then diff --git a/notify_templates/notify_discord.sh b/notify_templates/notify_discord.sh index fa1a32d..293893b 100644 --- a/notify_templates/notify_discord.sh +++ b/notify_templates/notify_discord.sh @@ -1,19 +1,30 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_DISCORD_VERSION="v0.4" +NOTIFY_DISCORD_VERSION="v0.5" # # Required receiving services must already be set up. # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set DISCORD_WEBHOOK_URL in your dockcheck.config file. -if [[ -z "${DISCORD_WEBHOOK_URL:-}" ]]; then - printf "Discord notification channel enabled, but required configuration variables are missing. Discord notifications will not be sent.\n" - - remove_channel discord -fi - trigger_discord_notification() { - DiscordWebhookUrl="${DISCORD_WEBHOOK_URL}" # e.g. DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/ + if [[ -n "$1" ]]; then + discord_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$discord_channel") + else + discord_channel="discord" + UpperChannel="DISCORD" + fi + + DiscordWebhookUrlVar="${UpperChannel}_WEBHOOK_URL" + + if [[ -z "${!DiscordWebhookUrlVar:-}" ]]; then + printf "The ${discord_channel} notification channel is enabled, but required configuration variables are missing. Discord notifications will not be sent.\n" + + remove_channel discord + return 1 + fi + + DiscordWebhookUrl="${!DiscordWebhookUrlVar}" # e.g. DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/ JsonData=$( "$jqbin" -n \ --arg username "$FromHost" \ diff --git a/notify_templates/notify_file.sh b/notify_templates/notify_file.sh index 9d84f00..75aa74a 100644 --- a/notify_templates/notify_file.sh +++ b/notify_templates/notify_file.sh @@ -5,7 +5,17 @@ NOTIFY_FILE_VERSION="v0.1" # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. trigger_file_notification() { - NotifyFile="${ScriptWorkDir}/updates_available.txt" + if [[ -n "$1" ]]; then + file_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$file_channel") + else + file_channel="file" + UpperChannel="FILE" + fi + + FilePathVar="${UpperChannel}_PATH" + + NotifyFile="${!FilePathVar:=${ScriptWorkDir}/updates_available.txt}" echo "${MessageBody}" > ${NotifyFile} diff --git a/notify_templates/notify_gotify.sh b/notify_templates/notify_gotify.sh index d3d2c67..ddf48cf 100644 --- a/notify_templates/notify_gotify.sh +++ b/notify_templates/notify_gotify.sh @@ -1,20 +1,32 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_GOTIFY_VERSION="v0.4" +NOTIFY_GOTIFY_VERSION="v0.5" # # Required receiving services must already be set up. # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set GOTIFY_TOKEN and GOTIFY_DOMAIN in your dockcheck.config file. -if [[ -z "${GOTIFY_TOKEN:-}" ]] || [[ -z "${GOTIFY_DOMAIN:-}" ]]; then - printf "Gotify notification channel enabled, but required configuration variables are missing. Gotify notifications will not be sent.\n" - - remove_channel gotify -fi - trigger_gotify_notification() { - GotifyToken="${GOTIFY_TOKEN}" # e.g. GOTIFY_TOKEN=token-value - GotifyUrl="${GOTIFY_DOMAIN}/message?token=${GotifyToken}" # e.g. GOTIFY_URL=https://gotify.domain.tld + if [[ -n "$1" ]]; then + gotify_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$gotify_channel") + else + gotify_channel="gotify" + UpperChannel="GOTIFY" + fi + + GotifyTokenVar="${UpperChannel}_TOKEN" + GotifyUrlVar="${UpperChannel}_DOMAIN" + + if [[ -z "${!GotifyTokenVar:-}" ]] || [[ -z "${!GotifyUrlVar:-}" ]]; then + printf "The ${gotify_channel} notification channel is enabled, but required configuration variables are missing. Gotify notifications will not be sent.\n" + + remove_channel gotify + return 1 + fi + + GotifyToken="${!GotifyTokenVar}" # e.g. GOTIFY_TOKEN=token-value + GotifyUrl="${!GotifyUrlVar}/message?token=${GotifyToken}" # e.g. GOTIFY_URL=https://gotify.domain.tld if [[ "$PrintMarkdownURL" == true ]]; then ContentType="text/markdown" diff --git a/notify_templates/notify_matrix.sh b/notify_templates/notify_matrix.sh index bcff5d2..9966a73 100644 --- a/notify_templates/notify_matrix.sh +++ b/notify_templates/notify_matrix.sh @@ -1,25 +1,38 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_MATRIX_VERSION="v0.3" +NOTIFY_MATRIX_VERSION="v0.4" # # Required receiving services must already be set up. # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set MATRIX_ACCESS_TOKEN, MATRIX_ROOM_ID, and MATRIX_SERVER_URL in your dockcheck.config file. -if [[ -z "${MATRIX_ACCESS_TOKEN:-}" ]] || [[ -z "${MATRIX_ROOM_ID}:-" ]] || [[ -z "${MATRIX_SERVER_URL}:-" ]]; then - printf "Matrix notification channel enabled, but required configuration variables are missing. Matrix notifications will not be sent.\n" - - remove_channel matrix -fi - trigger_matrix_notification() { - AccessToken="${MATRIX_ACCESS_TOKEN}" # e.g. MATRIX_ACCESS_TOKEN=token-value - Room_id="${MATRIX_ROOM_ID}" # e.g. MATRIX_ROOM_ID=myroom - MatrixServer="${MATRIX_SERVER_URL}" # e.g. MATRIX_SERVER_URL=http://matrix.yourdomain.tld + if [[ -n "$1" ]]; then + matrix_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$matrix_channel") + else + matrix_channel="matrix" + UpperChannel="MATRIX" + fi + + AccessTokenVar="${UpperChannel}_ACCESS_TOKEN" + RoomIdVar="${UpperChannel}_ROOM_ID" + MatrixServerVar="${UpperChannel}_SERVER_URL" + + if [[ -z "${!AccessTokenVar:-}" ]] || [[ -z "${!RoomIdVar:-}" ]] || [[ -z "${!MatrixServerVar:-}" ]]; then + printf "The ${matrix_channel} notification channel is enabled, but required configuration variables are missing. Matrix notifications will not be sent.\n" + + remove_channel matrix + return 1 + fi + + AccessToken="${!AccessTokenVar}" # e.g. MATRIX_ACCESS_TOKEN=token-value + RoomId="${!RoomIdVar}" # e.g. MATRIX_ROOM_ID=myroom + MatrixServer="${!MatrixServerVar}" # e.g. MATRIX_SERVER_URL=http://matrix.yourdomain.tld MsgBody="{\"msgtype\":\"m.text\",\"body\":\"$MessageBody\"}" # URL Example: https://matrix.org/_matrix/client/r0/rooms/!xxxxxx:example.com/send/m.room.message?access_token=xxxxxxxx - curl -S -o /dev/null ${CurlArgs} -X POST "$MatrixServer/_matrix/client/r0/rooms/$Room_id/send/m.room.message?access_token=$AccessToken" -H 'Content-Type: application/json' -d "$MsgBody" + curl -S -o /dev/null ${CurlArgs} -X POST "$MatrixServer/_matrix/client/r0/rooms/$RoomId/send/m.room.message?access_token=$AccessToken" -H 'Content-Type: application/json' -d "$MsgBody" if [[ $? -gt 0 ]]; then NotifyError=true diff --git a/notify_templates/notify_ntfy.sh b/notify_templates/notify_ntfy.sh index e19f455..22b41fb 100644 --- a/notify_templates/notify_ntfy.sh +++ b/notify_templates/notify_ntfy.sh @@ -1,19 +1,32 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_NTFYSH_VERSION="v0.6" +NOTIFY_NTFYSH_VERSION="v0.7" # # Setup app and subscription at https://ntfy.sh # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set NTFY_DOMAIN and NTFY_TOPIC_NAME in your dockcheck.config file. -if [[ -z "${NTFY_DOMAIN:-}" ]] || [[ -z "${NTFY_TOPIC_NAME:-}" ]]; then - printf "Ntfy notification channel enabled, but required configuration variables are missing. Ntfy notifications will not be sent.\n" - - remove_channel ntfy -fi - trigger_ntfy_notification() { - NtfyUrl="${NTFY_DOMAIN}/${NTFY_TOPIC_NAME}" + if [[ -n "$1" ]]; then + ntfy_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$ntfy_channel") + else + ntfy_channel="ntfy" + UpperChannel="NTFY" + fi + + NtfyDomainVar="${UpperChannel}_DOMAIN" + NtfyTopicNameVar="${UpperChannel}_TOPIC_NAME" + NtfyAuthVar="${UpperChannel}_AUTH" + + if [[ -z "${!GotifyTokenVar:-}" ]] || [[ -z "${!GotifyUrlVar:-}" ]]; then + printf "The ${ntfy_channel} notification channel is enabled, but required configuration variables are missing. Ntfy notifications will not be sent.\n" + + remove_channel ntfy + return 1 + fi + + NtfyUrl="${!NtfyDomainVar}/${!NtfyTopicNameVar}" # e.g. # NTFY_DOMAIN=ntfy.sh # NTFY_TOPIC_NAME=YourUniqueTopicName @@ -24,8 +37,8 @@ trigger_ntfy_notification() { ContentType="Markdown: no" #text/plain fi - if [[ -n "${NTFY_AUTH:-}" ]]; then - NtfyAuth="-u $NTFY_AUTH" + if [[ -n "${!NtfyAuthVar:-}" ]]; then + NtfyAuth="-u ${!NtfyAuthVar}" else NtfyAuth="" fi diff --git a/notify_templates/notify_pushbullet.sh b/notify_templates/notify_pushbullet.sh index 78dec0b..f272157 100644 --- a/notify_templates/notify_pushbullet.sh +++ b/notify_templates/notify_pushbullet.sh @@ -1,5 +1,5 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_PUSHBULLET_VERSION="v0.3" +NOTIFY_PUSHBULLET_VERSION="v0.4" # # Required receiving services must already be set up. # Requires jq installed and in PATH. @@ -7,15 +7,27 @@ NOTIFY_PUSHBULLET_VERSION="v0.3" # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set PUSHBULLET_TOKEN and PUSHBULLET_URL in your dockcheck.config file. -if [[ -z "${PUSHBULLET_URL:-}" ]] || [[ -z "${PUSHBULLET_TOKEN:-}" ]]; then - printf "Pushbullet notification channel enabled, but required configuration variables are missing. Pushbullet notifications will not be sent.\n" - - remove_channel pushbullet -fi - trigger_pushbullet_notification() { - PushUrl="${PUSHBULLET_URL}" # e.g. PUSHBULLET_URL=https://api.pushbullet.com/v2/pushes - PushToken="${PUSHBULLET_TOKEN}" # e.g. PUSHBULLET_TOKEN=token-value + if [[ -n "$1" ]]; then + pushbullet_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$pushbullet_channel") + else + pushbullet_channel="pushbullet" + UpperChannel="PUSHBULLET" + fi + + PushUrlVar="${UpperChannel}_URL" + PushTokenVar="${UpperChannel}_TOKEN" + + if [[ -z "${!PushUrlVar:-}" ]] || [[ -z "${!PushTokenVar:-}" ]]; then + printf "The ${pushbullet_channel} notification channel is enabled, but required configuration variables are missing. Pushbullet notifications will not be sent.\n" + + remove_channel pushbullet + return 1 + fi + + PushUrl="${!PushUrlVar}" # e.g. PUSHBULLET_URL=https://api.pushbullet.com/v2/pushes + PushToken="${!PushTokenVar}" # e.g. PUSHBULLET_TOKEN=token-value # Requires jq to process json data "$jqbin" -n --arg title "$MessageTitle" --arg body "$MessageBody" '{body: $body, title: $title, type: "note"}' | curl -S -o /dev/null ${CurlArgs} -X POST -H "Access-Token: $PushToken" -H "Content-type: application/json" $PushUrl -d @- diff --git a/notify_templates/notify_pushover.sh b/notify_templates/notify_pushover.sh index 60ffad6..389f68d 100644 --- a/notify_templates/notify_pushover.sh +++ b/notify_templates/notify_pushover.sh @@ -1,5 +1,5 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_PUSHOVER_VERSION="v0.3" +NOTIFY_PUSHOVER_VERSION="v0.4" # # Required receiving services must already be set up. # Requires jq installed and in PATH. @@ -7,16 +7,29 @@ NOTIFY_PUSHOVER_VERSION="v0.3" # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set PUSHOVER_USER_KEY, PUSHOVER_TOKEN, and PUSHOVER_URL in your dockcheck.config file. -if [[ -z "${PUSHOVER_URL:-}" ]] || [[ -z "${PUSHOVER_USER_KEY:-}" ]] || [[ -z "${PUSHOVER_TOKEN:-}" ]]; then - printf "Pushover notification channel enabled, but required configuration variables are missing. Pushover notifications will not be sent.\n" - - remove_channel pushover -fi - trigger_pushover_notification() { - PushoverUrl="${PUSHOVER_URL}" # e.g. PUSHOVER_URL=https://api.pushover.net/1/messages.json - PushoverUserKey="${PUSHOVER_USER_KEY}" # e.g. PUSHOVER_USER_KEY=userkey - PushoverToken="${PUSHOVER_TOKEN}" # e.g. PUSHOVER_TOKEN=token-value + if [[ -n "$1" ]]; then + pushover_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$pushover_channel") + else + pushover_channel="pushover" + UpperChannel="PUSHOVER" + fi + + PushoverUrlVar="${UpperChannel}_URL" + PushoverUserKeyVar="${UpperChannel}_USER_KEY" + PushoverTokenVar="${UpperChannel}_TOKEN" + + if [[ -z "${!PushoverUrlVar:-}" ]] || [[ -z "${!PushoverUserKeyVar:-}" ]] || [[ -z "${!PushoverTokenVar:-}" ]]; then + printf "The ${pushover_channel} notification channel is enabled, but required configuration variables are missing. Pushover notifications will not be sent.\n" + + remove_channel pushover + return 1 + fi + + PushoverUrl="${!PushoverUrlVar}" # e.g. PUSHOVER_URL=https://api.pushover.net/1/messages.json + PushoverUserKey="${!PushoverUserKeyVar}" # e.g. PUSHOVER_USER_KEY=userkey + PushoverToken="${!PushoverTokenVar}" # e.g. PUSHOVER_TOKEN=token-value # Sending the notification via Pushover curl -S -o /dev/null ${CurlArgs} -X POST \ diff --git a/notify_templates/notify_slack.sh b/notify_templates/notify_slack.sh index 0a9cd7a..aa65a9d 100644 --- a/notify_templates/notify_slack.sh +++ b/notify_templates/notify_slack.sh @@ -1,20 +1,32 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_SLACK_VERSION="v0.3" +NOTIFY_SLACK_VERSION="v0.4" # # Setup app and token at https://api.slack.com/tutorials/tracks/posting-messages-with-curl # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set SLACK_ACCESS_TOKEN, and SLACK_CHANNEL_ID in your dockcheck.config file. -if [[ -z "${SLACK_ACCESS_TOKEN:-}" ]] || [[ -z "${SLACK_CHANNEL_ID:-}" ]]; then - printf "Slack notification channel enabled, but required configuration variables are missing. Slack notifications will not be sent.\n" - - remove_channel slack -fi - trigger_slack_notification() { - AccessToken="${SLACK_ACCESS_TOKEN}" # e.g. SLACK_ACCESS_TOKEN=some-token - ChannelID="${SLACK_CHANNEL_ID}" # e.g. CHANNEL_ID=mychannel + if [[ -n "$1" ]]; then + slack_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$slack_channel") + else + slack_channel="slack" + UpperChannel="SLACK" + fi + + AccessTokenVar="${UpperChannel}_ACCESS_TOKEN" + ChannelIDVar="${UpperChannel}_CHANNEL_ID" + + if [[ -z "${!AccessTokenVar:-}" ]] || [[ -z "${!ChannelIDVar:-}" ]]; then + printf "The ${slack_channel} notification channel is enabled, but required configuration variables are missing. Slack notifications will not be sent.\n" + + remove_channel slack + return 1 + fi + + AccessToken="${!AccessTokenVar}" # e.g. SLACK_ACCESS_TOKEN=some-token + ChannelID="${!ChannelIDVar}" # e.g. CHANNEL_ID=mychannel SlackUrl="https://slack.com/api/chat.postMessage" curl -S -o /dev/null ${CurlArgs} \ diff --git a/notify_templates/notify_smtp.sh b/notify_templates/notify_smtp.sh index 9cfc76c..41d2895 100644 --- a/notify_templates/notify_smtp.sh +++ b/notify_templates/notify_smtp.sh @@ -1,5 +1,5 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_SMTP_VERSION="v0.4" +NOTIFY_SMTP_VERSION="v0.5" # INFO: ssmtp is depcerated - consider to use msmtp instead. # # mSMTP/sSMTP has to be installed and configured manually. @@ -7,12 +7,6 @@ NOTIFY_SMTP_VERSION="v0.4" # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set SMTP_MAIL_FROM, SMTP_MAIL_TO, and SMTP_SUBJECT_TAG in your dockcheck.config file. -if [[ -z "${SMTP_MAIL_FROM:-}" ]] || [[ -z "${SMTP_MAIL_TO:-}" ]] || [[ -z "${SMTP_SUBJECT_TAG:-}" ]]; then - printf "SMTP notification channel enabled, but required configuration variables are missing. SMTP notifications will not be sent.\n" - - remove_channel smtp -fi - MSMTP=$(which msmtp) SSMTP=$(which ssmtp) SENDMAIL=$(which sendmail) @@ -28,9 +22,28 @@ else fi trigger_smtp_notification() { -SendMailFrom="${SMTP_MAIL_FROM}" # e.g. MAIL_FROM=me@mydomain.tld -SendMailTo="${SMTP_MAIL_TO}" # e.g. MAIL_TO=me@mydomain.tld -SubjectTag="${SMTP_SUBJECT_TAG}" # e.g. SUBJECT_TAG=dockcheck + if [[ -n "$1" ]]; then + smtp_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$smtp_channel") + else + smtp_channel="smtp" + UpperChannel="SMTP" + fi + + SendMailFromVar="${UpperChannel}_MAIL_FROM" + SendMailToVar="${UpperChannel}_MAIL_TO" + SubjectTagVar="${UpperChannel}_SUBJECT_TAG" + + if [[ -z "${!SendMailFromVar:-}" ]] || [[ -z "${!SendMailToVar:-}" ]] || [[ -z "${!SubjectTagVar:-}" ]]; then + printf "The ${smtp_channel} notification channel is enabled, but required configuration variables are missing. SMTP notifications will not be sent.\n" + + remove_channel smtp + return 1 + fi + + SendMailFrom="${!SendMailFromVar}" # e.g. MAIL_FROM=me@mydomain.tld + SendMailTo="${!SendMailToVar}" # e.g. MAIL_TO=me@mydomain.tld + SubjectTag="${!SubjectTagVar}" # e.g. SUBJECT_TAG=dockcheck $MailPkg $SendMailTo << __EOF From: "$FromHost" <$SendMailFrom> diff --git a/notify_templates/notify_telegram.sh b/notify_templates/notify_telegram.sh index 4f114eb..b42352c 100644 --- a/notify_templates/notify_telegram.sh +++ b/notify_templates/notify_telegram.sh @@ -1,28 +1,41 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_TELEGRAM_VERSION="v0.4" +NOTIFY_TELEGRAM_VERSION="v0.5" # # Required receiving services must already be set up. # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. # If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script. # Do not modify this file directly within the "notify_templates" subdirectory. Set TELEGRAM_CHAT_ID and TELEGRAM_TOKEN in your dockcheck.config file. -if [[ -z "${TELEGRAM_CHAT_ID:-}" ]] || [[ -z "${TELEGRAM_TOKEN:-}" ]]; then - printf "Telegram notification channel enabled, but required configuration variables are missing. Telegram notifications will not be sent.\n" - - remove_channel telegram -fi - trigger_telegram_notification() { + if [[ -n "$1" ]]; then + telegram_channel="$1" + UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$telegram_channel") + else + telegram_channel="telegram" + UpperChannel="TELEGRAM" + fi + + TelegramTokenVar="${UpperChannel}_TOKEN" + TelegramChatIdVar="${UpperChannel}_CHAT_ID" + TelegramTopicIdVar="${UpperChannel}_TOPIC_ID" + + if [[ -z "${!TelegramChatIdVar:-}" ]] || [[ -z "${!TelegramTokenVar:-}" ]]; then + printf "The ${telegram_channel} notification channel is enabled, but required configuration variables are missing. Telegram notifications will not be sent.\n" + + remove_channel telegram + return 1 + fi + if [[ "$PrintMarkdownURL" == true ]]; then ParseMode="Markdown" else ParseMode="HTML" fi - TelegramToken="${TELEGRAM_TOKEN}" # e.g. TELEGRAM_TOKEN=token-value - TelegramChatId="${TELEGRAM_CHAT_ID}" # e.g. TELEGRAM_CHAT_ID=mychatid + TelegramToken="${!TelegramTokenVar}" # e.g. TELEGRAM_TOKEN=token-value + TelegramChatId="${!TelegramChatIdVar}" # e.g. TELEGRAM_CHAT_ID=mychatid TelegramUrl="https://api.telegram.org/bot$TelegramToken" - TelegramTopicID=${TELEGRAM_TOPIC_ID:="0"} + TelegramTopicID=${!TelegramTopicIdVar:="0"} JsonData=$( "$jqbin" -n \ --arg chatid "$TelegramChatId" \ diff --git a/notify_templates/notify_v2.sh b/notify_templates/notify_v2.sh index d2c3503..436e312 100644 --- a/notify_templates/notify_v2.sh +++ b/notify_templates/notify_v2.sh @@ -1,4 +1,4 @@ -NOTIFY_V2_VERSION="v0.5" +NOTIFY_V2_VERSION="v0.6" # # If migrating from an older notify template, remove your existing notify.sh file. # Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script. @@ -13,9 +13,33 @@ NOTIFY_V2_VERSION="v0.5" # Actual snooze will be 60 seconds less to avoid the chance of missed notifications due to minor scheduling or script run time issues. snooze="${SNOOZE_SECONDS:-}" SnoozeFile="${ScriptWorkDir}/snooze.list" +[[ ! -f "${SnoozeFile}" ]] && touch "${SnoozeFile}" enabled_notify_channels=( ${NOTIFY_CHANNELS:-} ) +# Global output string variable for modification by functions +UpdToString="" +FormattedOutput="" + +get_channel_template() { + local UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$1") + local TemplateVar="${UpperChannel}_TEMPLATE" + if [[ -n "${!TemplateVar:-}" ]]; then + printf "${!TemplateVar}" + else + printf "$1" + fi +} + +declare -A unique_templates + +for channel in "${enabled_notify_channels[@]}"; do + template=$(get_channel_template "${channel}") + unique_templates["${template}"]=1 +done + +enabled_notify_templates=( "${!unique_templates[@]}" ) + FromHost=$(cat /etc/hostname) CurrentEpochTime=$(date +"%Y-%m-%dT%H:%M:%S") @@ -23,49 +47,96 @@ CurrentEpochSeconds=$(date +%s) NotifyError=false +for template in "${enabled_notify_templates[@]}"; do + source_if_exists_or_fail "${ScriptWorkDir}/notify_${template}.sh" || \ + source_if_exists_or_fail "${ScriptWorkDir}/notify_templates/notify_${template}.sh" || \ + printf "The notification channel template ${template} is enabled, but notify_${template}.sh was not found. Check the ${ScriptWorkDir} directory or the notify_templates subdirectory.\n" +done + +skip_snooze() { + local UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$1") + local SkipSnoozeVar="${UpperChannel}_SKIPSNOOZE" + if [[ "${!SkipSnoozeVar:-}" == "true" ]]; then + printf "true" + else + printf "false" + fi +} + +allow_empty() { + local UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$1") + local AllowEmptyVar="${UpperChannel}_ALLOWEMPTY" + if [[ "${!AllowEmptyVar:-}" == "true" ]]; then + printf "true" + else + printf "false" + fi +} + +containers_only() { + local UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$1") + local ContainersOnlyVar="${UpperChannel}_CONTAINERSONLY" + if [[ "${!ContainersOnlyVar:-}" == "true" ]]; then + printf "true" + else + printf "false" + fi +} + +output_format() { + local UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$1") + local OutputFormatVar="${UpperChannel}_OUTPUT" + if [[ -z "${!OutputFormatVar:-}" ]]; then + printf "text" + else + printf "${!OutputFormatVar:-}" + fi +} + remove_channel() { local temp_array=() for channel in "${enabled_notify_channels[@]}"; do - [[ "${channel}" != "$1" ]] && temp_array+=("${channel}") + local channel_template=$(get_channel_template "${channel}") + [[ "${channel_template}" != "$1" ]] && temp_array+=("${channel}") done enabled_notify_channels=( "${temp_array[@]}" ) } -for channel in "${enabled_notify_channels[@]}"; do - source_if_exists_or_fail "${ScriptWorkDir}/notify_${channel}.sh" || \ - source_if_exists_or_fail "${ScriptWorkDir}/notify_templates/notify_${channel}.sh" || \ - printf "The notification channel ${channel} is enabled, but notify_${channel}.sh was not found. Check the ${ScriptWorkDir} directory or the notify_templates subdirectory.\n" -done - -notify_containers_count() { - unset NotifyContainers - NotifyContainers=() - - [[ ! -f "${SnoozeFile}" ]] && touch "${SnoozeFile}" - - for update in "$@" - do - read -a container <<< "${update}" - found=$(grep -w "${container[0]}" "${SnoozeFile}" || printf "") - +is_snoozed() { + if [[ -n "${snooze}" ]] && [[ -f "${SnoozeFile}" ]]; then + local found=$(grep -w "$1" "${SnoozeFile}" || printf "") if [[ -n "${found}" ]]; then read -a arr <<< "${found}" CheckEpochSeconds=$(( $(date -d "${arr[1]}" +%s 2>/dev/null) + ${snooze} - 60 )) || CheckEpochSeconds=$(( $(date -f "%Y-%m-%d" -j "${arr[1]}" +%s) + ${snooze} - 60 )) if [[ "${CurrentEpochSeconds}" -gt "${CheckEpochSeconds}" ]]; then - NotifyContainers+=("${update}") + printf "false" + else + printf "true" fi else - NotifyContainers+=("${update}") + printf "false" + fi + else + printf "false" + fi +} + +unsnoozed_count() { + unset Unsnoozed + Unsnoozed=() + + for element in "$@" + do + read -a item <<< "${element}" + if [[ $(is_snoozed "${item[0]}") == "false" ]]; then + Unsnoozed+=("${element}") fi done - printf "${#NotifyContainers[@]}" + printf "${#Unsnoozed[@]}" } update_snooze() { - - [[ ! -f "${SnoozeFile}" ]] && touch "${SnoozeFile}" - for arg in "$@" do read -a entry <<< "${arg}" @@ -85,8 +156,6 @@ cleanup_snooze() { NotifyEntries=() switch="" - [[ ! -f "${SnoozeFile}" ]] && touch "${SnoozeFile}" - for arg in "$@" do read -a entry <<< "${arg}" @@ -105,57 +174,90 @@ cleanup_snooze() { done <<< "$(grep ${switch} '\.sh ' ${SnoozeFile})" } +format_output() { + local UpdateType="$1" + local OutputFormat="$2" + local FormattedTextTemplate="$3" + local tempcsv="" + tempcsv="${UpdToString// -> /,}" + tempcsv="${tempcsv//.sh /.sh,}" + + if [[ "${OutputFormat}" == "csv" ]]; then + if [[ -z "${UpdToString}" ]]; then + FormattedOutput="None" + else + FormattedOutput="${tempcsv}" + fi + elif [[ "${OutputFormat}" == "json" ]]; then + if [[ -z "${UpdToString}" ]]; then + FormattedOutput='{"updates": []}' + else + if [[ "${UpdateType}" == "container_update" ]]; then + # container updates case + FormattedOutput=$(jq --compact-output --null-input --arg updates "${tempcsv}" '($updates | split("\\n")) | map(split(",")) | {"updates": map({"container_name": .[0], "release_notes": .[1]})} | del(..|nulls)') + elif [[ "${UpdateType}" == "notify_update" ]]; then + # script updates case + FormattedOutput=$(jq --compact-output --null-input --arg updates "${tempcsv}" '($updates | split("\\n")) | map(split(",")) | {"updates": map({"script_name": .[0], "installed_version": .[1], "latest_version": .[2]})}') + elif [[ "${UpdateType}" == "dockcheck_update" ]]; then + # dockcheck update case + FormattedOutput=$(jq --compact-output --null-input --arg updates "${tempcsv}" '($updates | split("\\n")) | map(split(",")) | {"updates": map({"script_name": .[0], "installed_version": .[1], "latest_version": .[2], "release_notes": .[3]})}') + else + FormattedOutput="Invalid input" + fi + fi + else + if [[ -z "${UpdToString}" ]]; then + FormattedOutput="None" + else + if [[ "${UpdateType}" == "dockcheck_update" ]]; then + FormattedOutput="${FormattedTextTemplate//$4}" + FormattedOutput="${FormattedTextTemplate//$5}" + FormattedOutput="${FormattedTextTemplate//$6}" + else + FormattedOutput="${FormattedTextTemplate//${UpdToString}}" + fi + fi + fi +} + send_notification() { [[ -s "$ScriptWorkDir"/urls.list ]] && releasenotes || Updates=("$@") - if [[ -n "${snooze}" ]] && [[ -f "${SnoozeFile}" ]]; then - UpdNotifyCount=$(notify_containers_count "${Updates[@]}") - else - UpdNotifyCount="${#Updates[@]}" - fi - - if [[ "${enabled_notify_channels[@]}" == *"file"* ]]; then - UpdToString=$( printf '%s, ' "${Updates[@]}" ) - UpdToString="${UpdToString%, }" - - if [[ -z "${UpdToString}" ]]; then - UpdToString="None" - fi - - printf "\nSending file notification\n" - printf -v MessageBody "${UpdToString}" - - exec_if_exists_or_fail trigger_file_notification || \ - printf "Attempted to send notification to channel file, but the function was not found. Make sure notify_file.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory.\n" - - remove_channel file - fi - + UnsnoozedContainers=$(unsnoozed_count "${Updates[@]}") NotifyError=false + Notified="false" - if [[ "${UpdNotifyCount}" -gt 0 ]]; then - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - UpdToString=${UpdToString%\\n} + # To be added in the MessageBody if "-d X" was used + # Trailing space is left intentionally for clean output + [[ -n "$DaysOld" ]] && msgdaysold="with images ${DaysOld}+ days old " || msgdaysold="" + MessageTitle="$FromHost - updates ${msgdaysold}available." - for channel in "${enabled_notify_channels[@]}"; do - printf "\nSending ${channel} notification\n" + UpdToString=$( printf '%s\\n' "${Updates[@]}" ) + UpdToString="${UpdToString%, }" + UpdToString=${UpdToString%\\n} - # To be added in the MessageBody if "-d X" was used - # leading space is left intentionally for clean output - [[ -n "$DaysOld" ]] && msgdaysold="with images ${DaysOld}+ days old " || msgdaysold="" + for channel in "${enabled_notify_channels[@]}"; do + local template=$(get_channel_template "${channel}") - MessageTitle="$FromHost - updates ${msgdaysold}available." - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n${UpdToString}\n" + # Formats UpdToString variable per channel settings + format_output "container_update" "$(output_format "${channel}")" "🐋 Containers on $FromHost with updates available:\n\n" - exec_if_exists_or_fail trigger_${channel}_notification || \ - printf "Attempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${channel}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory.\n" - done + # Setting the MessageBody variable here. + printf -v MessageBody "${FormattedOutput}" - [[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && update_snooze "${Updates[@]}" + if { [[ $(skip_snooze "${channel}") == "true" ]] || [[ ${UnsnoozedContainers} -gt 0 ]] } && { { [[ "${MessageBody}" != "None" ]] && [[ "${MessageBody}" != '{"updates": []}' ]] } || [[ $(allow_empty "${channel}") == "true" ]] }; then + printf "\nSending ${channel} notification" + exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \ + printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory." + Notified="true" + fi + done + + if [[ "${Notified}" == "true" ]]; then + [[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && [[ "${FormattedOutput}" != "None" ]] && [[ "${Notified}" == "true" ]] && update_snooze "${Updates[@]}" + printf "\n" fi - - [[ -n "${snooze}" ]] && cleanup_snooze "${Updates[@]}" + [[ -n "${snooze}" ]] && [[ "${FormattedOutput}" != "None" ]] && cleanup_snooze "${Updates[@]}" return 0 } @@ -164,39 +266,30 @@ send_notification() { ### to not send notifications when dockcheck itself has updates. dockcheck_notification() { if [[ ! "${DISABLE_DOCKCHECK_NOTIFICATION:-}" == "true" ]]; then - DockcheckNotify=false NotifyError=false + Notified=false - if [[ -n "${snooze}" ]] && [[ -f "${SnoozeFile}" ]]; then - found=$(grep -w "dockcheck\.sh" "${SnoozeFile}" || printf "") - if [[ -n "${found}" ]]; then - read -a arr <<< "${found}" - CheckEpochSeconds=$(( $(date -d "${arr[1]}" +%s 2>/dev/null) + ${snooze} - 60 )) || CheckEpochSeconds=$(( $(date -f "%Y-%m-%d" -j "${arr[1]}" +%s) + ${snooze} - 60 )) - if [[ "${CurrentEpochSeconds}" -gt "${CheckEpochSeconds}" ]]; then - DockcheckNotify=true - fi - else - DockcheckNotify=true + MessageTitle="$FromHost - New version of dockcheck available." + + # Formats UpdToString variable per channel settings + format_output "dockcheck_update" "$(output_format "${channel}")" "Installed version: \nLatest version: \n\nChangenotes: \n" "$1" "$2" "$3" + + # Setting the MessageBody variable here. + printf -v MessageBody "${FormattedOutput}" + + for channel in "${enabled_notify_channels[@]}"; do + local template=$(get_channel_template "${channel}") + if { [[ $(skip_snooze "${channel}") == "true" ]] || [[ $(is_snoozed "dockcheck\.sh") == "false" ]] } && [[ $(containers_only "${channel}") == "false" ]] && { { [[ "${MessageBody}" != "None" ]] && [[ "${MessageBody}" != '{"updates": []}' ]] } || [[ $(allow_empty "${channel}") == "true" ]] }; then + printf "\nSending dockcheck update notification - ${channel}" + exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \ + printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory." + Notified="true" fi - else - DockcheckNotify=true - fi - - if [[ "${DockcheckNotify}" == "true" ]]; then - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1\nLatest version: $2\n\nChangenotes: $3\n" - - if [[ ${#enabled_notify_channels[@]} -gt 0 ]]; then printf "\n"; fi - for channel in "${enabled_notify_channels[@]}"; do - if [[ ! "${channel}" == "file" ]]; then - printf "Sending dockcheck update notification - ${channel}\n" - exec_if_exists_or_fail trigger_${channel}_notification || \ - printf "Attempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${channel}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory.\n" - fi - done + done + if [[ "${Notified}" == "true" ]]; then [[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && update_snooze "dockcheck.sh" + printf "\n" fi fi @@ -207,11 +300,11 @@ dockcheck_notification() { ### to not send notifications when notify scripts themselves have updates. notify_update_notification() { if [[ ! "${DISABLE_NOTIFY_NOTIFICATION:-}" == "true" ]]; then - NotifyUpdateNotify=false NotifyError=false NotifyUpdates=() + Notified=false - UpdateChannels=( "${enabled_notify_channels[@]}" "v2" ) + UpdateChannels=( "${enabled_notify_templates[@]}" "v2" ) for NotifyScript in "${UpdateChannels[@]}"; do UpperChannel=$(tr '[:lower:]' '[:upper:]' <<< "$NotifyScript") @@ -222,55 +315,45 @@ notify_update_notification() { LatestNotifyRelease="$(echo "$LatestNotifySnippet" | sed -n "/${VersionVar}/s/${VersionVar}=//p" | tr -d '"')" if [[ ! "${LatestNotifyRelease}" == "undefined" ]]; then if [[ "${!VersionVar}" != "${LatestNotifyRelease}" ]] ; then - NotifyUpdates+=("${NotifyScript}.sh ${!VersionVar} -> ${LatestNotifyRelease}") + NotifyUpdates+=("${NotifyScript}.sh ${!VersionVar} -> ${LatestNotifyRelease}") fi fi fi done - if [[ -n "${snooze}" ]] && [[ -f "${SnoozeFile}" ]]; then - for update in "${NotifyUpdates[@]}"; do - read -a NotifyScript <<< "${update}" - found=$(grep -w "${NotifyScript}" "${SnoozeFile}" || printf "") - if [[ -n "${found}" ]]; then - read -a arr <<< "${found}" - CheckEpochSeconds=$(( $(date -d "${arr[1]}" +%s 2>/dev/null) + ${snooze} - 60 )) || CheckEpochSeconds=$(( $(date -f "%Y-%m-%d" -j "${arr[1]}" +%s) + ${snooze} - 60 )) - if [[ "${CurrentEpochSeconds}" -gt "${CheckEpochSeconds}" ]]; then - NotifyUpdateNotify=true - fi - else - NotifyUpdateNotify=true - fi - done - else - NotifyUpdateNotify=true - fi + UnsnoozedTemplates=$(unsnoozed_count "${NotifyUpdates[@]}") - if [[ "${NotifyUpdateNotify}" == "true" ]]; then - if [[ "${#NotifyUpdates[@]}" -gt 0 ]]; then - UpdToString=$( printf '%s\\n' "${NotifyUpdates[@]}" ) - UpdToString=${UpdToString%\\n} - NotifyError=false + MessageTitle="$FromHost - New version of notify templates available." - MessageTitle="$FromHost - New version of notify templates available." + UpdToString=$( printf '%s\\n' "${NotifyUpdates[@]}" ) + UpdToString="${UpdToString%, }" + UpdToString=${UpdToString%\\n} - printf -v MessageBody "Notify templates on $FromHost with updates available:\n${UpdToString}\n" + for channel in "${enabled_notify_channels[@]}"; do + local template=$(get_channel_template "${channel}") - for channel in "${enabled_notify_channels[@]}"; do - if [[ ! "${channel}" == "file" ]]; then - printf "Sending notify template update notification - ${channel}\n" - exec_if_exists_or_fail trigger_${channel}_notification || \ - printf "Attempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${channel}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory.\n" - fi - done + # Formats UpdToString variable per channel settings + format_output "notify_update" "$(output_format "${channel}")" "Notify templates on $FromHost with updates available:\n\n" - [[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && update_snooze "${NotifyUpdates[@]}" + # Setting the MessageBody variable here. + printf -v MessageBody "${FormattedOutput}" + + if { [[ $(skip_snooze "${channel}") == "true" ]] || [[ ${UnsnoozedTemplates} -gt 0 ]] } && { { [[ "${MessageBody}" != "None" ]] && [[ "${MessageBody}" != '{"updates": []}' ]] } || [[ $(allow_empty "${channel}") == "true" ]] } && [[ $(containers_only "${channel}") == "false" ]]; then + printf "\nSending notify template update notification - ${channel}" + exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \ + printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory." + Notified="true" fi + done + + if [[ "${Notified}" == "true" ]]; then + [[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && [[ "${FormattedOutput}" != "None" ]] && [[ "${Notified}" == "true" ]] && update_snooze "${NotifyUpdates[@]}" + printf "\n" fi UpdatesPlusDockcheck=("${NotifyUpdates[@]}") UpdatesPlusDockcheck+=("dockcheck.sh") - [[ -n "${snooze}" ]] && cleanup_snooze "${UpdatesPlusDockcheck[@]}" + [[ -n "${snooze}" ]] && [[ "${FormattedOutput}" != "None" ]] && cleanup_snooze "${UpdatesPlusDockcheck[@]}" fi return 0