4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
8 # Script to send alarm notifications for netdata
11 # - multiple notification methods
12 # - multiple roles per alarm
13 # - multiple recipients per role
14 # - severity filtering per recipient
16 # Supported notification methods:
18 # - slack.com notifications by @ktsaou
19 # - discordapp.com notifications by @lowfive
20 # - pushover.net notifications by @ktsaou
21 # - pushbullet.com push notifications by Tiago Peralta @tperalta82 PR #1070
22 # - telegram.org notifications by @hashworks PR #1002
23 # - twilio.com notifications by Levi Blaney @shadycuz PR #1211
24 # - kafka notifications by @ktsaou #1342
25 # - pagerduty.com notifications by Jim Cooley @jimcooley PR #1373
26 # - messagebird.com notifications by @tech_no_logical #1453
27 # - hipchart notifications by @ktsaou #1561
29 # -----------------------------------------------------------------------------
30 # testing notifications
32 if [ \( "${1}" = "test" -o "${2}" = "test" \) -a "${#}" -le 2 ]
34 if [ "${2}" = "test" ]
41 [ -z "${recipient}" ] && recipient="sysadmin"
45 for x in "CRITICAL" "WARNING" "CLEAR"
48 echo >&2 "# SENDING TEST ${x} ALARM TO ROLE: ${recipient}"
50 "${0}" "${recipient}" "$(hostname)" 1 1 "${id}" "$(date +%s)" "test_alarm" "test.chart" "test.family" "${x}" "${last}" 100 90 "${0}" 1 $((0 + id)) "units" "this is a test alarm to verify notifications work"
65 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
68 # -----------------------------------------------------------------------------
70 PROGRAM_NAME="$(basename "${0}")"
73 date "+%Y-%m-%d %H:%M:%S"
80 echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
103 [ ${debug} -eq 1 ] && log DEBUG "${@}"
106 # -----------------------------------------------------------------------------
108 # check for BASH v4+ (required for associative arrays)
109 [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ] && \
110 fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
112 # -----------------------------------------------------------------------------
113 # defaults to allow running this script by hand
115 NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
116 NETDATA_CACHE_DIR="${NETDATA_CACHE_DIR-/var/cache/netdata}"
117 [ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
118 [ -z "${NETDATA_HOSTNAME}" ] && NETDATA_HOSTNAME="$(hostname)"
119 [ -z "${NETDATA_REGISTRY_HOSTNAME}" ] && NETDATA_REGISTRY_HOSTNAME="${NETDATA_HOSTNAME}"
121 # -----------------------------------------------------------------------------
122 # parse command line parameters
124 roles="${1}" # the roles that should be notified for this event
125 host="${2}" # the host generated this event
126 unique_id="${3}" # the unique id of this event
127 alarm_id="${4}" # the unique id of the alarm that generated this event
128 event_id="${5}" # the incremental id of the event, for this alarm id
129 when="${6}" # the timestamp this event occurred
130 name="${7}" # the name of the alarm, as given in netdata health.d entries
131 chart="${8}" # the name of the chart (type.id)
132 family="${9}" # the family of the chart
133 status="${10}" # the current status : REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
134 old_status="${11}" # the previous status: REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
135 value="${12}" # the current value of the alarm
136 old_value="${13}" # the previous value of the alarm
137 src="${14}" # the line number and file the alarm has been configured
138 duration="${15}" # the duration in seconds of the previous alarm state
139 non_clear_duration="${16}" # the total duration in seconds this is/was non-clear
140 units="${17}" # the units of the value
141 info="${18}" # a short description of the alarm
143 # -----------------------------------------------------------------------------
144 # screen statuses we don't need to send a notification
146 # don't do anything if this is not WARNING, CRITICAL or CLEAR
147 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
149 info "not sending notification for ${status} on '${chart}.${name}'"
153 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
154 if [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
156 info "not sending notification for ${status} on '${chart}.${name}' (last status was ${old_status})"
160 # -----------------------------------------------------------------------------
163 # By default fetch images from the global public registry.
164 # This is required by default, since all notification methods need to download
165 # images via the Internet, and private registries might not be reachable.
166 # This can be overwritten at the configuration file.
167 images_base_url="https://registry.my-netdata.io"
170 # if empty they will be searched in the system path
174 # enable / disable features
180 SEND_MESSAGEBIRD="YES"
183 SEND_PUSHBULLET="YES"
189 DEFAULT_RECIPIENT_SLACK=
190 declare -A role_recipients_slack=()
194 DEFAULT_RECIPIENT_DISCORD=
195 declare -A role_recipients_discord=()
199 DEFAULT_RECIPIENT_PUSHOVER=
200 declare -A role_recipients_pushover=()
203 PUSHBULLET_ACCESS_TOKEN=
204 DEFAULT_RECIPIENT_PUSHBULLET=
205 declare -A role_recipients_pushbullet=()
209 TWILIO_ACCOUNT_TOKEN=
211 DEFAULT_RECIPIENT_TWILIO=
212 declare -A role_recipients_twilio=()
216 DEFAULT_RECIPIENT_HIPCHAT=
217 declare -A role_recipients_hipchat=()
219 # messagebird configs
220 MESSAGEBIRD_ACCESS_KEY=
222 DEFAULT_RECIPIENT_MESSAGEBIRD=
223 declare -A role_recipients_messagebird=()
227 DEFAULT_RECIPIENT_TELEGRAM=
228 declare -A role_recipients_telegram=()
234 # pagerduty.com configs
236 declare -A role_recipients_pd=()
239 DEFAULT_RECIPIENT_EMAIL="root"
240 declare -A role_recipients_email=()
242 # load the user configuration
243 # this will overwrite the variables above
244 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
246 source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
249 # -----------------------------------------------------------------------------
250 # filter a recipient based on alarm event severity
252 filter_recipient_by_criticality() {
253 local method="${1}" x="${2}" r s
256 r="${x/|*/}" # the recipient
257 s="${x/*|/}" # the severity required for notifying this recipient
259 # no severity filtering for this person
260 [ "${r}" = "${s}" ] && return 0
262 # the severity is invalid
264 [ "${s}" != "CRITICAL" ] && return 0
266 # the new or the old status matches the severity
267 if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
269 [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
270 mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
272 # we need to keep track of the notifications we sent
273 # so that the same user will receive the recovery
274 # even if old_status does not match the required severity
275 touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
279 # it is a cleared alarm we have sent notification for
280 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
282 rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
289 # -----------------------------------------------------------------------------
290 # find the recipients' addresses per method
292 declare -A arr_slack=()
293 declare -A arr_discord=()
294 declare -A arr_pushover=()
295 declare -A arr_pushbullet=()
296 declare -A arr_twilio=()
297 declare -A arr_hipchat=()
298 declare -A arr_telegram=()
300 declare -A arr_email=()
302 # netdata may call us with multiple roles, and roles may have multiple but
303 # overlapping recipients - so, here we find the unique recipients.
304 for x in ${roles//,/ }
306 # the roles 'silent' and 'disabled' mean:
307 # don't send a notification for this role
308 [ "${x}" = "silent" -o "${x}" = "disabled" ] && continue
311 a="${role_recipients_email[${x}]}"
312 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
315 [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
319 a="${role_recipients_pushover[${x}]}"
320 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
323 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
327 a="${role_recipients_pushbullet[${x}]}"
328 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHBULLET}"
331 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1"
335 a="${role_recipients_twilio[${x}]}"
336 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TWILIO}"
339 [ "${r}" != "disabled" ] && filter_recipient_by_criticality twilio "${r}" && arr_twilio[${r/|*/}]="1"
343 a="${role_recipients_hipchat[${x}]}"
344 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_HIPCHAT}"
347 [ "${r}" != "disabled" ] && filter_recipient_by_criticality hipchat "${r}" && arr_hipchat[${r/|*/}]="1"
351 a="${role_recipients_messagebird[${x}]}"
352 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_MESSAGEBIRD}"
355 [ "${r}" != "disabled" ] && filter_recipient_by_criticality messagebird "${r}" && arr_messagebird[${r/|*/}]="1"
359 a="${role_recipients_telegram[${x}]}"
360 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
363 [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
367 a="${role_recipients_slack[${x}]}"
368 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
371 [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
375 a="${role_recipients_discord[${x}]}"
376 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_DISCORD}"
379 [ "${r}" != "disabled" ] && filter_recipient_by_criticality discord "${r}" && arr_discord[${r/|*/}]="1"
383 a="${role_recipients_pd[${x}]}"
384 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PD}"
387 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pd "${r}" && arr_pd[${r/|*/}]="1"
391 # build the list of slack recipients (channels)
392 to_slack="${!arr_slack[*]}"
393 [ -z "${to_slack}" ] && SEND_SLACK="NO"
395 # build the list of discord recipients (channels)
396 to_discord="${!arr_discord[*]}"
397 [ -z "${to_discord}" ] && SEND_DISCORD="NO"
399 # build the list of pushover recipients (user tokens)
400 to_pushover="${!arr_pushover[*]}"
401 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
403 # build the list of pushbulet recipients (user tokens)
404 to_pushbullet="${!arr_pushbullet[*]}"
405 [ -z "${to_pushbullet}" ] && SEND_PUSHBULLET="NO"
407 # build the list of twilio recipients (phone numbers)
408 to_twilio="${!arr_twilio[*]}"
409 [ -z "${to_twilio}" ] && SEND_TWILIO="NO"
411 # build the list of hipchat recipients (rooms)
412 to_hipchat="${!arr_hipchat[*]}"
413 [ -z "${to_hipchat}" ] && SEND_HIPCHAT="NO"
415 # build the list of messagebird recipients (phone numbers)
416 to_messagebird="${!arr_messagebird[*]}"
417 [ -z "${to_messagebird}" ] && SEND_MESSAGEBIRD="NO"
419 # check array of telegram recipients (chat ids)
420 to_telegram="${!arr_telegram[*]}"
421 [ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
423 # build the list of pagerduty recipients (service keys)
424 to_pd="${!arr_pd[*]}"
425 [ -z "${to_pd}" ] && SEND_PD="NO"
427 # build the list of email recipients (email addresses)
429 for x in "${!arr_email[@]}"
431 [ ! -z "${to_email}" ] && to_email="${to_email}, "
432 to_email="${to_email}${x}"
434 [ -z "${to_email}" ] && SEND_EMAIL="NO"
437 # -----------------------------------------------------------------------------
438 # verify the delivery methods supported
441 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
444 [ -z "${DISCORD_WEBHOOK_URL}" ] && SEND_DISCORD="NO"
447 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
450 [ -z "${PUSHBULLET_ACCESS_TOKEN}" ] && SEND_PUSHBULLET="NO"
453 [ -z "${TWILIO_ACCOUNT_TOKEN}" -o -z "${TWILIO_ACCOUNT_SID}" -o -z "${TWILIO_NUMBER}" ] && SEND_TWILIO="NO"
456 [ -z "${HIPCHAT_AUTH_TOKEN}" ] && SEND_HIPCHAT="NO"
459 [ -z "${MESSAGEBIRD_ACCESS_KEY}" -o -z "${MESSAGEBIRD_NUMBER}" ] && SEND_MESSAGEBIRD="NO"
462 [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
465 [ -z "${KAFKA_URL}" -o -z "${KAFKA_SENDER_IP}" ] && SEND_KAFKA="NO"
467 # check pagerduty.com
468 # if we need pd-send, check for the pd-send command
469 # https://www.pagerduty.com/docs/guides/agent-install-guide/
470 if [ "${SEND_PD}" = "YES" ]
472 pd_send="$(which pd-send 2>/dev/null || command -v pd-send 2>/dev/null)"
473 if [ -z "${pd_send}" ]
475 # no pd-send available
476 # disable pagerduty.com
481 # if we need curl, check for the curl command
483 "${SEND_PUSHOVER}" = "YES" \
484 -o "${SEND_SLACK}" = "YES" \
485 -o "${SEND_DISCORD}" = "YES" \
486 -o "${SEND_HIPCHAT}" = "YES" \
487 -o "${SEND_TWILIO}" = "YES" \
488 -o "${SEND_MESSAGEBIRD}" = "YES" \
489 -o "${SEND_TELEGRAM}" = "YES" \
490 -o "${SEND_PUSHBULLET}" = "YES" \
491 -o "${SEND_KAFKA}" = "YES" \
494 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
498 # disable all curl based methods
506 SEND_MESSAGEBIRD="NO"
511 # if we need sendmail, check for the sendmail command
512 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
514 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
515 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
518 # check that we have at least a method enabled
519 if [ "${SEND_EMAIL}" != "YES" \
520 -a "${SEND_PUSHOVER}" != "YES" \
521 -a "${SEND_TELEGRAM}" != "YES" \
522 -a "${SEND_SLACK}" != "YES" \
523 -a "${SEND_DISCORD}" != "YES" \
524 -a "${SEND_TWILIO}" != "YES" \
525 -a "${SEND_HIPCHAT}" != "YES" \
526 -a "${SEND_MESSAGEBIRD}" != "YES" \
527 -a "${SEND_PUSHBULLET}" != "YES" \
528 -a "${SEND_KAFKA}" != "YES" \
529 -a "${SEND_PD}" != "YES" \
532 fatal "All notification methods are disabled. Not sending notification to '${roles}' for '${name}' = '${value}' of chart '${chart}' for status '${status}'."
535 # -----------------------------------------------------------------------------
536 # find a suitable hostname to use, if netdata did not supply a hostname
538 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
539 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
540 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
542 # -----------------------------------------------------------------------------
543 # get the date the alarm happened
545 date="$(date --date=@${when} 2>/dev/null)"
546 [ -z "${date}" ] && date="$(date 2>/dev/null)"
548 # -----------------------------------------------------------------------------
549 # function to URL encode a string
552 local string="${1}" strlen encoded pos c o
555 for (( pos=0 ; pos<strlen ; pos++ ))
564 printf -v o '%%%02x' "'${c}"
574 # -----------------------------------------------------------------------------
575 # function to convert a duration in seconds, to a human readable duration
576 # using DAYS, MINUTES, SECONDS
579 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
581 s=$(( s - (d * 86400) ))
583 s=$(( s - (h * 3600) ))
585 s=$(( s - (m * 60) ))
589 [ ${m} -ge 30 ] && h=$(( h + 1 ))
590 [ ${d} -gt 1 ] && ds="days"
591 [ ${h} -gt 1 ] && hs="hours"
594 ret="${d} ${ds} and ${h} ${hs}"
600 [ ${s} -ge 30 ] && m=$(( m + 1 ))
601 [ ${h} -gt 1 ] && hs="hours"
602 [ ${m} -gt 1 ] && ms="minutes"
605 ret="${h} ${hs} and ${m} ${ms}"
611 [ ${m} -gt 1 ] && ms="minutes"
612 [ ${s} -gt 1 ] && ss="seconds"
615 ret="${m} ${ms} and ${s} ${ss}"
620 [ ${s} -gt 1 ] && ss="seconds"
628 # -----------------------------------------------------------------------------
633 if [ "${SEND_EMAIL}" = "YES" ]
641 info "sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
644 error "failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
652 # -----------------------------------------------------------------------------
656 local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
658 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
661 # https://pushover.net/api
664 CLEAR) priority=-1;; # low priority: no sound or vibration
665 WARNING) priotity=0;; # normal priority: respect quiet hours
666 CRITICAL) priority=1;; # high priority: bypass quiet hours
667 *) priority=-2;; # lowest priority: no notification at all
670 for user in ${usertokens}
672 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
673 --form-string "token=${apptoken}" \
674 --form-string "user=${user}" \
675 --form-string "html=1" \
676 --form-string "title=${title}" \
677 --form-string "message=${message}" \
678 --form-string "timestamp=${when}" \
679 --form-string "url=${url}" \
680 --form-string "url_title=Open netdata dashboard to view the alarm" \
681 --form-string "priority=${priority}" \
682 https://api.pushover.net/1/messages.json)
684 if [ "${httpcode}" == "200" ]
686 info "sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
689 error "failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
693 [ ${sent} -gt 0 ] && return 0
699 # -----------------------------------------------------------------------------
703 local userapikey="${1}" recipients="${2}" title="${3}" message="${4}" httpcode sent=0 user
704 if [ "${SEND_PUSHBULLET}" = "YES" -a ! -z "${userapikey}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
706 #https://docs.pushbullet.com/#create-push
707 for user in ${recipients}
709 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
710 --header 'Access-Token: '${userapikey}'' \
711 --header 'Content-Type: application/json' \
712 --data-binary @<(cat <<EOF
713 {"title": "${title}",
716 "body": "$( echo -n ${message})"}
718 ) "https://api.pushbullet.com/v2/pushes" -X POST)
720 if [ "${httpcode}" == "200" ]
722 info "sent pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
725 error "failed to send pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
729 [ ${sent} -gt 0 ] && return 0
735 # -----------------------------------------------------------------------------
739 local httpcode sent=0
740 if [ "${SEND_KAFKA}" = "YES" ]
742 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
743 --data "{host_ip:\"${KAFKA_SENDER_IP}\",when:${when},name:\"${name}\",chart:\"${chart}\",family:\"${family}\",status:\"${status}\",old_status:\"${old_status}\",value:${value},old_value:${old_value},duration:${duration},non_clear_duration:${non_clear_duration},units:\"${units}\",info:\"${info}\"}" \
746 if [ "${httpcode}" == "204" ]
748 info "sent kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}'"
751 error "failed to send kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}' with HTTP error code ${httpcode}."
754 [ ${sent} -gt 0 ] && return 0
760 # -----------------------------------------------------------------------------
761 # pagerduty.com sender
764 local recipients="${1}" sent=0
768 WARNING) t='trigger';;
769 CRITICAL) t='trigger';;
772 if [ ${SEND_PD} = "YES" -a ! -z "${t}" ]
774 for PD_SERVICE_KEY in ${recipients}
776 d="${status} ${name}=${value} ${units} - ${host}, ${family}"
777 ${pd_send} -k ${PD_SERVICE_KEY} \
781 -f 'info'="${info}" \
782 -f 'value_w_units'="${value} ${units}" \
783 -f 'when'="${when}" \
784 -f 'duration'="${duration}" \
785 -f 'roles'="${roles}" \
786 -f 'host'="${host}" \
787 -f 'unique_id'="${unique_id}" \
788 -f 'alarm_id'="${alarm_id}" \
789 -f 'event_id'="${event_id}" \
790 -f 'name'="${name}" \
791 -f 'chart'="${chart}" \
792 -f 'family'="${family}" \
793 -f 'status'="${status}" \
794 -f 'old_status'="${old_status}" \
795 -f 'value'="${value}" \
796 -f 'old_value'="${old_value}" \
798 -f 'non_clear_duration'="${non_clear_duration}" \
799 -f 'units'="${units}"
801 if [ ${retval} -eq 0 ]
803 info "sent pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}....: ${d}"
806 error "failed to send pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}.... (error code ${retval}): ${d}"
810 [ ${sent} -gt 0 ] && return 0
816 # -----------------------------------------------------------------------------
820 local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}" title="${5}" message="${6}" httpcode sent=0 user
821 if [ "${SEND_TWILIO}" = "YES" -a ! -z "${accountsid}" -a ! -z "${accounttoken}" -a ! -z "${twilionumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
823 #https://www.twilio.com/packages/labs/code/bash/twilio-sms
824 for user in ${recipients}
826 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
827 --data-urlencode "From=${twilionumber}" \
828 --data-urlencode "To=${user}" \
829 --data-urlencode "Body=${title} ${message}" \
830 -u "${accountsid}:${accounttoken}" \
831 "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json")
833 if [ "${httpcode}" == "201" ]
835 info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
838 error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
842 [ ${sent} -gt 0 ] && return 0
849 # -----------------------------------------------------------------------------
853 local authtoken="${1}" recipients="${2}" message="${3}" httpcode sent=0 room color sender msg_format notify
855 if [ "${SEND_HIPCHAT}" = "YES" -a ! -z "${authtoken}" -a ! -z "${recipients}" -a ! -z "${message}" ]
858 # A label to be shown in addition to the sender's name
859 # Valid length range: 0 - 64.
862 # Valid values: html, text.
863 # Defaults to 'html'.
866 # Background color for message. Valid values: yellow, green, red, purple, gray, random. Defaults to 'yellow'.
868 WARNING) color="yellow" ;;
869 CRITICAL) color="red" ;;
870 CLEAR) color="green" ;;
874 # Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc).
875 # Each recipient's notification preferences are taken into account.
879 for room in ${recipients}
881 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
882 -H "Content-type: application/json" \
883 -H "Authorization: Bearer ${authtoken}" \
884 -d "{\"color\": \"${color}\", \"from\": \"${netdata}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
885 "https://api.hipchat.com/v2/room/${room}/notification")
887 if [ "${httpcode}" == "200" ]
889 info "sent HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
892 error "failed to send HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}' with HTTP error code ${httpcode}."
896 [ ${sent} -gt 0 ] && return 0
903 # -----------------------------------------------------------------------------
907 local accesskey="${1}" messagebirdnumber="${2}" recipients="${3}" title="${4}" message="${5}" httpcode sent=0 user
908 if [ "${SEND_MESSAGEBIRD}" = "YES" -a ! -z "${accesskey}" -a ! -z "${messagebirdnumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
910 #https://developers.messagebird.com/docs/messaging
911 for user in ${recipients}
913 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
914 --data-urlencode "originator=${messagebirdnumber}" \
915 --data-urlencode "recipients=${user}" \
916 --data-urlencode "body=${title} ${message}" \
917 --data-urlencode "datacoding=auto" \
918 -H "Authorization: AccessKey ${accesskey}" \
919 "https://rest.messagebird.com/messages")
921 if [ "${httpcode}" == "201" ]
923 info "sent Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
926 error "failed to send Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
930 [ ${sent} -gt 0 ] && return 0
936 # -----------------------------------------------------------------------------
940 local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
942 if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
944 if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
946 for chatid in ${chatids}
948 # https://core.telegram.org/bots/api#sendmessage
949 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
950 --data-urlencode "parse_mode=HTML" \
951 --data-urlencode "disable_web_page_preview=true" \
952 --data-urlencode "text=${message}" \
953 "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
955 if [ "${httpcode}" == "200" ]
957 info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
959 elif [ "${httpcode}" == "401" ]
961 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
963 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
967 [ ${sent} -gt 0 ] && return 0
973 # -----------------------------------------------------------------------------
977 local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
979 [ "${SEND_SLACK}" != "YES" ] && return 1
982 WARNING) color="warning" ;;
983 CRITICAL) color="danger" ;;
984 CLEAR) color="good" ;;
985 *) color="#777777" ;;
988 for channel in ${channels}
992 "channel": "#${channel}",
993 "username": "netdata on ${host}",
994 "icon_url": "${images_base_url}/images/seo-performance-128.png",
995 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
998 "fallback": "${alarm} - ${chart} (${family}) - ${info}",
1000 "title": "${alarm}",
1001 "title_link": "${goto_url}",
1005 "title": "${chart}",
1009 "title": "${family}",
1013 "thumb_url": "${image}",
1014 "footer": "<${goto_url}|${host}>",
1022 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1023 if [ "${httpcode}" == "200" ]
1025 info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1028 error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1032 [ ${sent} -gt 0 ] && return 0
1037 # -----------------------------------------------------------------------------
1041 local webhook="${1}/slack" channels="${2}" httpcode sent=0 channel color payload
1043 [ "${SEND_DISCORD}" != "YES" ] && return 1
1046 WARNING) color="warning" ;;
1047 CRITICAL) color="danger" ;;
1048 CLEAR) color="good" ;;
1049 *) color="#777777" ;;
1052 for channel in ${channels}
1054 payload="$(cat <<EOF
1056 "channel": "#${channel}",
1057 "username": "netdata on ${host}",
1058 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1059 "icon_url": "https://discordapp.com/assets/f78426a064bc9dd24847519259bc42af.png",
1062 "color": "${color}",
1063 "title": "${alarm}",
1064 "title_link": "${goto_url}",
1068 "title": "${chart}",
1069 "value": "${family}"
1072 "thumb_url": "${image}",
1073 "footer_icon": "${images_base_url}/images/seo-performance-128.png",
1074 "footer": "${host}",
1082 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1083 if [ "${httpcode}" == "200" ]
1085 info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1088 error "failed to send discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1092 [ ${sent} -gt 0 ] && return 0
1098 # -----------------------------------------------------------------------------
1099 # prepare the content of the notification
1101 # the url to send the user on click
1102 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
1103 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
1104 urlencode "${family}" >/dev/null; url_family="${REPLY}"
1105 urlencode "${name}" >/dev/null; url_name="${REPLY}"
1106 goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?host=${url_host}&chart=${url_chart}&family=${url_family}&alarm=${url_name}&alarm_unique_id=${unique_id}&alarm_id=${alarm_id}&alarm_event_id=${event_id}"
1108 # the severity of the alarm
1109 severity="${status}"
1111 # the time the alarm was raised
1112 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
1113 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
1114 raised_for="(was ${old_status,,} for ${duration_txt})"
1116 # the key status message
1117 status_message="status unknown"
1119 # the color of the alarm
1123 alarm="${name//_/ } = ${value} ${units}"
1125 # the image of the alarm
1126 image="${images_base_url}/images/seo-performance-128.png"
1128 # prepare the title based on status
1131 image="${images_base_url}/images/alert-128-red.png"
1132 status_message="is critical"
1137 image="${images_base_url}/images/alert-128-orange.png"
1138 status_message="needs attention"
1143 image="${images_base_url}/images/check-mark-2-128-green.png"
1144 status_message="recovered"
1149 if [ "${status}" = "CLEAR" ]
1151 severity="Recovered from ${old_status}"
1152 if [ ${non_clear_duration} -gt ${duration} ]
1154 raised_for="(alarm was raised for ${non_clear_duration_txt})"
1157 # don't show the value when the status is CLEAR
1158 # for certain alarms, this value might not have any meaning
1159 alarm="${name//_/ } ${raised_for}"
1161 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
1163 severity="Escalated to ${status}"
1164 if [ ${non_clear_duration} -gt ${duration} ]
1166 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1169 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
1171 severity="Demoted to ${status}"
1172 if [ ${non_clear_duration} -gt ${duration} ]
1174 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1181 # prepare HTML versions of elements
1183 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
1186 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
1188 # -----------------------------------------------------------------------------
1189 # send the slack notification
1191 # slack aggregates posts from the same username
1192 # so we use "${host} ${status}" as the bot username, to make them diff
1194 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1197 # -----------------------------------------------------------------------------
1198 # send the discord notification
1200 # discord aggregates posts from the same username
1201 # so we use "${host} ${status}" as the bot username, to make them diff
1203 send_discord "${DISCORD_WEBHOOK_URL}" "${to_discord}"
1206 # -----------------------------------------------------------------------------
1207 # send the pushover notification
1209 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
1210 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>
1211 <small><b>${chart}</b><br/>Chart<br/> </small>
1212 <small><b>${family}</b><br/>Family<br/> </small>
1213 <small><b>${severity}</b><br/>Severity<br/> </small>
1214 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
1215 <a href=\"${goto_url}\">View Netdata</a><br/>
1216 <small><small>The source of this alarm is line ${src}</small></small>
1221 # -----------------------------------------------------------------------------
1222 # send the pushbullet notification
1224 send_pushbullet "${PUSHBULLET_ACCESS_TOKEN}" "${to_pushbullet}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}\n
1225 Severity: ${severity}\n
1228 To View Netdata go to: ${goto_url}\n
1229 The source of this alarm is line ${src}"
1233 # -----------------------------------------------------------------------------
1234 # send the twilio SMS
1236 send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1237 Severity: ${severity}
1244 # -----------------------------------------------------------------------------
1245 # send the messagebird SMS
1247 send_messagebird "${MESSAGEBIRD_ACCESS_KEY}" "${MESSAGEBIRD_NUMBER}" "${to_messagebird}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1248 Severity: ${severity}
1256 # -----------------------------------------------------------------------------
1257 # send the telegram.org message
1259 # https://core.telegram.org/bots/api#formatting-options
1260 send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${host} ${status_message} - <b>${name//_/ }</b>
1261 ${chart} (${family})
1262 <a href=\"${goto_url}\">${alarm}</a>
1268 # -----------------------------------------------------------------------------
1269 # send the kafka message
1275 # -----------------------------------------------------------------------------
1276 # send the pagerduty.com message
1282 # -----------------------------------------------------------------------------
1283 # send hipchat message
1285 send_hipchat "${HIPCHAT_AUTH_TOKEN}" "${to_hipchat}" "
1286 <b>${alarm}</b> ${info_html}<br/>
1287 <small><b>${chart}</b><br/>Chart<br/> </small>
1288 <small><b>${family}</b><br/>Family<br/> </small>
1289 <small><b>${severity}</b><br/>Severity<br/> </small>
1290 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
1291 <a href=\"${goto_url}\">View Netdata</a><br/>
1292 <small><small>The source of this alarm is line ${src}</small></small>
1297 # -----------------------------------------------------------------------------
1302 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
1303 Content-Type: text/html
1305 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1306 <html xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
1307 <body style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 14px; width: 100% !important; min-height: 100%; line-height: 1.6; background: #f6f6f6; margin:0; padding: 0;">
1311 <td style="vertical-align: top;" valign="top"></td>
1312 <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
1313 <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
1314 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
1317 <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
1318 <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
1322 <td bgcolor="${color}" style="font-size: 16px; vertical-align: top; font-weight: 400; text-align: center; margin: 0; padding: 10px; color: #ffffff; background: ${color} !important; border: 1px solid ${color}; border-top-color: ${color};" align="center" valign="top">
1323 <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
1327 <td style="vertical-align: top;" valign="top">
1328 <div style="margin: 0; padding: 20px; max-width: 700px;">
1329 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
1332 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding:0 0 20px;" align="left" valign="top">
1333 <span>${chart}</span>
1334 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
1337 <tr style="margin: 0; padding: 0;">
1338 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
1339 <span><b>${alarm}</b>${info_html}</span>
1340 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
1344 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
1345 <span>${family}</span>
1346 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
1349 <tr style="margin: 0; padding: 0;">
1350 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
1351 <span>${severity}</span>
1352 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
1355 <tr style="margin: 0; padding: 0;">
1356 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top"><span>${date}</span>
1357 <span>${raised_for_html}</span> <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Time</span>
1360 <tr style="margin: 0; padding: 0;">
1361 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
1362 <a href="${goto_url}" style="font-size: 14px; color: #ffffff; text-decoration: none; line-height: 1.5; font-weight: bold; text-align: center; display: inline-block; text-transform: capitalize; background: #35568d; border-width: 1px; border-style: solid; border-color: #2b4c86; margin: 0; padding: 10px 15px;" target="_blank">View Netdata</a>
1365 <tr style="text-align: center; margin: 0; padding: 0;">
1366 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 11px; vertical-align: top; margin: 0; padding: 10px 0 0 0; color: #666666;" align="center" valign="bottom">The source of this alarm is line <code>${src}</code>
1369 <tr style="text-align: center; margin: 0; padding: 0;">
1370 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 12px; vertical-align: top; margin:0; padding: 20px 0 0 0; color: #666666; border-top: 1px solid #f0f0f0;" align="center" valign="bottom">Sent by
1371 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
1392 # -----------------------------------------------------------------------------
1395 if [ ${SENT_EMAIL} -eq 0 \
1396 -o ${SENT_PUSHOVER} -eq 0 \
1397 -o ${SENT_TELEGRAM} -eq 0 \
1398 -o ${SENT_SLACK} -eq 0 \
1399 -o ${SENT_DISCORD} -eq 0 \
1400 -o ${SENT_TWILIO} -eq 0 \
1401 -o ${SENT_HIPCHAT} -eq 0 \
1402 -o ${SENT_MESSAGEBIRD} -eq 0 \
1403 -o ${SENT_PUSHBULLET} -eq 0 \
1404 -o ${SENT_KAFKA} -eq 0 \
1405 -o ${SENT_PD} -eq 0 \
1408 # we did send something
1412 # we did not send anything