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=()
218 DEFAULT_RECIPIENT_HIPCHAT=
219 declare -A role_recipients_hipchat=()
221 # messagebird configs
222 MESSAGEBIRD_ACCESS_KEY=
224 DEFAULT_RECIPIENT_MESSAGEBIRD=
225 declare -A role_recipients_messagebird=()
229 DEFAULT_RECIPIENT_TELEGRAM=
230 declare -A role_recipients_telegram=()
236 # pagerduty.com configs
238 declare -A role_recipients_pd=()
241 DEFAULT_RECIPIENT_EMAIL="root"
242 declare -A role_recipients_email=()
244 # load the user configuration
245 # this will overwrite the variables above
246 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
248 source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
251 # -----------------------------------------------------------------------------
252 # filter a recipient based on alarm event severity
254 filter_recipient_by_criticality() {
255 local method="${1}" x="${2}" r s
258 r="${x/|*/}" # the recipient
259 s="${x/*|/}" # the severity required for notifying this recipient
261 # no severity filtering for this person
262 [ "${r}" = "${s}" ] && return 0
264 # the severity is invalid
266 [ "${s}" != "CRITICAL" ] && return 0
268 # the new or the old status matches the severity
269 if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
271 [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
272 mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
274 # we need to keep track of the notifications we sent
275 # so that the same user will receive the recovery
276 # even if old_status does not match the required severity
277 touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
281 # it is a cleared alarm we have sent notification for
282 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
284 rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
291 # -----------------------------------------------------------------------------
292 # find the recipients' addresses per method
294 declare -A arr_slack=()
295 declare -A arr_discord=()
296 declare -A arr_pushover=()
297 declare -A arr_pushbullet=()
298 declare -A arr_twilio=()
299 declare -A arr_hipchat=()
300 declare -A arr_telegram=()
302 declare -A arr_email=()
304 # netdata may call us with multiple roles, and roles may have multiple but
305 # overlapping recipients - so, here we find the unique recipients.
306 for x in ${roles//,/ }
308 # the roles 'silent' and 'disabled' mean:
309 # don't send a notification for this role
310 [ "${x}" = "silent" -o "${x}" = "disabled" ] && continue
313 a="${role_recipients_email[${x}]}"
314 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
317 [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
321 a="${role_recipients_pushover[${x}]}"
322 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
325 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
329 a="${role_recipients_pushbullet[${x}]}"
330 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHBULLET}"
333 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1"
337 a="${role_recipients_twilio[${x}]}"
338 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TWILIO}"
341 [ "${r}" != "disabled" ] && filter_recipient_by_criticality twilio "${r}" && arr_twilio[${r/|*/}]="1"
345 a="${role_recipients_hipchat[${x}]}"
346 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_HIPCHAT}"
349 [ "${r}" != "disabled" ] && filter_recipient_by_criticality hipchat "${r}" && arr_hipchat[${r/|*/}]="1"
353 a="${role_recipients_messagebird[${x}]}"
354 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_MESSAGEBIRD}"
357 [ "${r}" != "disabled" ] && filter_recipient_by_criticality messagebird "${r}" && arr_messagebird[${r/|*/}]="1"
361 a="${role_recipients_telegram[${x}]}"
362 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
365 [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
369 a="${role_recipients_slack[${x}]}"
370 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
373 [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
377 a="${role_recipients_discord[${x}]}"
378 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_DISCORD}"
381 [ "${r}" != "disabled" ] && filter_recipient_by_criticality discord "${r}" && arr_discord[${r/|*/}]="1"
385 a="${role_recipients_pd[${x}]}"
386 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PD}"
389 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pd "${r}" && arr_pd[${r/|*/}]="1"
393 # build the list of slack recipients (channels)
394 to_slack="${!arr_slack[*]}"
395 [ -z "${to_slack}" ] && SEND_SLACK="NO"
397 # build the list of discord recipients (channels)
398 to_discord="${!arr_discord[*]}"
399 [ -z "${to_discord}" ] && SEND_DISCORD="NO"
401 # build the list of pushover recipients (user tokens)
402 to_pushover="${!arr_pushover[*]}"
403 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
405 # build the list of pushbulet recipients (user tokens)
406 to_pushbullet="${!arr_pushbullet[*]}"
407 [ -z "${to_pushbullet}" ] && SEND_PUSHBULLET="NO"
409 # build the list of twilio recipients (phone numbers)
410 to_twilio="${!arr_twilio[*]}"
411 [ -z "${to_twilio}" ] && SEND_TWILIO="NO"
413 # build the list of hipchat recipients (rooms)
414 to_hipchat="${!arr_hipchat[*]}"
415 [ -z "${to_hipchat}" ] && SEND_HIPCHAT="NO"
417 # build the list of messagebird recipients (phone numbers)
418 to_messagebird="${!arr_messagebird[*]}"
419 [ -z "${to_messagebird}" ] && SEND_MESSAGEBIRD="NO"
421 # check array of telegram recipients (chat ids)
422 to_telegram="${!arr_telegram[*]}"
423 [ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
425 # build the list of pagerduty recipients (service keys)
426 to_pd="${!arr_pd[*]}"
427 [ -z "${to_pd}" ] && SEND_PD="NO"
429 # build the list of email recipients (email addresses)
431 for x in "${!arr_email[@]}"
433 [ ! -z "${to_email}" ] && to_email="${to_email}, "
434 to_email="${to_email}${x}"
436 [ -z "${to_email}" ] && SEND_EMAIL="NO"
439 # -----------------------------------------------------------------------------
440 # verify the delivery methods supported
443 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
446 [ -z "${DISCORD_WEBHOOK_URL}" ] && SEND_DISCORD="NO"
449 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
452 [ -z "${PUSHBULLET_ACCESS_TOKEN}" ] && SEND_PUSHBULLET="NO"
455 [ -z "${TWILIO_ACCOUNT_TOKEN}" -o -z "${TWILIO_ACCOUNT_SID}" -o -z "${TWILIO_NUMBER}" ] && SEND_TWILIO="NO"
458 [ -z "${HIPCHAT_AUTH_TOKEN}" ] && SEND_HIPCHAT="NO"
461 [ -z "${MESSAGEBIRD_ACCESS_KEY}" -o -z "${MESSAGEBIRD_NUMBER}" ] && SEND_MESSAGEBIRD="NO"
464 [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
467 [ -z "${KAFKA_URL}" -o -z "${KAFKA_SENDER_IP}" ] && SEND_KAFKA="NO"
469 # check pagerduty.com
470 # if we need pd-send, check for the pd-send command
471 # https://www.pagerduty.com/docs/guides/agent-install-guide/
472 if [ "${SEND_PD}" = "YES" ]
474 pd_send="$(which pd-send 2>/dev/null || command -v pd-send 2>/dev/null)"
475 if [ -z "${pd_send}" ]
477 # no pd-send available
478 # disable pagerduty.com
483 # if we need curl, check for the curl command
485 "${SEND_PUSHOVER}" = "YES" \
486 -o "${SEND_SLACK}" = "YES" \
487 -o "${SEND_DISCORD}" = "YES" \
488 -o "${SEND_HIPCHAT}" = "YES" \
489 -o "${SEND_TWILIO}" = "YES" \
490 -o "${SEND_MESSAGEBIRD}" = "YES" \
491 -o "${SEND_TELEGRAM}" = "YES" \
492 -o "${SEND_PUSHBULLET}" = "YES" \
493 -o "${SEND_KAFKA}" = "YES" \
496 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
500 # disable all curl based methods
508 SEND_MESSAGEBIRD="NO"
513 # if we need sendmail, check for the sendmail command
514 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
516 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
517 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
520 # check that we have at least a method enabled
521 if [ "${SEND_EMAIL}" != "YES" \
522 -a "${SEND_PUSHOVER}" != "YES" \
523 -a "${SEND_TELEGRAM}" != "YES" \
524 -a "${SEND_SLACK}" != "YES" \
525 -a "${SEND_DISCORD}" != "YES" \
526 -a "${SEND_TWILIO}" != "YES" \
527 -a "${SEND_HIPCHAT}" != "YES" \
528 -a "${SEND_MESSAGEBIRD}" != "YES" \
529 -a "${SEND_PUSHBULLET}" != "YES" \
530 -a "${SEND_KAFKA}" != "YES" \
531 -a "${SEND_PD}" != "YES" \
534 fatal "All notification methods are disabled. Not sending notification to '${roles}' for '${name}' = '${value}' of chart '${chart}' for status '${status}'."
537 # -----------------------------------------------------------------------------
538 # find a suitable hostname to use, if netdata did not supply a hostname
540 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
541 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
542 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
544 # -----------------------------------------------------------------------------
545 # get the date the alarm happened
547 date="$(date --date=@${when} 2>/dev/null)"
548 [ -z "${date}" ] && date="$(date 2>/dev/null)"
550 # -----------------------------------------------------------------------------
551 # function to URL encode a string
554 local string="${1}" strlen encoded pos c o
557 for (( pos=0 ; pos<strlen ; pos++ ))
566 printf -v o '%%%02x' "'${c}"
576 # -----------------------------------------------------------------------------
577 # function to convert a duration in seconds, to a human readable duration
578 # using DAYS, MINUTES, SECONDS
581 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
583 s=$(( s - (d * 86400) ))
585 s=$(( s - (h * 3600) ))
587 s=$(( s - (m * 60) ))
591 [ ${m} -ge 30 ] && h=$(( h + 1 ))
592 [ ${d} -gt 1 ] && ds="days"
593 [ ${h} -gt 1 ] && hs="hours"
596 ret="${d} ${ds} and ${h} ${hs}"
602 [ ${s} -ge 30 ] && m=$(( m + 1 ))
603 [ ${h} -gt 1 ] && hs="hours"
604 [ ${m} -gt 1 ] && ms="minutes"
607 ret="${h} ${hs} and ${m} ${ms}"
613 [ ${m} -gt 1 ] && ms="minutes"
614 [ ${s} -gt 1 ] && ss="seconds"
617 ret="${m} ${ms} and ${s} ${ss}"
622 [ ${s} -gt 1 ] && ss="seconds"
630 # -----------------------------------------------------------------------------
635 if [ "${SEND_EMAIL}" = "YES" ]
643 info "sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
646 error "failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
654 # -----------------------------------------------------------------------------
658 local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
660 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
663 # https://pushover.net/api
666 CLEAR) priority=-1;; # low priority: no sound or vibration
667 WARNING) priotity=0;; # normal priority: respect quiet hours
668 CRITICAL) priority=1;; # high priority: bypass quiet hours
669 *) priority=-2;; # lowest priority: no notification at all
672 for user in ${usertokens}
674 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
675 --form-string "token=${apptoken}" \
676 --form-string "user=${user}" \
677 --form-string "html=1" \
678 --form-string "title=${title}" \
679 --form-string "message=${message}" \
680 --form-string "timestamp=${when}" \
681 --form-string "url=${url}" \
682 --form-string "url_title=Open netdata dashboard to view the alarm" \
683 --form-string "priority=${priority}" \
684 https://api.pushover.net/1/messages.json)
686 if [ "${httpcode}" == "200" ]
688 info "sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
691 error "failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
695 [ ${sent} -gt 0 ] && return 0
701 # -----------------------------------------------------------------------------
705 local userapikey="${1}" recipients="${2}" title="${3}" message="${4}" httpcode sent=0 user
706 if [ "${SEND_PUSHBULLET}" = "YES" -a ! -z "${userapikey}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
708 #https://docs.pushbullet.com/#create-push
709 for user in ${recipients}
711 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
712 --header 'Access-Token: '${userapikey}'' \
713 --header 'Content-Type: application/json' \
714 --data-binary @<(cat <<EOF
715 {"title": "${title}",
718 "body": "$( echo -n ${message})"}
720 ) "https://api.pushbullet.com/v2/pushes" -X POST)
722 if [ "${httpcode}" == "200" ]
724 info "sent pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
727 error "failed to send pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
731 [ ${sent} -gt 0 ] && return 0
737 # -----------------------------------------------------------------------------
741 local httpcode sent=0
742 if [ "${SEND_KAFKA}" = "YES" ]
744 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
745 --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}\"}" \
748 if [ "${httpcode}" == "204" ]
750 info "sent kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}'"
753 error "failed to send kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}' with HTTP error code ${httpcode}."
756 [ ${sent} -gt 0 ] && return 0
762 # -----------------------------------------------------------------------------
763 # pagerduty.com sender
766 local recipients="${1}" sent=0
770 WARNING) t='trigger';;
771 CRITICAL) t='trigger';;
774 if [ ${SEND_PD} = "YES" -a ! -z "${t}" ]
776 for PD_SERVICE_KEY in ${recipients}
778 d="${status} ${name} = ${value_string} - ${host}, ${family}"
779 ${pd_send} -k ${PD_SERVICE_KEY} \
783 -f 'info'="${info}" \
784 -f 'value_w_units'="${value_string}" \
785 -f 'when'="${when}" \
786 -f 'duration'="${duration}" \
787 -f 'roles'="${roles}" \
788 -f 'host'="${host}" \
789 -f 'unique_id'="${unique_id}" \
790 -f 'alarm_id'="${alarm_id}" \
791 -f 'event_id'="${event_id}" \
792 -f 'name'="${name}" \
793 -f 'chart'="${chart}" \
794 -f 'family'="${family}" \
795 -f 'status'="${status}" \
796 -f 'old_status'="${old_status}" \
797 -f 'value'="${value}" \
798 -f 'old_value'="${old_value}" \
800 -f 'non_clear_duration'="${non_clear_duration}" \
801 -f 'units'="${units}"
803 if [ ${retval} -eq 0 ]
805 info "sent pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}....: ${d}"
808 error "failed to send pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}.... (error code ${retval}): ${d}"
812 [ ${sent} -gt 0 ] && return 0
818 # -----------------------------------------------------------------------------
822 local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}" title="${5}" message="${6}" httpcode sent=0 user
823 if [ "${SEND_TWILIO}" = "YES" -a ! -z "${accountsid}" -a ! -z "${accounttoken}" -a ! -z "${twilionumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
825 #https://www.twilio.com/packages/labs/code/bash/twilio-sms
826 for user in ${recipients}
828 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
829 --data-urlencode "From=${twilionumber}" \
830 --data-urlencode "To=${user}" \
831 --data-urlencode "Body=${title} ${message}" \
832 -u "${accountsid}:${accounttoken}" \
833 "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json")
835 if [ "${httpcode}" == "201" ]
837 info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
840 error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
844 [ ${sent} -gt 0 ] && return 0
851 # -----------------------------------------------------------------------------
855 local authtoken="${1}" recipients="${2}" message="${3}" httpcode sent=0 room color sender msg_format notify
857 if [ "${SEND_HIPCHAT}" = "YES" -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://api.hipchat.com/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