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 # - hipchat 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" "new value" "old value"
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
142 value_string="${19}" # friendly value (with units)
143 old_value_string="${20}" # friendly old value (with units)
145 # -----------------------------------------------------------------------------
146 # screen statuses we don't need to send a notification
148 # don't do anything if this is not WARNING, CRITICAL or CLEAR
149 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
151 info "not sending notification for ${status} on '${chart}.${name}'"
155 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
156 if [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
158 info "not sending notification for ${status} on '${chart}.${name}' (last status was ${old_status})"
162 # -----------------------------------------------------------------------------
165 # By default fetch images from the global public registry.
166 # This is required by default, since all notification methods need to download
167 # images via the Internet, and private registries might not be reachable.
168 # This can be overwritten at the configuration file.
169 images_base_url="https://registry.my-netdata.io"
172 # if empty they will be searched in the system path
176 # enable / disable features
182 SEND_MESSAGEBIRD="YES"
185 SEND_PUSHBULLET="YES"
191 DEFAULT_RECIPIENT_SLACK=
192 declare -A role_recipients_slack=()
196 DEFAULT_RECIPIENT_DISCORD=
197 declare -A role_recipients_discord=()
201 DEFAULT_RECIPIENT_PUSHOVER=
202 declare -A role_recipients_pushover=()
205 PUSHBULLET_ACCESS_TOKEN=
206 DEFAULT_RECIPIENT_PUSHBULLET=
207 declare -A role_recipients_pushbullet=()
211 TWILIO_ACCOUNT_TOKEN=
213 DEFAULT_RECIPIENT_TWILIO=
214 declare -A role_recipients_twilio=()
219 DEFAULT_RECIPIENT_HIPCHAT=
220 declare -A role_recipients_hipchat=()
222 # messagebird configs
223 MESSAGEBIRD_ACCESS_KEY=
225 DEFAULT_RECIPIENT_MESSAGEBIRD=
226 declare -A role_recipients_messagebird=()
230 DEFAULT_RECIPIENT_TELEGRAM=
231 declare -A role_recipients_telegram=()
237 # pagerduty.com configs
239 declare -A role_recipients_pd=()
242 DEFAULT_RECIPIENT_EMAIL="root"
243 declare -A role_recipients_email=()
245 # load the user configuration
246 # this will overwrite the variables above
247 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
249 source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
252 # -----------------------------------------------------------------------------
253 # filter a recipient based on alarm event severity
255 filter_recipient_by_criticality() {
256 local method="${1}" x="${2}" r s
259 r="${x/|*/}" # the recipient
260 s="${x/*|/}" # the severity required for notifying this recipient
262 # no severity filtering for this person
263 [ "${r}" = "${s}" ] && return 0
265 # the severity is invalid
267 [ "${s}" != "CRITICAL" ] && return 0
269 # the new or the old status matches the severity
270 if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
272 [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
273 mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
275 # we need to keep track of the notifications we sent
276 # so that the same user will receive the recovery
277 # even if old_status does not match the required severity
278 touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
282 # it is a cleared alarm we have sent notification for
283 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
285 rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
292 # -----------------------------------------------------------------------------
293 # find the recipients' addresses per method
295 declare -A arr_slack=()
296 declare -A arr_discord=()
297 declare -A arr_pushover=()
298 declare -A arr_pushbullet=()
299 declare -A arr_twilio=()
300 declare -A arr_hipchat=()
301 declare -A arr_telegram=()
303 declare -A arr_email=()
305 # netdata may call us with multiple roles, and roles may have multiple but
306 # overlapping recipients - so, here we find the unique recipients.
307 for x in ${roles//,/ }
309 # the roles 'silent' and 'disabled' mean:
310 # don't send a notification for this role
311 [ "${x}" = "silent" -o "${x}" = "disabled" ] && continue
314 a="${role_recipients_email[${x}]}"
315 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
318 [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
322 a="${role_recipients_pushover[${x}]}"
323 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
326 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
330 a="${role_recipients_pushbullet[${x}]}"
331 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHBULLET}"
334 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1"
338 a="${role_recipients_twilio[${x}]}"
339 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TWILIO}"
342 [ "${r}" != "disabled" ] && filter_recipient_by_criticality twilio "${r}" && arr_twilio[${r/|*/}]="1"
346 a="${role_recipients_hipchat[${x}]}"
347 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_HIPCHAT}"
350 [ "${r}" != "disabled" ] && filter_recipient_by_criticality hipchat "${r}" && arr_hipchat[${r/|*/}]="1"
354 a="${role_recipients_messagebird[${x}]}"
355 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_MESSAGEBIRD}"
358 [ "${r}" != "disabled" ] && filter_recipient_by_criticality messagebird "${r}" && arr_messagebird[${r/|*/}]="1"
362 a="${role_recipients_telegram[${x}]}"
363 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
366 [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
370 a="${role_recipients_slack[${x}]}"
371 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
374 [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
378 a="${role_recipients_discord[${x}]}"
379 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_DISCORD}"
382 [ "${r}" != "disabled" ] && filter_recipient_by_criticality discord "${r}" && arr_discord[${r/|*/}]="1"
386 a="${role_recipients_pd[${x}]}"
387 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PD}"
390 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pd "${r}" && arr_pd[${r/|*/}]="1"
394 # build the list of slack recipients (channels)
395 to_slack="${!arr_slack[*]}"
396 [ -z "${to_slack}" ] && SEND_SLACK="NO"
398 # build the list of discord recipients (channels)
399 to_discord="${!arr_discord[*]}"
400 [ -z "${to_discord}" ] && SEND_DISCORD="NO"
402 # build the list of pushover recipients (user tokens)
403 to_pushover="${!arr_pushover[*]}"
404 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
406 # build the list of pushbulet recipients (user tokens)
407 to_pushbullet="${!arr_pushbullet[*]}"
408 [ -z "${to_pushbullet}" ] && SEND_PUSHBULLET="NO"
410 # build the list of twilio recipients (phone numbers)
411 to_twilio="${!arr_twilio[*]}"
412 [ -z "${to_twilio}" ] && SEND_TWILIO="NO"
414 # build the list of hipchat recipients (rooms)
415 to_hipchat="${!arr_hipchat[*]}"
416 [ -z "${to_hipchat}" ] && SEND_HIPCHAT="NO"
418 # build the list of messagebird recipients (phone numbers)
419 to_messagebird="${!arr_messagebird[*]}"
420 [ -z "${to_messagebird}" ] && SEND_MESSAGEBIRD="NO"
422 # check array of telegram recipients (chat ids)
423 to_telegram="${!arr_telegram[*]}"
424 [ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
426 # build the list of pagerduty recipients (service keys)
427 to_pd="${!arr_pd[*]}"
428 [ -z "${to_pd}" ] && SEND_PD="NO"
430 # build the list of email recipients (email addresses)
432 for x in "${!arr_email[@]}"
434 [ ! -z "${to_email}" ] && to_email="${to_email}, "
435 to_email="${to_email}${x}"
437 [ -z "${to_email}" ] && SEND_EMAIL="NO"
440 # -----------------------------------------------------------------------------
441 # verify the delivery methods supported
444 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
447 [ -z "${DISCORD_WEBHOOK_URL}" ] && SEND_DISCORD="NO"
450 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
453 [ -z "${PUSHBULLET_ACCESS_TOKEN}" ] && SEND_PUSHBULLET="NO"
456 [ -z "${TWILIO_ACCOUNT_TOKEN}" -o -z "${TWILIO_ACCOUNT_SID}" -o -z "${TWILIO_NUMBER}" ] && SEND_TWILIO="NO"
459 [ -z "${HIPCHAT_AUTH_TOKEN}" ] && SEND_HIPCHAT="NO"
462 [ -z "${MESSAGEBIRD_ACCESS_KEY}" -o -z "${MESSAGEBIRD_NUMBER}" ] && SEND_MESSAGEBIRD="NO"
465 [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
468 [ -z "${KAFKA_URL}" -o -z "${KAFKA_SENDER_IP}" ] && SEND_KAFKA="NO"
470 # check pagerduty.com
471 # if we need pd-send, check for the pd-send command
472 # https://www.pagerduty.com/docs/guides/agent-install-guide/
473 if [ "${SEND_PD}" = "YES" ]
475 pd_send="$(which pd-send 2>/dev/null || command -v pd-send 2>/dev/null)"
476 if [ -z "${pd_send}" ]
478 # no pd-send available
479 # disable pagerduty.com
484 # if we need curl, check for the curl command
486 "${SEND_PUSHOVER}" = "YES" \
487 -o "${SEND_SLACK}" = "YES" \
488 -o "${SEND_DISCORD}" = "YES" \
489 -o "${SEND_HIPCHAT}" = "YES" \
490 -o "${SEND_TWILIO}" = "YES" \
491 -o "${SEND_MESSAGEBIRD}" = "YES" \
492 -o "${SEND_TELEGRAM}" = "YES" \
493 -o "${SEND_PUSHBULLET}" = "YES" \
494 -o "${SEND_KAFKA}" = "YES" \
497 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
501 # disable all curl based methods
509 SEND_MESSAGEBIRD="NO"
514 # if we need sendmail, check for the sendmail command
515 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
517 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
518 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
521 # check that we have at least a method enabled
522 if [ "${SEND_EMAIL}" != "YES" \
523 -a "${SEND_PUSHOVER}" != "YES" \
524 -a "${SEND_TELEGRAM}" != "YES" \
525 -a "${SEND_SLACK}" != "YES" \
526 -a "${SEND_DISCORD}" != "YES" \
527 -a "${SEND_TWILIO}" != "YES" \
528 -a "${SEND_HIPCHAT}" != "YES" \
529 -a "${SEND_MESSAGEBIRD}" != "YES" \
530 -a "${SEND_PUSHBULLET}" != "YES" \
531 -a "${SEND_KAFKA}" != "YES" \
532 -a "${SEND_PD}" != "YES" \
535 fatal "All notification methods are disabled. Not sending notification to '${roles}' for '${name}' = '${value}' of chart '${chart}' for status '${status}'."
538 # -----------------------------------------------------------------------------
539 # find a suitable hostname to use, if netdata did not supply a hostname
541 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
542 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
543 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
545 # -----------------------------------------------------------------------------
546 # get the date the alarm happened
548 date="$(date --date=@${when} 2>/dev/null)"
549 [ -z "${date}" ] && date="$(date 2>/dev/null)"
551 # -----------------------------------------------------------------------------
552 # function to URL encode a string
555 local string="${1}" strlen encoded pos c o
558 for (( pos=0 ; pos<strlen ; pos++ ))
567 printf -v o '%%%02x' "'${c}"
577 # -----------------------------------------------------------------------------
578 # function to convert a duration in seconds, to a human readable duration
579 # using DAYS, MINUTES, SECONDS
582 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
584 s=$(( s - (d * 86400) ))
586 s=$(( s - (h * 3600) ))
588 s=$(( s - (m * 60) ))
592 [ ${m} -ge 30 ] && h=$(( h + 1 ))
593 [ ${d} -gt 1 ] && ds="days"
594 [ ${h} -gt 1 ] && hs="hours"
597 ret="${d} ${ds} and ${h} ${hs}"
603 [ ${s} -ge 30 ] && m=$(( m + 1 ))
604 [ ${h} -gt 1 ] && hs="hours"
605 [ ${m} -gt 1 ] && ms="minutes"
608 ret="${h} ${hs} and ${m} ${ms}"
614 [ ${m} -gt 1 ] && ms="minutes"
615 [ ${s} -gt 1 ] && ss="seconds"
618 ret="${m} ${ms} and ${s} ${ss}"
623 [ ${s} -gt 1 ] && ss="seconds"
631 # -----------------------------------------------------------------------------
636 if [ "${SEND_EMAIL}" = "YES" ]
644 info "sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
647 error "failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
655 # -----------------------------------------------------------------------------
659 local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
661 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
664 # https://pushover.net/api
667 CLEAR) priority=-1;; # low priority: no sound or vibration
668 WARNING) priotity=0;; # normal priority: respect quiet hours
669 CRITICAL) priority=1;; # high priority: bypass quiet hours
670 *) priority=-2;; # lowest priority: no notification at all
673 for user in ${usertokens}
675 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
676 --form-string "token=${apptoken}" \
677 --form-string "user=${user}" \
678 --form-string "html=1" \
679 --form-string "title=${title}" \
680 --form-string "message=${message}" \
681 --form-string "timestamp=${when}" \
682 --form-string "url=${url}" \
683 --form-string "url_title=Open netdata dashboard to view the alarm" \
684 --form-string "priority=${priority}" \
685 https://api.pushover.net/1/messages.json)
687 if [ "${httpcode}" == "200" ]
689 info "sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
692 error "failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
696 [ ${sent} -gt 0 ] && return 0
702 # -----------------------------------------------------------------------------
706 local userapikey="${1}" recipients="${2}" title="${3}" message="${4}" httpcode sent=0 user
707 if [ "${SEND_PUSHBULLET}" = "YES" -a ! -z "${userapikey}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
709 #https://docs.pushbullet.com/#create-push
710 for user in ${recipients}
712 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
713 --header 'Access-Token: '${userapikey}'' \
714 --header 'Content-Type: application/json' \
715 --data-binary @<(cat <<EOF
716 {"title": "${title}",
719 "body": "$( echo -n ${message})"}
721 ) "https://api.pushbullet.com/v2/pushes" -X POST)
723 if [ "${httpcode}" == "200" ]
725 info "sent pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
728 error "failed to send pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
732 [ ${sent} -gt 0 ] && return 0
738 # -----------------------------------------------------------------------------
742 local httpcode sent=0
743 if [ "${SEND_KAFKA}" = "YES" ]
745 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
746 --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}\"}" \
749 if [ "${httpcode}" == "204" ]
751 info "sent kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}'"
754 error "failed to send kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}' with HTTP error code ${httpcode}."
757 [ ${sent} -gt 0 ] && return 0
763 # -----------------------------------------------------------------------------
764 # pagerduty.com sender
767 local recipients="${1}" sent=0
771 WARNING) t='trigger';;
772 CRITICAL) t='trigger';;
775 if [ ${SEND_PD} = "YES" -a ! -z "${t}" ]
777 for PD_SERVICE_KEY in ${recipients}
779 d="${status} ${name} = ${value_string} - ${host}, ${family}"
780 ${pd_send} -k ${PD_SERVICE_KEY} \
784 -f 'info'="${info}" \
785 -f 'value_w_units'="${value_string}" \
786 -f 'when'="${when}" \
787 -f 'duration'="${duration}" \
788 -f 'roles'="${roles}" \
789 -f 'host'="${host}" \
790 -f 'unique_id'="${unique_id}" \
791 -f 'alarm_id'="${alarm_id}" \
792 -f 'event_id'="${event_id}" \
793 -f 'name'="${name}" \
794 -f 'chart'="${chart}" \
795 -f 'family'="${family}" \
796 -f 'status'="${status}" \
797 -f 'old_status'="${old_status}" \
798 -f 'value'="${value}" \
799 -f 'old_value'="${old_value}" \
801 -f 'non_clear_duration'="${non_clear_duration}" \
802 -f 'units'="${units}"
804 if [ ${retval} -eq 0 ]
806 info "sent pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}....: ${d}"
809 error "failed to send pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}.... (error code ${retval}): ${d}"
813 [ ${sent} -gt 0 ] && return 0
819 # -----------------------------------------------------------------------------
823 local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}" title="${5}" message="${6}" httpcode sent=0 user
824 if [ "${SEND_TWILIO}" = "YES" -a ! -z "${accountsid}" -a ! -z "${accounttoken}" -a ! -z "${twilionumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
826 #https://www.twilio.com/packages/labs/code/bash/twilio-sms
827 for user in ${recipients}
829 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
830 --data-urlencode "From=${twilionumber}" \
831 --data-urlencode "To=${user}" \
832 --data-urlencode "Body=${title} ${message}" \
833 -u "${accountsid}:${accounttoken}" \
834 "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json")
836 if [ "${httpcode}" == "201" ]
838 info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
841 error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
845 [ ${sent} -gt 0 ] && return 0
852 # -----------------------------------------------------------------------------
856 local authtoken="${1}" recipients="${2}" message="${3}" httpcode sent=0 room color sender msg_format notify
858 if [ "${SEND_HIPCHAT}" = "YES" -a ! -z "${HIPCHAT_SERVER}" -a ! -z "${authtoken}" -a ! -z "${recipients}" -a ! -z "${message}" ]
860 # A label to be shown in addition to the sender's name
861 # Valid length range: 0 - 64.
864 # Valid values: html, text.
865 # Defaults to 'html'.
868 # Background color for message. Valid values: yellow, green, red, purple, gray, random. Defaults to 'yellow'.
870 WARNING) color="yellow" ;;
871 CRITICAL) color="red" ;;
872 CLEAR) color="green" ;;
876 # Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc).
877 # Each recipient's notification preferences are taken into account.
881 for room in ${recipients}
883 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
884 -H "Content-type: application/json" \
885 -H "Authorization: Bearer ${authtoken}" \
886 -d "{\"color\": \"${color}\", \"from\": \"${netdata}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
887 "https://${HIPCHAT_SERVER}/v2/room/${room}/notification")
889 if [ "${httpcode}" == "204" ]
891 info "sent HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
894 error "failed to send HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}' with HTTP error code ${httpcode}."
898 [ ${sent} -gt 0 ] && return 0
905 # -----------------------------------------------------------------------------
909 local accesskey="${1}" messagebirdnumber="${2}" recipients="${3}" title="${4}" message="${5}" httpcode sent=0 user
910 if [ "${SEND_MESSAGEBIRD}" = "YES" -a ! -z "${accesskey}" -a ! -z "${messagebirdnumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
912 #https://developers.messagebird.com/docs/messaging
913 for user in ${recipients}
915 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
916 --data-urlencode "originator=${messagebirdnumber}" \
917 --data-urlencode "recipients=${user}" \
918 --data-urlencode "body=${title} ${message}" \
919 --data-urlencode "datacoding=auto" \
920 -H "Authorization: AccessKey ${accesskey}" \
921 "https://rest.messagebird.com/messages")
923 if [ "${httpcode}" == "201" ]
925 info "sent Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
928 error "failed to send Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
932 [ ${sent} -gt 0 ] && return 0
938 # -----------------------------------------------------------------------------
942 local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
944 if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
946 if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
948 for chatid in ${chatids}
950 # https://core.telegram.org/bots/api#sendmessage
951 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
952 --data-urlencode "parse_mode=HTML" \
953 --data-urlencode "disable_web_page_preview=true" \
954 --data-urlencode "text=${message}" \
955 "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
957 if [ "${httpcode}" == "200" ]
959 info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
961 elif [ "${httpcode}" == "401" ]
963 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
965 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
969 [ ${sent} -gt 0 ] && return 0
975 # -----------------------------------------------------------------------------
979 local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
981 [ "${SEND_SLACK}" != "YES" ] && return 1
984 WARNING) color="warning" ;;
985 CRITICAL) color="danger" ;;
986 CLEAR) color="good" ;;
987 *) color="#777777" ;;
990 for channel in ${channels}
994 "channel": "#${channel}",
995 "username": "netdata on ${host}",
996 "icon_url": "${images_base_url}/images/seo-performance-128.png",
997 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1000 "fallback": "${alarm} - ${chart} (${family}) - ${info}",
1001 "color": "${color}",
1002 "title": "${alarm}",
1003 "title_link": "${goto_url}",
1007 "title": "${chart}",
1011 "title": "${family}",
1015 "thumb_url": "${image}",
1016 "footer": "<${goto_url}|${host}>",
1024 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1025 if [ "${httpcode}" == "200" ]
1027 info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1030 error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1034 [ ${sent} -gt 0 ] && return 0
1039 # -----------------------------------------------------------------------------
1043 local webhook="${1}/slack" channels="${2}" httpcode sent=0 channel color payload
1045 [ "${SEND_DISCORD}" != "YES" ] && return 1
1048 WARNING) color="warning" ;;
1049 CRITICAL) color="danger" ;;
1050 CLEAR) color="good" ;;
1051 *) color="#777777" ;;
1054 for channel in ${channels}
1056 payload="$(cat <<EOF
1058 "channel": "#${channel}",
1059 "username": "netdata on ${host}",
1060 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1061 "icon_url": "${images_base_url}/images/seo-performance-128.png",
1064 "color": "${color}",
1065 "title": "${alarm}",
1066 "title_link": "${goto_url}",
1070 "title": "${chart}",
1071 "value": "${family}"
1074 "thumb_url": "${image}",
1075 "footer_icon": "${images_base_url}/images/seo-performance-128.png",
1076 "footer": "${host}",
1084 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1085 if [ "${httpcode}" == "200" ]
1087 info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1090 error "failed to send discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1094 [ ${sent} -gt 0 ] && return 0
1100 # -----------------------------------------------------------------------------
1101 # prepare the content of the notification
1103 # the url to send the user on click
1104 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
1105 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
1106 urlencode "${family}" >/dev/null; url_family="${REPLY}"
1107 urlencode "${name}" >/dev/null; url_name="${REPLY}"
1108 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}"
1110 # the severity of the alarm
1111 severity="${status}"
1113 # the time the alarm was raised
1114 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
1115 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
1116 raised_for="(was ${old_status,,} for ${duration_txt})"
1118 # the key status message
1119 status_message="status unknown"
1121 # the color of the alarm
1125 alarm="${name//_/ } = ${value_string}"
1127 # the image of the alarm
1128 image="${images_base_url}/images/seo-performance-128.png"
1130 # prepare the title based on status
1133 image="${images_base_url}/images/alert-128-red.png"
1134 status_message="is critical"
1139 image="${images_base_url}/images/alert-128-orange.png"
1140 status_message="needs attention"
1145 image="${images_base_url}/images/check-mark-2-128-green.png"
1146 status_message="recovered"
1151 if [ "${status}" = "CLEAR" ]
1153 severity="Recovered from ${old_status}"
1154 if [ ${non_clear_duration} -gt ${duration} ]
1156 raised_for="(alarm was raised for ${non_clear_duration_txt})"
1159 # don't show the value when the status is CLEAR
1160 # for certain alarms, this value might not have any meaning
1161 alarm="${name//_/ } ${raised_for}"
1163 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
1165 severity="Escalated to ${status}"
1166 if [ ${non_clear_duration} -gt ${duration} ]
1168 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1171 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
1173 severity="Demoted to ${status}"
1174 if [ ${non_clear_duration} -gt ${duration} ]
1176 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1183 # prepare HTML versions of elements
1185 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
1188 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
1190 # -----------------------------------------------------------------------------
1191 # send the slack notification
1193 # slack aggregates posts from the same username
1194 # so we use "${host} ${status}" as the bot username, to make them diff
1196 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1199 # -----------------------------------------------------------------------------
1200 # send the discord notification
1202 # discord aggregates posts from the same username
1203 # so we use "${host} ${status}" as the bot username, to make them diff
1205 send_discord "${DISCORD_WEBHOOK_URL}" "${to_discord}"
1208 # -----------------------------------------------------------------------------
1209 # send the pushover notification
1211 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
1212 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>
1213 <small><b>${chart}</b><br/>Chart<br/> </small>
1214 <small><b>${family}</b><br/>Family<br/> </small>
1215 <small><b>${severity}</b><br/>Severity<br/> </small>
1216 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
1217 <a href=\"${goto_url}\">View Netdata</a><br/>
1218 <small><small>The source of this alarm is line ${src}</small></small>
1223 # -----------------------------------------------------------------------------
1224 # send the pushbullet notification
1226 send_pushbullet "${PUSHBULLET_ACCESS_TOKEN}" "${to_pushbullet}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}\n
1227 Severity: ${severity}\n
1230 To View Netdata go to: ${goto_url}\n
1231 The source of this alarm is line ${src}"
1235 # -----------------------------------------------------------------------------
1236 # send the twilio SMS
1238 send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1239 Severity: ${severity}
1246 # -----------------------------------------------------------------------------
1247 # send the messagebird SMS
1249 send_messagebird "${MESSAGEBIRD_ACCESS_KEY}" "${MESSAGEBIRD_NUMBER}" "${to_messagebird}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1250 Severity: ${severity}
1258 # -----------------------------------------------------------------------------
1259 # send the telegram.org message
1261 # https://core.telegram.org/bots/api#formatting-options
1262 send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${host} ${status_message} - <b>${name//_/ }</b>
1263 ${chart} (${family})
1264 <a href=\"${goto_url}\">${alarm}</a>
1270 # -----------------------------------------------------------------------------
1271 # send the kafka message
1277 # -----------------------------------------------------------------------------
1278 # send the pagerduty.com message
1284 # -----------------------------------------------------------------------------
1285 # send hipchat message
1287 send_hipchat "${HIPCHAT_AUTH_TOKEN}" "${to_hipchat}" " \
1288 ${host} ${status_message}<br/> \
1289 <b>${alarm}</b> ${info_html}<br/> \
1290 <b>${chart}</b> (family <b>${family}</b>)<br/> \
1291 <b>${date}${raised_for_html}</b><br/> \
1292 <a href=\\\"${goto_url}\\\">View netdata dashboard</a> \
1293 (source of alarm ${src}) \
1298 # -----------------------------------------------------------------------------
1303 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
1304 Content-Type: text/html
1306 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1307 <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;">
1308 <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;">
1312 <td style="vertical-align: top;" valign="top"></td>
1313 <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
1314 <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
1315 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
1318 <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
1319 <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
1323 <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">
1324 <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
1328 <td style="vertical-align: top;" valign="top">
1329 <div style="margin: 0; padding: 20px; max-width: 700px;">
1330 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
1333 <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">
1334 <span>${chart}</span>
1335 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
1338 <tr style="margin: 0; padding: 0;">
1339 <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">
1340 <span><b>${alarm}</b>${info_html}</span>
1341 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
1345 <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">
1346 <span>${family}</span>
1347 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
1350 <tr style="margin: 0; padding: 0;">
1351 <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">
1352 <span>${severity}</span>
1353 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
1356 <tr style="margin: 0; padding: 0;">
1357 <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>
1358 <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>
1361 <tr style="margin: 0; padding: 0;">
1362 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
1363 <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>
1366 <tr style="text-align: center; margin: 0; padding: 0;">
1367 <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>
1370 <tr style="text-align: center; margin: 0; padding: 0;">
1371 <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
1372 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
1393 # -----------------------------------------------------------------------------
1396 if [ ${SENT_EMAIL} -eq 0 \
1397 -o ${SENT_PUSHOVER} -eq 0 \
1398 -o ${SENT_TELEGRAM} -eq 0 \
1399 -o ${SENT_SLACK} -eq 0 \
1400 -o ${SENT_DISCORD} -eq 0 \
1401 -o ${SENT_TWILIO} -eq 0 \
1402 -o ${SENT_HIPCHAT} -eq 0 \
1403 -o ${SENT_MESSAGEBIRD} -eq 0 \
1404 -o ${SENT_PUSHBULLET} -eq 0 \
1405 -o ${SENT_KAFKA} -eq 0 \
1406 -o ${SENT_PD} -eq 0 \
1409 # we did send something
1413 # we did not send anything