2025-06-27 09:22:10 +02:00
NOTIFY_V2_VERSION = "v0.4"
2025-05-25 12:26:13 -04:00
#
# If migrating from an older notify template, remove your existing notify.sh file.
2025-05-29 16:43:34 -04:00
# Leave (or place) this file in the "notify_templates" subdirectory within the same directory as the main dockcheck.sh script.
2025-06-24 09:16:48 -04:00
# If you instead wish make your own modifications, make a copy in the same directory as the main dockcheck.sh script and rename to notify.sh.
2025-05-25 12:26:13 -04:00
# 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
2025-06-24 09:16:48 -04:00
# Number of seconds to snooze identical update notifications based on local image name
# or dockcheck.sh/notify.sh template file updates.
# 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 "
2025-05-25 12:26:13 -04:00
enabled_notify_channels = ( ${ NOTIFY_CHANNELS :- } )
2025-05-29 16:43:34 -04:00
FromHost = $( cat /etc/hostname)
2025-05-25 12:26:13 -04:00
2025-06-24 09:16:48 -04:00
CurrentEpochTime = $( date +"%Y-%m-%dT%H:%M:%S" )
CurrentEpochSeconds = $( date +%s)
NotifyError = false
2025-05-25 12:26:13 -04:00
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
2025-05-29 16:43:34 -04:00
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 "
2025-05-25 12:26:13 -04:00
done
2025-06-24 09:16:48 -04:00
notify_containers_count( ) {
unset NotifyContainers
NotifyContainers = ( )
2025-05-25 12:26:13 -04:00
2025-06-24 09:16:48 -04:00
[ [ ! -f " ${ SnoozeFile } " ] ] && touch " ${ SnoozeFile } "
for update in " $@ "
do
read -a container <<< " ${ update } "
found = $( grep -w " ${ container [0] } " " ${ 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 } " )
fi
else
NotifyContainers += ( " ${ update } " )
fi
done
printf " ${# NotifyContainers [@] } "
}
2025-05-25 12:26:13 -04:00
2025-06-24 09:16:48 -04:00
update_snooze( ) {
2025-05-29 16:43:34 -04:00
2025-06-24 09:16:48 -04:00
[ [ ! -f " ${ SnoozeFile } " ] ] && touch " ${ SnoozeFile } "
2025-05-25 12:26:13 -04:00
2025-06-24 09:16:48 -04:00
for arg in " $@ "
do
read -a entry <<< " ${ arg } "
found = $( grep -w " ${ entry [0] } " " ${ SnoozeFile } " || printf "" )
if [ [ -n " ${ found } " ] ] ; then
sed -e " s/ ${ entry [0] } .*/ ${ entry [0] } ${ CurrentEpochTime } / " " ${ SnoozeFile } " > " ${ SnoozeFile } .new "
mv " ${ SnoozeFile } .new " " ${ SnoozeFile } "
else
printf " ${ entry [0] } ${ CurrentEpochTime } \n " >> " ${ SnoozeFile } "
fi
2025-05-25 12:26:13 -04:00
done
}
2025-06-24 09:16:48 -04:00
cleanup_snooze( ) {
unset NotifyEntries
NotifyEntries = ( )
switch = ""
[ [ ! -f " ${ SnoozeFile } " ] ] && touch " ${ SnoozeFile } "
for arg in " $@ "
do
read -a entry <<< " ${ arg } "
NotifyEntries += ( " ${ entry [0] } " )
done
if [ [ ! " ${ NotifyEntries [@] } " = = *".sh" * ] ] ; then
switch = "-v"
fi
while read -r entry datestamp; do
if [ [ ! " ${ NotifyEntries [@] } " = = *" $entry " * ] ] ; then
sed -e " / ${ entry } /d " " ${ SnoozeFile } " > " ${ SnoozeFile } .new "
mv " ${ SnoozeFile } .new " " ${ SnoozeFile } "
fi
done <<< " $( grep ${ switch } '\.sh ' ${ SnoozeFile } ) "
}
send_notification( ) {
[ [ -s " $ScriptWorkDir " /urls.list ] ] && releasenotes || Updates = ( " $@ " )
if [ [ -n " ${ snooze } " ] ] && [ [ -f " ${ SnoozeFile } " ] ] ; then
UpdNotifyCount = $( notify_containers_count " ${ Updates [@] } " )
else
UpdNotifyCount = " ${# Updates [@] } "
fi
NotifyError = false
if [ [ " ${ UpdNotifyCount } " -gt 0 ] ] ; then
UpdToString = $( printf '%s\\n' " ${ Updates [@] } " )
UpdToString = ${ UpdToString % \\ n }
2025-05-25 12:26:13 -04:00
for channel in " ${ enabled_notify_channels [@] } " ; do
2025-06-24 09:16:48 -04:00
printf " \nSending ${ channel } notification\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 = ""
MessageTitle = " $FromHost - updates ${ msgdaysold } available. "
# Setting the MessageBody variable here.
printf -v MessageBody " 🐋 Containers on $FromHost with updates available:\n ${ UpdToString } \n "
2025-05-29 16:43:34 -04:00
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 "
2025-05-25 12:26:13 -04:00
done
2025-06-24 09:16:48 -04:00
[ [ -n " ${ snooze } " ] ] && [ [ " ${ NotifyError } " = = "false" ] ] && update_snooze " ${ Updates [@] } "
fi
[ [ -n " ${ snooze } " ] ] && cleanup_snooze " ${ Updates [@] } "
2025-06-27 03:10:31 -04:00
return 0
2025-06-24 09:16:48 -04:00
}
### 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
DockcheckNotify = false
NotifyError = 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
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
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 "
done
if [ [ -n " ${ snooze } " ] ] && [ [ -f " ${ SnoozeFile } " ] ] ; then
if [ [ " ${ NotifyError } " = = "false" ] ] ; then
if [ [ -n " ${ found } " ] ] ; then
sed -e " s/dockcheck\.sh.*/dockcheck\.sh ${ CurrentEpochTime } / " " ${ SnoozeFile } " > " ${ SnoozeFile } .new "
mv " ${ SnoozeFile } .new " " ${ SnoozeFile } "
else
printf " dockcheck.sh ${ CurrentEpochTime } \n " >> " ${ SnoozeFile } "
fi
fi
fi
fi
2025-05-25 12:26:13 -04:00
fi
2025-06-27 03:10:31 -04:00
return 0
2025-05-25 12:26:13 -04:00
}
2025-06-27 03:10:31 -04:00
### Set DISABLE_NOTIFY_NOTIFICATION=false in dockcheck.config
2025-05-25 12:26:13 -04:00
### to not send notifications when notify scripts themselves have updates.
notify_update_notification( ) {
2025-06-27 03:10:31 -04:00
if [ [ ! " ${ DISABLE_NOTIFY_NOTIFICATION :- } " = = "true" ] ] ; then
2025-06-24 09:16:48 -04:00
NotifyUpdateNotify = false
NotifyError = false
2025-06-27 03:10:31 -04:00
NotifyUpdates = ( )
2025-05-25 12:26:13 -04:00
2025-06-24 09:16:48 -04:00
UpdateChannels = ( " ${ enabled_notify_channels [@] } " "v2" )
for NotifyScript in " ${ UpdateChannels [@] } " ; do
UpperChannel = $( tr '[:lower:]' '[:upper:]' <<< " $NotifyScript " )
VersionVar = " NOTIFY_ ${ UpperChannel } _VERSION "
2025-05-29 16:43:34 -04:00
if [ [ -n " ${ !VersionVar :- } " ] ] ; then
2025-06-24 09:16:48 -04:00
RawNotifyUrl = " https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_ ${ NotifyScript } .sh "
LatestNotifySnippet = " $( curl ${ CurlArgs } -r 0-150 " $RawNotifyUrl " || printf "undefined" ) "
LatestNotifyRelease = " $( echo " $LatestNotifySnippet " | sed -n " / ${ VersionVar } /s/ ${ VersionVar } =//p " | tr -d '"' ) "
if [ [ ! " ${ LatestNotifyRelease } " = = "undefined" ] ] ; then
if [ [ " ${ !VersionVar } " != " ${ LatestNotifyRelease } " ] ] ; then
2025-06-27 03:10:31 -04:00
NotifyUpdates += ( " ${ NotifyScript } .sh ${ !VersionVar } -> ${ LatestNotifyRelease } " )
2025-05-25 12:26:13 -04:00
fi
fi
fi
done
2025-06-24 09:16:48 -04:00
if [ [ -n " ${ snooze } " ] ] && [ [ -f " ${ SnoozeFile } " ] ] ; then
2025-06-27 03:10:31 -04:00
for update in " ${ NotifyUpdates [@] } " ; do
2025-06-24 09:16:48 -04:00
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
if [ [ " ${ NotifyUpdateNotify } " = = "true" ] ] ; then
2025-06-27 03:10:31 -04:00
if [ [ " ${# NotifyUpdates [@] } " -gt 0 ] ] ; then
UpdToString = $( printf '%s\\n' " ${ NotifyUpdates [@] } " )
2025-06-24 09:16:48 -04:00
UpdToString = ${ UpdToString % \\ n }
NotifyError = false
MessageTitle = " $FromHost - New version of notify templates available. "
printf -v MessageBody " Notify templates on $FromHost with updates available:\n ${ UpdToString } \n "
for channel in " ${ enabled_notify_channels [@] } " ; do
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 "
done
2025-06-27 03:10:31 -04:00
[ [ -n " ${ snooze } " ] ] && [ [ " ${ NotifyError } " = = "false" ] ] && update_snooze " ${ NotifyUpdates [@] } "
2025-06-24 09:16:48 -04:00
fi
fi
2025-06-27 03:10:31 -04:00
UpdatesPlusDockcheck = ( " ${ NotifyUpdates [@] } " )
2025-06-24 09:16:48 -04:00
UpdatesPlusDockcheck += ( "dockcheck.sh" )
[ [ -n " ${ snooze } " ] ] && cleanup_snooze " ${ UpdatesPlusDockcheck [@] } "
2025-05-25 12:26:13 -04:00
fi
2025-06-27 03:10:31 -04:00
return 0
2025-05-25 12:26:13 -04:00
}