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=()
217 HIPCHAT_CUSTOM_SERVER=
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 hipchat_server="${1}" authtoken="${2}" recipients="${3}" message="${4}" httpcode sent=0 room color sender msg_format notify
858 # Use HipChat Cloud if no server specified
859 if [ -z "${hipchat_server}" ]
861 hipchat_server="api.hipchat.com"
864 if [ "${SEND_HIPCHAT}" = "YES" -a ! -z "${hipchat_server}" -a ! -z "${authtoken}" -a ! -z "${recipients}" -a ! -z "${message}" ]
866 # A label to be shown in addition to the sender's name
867 # Valid length range: 0 - 64.
870 # Valid values: html, text.
871 # Defaults to 'html'.
874 # Detect html messages
875 if echo "${message}" | tr -d '\n' | grep -q "<[a-z]\+>.*</[a-z]\+>\|<[a-z]\+/>"
880 # Background color for message. Valid values: yellow, green, red, purple, gray, random. Defaults to 'yellow'.
882 WARNING) color="yellow" ;;
883 CRITICAL) color="red" ;;
884 CLEAR) color="green" ;;
888 # Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc).
889 # Each recipient's notification preferences are taken into account.
893 for room in ${recipients}
895 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
896 -H "Content-type: application/json" \
897 -H "Authorization: Bearer ${authtoken}" \
898 -d "{\"color\": \"${color}\", \"from\": \"${netdata}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
899 "https://${hipchat_server}/v2/room/${room}/notification")
901 if [ "${httpcode}" == "204" ]
903 info "sent HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
906 error "failed to send HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}' with HTTP error code ${httpcode}."
910 [ ${sent} -gt 0 ] && return 0
917 # -----------------------------------------------------------------------------
921 local accesskey="${1}" messagebirdnumber="${2}" recipients="${3}" title="${4}" message="${5}" httpcode sent=0 user
922 if [ "${SEND_MESSAGEBIRD}" = "YES" -a ! -z "${accesskey}" -a ! -z "${messagebirdnumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
924 #https://developers.messagebird.com/docs/messaging
925 for user in ${recipients}
927 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
928 --data-urlencode "originator=${messagebirdnumber}" \
929 --data-urlencode "recipients=${user}" \
930 --data-urlencode "body=${title} ${message}" \
931 --data-urlencode "datacoding=auto" \
932 -H "Authorization: AccessKey ${accesskey}" \
933 "https://rest.messagebird.com/messages")
935 if [ "${httpcode}" == "201" ]
937 info "sent Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
940 error "failed to send Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
944 [ ${sent} -gt 0 ] && return 0
950 # -----------------------------------------------------------------------------
954 local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
956 if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
958 if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
960 for chatid in ${chatids}
962 # https://core.telegram.org/bots/api#sendmessage
963 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
964 --data-urlencode "parse_mode=HTML" \
965 --data-urlencode "disable_web_page_preview=true" \
966 --data-urlencode "text=${message}" \
967 "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
969 if [ "${httpcode}" == "200" ]
971 info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
973 elif [ "${httpcode}" == "401" ]
975 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
977 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
981 [ ${sent} -gt 0 ] && return 0
987 # -----------------------------------------------------------------------------
991 local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
993 [ "${SEND_SLACK}" != "YES" ] && return 1
996 WARNING) color="warning" ;;
997 CRITICAL) color="danger" ;;
998 CLEAR) color="good" ;;
999 *) color="#777777" ;;
1002 for channel in ${channels}
1004 payload="$(cat <<EOF
1006 "channel": "#${channel}",
1007 "username": "netdata on ${host}",
1008 "icon_url": "${images_base_url}/images/seo-performance-128.png",
1009 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1012 "fallback": "${alarm} - ${chart} (${family}) - ${info}",
1013 "color": "${color}",
1014 "title": "${alarm}",
1015 "title_link": "${goto_url}",
1019 "title": "${chart}",
1023 "title": "${family}",
1027 "thumb_url": "${image}",
1028 "footer": "<${goto_url}|${host}>",
1036 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1037 if [ "${httpcode}" == "200" ]
1039 info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1042 error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1046 [ ${sent} -gt 0 ] && return 0
1051 # -----------------------------------------------------------------------------
1055 local webhook="${1}/slack" channels="${2}" httpcode sent=0 channel color payload
1057 [ "${SEND_DISCORD}" != "YES" ] && return 1
1060 WARNING) color="warning" ;;
1061 CRITICAL) color="danger" ;;
1062 CLEAR) color="good" ;;
1063 *) color="#777777" ;;
1066 for channel in ${channels}
1068 payload="$(cat <<EOF
1070 "channel": "#${channel}",
1071 "username": "netdata on ${host}",
1072 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1073 "icon_url": "${images_base_url}/images/seo-performance-128.png",
1076 "color": "${color}",
1077 "title": "${alarm}",
1078 "title_link": "${goto_url}",
1082 "title": "${chart}",
1083 "value": "${family}"
1086 "thumb_url": "${image}",
1087 "footer_icon": "${images_base_url}/images/seo-performance-128.png",
1088 "footer": "${host}",
1096 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1097 if [ "${httpcode}" == "200" ]
1099 info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1102 error "failed to send discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1106 [ ${sent} -gt 0 ] && return 0
1112 # -----------------------------------------------------------------------------
1113 # prepare the content of the notification
1115 # the url to send the user on click
1116 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
1117 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
1118 urlencode "${family}" >/dev/null; url_family="${REPLY}"
1119 urlencode "${name}" >/dev/null; url_name="${REPLY}"
1120 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}"
1122 # the severity of the alarm
1123 severity="${status}"
1125 # the time the alarm was raised
1126 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
1127 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
1128 raised_for="(was ${old_status,,} for ${duration_txt})"
1130 # the key status message
1131 status_message="status unknown"
1133 # the color of the alarm
1137 alarm="${name//_/ } = ${value_string}"
1139 # the image of the alarm
1140 image="${images_base_url}/images/seo-performance-128.png"
1142 # prepare the title based on status
1145 image="${images_base_url}/images/alert-128-red.png"
1146 status_message="is critical"
1151 image="${images_base_url}/images/alert-128-orange.png"
1152 status_message="needs attention"
1157 image="${images_base_url}/images/check-mark-2-128-green.png"
1158 status_message="recovered"
1163 if [ "${status}" = "CLEAR" ]
1165 severity="Recovered from ${old_status}"
1166 if [ ${non_clear_duration} -gt ${duration} ]
1168 raised_for="(alarm was raised for ${non_clear_duration_txt})"
1171 # don't show the value when the status is CLEAR
1172 # for certain alarms, this value might not have any meaning
1173 alarm="${name//_/ } ${raised_for}"
1175 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
1177 severity="Escalated to ${status}"
1178 if [ ${non_clear_duration} -gt ${duration} ]
1180 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1183 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
1185 severity="Demoted to ${status}"
1186 if [ ${non_clear_duration} -gt ${duration} ]
1188 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1195 # prepare HTML versions of elements
1197 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
1200 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
1202 # -----------------------------------------------------------------------------
1203 # send the slack notification
1205 # slack aggregates posts from the same username
1206 # so we use "${host} ${status}" as the bot username, to make them diff
1208 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1211 # -----------------------------------------------------------------------------
1212 # send the discord notification
1214 # discord aggregates posts from the same username
1215 # so we use "${host} ${status}" as the bot username, to make them diff
1217 send_discord "${DISCORD_WEBHOOK_URL}" "${to_discord}"
1220 # -----------------------------------------------------------------------------
1221 # send the pushover notification
1223 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
1224 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>
1225 <small><b>${chart}</b><br/>Chart<br/> </small>
1226 <small><b>${family}</b><br/>Family<br/> </small>
1227 <small><b>${severity}</b><br/>Severity<br/> </small>
1228 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
1229 <a href=\"${goto_url}\">View Netdata</a><br/>
1230 <small><small>The source of this alarm is line ${src}</small></small>
1235 # -----------------------------------------------------------------------------
1236 # send the pushbullet notification
1238 send_pushbullet "${PUSHBULLET_ACCESS_TOKEN}" "${to_pushbullet}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}\n
1239 Severity: ${severity}\n
1242 To View Netdata go to: ${goto_url}\n
1243 The source of this alarm is line ${src}"
1247 # -----------------------------------------------------------------------------
1248 # send the twilio SMS
1250 send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1251 Severity: ${severity}
1258 # -----------------------------------------------------------------------------
1259 # send the messagebird SMS
1261 send_messagebird "${MESSAGEBIRD_ACCESS_KEY}" "${MESSAGEBIRD_NUMBER}" "${to_messagebird}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1262 Severity: ${severity}
1270 # -----------------------------------------------------------------------------
1271 # send the telegram.org message
1273 # https://core.telegram.org/bots/api#formatting-options
1274 send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${host} ${status_message} - <b>${name//_/ }</b>
1275 ${chart} (${family})
1276 <a href=\"${goto_url}\">${alarm}</a>
1282 # -----------------------------------------------------------------------------
1283 # send the kafka message
1289 # -----------------------------------------------------------------------------
1290 # send the pagerduty.com message
1296 # -----------------------------------------------------------------------------
1297 # send hipchat message
1299 send_hipchat "${HIPCHAT_CUSTOM_SERVER}" "${HIPCHAT_AUTH_TOKEN}" "${to_hipchat}" " \
1300 ${host} ${status_message}<br/> \
1301 <b>${alarm}</b> ${info_html}<br/> \
1302 <b>${chart}</b> (family <b>${family}</b>)<br/> \
1303 <b>${date}${raised_for_html}</b><br/> \
1304 <a href=\\\"${goto_url}\\\">View netdata dashboard</a> \
1305 (source of alarm ${src}) \
1310 # -----------------------------------------------------------------------------
1315 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
1316 Content-Type: text/html
1318 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1319 <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;">
1320 <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;">
1324 <td style="vertical-align: top;" valign="top"></td>
1325 <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
1326 <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
1327 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
1330 <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
1331 <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
1335 <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">
1336 <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
1340 <td style="vertical-align: top;" valign="top">
1341 <div style="margin: 0; padding: 20px; max-width: 700px;">
1342 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
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>${chart}</span>
1347 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</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><b>${alarm}</b>${info_html}</span>
1353 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
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">
1358 <span>${family}</span>
1359 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
1362 <tr style="margin: 0; padding: 0;">
1363 <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">
1364 <span>${severity}</span>
1365 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
1368 <tr style="margin: 0; padding: 0;">
1369 <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>
1370 <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>
1373 <tr style="margin: 0; padding: 0;">
1374 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
1375 <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>
1378 <tr style="text-align: center; margin: 0; padding: 0;">
1379 <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>
1382 <tr style="text-align: center; margin: 0; padding: 0;">
1383 <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
1384 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
1405 # -----------------------------------------------------------------------------
1408 if [ ${SENT_EMAIL} -eq 0 \
1409 -o ${SENT_PUSHOVER} -eq 0 \
1410 -o ${SENT_TELEGRAM} -eq 0 \
1411 -o ${SENT_SLACK} -eq 0 \
1412 -o ${SENT_DISCORD} -eq 0 \
1413 -o ${SENT_TWILIO} -eq 0 \
1414 -o ${SENT_HIPCHAT} -eq 0 \
1415 -o ${SENT_MESSAGEBIRD} -eq 0 \
1416 -o ${SENT_PUSHBULLET} -eq 0 \
1417 -o ${SENT_KAFKA} -eq 0 \
1418 -o ${SENT_PD} -eq 0 \
1421 # we did send something
1425 # we did not send anything