From c63e2441faf8ed6001a44ce256ae91eab71f807f Mon Sep 17 00:00:00 2001 From: vorezal <37914382+vorezal@users.noreply.github.com> Date: Sun, 25 May 2025 12:26:13 -0400 Subject: [PATCH] Refactor notifications and add helper functions (#182) * Refactor notifications and add helper functions * Add helper functions to simplify sourcing files and executing functions if they exist * Create notify_v2.sh wrapper script * Simplify and consolidate notification logic within notify_v2.sh * Support notification management via environment variables * Move secrets to dockcheck.config * Fix NOTIFY_CHANNELS default value when not set * Feedback changes * Remove leading spaces from MessageBody * Check for valid notify v2 variables * Warn on missing configuration and bypass notifications * Update readme * Additional feedback fixes * More comments in default.config with different # depth for comments and settings * Rename NOTIFY_TOPIC_NAME variable to NTFY_TOPIC_NAME for consistency * Add TELEGRAM_TOPIC_ID * Fix AppriseURL variable * Add an ending newline to all MessageBody statements for consistency * Remove troubleshooting echo statement * Prevent attempting to trigger notifications for notification templates if versions are the same --------- Co-authored-by: Matthew Oleksowicz --- README.md | 25 ++++++-- default.config | 60 +++++++++++++++++-- dockcheck.sh | 50 ++++++++++------ notify_templates/notify_DSM.sh | 52 +++-------------- notify_templates/notify_apprise.sh | 67 +++++++-------------- notify_templates/notify_discord.sh | 47 ++++----------- notify_templates/notify_generic.sh | 41 +------------ notify_templates/notify_gotify.sh | 73 +++++++---------------- notify_templates/notify_matrix.sh | 58 +++++------------- notify_templates/notify_ntfy-sh.sh | 68 ++++++---------------- notify_templates/notify_pushbullet.sh | 55 ++++-------------- notify_templates/notify_pushover.sh | 67 ++++++--------------- notify_templates/notify_slack.sh | 62 +++++--------------- notify_templates/notify_smtp.sh | 54 ++++------------- notify_templates/notify_telegram.sh | 67 ++++++--------------- notify_templates/notify_v2.sh | 84 +++++++++++++++++++++++++++ 16 files changed, 366 insertions(+), 564 deletions(-) create mode 100644 notify_templates/notify_v2.sh diff --git a/README.md b/README.md index 4e33e3d..27ef1ad 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,12 @@ ___ ## :bell: Changelog +- **v0.6.5**: Refactored notification logic. See notify_templates/notify_v2.sh for upgrade steps. + - Added helper functions to simplify sourcing files and executing functions if they exist. + - Created notify_v2.sh wrapper script. + - Simplified and consolidated notification logic within notify_v2.sh. + - Added support for notification management via environment variables. + - Moved notification secrets to dockcheck.config. - **v0.6.4**: Restructured the update process - first pulls all updates, then recreates all containers. - Added logic to skip update check on non-compose containers (unless `-r` option). - Added option `-F` to revert to `compose up -d ` targeting specific container and not the stack. @@ -126,11 +132,22 @@ Alternatively create an alias where specific flags and values are set. Example `alias dc=dockcheck.sh -p -x 10 -t 3`. ## :loudspeaker: Notifications -Trigger with the `-i` flag if `notify.sh` is present and configured. -Will send a list of containers with updates available and a notification when `dockcheck.sh` itself has an update. -Run it scheduled with `-ni` to only get notified when there's updates available! +Trigger with the `-i` flag. +If `notify.sh` is present and configured, it will be used. Otherwise, `notify_v2.sh` will be enabled. +Will send a list of containers with updates available and a notification when `dockcheck.sh` itself has an update. +Run it scheduled with `-ni` to only get notified when there's updates available! + +V2 installation and configuration (tag v0.6.5 or later): +Remove or rename `notify.sh` if previously configured using the legacy method. +Uncomment and set the NOTIFY_CHANNELS environment variable in `dockcheck.config` to a space separated string of your desired notification channels to enable. +Uncomment and set the environment variables related to the enabled notification channels. +It is recommended not to make changes directly to the `notify_X.sh` template files and to use only environment variables defined in `dockcheck.config` using this method. + +Legacy installation and configuration: +Use a previous version of a `notify_X.sh` template file (tag v0.6.4 or earlier) from the **notify_templates** directory, +copy it to `notify.sh` alongside the script, modify it to your needs! (notify.sh is added to .gitignore) + -Use a `notify_X.sh` template file from the **notify_templates** directory, copy it to `notify.sh` alongside the script, modify it to your needs! (notify.sh is added to .gitignore) **Current templates:** - Synology [DSM](https://www.synology.com/en-global/dsm) - Email with [mSMTP](https://wiki.debian.org/msmtp) (or deprecated alternative [sSMTP](https://wiki.debian.org/sSMTP)) diff --git a/default.config b/default.config index 406ccbf..9c13be0 100644 --- a/default.config +++ b/default.config @@ -1,9 +1,9 @@ ### Custom user variables -# Copy this file to "dockcheck.config" to make it active -# Can be placed in ~/.config/ or alongside dockcheck.sh -# -# Uncomment and set your preferred configuration variables here -# This will not be replaced on updates +## Copy this file to "dockcheck.config" to make it active +## Can be placed in ~/.config/ or alongside dockcheck.sh +## +## Uncomment and set your preferred configuration variables here +## This will not be replaced on updates #Timeout=10 # Set a timeout (in seconds) per container for registry checkups. #MaxAsync=10 # Set max asynchronous subprocesses, 1 default, 0 to disable. @@ -23,3 +23,53 @@ #PrintReleaseURL=true # Prints custom releasenote urls alongside each container with updates (requires urls.list)` #PrintMarkdownURL=true # Prints custom releasenote urls as markdown #OnlySpecific=true # Only compose up the specific container, not the whole compose. (useful for master-compose structure). + +### Notify settings +## 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 generic gotify matrix ntfy-sh pushbullet pushover slack smtp telegram" +# +## Uncomment to not send notifications when dockcheck itself has updates. +# DISABLE_DOCKCHECK_NOTIFICATION=false +## Uncomment to not send notifications when notify scripts themselves have updates. +# DISABLE_NOTIFY_NOTIFICATION=false +# +## Apprise configuration variables. Set APPRISE_PAYLOAD to make a CLI call or set APPRISE_URL to make an API request instead. +# APPRISE_PAYLOAD='mailto://myemail:mypass@gmail.com +# mastodons://{token}@{host} +# pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b +# tgram://{bot_token}/{chat_id}/' +# APPRISE_URL="http://apprise.mydomain.tld:1234/notify/apprise" +# +# DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/" +# +# DSM_SENDMAILTO="me@mydomain.com" +# DSM_SUBJECTTAG="Email Subject Prefix" +# +# GOTIFY_DOMAIN="https://gotify.domain.tld" +# GOTIFY_TOKEN="token-value" +# +# MATRIX_ACCESS_TOKEN="token-value" +# MATRIX_ROOM_ID="myroom" +# MATRIX_SERVER_URL="https://matrix.yourdomain.tld" +# +# NTFY_TOPIC_NAME="YourUniqueTopicName" +# +# PUSHBULLET_URL="https://api.pushbullet.com/v2/pushes" +# PUSHBULLET_TOKEN="token-value" +# +# PUSHOVER_URL="https://api.pushover.net/1/messages.json" +# PUSHOVER_USER_KEY="userkey" +# PUSHOVER_TOKEN="token-value" +# +# SLACK_CHANNEL_ID=mychannel +# SLACK_ACCESS_TOKEN=xoxb-token-value +# +# SMTP_MAIL_FROM="me@mydomain.tld" +# SMTP_MAIL_TO="you@yourdomain.tld" +# SMTP_SUBJECT_TAG="dockcheck" +# +# TELEGRAM_CHAT_ID="mychatid" +# TELEGRAM_TOKEN="token-value" +# TELEGRAM_TOPIC_ID="0" diff --git a/dockcheck.sh b/dockcheck.sh index 615fcd9..a0a5be2 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -VERSION="v0.6.4" -### ChangeNotes: Restructured update process - first pulls all images, then recreates all containers. Added -F option. +VERSION="v0.6.5" +### ChangeNotes: Refactored notification logic. See README.md for upgrade steps. Github="https://github.com/mag37/dockcheck" RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh" @@ -17,12 +17,17 @@ ScriptWorkDir="$(dirname "$ScriptPath")" LatestRelease="$(curl -s -r 0-50 "$RawUrl" | sed -n "/VERSION/s/VERSION=//p" | tr -d '"')" LatestChanges="$(curl -s -r 0-200 "$RawUrl" | sed -n "/ChangeNotes/s/# ChangeNotes: //p")" +# Source helper functions +source_if_exists() { + if [[ -s "$1" ]]; then source "$1"; fi +} + +source_if_exists_or_fail() { + [[ -s "$1" ]] && source "$1" +} + # User customizable defaults -if [[ -s "${HOME}/.config/dockcheck.config" ]]; then - source "${HOME}/.config/dockcheck.config" -elif [[ -s "${ScriptWorkDir}/dockcheck.config" ]]; then - source "${ScriptWorkDir}/dockcheck.config" -fi +source_if_exists_or_fail "${HOME}/.config/dockcheck.config" || source_if_exists "${ScriptWorkDir}/dockcheck.config" # Help Function Help() { @@ -120,14 +125,16 @@ shift "$((OPTIND-1))" # Set $1 to a variable for name filtering later SearchName="${1:-}" +# Basic notify configuration check +if [[ "${Notify}" == true ]] && [[ ! -s "${ScriptWorkDir}/notify.sh" ]] && [[ -z "${NOTIFY_CHANNELS:-}" ]]; then + printf "Using v2 notifications with -i flag passed but no notify channels configured in dockcheck.config. This will result in no notifications being sent.\n" +fi + # Setting up options and sourcing functions if [[ "$DontUpdate" == true ]]; then AutoMode=true; fi if [[ "$MonoMode" == true ]]; then declare c_{red,green,yellow,blue,teal,reset}=""; fi if [[ "$Notify" == true ]]; then - if [[ -s "${ScriptWorkDir}/notify.sh" ]]; then - source "${ScriptWorkDir}/notify.sh" - else Notify=false - fi + source_if_exists_or_fail "${ScriptWorkDir}/notify.sh" || source_if_exists_or_fail "${ScriptWorkDir}/notify_templates/notify_v2.sh" || Notify=false fi if [[ -n "$Exclude" ]]; then IFS=',' read -ra Excludes <<< "$Exclude" @@ -148,6 +155,14 @@ if [[ -n "$CollectorTextFileDirectory" ]]; then fi fi +exec_if_exists() { + if [[ $(type -t $1) == function ]]; then "$@"; fi +} + +exec_if_exists_or_fail() { + [[ $(type -t $1) == function ]] && "$@" +} + self_update_curl() { cp "$ScriptPath" "$ScriptPath".bak if command -v curl &>/dev/null; then @@ -335,10 +350,13 @@ if [[ "$VERSION" != "$LatestRelease" ]]; then [[ "$SelfUpdate" =~ [yY] ]] && self_update elif [[ "$AutoMode" == true ]] && [[ "$AutoSelfUpdate" == true ]]; then self_update; else - [[ "$Notify" == true ]] && { [[ $(type -t dockcheck_notification) == function ]] && dockcheck_notification "$VERSION" "$LatestRelease" "$LatestChanges" || printf "Could not source notification function.\n"; } + [[ "$Notify" == true ]] && { exec_if_exists_or_fail dockcheck_notification "$VERSION" "$LatestRelease" "$LatestChanges" || printf "Could not source notification function.\n"; } fi fi +# Version check for notify templates +[[ "$Notify" == true ]] && { exec_if_exists_or_fail notify_update_notification || printf "Could not source notify notification function.\n"; } + dependency_check "regctl" "regbin" "https://github.com/regclient/regclient/releases/latest/download/regctl-linux-TEMP" dependency_check "jq" "jqbin" "https://github.com/jqlang/jq/releases/latest/download/jq-linux-TEMP" @@ -457,11 +475,7 @@ unset IFS # Run the prometheus exporter function if [[ -n "${CollectorTextFileDirectory:-}" ]]; then - if type -t prometheus_exporter &>/dev/null; then - prometheus_exporter ${#NoUpdates[@]} ${#GotUpdates[@]} ${#GotErrors[@]} - else - printf "%s\n" "Could not source prometheus exporter function." - fi + exec_if_exists_or_fail prometheus_exporter ${#NoUpdates[@]} ${#GotUpdates[@]} ${#GotErrors[@]} || printf "%s\n" "Could not source prometheus exporter function." fi # Define how many updates are available @@ -481,7 +495,7 @@ if [[ -n ${GotUpdates[*]:-} ]]; then printf "\n%bContainers with updates available:%b\n" "$c_yellow" "$c_reset" if [[ -s "$ScriptWorkDir/urls.list" ]] && [[ "$PrintReleaseURL" == true ]]; then releasenotes; else Updates=("${GotUpdates[@]}"); fi [[ "$AutoMode" == false ]] && list_options || printf "%s\n" "${Updates[@]}" - [[ "$Notify" == true ]] && { type -t send_notification &>/dev/null && send_notification "${GotUpdates[@]}" || printf "\nCould not source notification function.\n"; } + [[ "$Notify" == true ]] && { exec_if_exists_or_fail send_notification "${GotUpdates[@]}" || printf "\nCould not source notification function.\n"; } fi # Optionally get updates if there's any diff --git a/notify_templates/notify_DSM.sh b/notify_templates/notify_DSM.sh index ddbc2c6..17d697f 100644 --- a/notify_templates/notify_DSM.sh +++ b/notify_templates/notify_DSM.sh @@ -1,11 +1,10 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_DSM_VERSION="v0.1" +NOTIFY_DSM_VERSION="v0.2" # INFO: ssmtp is deprecated - consider to use msmtp instead. # -# Copy/rename this file to notify.sh to enable the notification snipppet. # mSMTP/sSMTP has to be installed and configured manually. # The existing DSM Notification Email configuration will be used automatically. -# Modify to your liking - changing SendMailTo and Subject and content. +# Do not modify this file directly. Set DSM_SENDMAILTO and DSM_SUBJECTTAG in your dockcheck.config file. MSMTP=$(which msmtp) SSMTP=$(which ssmtp) @@ -18,18 +17,17 @@ else echo "No msmtp or ssmtp binary found in PATH: $PATH" ; exit 1 fi -FromHost=$(hostname) - -trigger_notification() { +trigger_DSM_notification() { 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 SendMailTo below. -SendMailTo=$(grep 'eventmail1' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p') -#SendMailTo="me@mydomain.com" +# 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')} +# e.g. DSM_SENDMAILTO="me@mydomain.com" -SubjectTag=$(grep 'eventsubjectprefix' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p') +SubjectTag=${DSM_SUBJECTTAG:-$(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') SenderMail=${SenderMail:-$(grep 'eventmail1' $CfgFile | sed -n 's/.*"\([^"]*\)".*/\1/p')} @@ -38,7 +36,7 @@ $MailPkg $SendMailTo << __EOF From: "$SenderName" <$SenderMail> date:$(date -R) To: <$SendMailTo> -Subject: $SubjectTag $MessageTitle $FromHost +Subject: $SubjectTag $MessageTitle Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit @@ -48,35 +46,3 @@ __EOF # This ensures DSM's container manager will also see the update /var/packages/ContainerManager/target/tool/image_upgradable_checker } - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - printf "\nSending email notification.\n" - - MessageTitle="Updates available on" - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending email dockcheck notification.\n" - - MessageTitle="New version of dockcheck available on" - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1\nLatest version: $2\n\nChangenotes: $3\n" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_DSM.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_DSM_VERSION/s/NOTIFY_DSM_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_DSM_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_DSM.sh update avialable:\n $NOTIFY_DSM_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification -} diff --git a/notify_templates/notify_apprise.sh b/notify_templates/notify_apprise.sh index fef4303..e71a4df 100644 --- a/notify_templates/notify_apprise.sh +++ b/notify_templates/notify_apprise.sh @@ -1,55 +1,30 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_APPRISE_VERSION="v0.1" +NOTIFY_APPRISE_VERSION="v0.2" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Required receiving services must already be set up. -# Modify to fit your setup - if API, set AppriseURL to your Apprise ip/domain. +# Do not modify this file directly. Set APPRISE_PAYLOAD in your dockcheck.config file. +# If API, set APPRISE_URL instead. -FromHost=$(hostname) +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" -trigger_notification() { + remove_channel apprise +fi - ### Modify to fit your setup: - apprise -vv -t "$MessageTitle" -b "$MessageBody" \ - mailto://myemail:mypass@gmail.com \ - mastodons://{token}@{host} \ - pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b \ - tgram://{bot_token}/{chat_id}/ +trigger_apprise_notification() { - ### If you use the Apprise-API - Comment out the apprise command above. - ### Uncomment the AppriseURL and the curl-line below: - # AppriseURL="http://apprise.mydomain.tld:1234/notify/apprise" - # curl -X POST -F "title=$MessageTitle" -F "body=$MessageBody" -F "tags=all" $AppriseURL -} + if [[ -n "${APPRISE_PAYLOAD:-}" ]]; then + apprise -vv -t "$MessageTitle" -b "$MessageBody" \ + ${APPRISE_PAYLOAD} + fi -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) + # e.g. APPRISE_PAYLOAD='mailto://myemail:mypass@gmail.com + # mastodons://{token}@{host} + # pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b + # tgram://{bot_token}/{chat_id}/' - printf "\nSending Apprise notification\n" - - MessageTitle="$FromHost - updates available." - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending Apprise dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_apprise.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_APPRISE_VERSION/s/NOTIFY_APPRISE_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_APPRISE_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_apprise.sh update avialable:\n $NOTIFY_APPRISE_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification -} + if [[ -n "${APPRISE_URL:-}" ]]; then + AppriseURL="${APPRISE_URL}" + curl -X POST -F "title=$MessageTitle" -F "body=$MessageBody" -F "tags=all" $AppriseURL # e.g. APPRISE_URL=http://apprise.mydomain.tld:1234/notify/apprise + fi +} \ No newline at end of file diff --git a/notify_templates/notify_discord.sh b/notify_templates/notify_discord.sh index 5728f74..a28cda5 100644 --- a/notify_templates/notify_discord.sh +++ b/notify_templates/notify_discord.sh @@ -1,43 +1,18 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_DISCORD_VERSION="v0.1" +NOTIFY_DISCORD_VERSION="v0.2" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Required receiving services must already be set up. -# Modify to fit your setup - set DiscordWebhookUrl +# Do not modify this file directly. Set DISCORD_WEBHOOK_URL in your dockcheck.config file. -FromHost=$(hostname) +if [[ -z "${DISCORD_WEBHOOK_URL:-}" ]]; then + printf "Discord notification channel enabled, but required configuration variables are missing. Discord notifications will not be sent.\n" -trigger_notification() { - # Modify to fit your setup: - DiscordWebhookUrl="PasteYourFullDiscordWebhookURL" + remove_channel discord +fi - MsgBody="{\"username\":\"$FromHost\",\"content\":\"$MessageBody\"}" - curl -sS -o /dev/null --fail -X POST -H "Content-Type: application/json" -d "$MsgBody" "$DiscordWebhookUrl" -} - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - printf "\nSending Discord notification\n" - # Setting the MessageBody variable here. - MessageBody="🐋 Containers on $FromHost with updates available: \n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending Discord dockcheck notification\n" - MessageBody="$FromHost - New version of dockcheck available: \n Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_discord.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_DISCORD_VERSION/s/NOTIFY_DISCORD_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_DISCORD_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_discord.sh update avialable:\n $NOTIFY_DISCORD_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification +trigger_discord_notification() { + DiscordWebhookUrl="${DISCORD_WEBHOOK_URL}" # e.g. DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/ + + MsgBody="{\"username\":\"$FromHost\",\"content\":\"$MessageBody\"}" + curl -sS -o /dev/null --fail -X POST -H "Content-Type: application/json" -d "$MsgBody" "$DiscordWebhookUrl" } diff --git a/notify_templates/notify_generic.sh b/notify_templates/notify_generic.sh index 700e84a..cbcbf6e 100644 --- a/notify_templates/notify_generic.sh +++ b/notify_templates/notify_generic.sh @@ -1,44 +1,9 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_GENERIC_VERSION="v0.1" +NOTIFY_GENERIC_VERSION="v0.2" # -# Copy/rename this file to notify.sh to enable the notification snippet. # generic sample, the "Hello World" of notification addons -FromHost=$(hostname) - -trigger_notification() { - # Modify to fit your setup: +trigger_generic_notification() { printf "\n$MessageTitle\n" printf "\n$MessageBody\n" -} - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - # platform specific notification code would go here - printf "\n%bGeneric notification addon:%b" "$c_green" "$c_reset" - MessageTitle="$FromHost - updates available." - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nGeneric dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_generic.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_GENERIC_VERSION/s/NOTIFY_GENERIC_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_GENERIC_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_generic.sh update avialable:\n $NOTIFY_GENERIC_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification -} +} \ No newline at end of file diff --git a/notify_templates/notify_gotify.sh b/notify_templates/notify_gotify.sh index 94d1d97..f66e7e8 100644 --- a/notify_templates/notify_gotify.sh +++ b/notify_templates/notify_gotify.sh @@ -1,61 +1,30 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_GOTIFY_VERSION="v0.2" +NOTIFY_GOTIFY_VERSION="v0.3" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Required receiving services must already be set up. -# Modify to fit your setup - set GotifyUrl and GotifyToken. +# Do not modify this file directly. Set GOTIFY_TOKEN and GOTIFY_DOMAIN in your dockcheck.config file. -FromHost=$(hostname) +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" -trigger_notification() { - # Modify to fit your setup: - GotifyToken="Your Gotify token here" - GotifyUrl="https://api.gotify/message?token=${GotifyToken}" + remove_channel gotify +fi - if [[ "$PrintMarkdownURL" == true ]]; then - ContentType="text/markdown" - else - ContentType="text/plain" - 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 - JsonData=$( jq -n \ - --arg body "$MessageBody" \ - --arg title "$MessageTitle" \ - --arg type "$ContentType" \ - '{message: $body, title: $title, priority: 5, extras: {"client::display": {"contentType": $type}}}' ) + if [[ "$PrintMarkdownURL" == true ]]; then + ContentType="text/markdown" + else + ContentType="text/plain" + fi - curl -s -S --data "${JsonData}" -H 'Content-Type: application/json' -X POST "${GotifyUrl}" 1> /dev/null -} - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - # platform specific notification code would go here - printf "\nSending Gotify notification\n" - - # Setting the MessageTitle and MessageBody variable here. - MessageTitle="${FromHost} - updates available." - printf -v MessageBody "Containers on $FromHost with updates available:\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending Gotify dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_gotify.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_GOTIFY_VERSION/s/NOTIFY_GOTIFY_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_GOTIFY_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_gotify.sh update avialable:\n $NOTIFY_GOTIFY_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification + JsonData=$( jq -n \ + --arg body "$MessageBody" \ + --arg title "$MessageTitle" \ + --arg type "$ContentType" \ + '{message: $body, title: $title, priority: 5, extras: {"client::display": {"contentType": $type}}}' ) + + curl -s -S --data "${JsonData}" -H 'Content-Type: application/json' -X POST "${GotifyUrl}" 1> /dev/null } diff --git a/notify_templates/notify_matrix.sh b/notify_templates/notify_matrix.sh index 686a4b1..87215ae 100644 --- a/notify_templates/notify_matrix.sh +++ b/notify_templates/notify_matrix.sh @@ -1,51 +1,21 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_MATRIX_VERSION="v0.1" +NOTIFY_MATRIX_VERSION="v0.2" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Required receiving services must already be set up. -# Modify to fit your setup - set MatrixServer, Room_id and AccessToken +# Do not modify this file directly. Set MATRIX_ACCESS_TOKEN, MATRIX_ROOM_ID, and MATRIX_SERVER_URL in your dockcheck.config file. -FromHost=$(hostname) +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" -trigger_notification() { - # Modify to fit your setup: - AccessToken="Your Matrix token here" - Room_id="Enter Room_id here" - MatrixServer="Enter Your HomeServer URL" - MsgBody="{\"msgtype\":\"m.text\",\"body\":\"$MessageBody\"}" + remove_channel matrix +fi - # URL Example: https://matrix.org/_matrix/client/r0/rooms/!xxxxxx:example.com/send/m.room.message?access_token=xxxxxxxx - curl -sS -o /dev/null --fail -X POST "$MatrixServer/_matrix/client/r0/rooms/$Room_id/send/m.room.message?access_token=$AccessToken" -H 'Content-Type: application/json' -d "$MsgBody" -} +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 + MsgBody="{\"msgtype\":\"m.text\",\"body\":\"$MessageBody\"}" -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - # platform specific notification code would go here - printf "\nSending Matrix notification\n" - - # Setting the MessageBody variable here. - MessageBody="🐋 Containers on $FromHost with updates available: \n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending Matrix dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_matrix.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_MATRIX_VERSION/s/NOTIFY_MATRIX_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_MATRIX_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_matrix.sh update avialable:\n $NOTIFY_MATRIX_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification -} + # URL Example: https://matrix.org/_matrix/client/r0/rooms/!xxxxxx:example.com/send/m.room.message?access_token=xxxxxxxx + curl -sS -o /dev/null --fail -X POST "$MatrixServer/_matrix/client/r0/rooms/$Room_id/send/m.room.message?access_token=$AccessToken" -H 'Content-Type: application/json' -d "$MsgBody" +} \ No newline at end of file diff --git a/notify_templates/notify_ntfy-sh.sh b/notify_templates/notify_ntfy-sh.sh index 996f275..0b5cc3a 100644 --- a/notify_templates/notify_ntfy-sh.sh +++ b/notify_templates/notify_ntfy-sh.sh @@ -1,57 +1,27 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_NTFYSH_VERSION="v0.2" +NOTIFY_NTFYSH_VERSION="v0.3" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Setup app and subscription at https://ntfy.sh -# Use your unique Topic Name in the URL below. +# Do not modify this file directly. Set NTFY_TOPIC_NAME in your dockcheck.config file. -FromHost=$(hostname) +if [[ -z "${NTFY_TOPIC_NAME:-}" ]]; then + printf "Ntfy.sh notification channel enabled, but required configuration variables are missing. Ntfy.sh notifications will not be sent.\n" -trigger_notification() { - # Modify to fit your setup: - NtfyUrl="ntfy.sh/YourUniqueTopicName" + remove_channel ntfy-sh +fi - if [[ "$PrintMarkdownURL" == true ]]; then - ContentType="Markdown: yes" - else - ContentType="Markdown: no" #text/plain - fi +trigger_ntfy-sh_notification() { + NtfyUrl="ntfy.sh/${NTFY_TOPIC_NAME}" # e.g. NTFY_TOPIC_NAME=YourUniqueTopicName - curl -sS -o /dev/null --show-error --fail \ - -H "Title: $MessageTitle" \ - -H "$ContentType" \ - -d "$MessageBody" \ - "$NtfyUrl" -} - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - printf "\nSending ntfy.sh notification\n" - - MessageTitle="$FromHost - updates available." - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending ntfy.sh dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_ntfy-sh.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_NTFYSH_VERSION/s/NOTIFY_NTFYSH_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_NTFYSH_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_ntfy-sh.sh update avialable:\n $NOTIFY_NTFYSH_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification + if [[ "$PrintMarkdownURL" == true ]]; then + ContentType="Markdown: yes" + else + ContentType="Markdown: no" #text/plain + fi + + curl -sS -o /dev/null --show-error --fail \ + -H "Title: $MessageTitle" \ + -H "$ContentType" \ + -d "$MessageBody" \ + "$NtfyUrl" } diff --git a/notify_templates/notify_pushbullet.sh b/notify_templates/notify_pushbullet.sh index 0df62cb..4bad2ff 100644 --- a/notify_templates/notify_pushbullet.sh +++ b/notify_templates/notify_pushbullet.sh @@ -1,51 +1,20 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_PUSHBULLET_VERSION="v0.1" +NOTIFY_PUSHBULLET_VERSION="v0.2" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Required receiving services must already be set up. # Requires jq installed and in PATH. -# Modify to fit your setup - set Url and Token. +# Do not modify this file directly. Set PUSHBULLET_TOKEN and PUSHBULLET_URL in your dockcheck.config file. -FromHost=$(hostname) +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" -trigger_notification() { - # Modify to fit your setup: - PushUrl="https://api.pushbullet.com/v2/pushes" - PushToken="Your Pushbullet token here" + remove_channel pushbullet +fi - # Requires jq to process json data - jq -n --arg title "$MessageTitle" --arg body "$MessageBody" '{body: $body, title: $title, type: "note"}' | curl -sS -o /dev/null --show-error --fail -X POST -H "Access-Token: $PushToken" -H "Content-type: application/json" $PushUrl -d @- -} +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 -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - # platform specific notification code would go here - printf "\nSending pushbullet notification\n" - - MessageTitle="$FromHost - updates available." - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending pushbullet dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_pushbullet.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_PUSHBULLET_VERSION/s/NOTIFY_PUSHBULLET_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_PUSHBULLET_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_pushbullet.sh update avialable:\n $NOTIFY_PUSHBULLET_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification -} + # Requires jq to process json data + jq -n --arg title "$MessageTitle" --arg body "$MessageBody" '{body: $body, title: $title, type: "note"}' | curl -sS -o /dev/null --show-error --fail -X POST -H "Access-Token: $PushToken" -H "Content-type: application/json" $PushUrl -d @- +} \ No newline at end of file diff --git a/notify_templates/notify_pushover.sh b/notify_templates/notify_pushover.sh index dc28493..16fff6a 100644 --- a/notify_templates/notify_pushover.sh +++ b/notify_templates/notify_pushover.sh @@ -1,57 +1,26 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_PUSHOVER_VERSION="v0.1" +NOTIFY_PUSHOVER_VERSION="v0.2" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Required receiving services must already be set up. # Requires jq installed and in PATH. -# Modify to fit your setup - set Url and Token. +# Do not modify this file directly. Set PUSHOVER_USER_KEY, PUSHOVER_TOKEN, and PUSHOVER_URL in your dockcheck.config file. -FromHost=$(hostname) +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" -trigger_notification() { - # Modify to fit your setup: - PushoverUrl="https://api.pushover.net/1/messages.json" - PushoverUserKey="Your Pushover User Key Here" - PushoverToken="Your Pushover API Token Here" + remove_channel pushover +fi - # Sending the notification via Pushover - curl -sS -o /dev/null --show-error --fail -X POST \ - -F "token=$PushoverToken" \ - -F "user=$PushoverUserKey" \ - -F "title=$MessageTitle" \ - -F "message=$MessageBody" \ - $PushoverUrl -} +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 -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - # platform specific notification code would go here - printf "\nSending pushover notification\n" - - MessageTitle="$FromHost - updates available." - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending pushover dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_pushover.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_PUSHOVER_VERSION/s/NOTIFY_PUSHOVER_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_PUSHOVER_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_pushover.sh update avialable:\n $NOTIFY_PUSHOVER_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification -} + # Sending the notification via Pushover + curl -sS -o /dev/null --show-error --fail -X POST \ + -F "token=$PushoverToken" \ + -F "user=$PushoverUserKey" \ + -F "title=$MessageTitle" \ + -F "message=$MessageBody" \ + $PushoverUrl +} \ No newline at end of file diff --git a/notify_templates/notify_slack.sh b/notify_templates/notify_slack.sh index 56ed28b..6dc3b28 100644 --- a/notify_templates/notify_slack.sh +++ b/notify_templates/notify_slack.sh @@ -1,52 +1,22 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_SLACK_VERSION="v0.1" +NOTIFY_SLACK_VERSION="v0.2" # -# Copy/rename this file to notify.sh in the same directory as dockcheck.sh to enable the notification snippet. -# Setu app and token at https://api.slack.com/tutorials/tracks/posting-messages-with-curl -# Add your AccessToken and ChannelID below +# Setup app and token at https://api.slack.com/tutorials/tracks/posting-messages-with-curl +# Do not modify this file directly. Set SLACK_ACCESS_TOKEN, and SLACK_CHANNEL_ID in your dockcheck.config file. -FromHost=$(hostname) +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" -trigger_notification() { - # Modify to fit your setup: - AccessToken="xoxb-not-a-real-token-this-will-not-work" - ChannelID="C123456" - SlackUrl="https://slack.com/api/chat.postMessage" + remove_channel slack +fi - curl -sS -o /dev/null --show-error --fail \ - -d "text=$MessageBody" -d "channel=$ChannelID" \ - -H "Authorization: Bearer $AccessToken" \ - -X POST $SlackUrl -} - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - printf "\nSending Slack notification\n" - - MessageTitle="$FromHost - updates available." - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending Slack dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_slack.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_SLACK_VERSION/s/NOTIFY_SLACK_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_SLACK_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_slack.sh update avialable:\n $NOTIFY_SLACK_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification +trigger_slack_notification() { + AccessToken="${SLACK_ACCESS_TOKEN}" # e.g. SLACK_ACCESS_TOKEN=some-token + ChannelID="${SLACK_CHANNEL_ID}" # e.g. CHANNEL_ID=mychannel + SlackUrl="https://slack.com/api/chat.postMessage" + + curl -sS -o /dev/null --show-error --fail \ + -d "text=$MessageBody" -d "channel=$ChannelID" \ + -H "Authorization: Bearer $AccessToken" \ + -X POST $SlackUrl } diff --git a/notify_templates/notify_smtp.sh b/notify_templates/notify_smtp.sh index 55e325b..8573640 100644 --- a/notify_templates/notify_smtp.sh +++ b/notify_templates/notify_smtp.sh @@ -1,10 +1,15 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_SMTP_VERSION="v0.1" +NOTIFY_SMTP_VERSION="v0.2" # INFO: ssmtp is depcerated - consider to use msmtp instead. # -# Copy/rename this file to notify.sh to enable the notification snipppet. # mSMTP/sSMTP has to be installed and configured manually. -# Modify to fit your setup - changing SendMailFrom, SendMailTo, SubjectTag +# Do not modify this file directly. 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) @@ -17,13 +22,10 @@ else echo "No msmtp or ssmtp binary found in PATH: $PATH" ; exit 1 fi -FromHost=$(hostname) - -trigger_notification() { -# User variables: -SendMailFrom="me@mydomain.tld" -SendMailTo="me@mydomain.tld" -SubjectTag="dockcheck" +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 $MailPkg $SendMailTo << __EOF From: "$FromHost" <$SendMailFrom> @@ -37,35 +39,3 @@ $MessageBody __EOF } - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - printf "\nSending email notification.\n" - - MessageTitle="Updates available on" - # Setting the MessageBody variable here. - printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n\n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending email dockcheck notification.\n" - - MessageTitle="New version of dockcheck available on" - # Setting the MessageBody variable here. - printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_smtp.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_SMTP_VERSION/s/NOTIFY_SMTP_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_SMTP_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_smtp.sh update avialable:\n $NOTIFY_SMTP_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification -} diff --git a/notify_templates/notify_telegram.sh b/notify_templates/notify_telegram.sh index 0850e51..1230524 100644 --- a/notify_templates/notify_telegram.sh +++ b/notify_templates/notify_telegram.sh @@ -1,58 +1,27 @@ ### DISCLAIMER: This is a third party addition to dockcheck - best effort testing. -NOTIFY_TELEGRAM_VERSION="v0.1" +NOTIFY_TELEGRAM_VERSION="v0.2" # -# Copy/rename this file to notify.sh to enable the notification snippet. # Required receiving services must already be set up. -# Modify to fit your setup - set TelegramChatId and TelegramToken. +# Do not modify this file directly. Set TELEGRAM_CHAT_ID and TELEGRAM_TOKEN in your dockcheck.config file. -FromHost=$(hostname) +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" -trigger_notification() { + remove_channel telegram +fi - if [[ "$PrintMarkdownURL" == true ]]; then - ParseMode="Markdown" - else - ParseMode="HTML" - fi +trigger_telegram_notification() { + if [[ "$PrintMarkdownURL" == true ]]; then + ParseMode="Markdown" + else + ParseMode="HTML" + fi - # Modify to fit your setup: - TelegramToken="Your Telegram token here" - TelegramChatId="Your Telegram ChatId here" - TelegramUrl="https://api.telegram.org/bot$TelegramToken" - TelegramTopicID=12345678 ## Set to 0 if not using specific topic within chat - TelegramData="{\"chat_id\":\"$TelegramChatId\",\"text\":\"$MessageBody\",\"message_thread_id\":\"$TelegramTopicID\",\"disable_notification\": false}" + TelegramToken="${TELEGRAM_TOKEN}" # e.g. TELEGRAM_TOKEN=token-value + TelegramChatId="${TELEGRAM_CHAT_ID}" # e.g. TELEGRAM_CHAT_ID=mychatid + TelegramUrl="https://api.telegram.org/bot$TelegramToken" + TelegramTopicID=${TELEGRAM_TOPIC_ID:="0"} + TelegramData="{\"chat_id\":\"$TelegramChatId\",\"text\":\"$MessageBody\",\"message_thread_id\":\"$TelegramTopicID\",\"disable_notification\": false}" - curl -sS -o /dev/null --fail -X POST "$TelegramUrl/sendMessage" -H 'Content-Type: application/json' -d "$TelegramData" -} - -send_notification() { - [ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@") - UpdToString=$( printf '%s\\n' "${Updates[@]}" ) - - # platform specific notification code would go here - printf "\nSending Telegram notification\n" - - # Setting the MessageBody variable here. - MessageBody="🐋 Containers on $FromHost with updates available: \n$UpdToString" - - trigger_notification -} - -### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function -### to not send notifications when dockcheck itself has updates. -dockcheck_notification() { - printf "\nSending Telegram dockcheck notification\n" - - MessageTitle="$FromHost - New version of dockcheck available." - # Setting the MessageBody variable here. - printf -v MessageBody "$FromHost - New version of dockcheck available.\n\nInstalled version: $1 \nLatest version: $2 \n\nChangenotes: $3" - - RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_telegram.sh" - LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_TELEGRAM_VERSION/s/NOTIFY_TELEGRAM_VERSION=//p" | tr -d '"')" - if [[ "$NOTIFY_TELEGRAM_VERSION" != "$LatestNotifyRelease" ]] ; then - printf -v NotifyUpdate "\n\nnotify_telegram.sh update avialable:\n $NOTIFY_TELEGRAM_VERSION -> $LatestNotifyRelease\n" - MessageBody="${MessageBody}${NotifyUpdate}" - fi - - trigger_notification + curl -sS -o /dev/null --fail -X POST "$TelegramUrl/sendMessage" -H 'Content-Type: application/json' -d "$TelegramData" } diff --git a/notify_templates/notify_v2.sh b/notify_templates/notify_v2.sh new file mode 100644 index 0000000..3beb5ef --- /dev/null +++ b/notify_templates/notify_v2.sh @@ -0,0 +1,84 @@ +NOTIFY_V2_VERSION="v0.1" +# +# If migrating from an older notify template, remove your existing notify.sh file. +# Enable and configure all required notification variables in your dockcheck.config file, e.g.: +# NOTIFY_CHANNELS=apprise gotify slack +# SLACK_TOKEN=xoxb-some-token-value +# GOTIFY_TOKEN=some.token + +enabled_notify_channels=( ${NOTIFY_CHANNELS:-} ) + +FromHost=$(hostname) + +remove_channel() { + local temp_array=() + for channel in "${enabled_notify_channels[@]}"; do + [[ "${channel}" != "$1" ]] && temp_array+=("${channel}") + done + enabled_notify_channels=( "${temp_array[@]}" ) +} + +for channel in "${enabled_notify_channels[@]}"; do + source_if_exists "${ScriptWorkDir}/notify_templates/notify_${channel}.sh" +done + +send_notification() { + [[ -s "$ScriptWorkDir"/urls.list ]] && releasenotes || Updates=("$@") + UpdToString=$( printf '%s\\n' "${Updates[@]}" ) + + for channel in "${enabled_notify_channels[@]}"; do + printf "\nSending ${channel} notification\n" + + MessageTitle="$FromHost - updates available." + # Setting the MessageBody variable here. + printf -v MessageBody "🐋 Containers on $FromHost with updates available:\n$UpdToString\n" + + exec_if_exists trigger_${channel}_notification "$@" + done +} + +### Set DISABLE_DOCKCHECK_NOTIFICATION=false in dockcheck.config +### to not send notifications when dockcheck itself has updates. +dockcheck_notification() { + if [[ ! "${DISABLE_DOCKCHECK_NOTIFICATION:-}" = "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 + printf "Sending dockcheck update notification - ${channel}\n" + exec_if_exists trigger_${channel}_notification + done + fi +} + +### Set DISABLE_NOTIFY_UPDATE_NOTIFICATION=false in dockcheck.config +### to not send notifications when notify scripts themselves have updates. +notify_update_notification() { + if [[ ! "${DISABLE_NOTIFY_UPDATE_NOTIFICATION:-}" = "true" ]]; then + update_channels=( "${enabled_notify_channels[@]}" "v2" ) + + for notify_script in "${update_channels[@]}"; do + upper_channel=$(tr '[:lower:]' '[:upper:]' <<< "$notify_script") + VersionVar="NOTIFY_${upper_channel}_VERSION" + if [[ -n "${!VersionVar}" ]]; then + RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_${notify_script}.sh" + LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_${upper_channel}_VERSION/s/NOTIFY_${upper_channel}_VERSION=//p" | tr -d '"')" + LatestNotifyRelease=${LatestNotifyRelease:-undefined} + if [[ ! "${LatestNotifyRelease}" = "undefined" ]]; then + if [[ "${!VersionVar}" != "$LatestNotifyRelease" ]] ; then + MessageTitle="$FromHost - New version of notify_${notify_script}.sh available." + + printf -v MessageBody "notify_${notify_script}.sh update available:\n ${!VersionVar} -> $LatestNotifyRelease\n" + + for channel in "${enabled_notify_channels[@]}"; do + printf "Sending notify_${notify_script}.sh update notification - ${channel}\n" + exec_if_exists trigger_${channel}_notification + done + fi + fi + fi + done + fi +}