#!/bin/bash function json_sanitize() { sed -E 's/"/\\"/g' <<< "$@" | tr -d '\n\r\t' } function json_object() { local -n ref=$1 local out='' out+='{' for key in ${!ref[@]}; do out+='"'"$(json_sanitize $key)"'":"'"$(json_sanitize ${ref[$key]})"'",' done out="${out::-1}" out+='}' echo "$out" } function json_array() { local -n ref=$1 local out='' out+='[' for i in "${ref[@]}"; do out+="$i," done [[ "$out" != '[' ]] && out="${out::-1}" out+=']' echo "$out" } # notify(data, status) function notify() { source src/mail.sh for j in $1; do send_data="$(jq -r '.[] | select(.name == "'"$j"'")' < storage/appconfig/notify.json)" notify_type="$(jq -r '.type' <<< "$send_data")" if [[ "$notify_type" == "mail" ]]; then for k in $(jq -r '.to[]' <<< "$send_data"); do if [[ "$2" == "1" ]]; then mailsend "$k" "$label is DOWN"\ "$label went down on $(date)" elif [[ "$2" == "0" ]]; then mailsend "$k" "$label is back UP"\ "$label came back up on $(date)" fi done elif [[ "$notify_type" == "telegram" ]]; then for k in $(jq -r '.to[]' <<< "$send_data"); do if [[ "$2" == "1" ]]; then msg="$label is DOWN" elif [[ "$2" == "0" ]]; then msg="$label is back UP" fi curl -X POST -H 'Content-Type: application/json'\ -d '{"chat_id": "'"$k"'", "text": "'"$msg"'", "disable_notification": false}'\ "https://api.telegram.org/bot${cfg[telegram_bot_token]}/sendMessage" > /dev/null done else echo "not supported (yet)" fi done }