2
0
Fork 0
main
Ducky 2024-04-01 23:15:12 +01:00
parent 2948a24fe8
commit d218f3b102
3 changed files with 48 additions and 23 deletions

View File

@ -127,14 +127,17 @@ function invoke_script() {
set -o pipefail
exec 3>&1
script_output=$("$backup_scripts_dir/$backup_script_filename" 2>&1 | tee /dev/fd/3)
script_error_log="$(create_tmp_file)"
script_output=$("$backup_scripts_dir/$backup_script_filename" 2>$script_error_log | tee /dev/fd/3)
if [[ "$?" == 0 ]]; then
trigger_notify "success" "Backup script succeeded: $backup_script_name" "$script_output"
trigger_notify "success" "Backup script succeeded: $backup_script_name" "..."
else
trigger_notify "error" "Backup script failed: $backup_script_name" "$script_output"
trigger_notify "error" "Backup script failed: $backup_script_name" "$(cat "$script_error_log")"
fi
rm -f"$script_error_log"
set +o pipefail
exec 3>&-
}
@ -145,13 +148,17 @@ function trigger_notify() {
output="$(echo "$3" | sed -r "s/\x1B\[[0-9;]*[JKmsu]//g")"
notify_prog="/usr/local/bin/sh.zio.notify"
log_path="$(create_log "$output")"
if [[ ! -f "$notify_prog" ]]; then
say warning "'$notify_prog' not found. Not sending notification"
else
"$notify_prog" \
--file "$log_path" \
--level "$level" \
--message '```\\n'"${output//$'\n'/'\\n'}"'\\n```' \
--title "$title"
#--message '```\\n'"${output//$'\n'/'\\n'}"'\\n```' \
fi
}

View File

@ -3,7 +3,7 @@
_PLUG_TITLE="ntfy Helper"
_PLUG_DESCRIPTION=""
_PLUG_ARGS=(
"message;m;;string;(No Message)"
"message;m;;string"
"title;t;;string;(No Title)"
"file;f;;path"
"level;l;;string;info"
@ -52,42 +52,43 @@ function main() {
hostname="$(hostname -s)"
invoked_by="$_invoked_by"
level_emoji="$(get_level_emoji "$_level")"
topic="$(get_topic "$_invoked_by")"
n_message="$_message"
n_priority="$(get_level_priority "$_level")"
n_title="$level_emoji [$hostname] $_title"
n_topic="$(get_topic "$_invoked_by")"
curl_command="curl"
body='{
"message": "'$n_message'",
"priority": '$n_priority',
"tags": ["'$hostname'", "'$invoked_by'"],
"title": "'$n_title'",
"topic": "'$n_topic'"
}'
curl_command="curl -d \"$body\""
curl_command+=" -H \"Authorization: Bearer $token\""
curl_command+=" -H \"Markdown: yes\""
curl_command+=" -H \"Priority: $(get_level_priority "$_level")\""
curl_command+=" -H \"Tags: $hostname,$invoked_by\""
curl_command+=" -H \"Title: $level_emoji [$hostname] $_title\""
if [[ "$n_topic" == "zio_test" ]]; then
curl_command+=" -H \"Cache: no\""
fi
if [[ "$_message" != "true" ]]; then
curl_command+=" -H \"Message: $_message\""
fi
if [[ "$_file" != "" ]]; then
if [[ ! -f "$_file" ]]; then
die "Cannot attach '$_file': file does not exist"
fi
if [[ "$n_message" != "" ]]; then
say warning "Cannot send --message/-m with --file/-f. Only sending file"
fi
full_file="$(realpath -s "$_file")"
curl_command+=" -T \"$full_file\""
curl_command+=" -H \"Filename: $(basename "$full_file")\""
else
# HACK: Attachments don't work if you use "Cache: no"
if [[ "$topic" == "zio_test" ]]; then
curl_command+=" -H \"Cache: no\""
fi
fi
curl_command+=" https://ntfy.zio.sh/"
curl_command+=" https://ntfy.zio.sh/$topic"
echo $curl_command
echo "$curl_command" | bash
}
if [[ $_PLUG_INVOKED != "true" ]]; then

View File

@ -4,6 +4,23 @@ function container_exec() {
podman_exec $@
}
function create_log() {
content="$1"
log_dir="/var/log/zio"
log_file="$(date +%Y%m%d%H%M%S).$(echo $RANDOM | md5sum | head -c 6; echo;).log"
if [[ ! $(id -u) = 0 ]]; then
log_dir="/tmp/zio/logs"
fi
log_path="$log_dir/$log_file"
mkdir -p "$log_dir"
echo -e "$content" > "$log_dir/$log_file"
echo "$log_path"
}
function create_tmp_file() {
name="$1"
prefix_dir="/tmp/zio"