mirror of
https://github.com/mag37/dockcheck.git
synced 2026-04-18 05:39:06 +02:00
Make buffer print and notification generic. Add print_buffer notification arguments.
This commit is contained in:
parent
bae266a58d
commit
37f81de556
2 changed files with 33 additions and 24 deletions
21
dockcheck.sh
21
dockcheck.sh
|
|
@ -218,7 +218,7 @@ log_print() {
|
||||||
|
|
||||||
if [[ $levelnum -ge $LOG_LEVEL_NUM ]]; then
|
if [[ $levelnum -ge $LOG_LEVEL_NUM ]]; then
|
||||||
# Don't print if in quiet mode
|
# Don't print if in quiet mode
|
||||||
[[ "$Quiet" == "false" ]] && printf "${format}\n" "$@"
|
[[ "$Quiet" == "false" ]] && printf "${format}" "$@"
|
||||||
# Also log to buffer and file if configured
|
# Also log to buffer and file if configured
|
||||||
log "$logvar" "$loglevel" "$format" "$@"
|
log "$logvar" "$loglevel" "$format" "$@"
|
||||||
fi
|
fi
|
||||||
|
|
@ -240,7 +240,10 @@ log() {
|
||||||
|
|
||||||
# optionally output to file if enabled
|
# optionally output to file if enabled
|
||||||
# Replace all newlines with literal \n and remove all color codes
|
# Replace all newlines with literal \n and remove all color codes
|
||||||
[[ "${LogToFile}" == "true" ]] && { printf "[%(%Y-%m-%d %H:%M:%S)T] [%-5s] ${format}" -1 "$loglevel" "$@" | awk '{printf "%s\\n", $0}' | sed -r 's/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g'; echo; } >> "${LogFile}" # write to file
|
[[ "${LogToFile}" == "true" ]] && \
|
||||||
|
{ printf "[%(%Y-%m-%d %H:%M:%S)T] [%-5s] ${format}" -1 "$loglevel" "$@" | \
|
||||||
|
awk 'NR > 1 {printf "%s\\n", prev} {prev = $0} END {printf "%s\n", prev}' | \
|
||||||
|
sed -r 's/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g'; } >> "${LogFile}"
|
||||||
|
|
||||||
declare -n out_array="${logvar}"
|
declare -n out_array="${logvar}"
|
||||||
out_array+=("${buffer}")
|
out_array+=("${buffer}")
|
||||||
|
|
@ -255,9 +258,15 @@ print_buffer() {
|
||||||
declare -n buffer="$1"
|
declare -n buffer="$1"
|
||||||
local BufToString
|
local BufToString
|
||||||
|
|
||||||
if [[ "${#buffer[@]}" -gt 0 ]]; then
|
if [[ -n "${buffer:-}" ]] && [[ "${#buffer[@]}" -gt 0 ]]; then
|
||||||
BufToString=$( printf '%s' "${buffer[@]}" )
|
if [[ "${2:-}" != "notifyonly" ]]; then
|
||||||
echo "${BufToString}"
|
BufToString=$( printf '%s' "${buffer[@]}" )
|
||||||
|
echo "${BufToString}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${2:-}" == "notify" ]] || [[ "${2:-}" == "notifyonly" ]]; then
|
||||||
|
exec_if_exists_or_fail send_buffer_notification $1 ${3:-} || printf "Could not source buffer notification function.\n";
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -865,7 +874,7 @@ fi
|
||||||
[[ -n "${BackupForDays:-}" ]] && remove_backups
|
[[ -n "${BackupForDays:-}" ]] && remove_backups
|
||||||
|
|
||||||
# Send final summary notification if enabled
|
# Send final summary notification if enabled
|
||||||
[[ "${Notify}" == true ]] && [[ "${ENABLE_SUMMARY_NOTIFICATION:-}" == true ]] && [[ -n "${actionbuf:-}" ]] && [[ "${#actionbuf[@]} -gt 0" ]] && { exec_if_exists_or_fail send_summary_notification || printf "Could not source summary notification function.\n"; }
|
[[ "${Notify}" == true ]] && [[ "${ENABLE_SUMMARY_NOTIFICATION:-}" == true ]] && print_buffer actionbuf notifyonly Actions;
|
||||||
|
|
||||||
if [[ -d $(dirname "${LogFile}") ]]; then
|
if [[ -d $(dirname "${LogFile}") ]]; then
|
||||||
log_cleanup
|
log_cleanup
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ format_output() {
|
||||||
local FormattedTextTemplate="$3"
|
local FormattedTextTemplate="$3"
|
||||||
local tempcsv=""
|
local tempcsv=""
|
||||||
|
|
||||||
if [[ "${UpdateType}" == "summary" ]]; then
|
if [[ "${UpdateType}" == "buffer" ]]; then
|
||||||
tempcsv="${UpdToString//;/,}"
|
tempcsv="${UpdToString//;/,}"
|
||||||
elif [[ ! "${UpdateType}" == "dockcheck_update" ]]; then
|
elif [[ ! "${UpdateType}" == "dockcheck_update" ]]; then
|
||||||
tempcsv="${UpdToString// -> /,}"
|
tempcsv="${UpdToString// -> /,}"
|
||||||
|
|
@ -196,17 +196,17 @@ format_output() {
|
||||||
FormattedOutput="${tempcsv}"
|
FormattedOutput="${tempcsv}"
|
||||||
fi
|
fi
|
||||||
elif [[ "${OutputFormat}" == "json" ]]; then
|
elif [[ "${OutputFormat}" == "json" ]]; then
|
||||||
if [[ "${UpdateType}" == "summary" ]] && [[ -z "${UpdToString}" ]]; then
|
if [[ "${UpdateType}" == "buffer" ]] && [[ -z "${UpdToString}" ]]; then
|
||||||
FormattedOutput='{"actions": []}'
|
FormattedOutput='{"buffer": []}'
|
||||||
elif [[ -z "${UpdToString}" ]]; then
|
elif [[ -z "${UpdToString}" ]]; then
|
||||||
FormattedOutput='{"updates": []}'
|
FormattedOutput='{"updates": []}'
|
||||||
else
|
else
|
||||||
if [[ "${UpdateType}" == "container_update" ]]; then
|
if [[ "${UpdateType}" == "container_update" ]]; then
|
||||||
# container updates case
|
# 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)')
|
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}" == "summary" ]]; then
|
elif [[ "${UpdateType}" == "buffer" ]]; then
|
||||||
# final summary notification case
|
# buffer notification case
|
||||||
FormattedOutput=$(jq --compact-output --null-input --arg actions "${tempcsv}" '($actions | split("\\n")) | map(split(",")) | {"actions": map({"action": .[0], "result": .[1], "description": .[2]})} | del(..|nulls)')
|
FormattedOutput=$(jq --compact-output --null-input --arg buffer "${tempcsv}" '($buffer | split("\\n")) | map(split(",")) | {"events": map({"event": .[0], "result": .[1], "description": .[2]})} | del(..|nulls)')
|
||||||
elif [[ "${UpdateType}" == "notify_update" ]]; then
|
elif [[ "${UpdateType}" == "notify_update" ]]; then
|
||||||
# script updates case
|
# 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]})}')
|
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]})}')
|
||||||
|
|
@ -223,7 +223,7 @@ format_output() {
|
||||||
else
|
else
|
||||||
if [[ "${UpdateType}" == "container_update" ]]; then
|
if [[ "${UpdateType}" == "container_update" ]]; then
|
||||||
FormattedOutput="${FormattedTextTemplate/<insert_text_cu>/${UpdToString}}"
|
FormattedOutput="${FormattedTextTemplate/<insert_text_cu>/${UpdToString}}"
|
||||||
elif [[ "${UpdateType}" == "summary" ]]; then
|
elif [[ "${UpdateType}" == "buffer" ]]; then
|
||||||
FormattedOutput="${FormattedTextTemplate/<insert_text_sn>/${UpdToString}}"
|
FormattedOutput="${FormattedTextTemplate/<insert_text_sn>/${UpdToString}}"
|
||||||
elif [[ "${UpdateType}" == "notify_update" ]]; then
|
elif [[ "${UpdateType}" == "notify_update" ]]; then
|
||||||
FormattedOutput="${FormattedTextTemplate/<insert_text_nu>/${UpdToString}}"
|
FormattedOutput="${FormattedTextTemplate/<insert_text_nu>/${UpdToString}}"
|
||||||
|
|
@ -310,31 +310,31 @@ send_notification() {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
### Set ENABLE_SUMMARY_NOTIFICATION=true in dockcheck.config
|
### Set notification settings in dockcheck.config
|
||||||
### to send a final action summary notification
|
### to send buffer notifications
|
||||||
send_summary_notification() {
|
send_buffer_notification() {
|
||||||
Notified="false"
|
Notified="false"
|
||||||
|
|
||||||
MessageTitle="$FromHost - Action summary"
|
declare -n buffer="$1"
|
||||||
|
|
||||||
UpdToString=$( printf '%s\\n' "${actionbuf[@]}" )
|
MessageTitle="$FromHost - ${2:-$1}"
|
||||||
UpdToString="${UpdToString%, }"
|
|
||||||
UpdToString=${UpdToString%\\n}
|
UpdToString=$( printf '%s' "${buffer[@]}" )
|
||||||
|
|
||||||
for channel in "${enabled_notify_channels[@]}"; do
|
for channel in "${enabled_notify_channels[@]}"; do
|
||||||
local SkipNotification=$(skip_notification "${channel}" "1" "summary")
|
local SkipNotification=$(skip_notification "${channel}" "1" "buffer")
|
||||||
if [[ "${SkipNotification}" == "false" ]]; then
|
if [[ "${SkipNotification}" == "false" ]]; then
|
||||||
local template=$(get_channel_template "${channel}")
|
local template=$(get_channel_template "${channel}")
|
||||||
|
|
||||||
# Formats UpdToString variable per channel settings
|
# Formats UpdToString variable per channel settings
|
||||||
format_output "summary" "$(output_format "${channel}")" "🐋 Actions taken by $FromHost:\n<insert_text_sn>\n"
|
format_output "buffer" "$(output_format "${channel}")" "🐋 ${2:-Events} on $FromHost:\n<insert_text_sn>\n"
|
||||||
|
|
||||||
# Setting the MessageBody variable here.
|
# Setting the MessageBody variable here.
|
||||||
printf -v MessageBody "${FormattedOutput}"
|
printf -v MessageBody "${FormattedOutput}"
|
||||||
|
|
||||||
printf "\nSending ${channel} summary notification"
|
printf "\nSending ${channel} buffer notification"
|
||||||
exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \
|
exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \
|
||||||
printf "\nAttempted to send summary 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."
|
printf "\nAttempted to send buffer 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"
|
Notified="true"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue