+ r="${x/|*/}" # the recipient
+ s="${x/*|/}" # the severity required for notifying this recipient
+
+ # no severity filtering for this person
+ [ "${r}" = "${s}" ] && return 0
+
+ # the severity is invalid
+ s="${s^^}"
+ [ "${s}" != "CRITICAL" ] && return 0
+
+ # the new or the old status matches the severity
+ if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
+ then
+ [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
+ mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
+
+ # we need to keep track of the notifications we sent
+ # so that the same user will receive the recovery
+ # even if old_status does not match the required severity
+ touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
+ return 0
+ fi
+
+ # it is a cleared alarm we have sent notification for
+ if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
+ then
+ rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
+ return 0
+ fi
+
+ return 1
+}
+
+# -----------------------------------------------------------------------------
+# find the recipients' addresses per method
+
+declare -A arr_slack=()
+declare -A arr_pushover=()
+declare -A arr_pushbullet=()
+declare -A arr_telegram=()
+declare -A arr_email=()
+
+# netdata may call us with multiple roles, and roles may have multiple but
+# overlapping recipients - so, here we find the unique recipients.
+for x in ${roles//,/ }
+do
+ # the roles 'silent' and 'disabled' mean:
+ # don't send a notification for this role
+ [ "${x}" = "silent" -o "${x}" = "disabled" ] && continue
+
+ # email
+ a="${role_recipients_email[${x}]}"
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
+ for r in ${a//,/ }
+ do
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
+ done
+
+ # pushover
+ a="${role_recipients_pushover[${x}]}"
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
+ for r in ${a//,/ }
+ do
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
+ done
+
+ # pushbullet
+ a="${role_recipients_pushbullet[${x}]}"
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHBULLET}"
+ for r in ${a//,/ }
+ do
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1"
+ done
+
+ # telegram
+ a="${role_recipients_telegram[${x}]}"
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
+ for r in ${a//,/ }
+ do
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
+ done
+
+ # slack
+ a="${role_recipients_slack[${x}]}"
+ [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
+ for r in ${a//,/ }
+ do
+ [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
+ done
+done
+
+# build the list of slack recipients (channels)
+to_slack="${!arr_slack[*]}"
+[ -z "${to_slack}" ] && SEND_SLACK="NO"
+
+# build the list of pushover recipients (user tokens)
+to_pushover="${!arr_pushover[*]}"