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 # - pushover.net notifications by @ktsaou
20 # - pushbullet.com push notifications by Tiago Peralta @tperalta82 PR #1070
21 # - telegram.org notifications by @hashworks PR #1002
22 # - twilio.com notifications by Levi Blaney @shadycuz PR #1211
23 # - kafka notifications by @ktsaou #1342
24 # - pagerduty.com notifications by Jim Cooley @jimcooley PR #1373
25 # - messagebird.com notifications by @tech_no_logical #1453
26 # - hipchart notifications by @ktsaou #1561
28 # -----------------------------------------------------------------------------
29 # testing notifications
31 if [ \( "${1}" = "test" -o "${2}" = "test" \) -a "${#}" -le 2 ]
33 if [ "${2}" = "test" ]
40 [ -z "${recipient}" ] && recipient="sysadmin"
44 for x in "CRITICAL" "WARNING" "CLEAR"
47 echo >&2 "# SENDING TEST ${x} ALARM TO ROLE: ${recipient}"
49 "${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"
64 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
67 # -----------------------------------------------------------------------------
69 PROGRAM_NAME="$(basename "${0}")"
72 date "+%Y-%m-%d %H:%M:%S"
79 echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
102 [ ${debug} -eq 1 ] && log DEBUG "${@}"
105 # -----------------------------------------------------------------------------
107 # check for BASH v4+ (required for associative arrays)
108 [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ] && \
109 fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
111 # -----------------------------------------------------------------------------
112 # defaults to allow running this script by hand
114 NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
115 NETDATA_CACHE_DIR="${NETDATA_CACHE_DIR-/var/cache/netdata}"
116 [ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
117 [ -z "${NETDATA_HOSTNAME}" ] && NETDATA_HOSTNAME="$(hostname)"
118 [ -z "${NETDATA_REGISTRY_HOSTNAME}" ] && NETDATA_REGISTRY_HOSTNAME="${NETDATA_HOSTNAME}"
120 # -----------------------------------------------------------------------------
121 # parse command line parameters
123 roles="${1}" # the roles that should be notified for this event
124 host="${2}" # the host generated this event
125 unique_id="${3}" # the unique id of this event
126 alarm_id="${4}" # the unique id of the alarm that generated this event
127 event_id="${5}" # the incremental id of the event, for this alarm id
128 when="${6}" # the timestamp this event occurred
129 name="${7}" # the name of the alarm, as given in netdata health.d entries
130 chart="${8}" # the name of the chart (type.id)
131 family="${9}" # the family of the chart
132 status="${10}" # the current status : REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
133 old_status="${11}" # the previous status: REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
134 value="${12}" # the current value of the alarm
135 old_value="${13}" # the previous value of the alarm
136 src="${14}" # the line number and file the alarm has been configured
137 duration="${15}" # the duration in seconds of the previous alarm state
138 non_clear_duration="${16}" # the total duration in seconds this is/was non-clear
139 units="${17}" # the units of the value
140 info="${18}" # a short description of the alarm
141 value_string="${19}" # friendly value (with units)
142 old_value_string="${20}" # friendly old value (with units)
144 # -----------------------------------------------------------------------------
145 # screen statuses we don't need to send a notification
147 # don't do anything if this is not WARNING, CRITICAL or CLEAR
148 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
150 info "not sending notification for ${status} on '${chart}.${name}'"
154 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
155 if [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
157 info "not sending notification for ${status} on '${chart}.${name}' (last status was ${old_status})"
161 # -----------------------------------------------------------------------------
164 # By default fetch images from the global public registry.
165 # This is required by default, since all notification methods need to download
166 # images via the Internet, and private registries might not be reachable.
167 # This can be overwritten at the configuration file.
168 images_base_url="https://registry.my-netdata.io"
171 # if empty they will be searched in the system path
175 # enable / disable features
180 SEND_MESSAGEBIRD="YES"
183 SEND_PUSHBULLET="YES"
189 DEFAULT_RECIPIENT_SLACK=
190 declare -A role_recipients_slack=()
194 DEFAULT_RECIPIENT_PUSHOVER=
195 declare -A role_recipients_pushover=()
198 PUSHBULLET_ACCESS_TOKEN=
199 DEFAULT_RECIPIENT_PUSHBULLET=
200 declare -A role_recipients_pushbullet=()
204 TWILIO_ACCOUNT_TOKEN=
206 DEFAULT_RECIPIENT_TWILIO=
207 declare -A role_recipients_twilio=()
211 DEFAULT_RECIPIENT_HIPCHAT=
212 declare -A role_recipients_hipchat=()
214 # messagebird configs
215 MESSAGEBIRD_ACCESS_KEY=
217 DEFAULT_RECIPIENT_MESSAGEBIRD=
218 declare -A role_recipients_messagebird=()
222 DEFAULT_RECIPIENT_TELEGRAM=
223 declare -A role_recipients_telegram=()
229 # pagerduty.com configs
231 declare -A role_recipients_pd=()
234 DEFAULT_RECIPIENT_EMAIL="root"
235 declare -A role_recipients_email=()
237 # load the user configuration
238 # this will overwrite the variables above
239 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
241 source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
244 # -----------------------------------------------------------------------------
245 # filter a recipient based on alarm event severity
247 filter_recipient_by_criticality() {
248 local method="${1}" x="${2}" r s
251 r="${x/|*/}" # the recipient
252 s="${x/*|/}" # the severity required for notifying this recipient
254 # no severity filtering for this person
255 [ "${r}" = "${s}" ] && return 0
257 # the severity is invalid
259 [ "${s}" != "CRITICAL" ] && return 0
261 # the new or the old status matches the severity
262 if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
264 [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
265 mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
267 # we need to keep track of the notifications we sent
268 # so that the same user will receive the recovery
269 # even if old_status does not match the required severity
270 touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
274 # it is a cleared alarm we have sent notification for
275 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
277 rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
284 # -----------------------------------------------------------------------------
285 # find the recipients' addresses per method
287 declare -A arr_slack=()
288 declare -A arr_pushover=()
289 declare -A arr_pushbullet=()
290 declare -A arr_twilio=()
291 declare -A arr_hipchat=()
292 declare -A arr_telegram=()
294 declare -A arr_email=()
296 # netdata may call us with multiple roles, and roles may have multiple but
297 # overlapping recipients - so, here we find the unique recipients.
298 for x in ${roles//,/ }
300 # the roles 'silent' and 'disabled' mean:
301 # don't send a notification for this role
302 [ "${x}" = "silent" -o "${x}" = "disabled" ] && continue
305 a="${role_recipients_email[${x}]}"
306 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
309 [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
313 a="${role_recipients_pushover[${x}]}"
314 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
317 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
321 a="${role_recipients_pushbullet[${x}]}"
322 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHBULLET}"
325 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1"
329 a="${role_recipients_twilio[${x}]}"
330 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TWILIO}"
333 [ "${r}" != "disabled" ] && filter_recipient_by_criticality twilio "${r}" && arr_twilio[${r/|*/}]="1"
337 a="${role_recipients_hipchat[${x}]}"
338 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_HIPCHAT}"
341 [ "${r}" != "disabled" ] && filter_recipient_by_criticality hipchat "${r}" && arr_hipchat[${r/|*/}]="1"
345 a="${role_recipients_messagebird[${x}]}"
346 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_MESSAGEBIRD}"
349 [ "${r}" != "disabled" ] && filter_recipient_by_criticality messagebird "${r}" && arr_messagebird[${r/|*/}]="1"
353 a="${role_recipients_telegram[${x}]}"
354 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
357 [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
361 a="${role_recipients_slack[${x}]}"
362 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
365 [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
369 a="${role_recipients_pd[${x}]}"
370 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PD}"
373 [ "${r}" != "disabled" ] && filter_recipient_by_criticality pd "${r}" && arr_pd[${r/|*/}]="1"
377 # build the list of slack recipients (channels)
378 to_slack="${!arr_slack[*]}"
379 [ -z "${to_slack}" ] && SEND_SLACK="NO"
381 # build the list of pushover recipients (user tokens)
382 to_pushover="${!arr_pushover[*]}"
383 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
385 # build the list of pushbulet recipients (user tokens)
386 to_pushbullet="${!arr_pushbullet[*]}"
387 [ -z "${to_pushbullet}" ] && SEND_PUSHBULLET="NO"
389 # build the list of twilio recipients (phone numbers)
390 to_twilio="${!arr_twilio[*]}"
391 [ -z "${to_twilio}" ] && SEND_TWILIO="NO"
393 # build the list of hipchat recipients (rooms)
394 to_hipchat="${!arr_hipchat[*]}"
395 [ -z "${to_hipchat}" ] && SEND_HIPCHAT="NO"
397 # build the list of messagebird recipients (phone numbers)
398 to_messagebird="${!arr_messagebird[*]}"
399 [ -z "${to_messagebird}" ] && SEND_MESSAGEBIRD="NO"
401 # check array of telegram recipients (chat ids)
402 to_telegram="${!arr_telegram[*]}"
403 [ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
405 # build the list of pagerduty recipients (service keys)
406 to_pd="${!arr_pd[*]}"
407 [ -z "${to_pd}" ] && SEND_PD="NO"
409 # build the list of email recipients (email addresses)
411 for x in "${!arr_email[@]}"
413 [ ! -z "${to_email}" ] && to_email="${to_email}, "
414 to_email="${to_email}${x}"
416 [ -z "${to_email}" ] && SEND_EMAIL="NO"
419 # -----------------------------------------------------------------------------
420 # verify the delivery methods supported
423 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
426 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
429 [ -z "${PUSHBULLET_ACCESS_TOKEN}" ] && SEND_PUSHBULLET="NO"
432 [ -z "${TWILIO_ACCOUNT_TOKEN}" -o -z "${TWILIO_ACCOUNT_SID}" -o -z "${TWILIO_NUMBER}" ] && SEND_TWILIO="NO"
435 [ -z "${HIPCHAT_AUTH_TOKEN}" ] && SEND_HIPCHAT="NO"
438 [ -z "${MESSAGEBIRD_ACCESS_KEY}" -o -z "${MESSAGEBIRD_NUMBER}" ] && SEND_MESSAGEBIRD="NO"
441 [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
444 [ -z "${KAFKA_URL}" -o -z "${KAFKA_SENDER_IP}" ] && SEND_KAFKA="NO"
446 # check pagerduty.com
447 # if we need pd-send, check for the pd-send command
448 # https://www.pagerduty.com/docs/guides/agent-install-guide/
449 if [ "${SEND_PD}" = "YES" ]
451 pd_send="$(which pd-send 2>/dev/null || command -v pd-send 2>/dev/null)"
452 if [ -z "${pd_send}" ]
454 # no pd-send available
455 # disable pagerduty.com
460 # if we need curl, check for the curl command
462 "${SEND_PUSHOVER}" = "YES" \
463 -o "${SEND_SLACK}" = "YES" \
464 -o "${SEND_HIPCHAT}" = "YES" \
465 -o "${SEND_TWILIO}" = "YES" \
466 -o "${SEND_MESSAGEBIRD}" = "YES" \
467 -o "${SEND_TELEGRAM}" = "YES" \
468 -o "${SEND_PUSHBULLET}" = "YES" \
469 -o "${SEND_KAFKA}" = "YES" \
472 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
476 # disable all curl based methods
483 SEND_MESSAGEBIRD="NO"
488 # if we need sendmail, check for the sendmail command
489 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
491 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
492 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
495 # check that we have at least a method enabled
496 if [ "${SEND_EMAIL}" != "YES" \
497 -a "${SEND_PUSHOVER}" != "YES" \
498 -a "${SEND_TELEGRAM}" != "YES" \
499 -a "${SEND_SLACK}" != "YES" \
500 -a "${SEND_TWILIO}" != "YES" \
501 -a "${SEND_HIPCHAT}" != "YES" \
502 -a "${SEND_MESSAGEBIRD}" != "YES" \
503 -a "${SEND_PUSHBULLET}" != "YES" \
504 -a "${SEND_KAFKA}" != "YES" \
505 -a "${SEND_PD}" != "YES" \
508 fatal "All notification methods are disabled. Not sending notification to '${roles}' for '${name}' = '${value}' of chart '${chart}' for status '${status}'."
511 # -----------------------------------------------------------------------------
512 # find a suitable hostname to use, if netdata did not supply a hostname
514 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
515 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
516 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
518 # -----------------------------------------------------------------------------
519 # get the date the alarm happened
521 date="$(date --date=@${when} 2>/dev/null)"
522 [ -z "${date}" ] && date="$(date 2>/dev/null)"
524 # -----------------------------------------------------------------------------
525 # function to URL encode a string
528 local string="${1}" strlen encoded pos c o
531 for (( pos=0 ; pos<strlen ; pos++ ))
540 printf -v o '%%%02x' "'${c}"
550 # -----------------------------------------------------------------------------
551 # function to convert a duration in seconds, to a human readable duration
552 # using DAYS, MINUTES, SECONDS
555 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
557 s=$(( s - (d * 86400) ))
559 s=$(( s - (h * 3600) ))
561 s=$(( s - (m * 60) ))
565 [ ${m} -ge 30 ] && h=$(( h + 1 ))
566 [ ${d} -gt 1 ] && ds="days"
567 [ ${h} -gt 1 ] && hs="hours"
570 ret="${d} ${ds} and ${h} ${hs}"
576 [ ${s} -ge 30 ] && m=$(( m + 1 ))
577 [ ${h} -gt 1 ] && hs="hours"
578 [ ${m} -gt 1 ] && ms="minutes"
581 ret="${h} ${hs} and ${m} ${ms}"
587 [ ${m} -gt 1 ] && ms="minutes"
588 [ ${s} -gt 1 ] && ss="seconds"
591 ret="${m} ${ms} and ${s} ${ss}"
596 [ ${s} -gt 1 ] && ss="seconds"
604 # -----------------------------------------------------------------------------
609 if [ "${SEND_EMAIL}" = "YES" ]
617 info "sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
620 error "failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
628 # -----------------------------------------------------------------------------
632 local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
634 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
637 # https://pushover.net/api
640 CLEAR) priority=-1;; # low priority: no sound or vibration
641 WARNING) priotity=0;; # normal priority: respect quiet hours
642 CRITICAL) priority=1;; # high priority: bypass quiet hours
643 *) priority=-2;; # lowest priority: no notification at all
646 for user in ${usertokens}
648 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
649 --form-string "token=${apptoken}" \
650 --form-string "user=${user}" \
651 --form-string "html=1" \
652 --form-string "title=${title}" \
653 --form-string "message=${message}" \
654 --form-string "timestamp=${when}" \
655 --form-string "url=${url}" \
656 --form-string "url_title=Open netdata dashboard to view the alarm" \
657 --form-string "priority=${priority}" \
658 https://api.pushover.net/1/messages.json)
660 if [ "${httpcode}" == "200" ]
662 info "sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
665 error "failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
669 [ ${sent} -gt 0 ] && return 0
675 # -----------------------------------------------------------------------------
679 local userapikey="${1}" recipients="${2}" title="${3}" message="${4}" httpcode sent=0 user
680 if [ "${SEND_PUSHBULLET}" = "YES" -a ! -z "${userapikey}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
682 #https://docs.pushbullet.com/#create-push
683 for user in ${recipients}
685 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
686 --header 'Access-Token: '${userapikey}'' \
687 --header 'Content-Type: application/json' \
688 --data-binary @<(cat <<EOF
689 {"title": "${title}",
692 "body": "$( echo -n ${message})"}
694 ) "https://api.pushbullet.com/v2/pushes" -X POST)
696 if [ "${httpcode}" == "200" ]
698 info "sent pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
701 error "failed to send pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
705 [ ${sent} -gt 0 ] && return 0
711 # -----------------------------------------------------------------------------
715 local httpcode sent=0
716 if [ "${SEND_KAFKA}" = "YES" ]
718 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
719 --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}\"}" \
722 if [ "${httpcode}" == "204" ]
724 info "sent kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}'"
727 error "failed to send kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}' with HTTP error code ${httpcode}."
730 [ ${sent} -gt 0 ] && return 0
736 # -----------------------------------------------------------------------------
737 # pagerduty.com sender
740 local recipients="${1}" sent=0
744 WARNING) t='trigger';;
745 CRITICAL) t='trigger';;
748 if [ ${SEND_PD} = "YES" -a ! -z "${t}" ]
750 for PD_SERVICE_KEY in ${recipients}
752 d="${status} ${name} = ${value_string} - ${host}, ${family}"
753 ${pd_send} -k ${PD_SERVICE_KEY} \
757 -f 'info'="${info}" \
758 -f 'value_w_units'="${value_string}" \
759 -f 'when'="${when}" \
760 -f 'duration'="${duration}" \
761 -f 'roles'="${roles}" \
762 -f 'host'="${host}" \
763 -f 'unique_id'="${unique_id}" \
764 -f 'alarm_id'="${alarm_id}" \
765 -f 'event_id'="${event_id}" \
766 -f 'name'="${name}" \
767 -f 'chart'="${chart}" \
768 -f 'family'="${family}" \
769 -f 'status'="${status}" \
770 -f 'old_status'="${old_status}" \
771 -f 'value'="${value}" \
772 -f 'old_value'="${old_value}" \
774 -f 'non_clear_duration'="${non_clear_duration}" \
775 -f 'units'="${units}"
777 if [ ${retval} -eq 0 ]
779 info "sent pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}....: ${d}"
782 error "failed to send pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}.... (error code ${retval}): ${d}"
786 [ ${sent} -gt 0 ] && return 0
792 # -----------------------------------------------------------------------------
796 local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}" title="${5}" message="${6}" httpcode sent=0 user
797 if [ "${SEND_TWILIO}" = "YES" -a ! -z "${accountsid}" -a ! -z "${accounttoken}" -a ! -z "${twilionumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
799 #https://www.twilio.com/packages/labs/code/bash/twilio-sms
800 for user in ${recipients}
802 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
803 --data-urlencode "From=${twilionumber}" \
804 --data-urlencode "To=${user}" \
805 --data-urlencode "Body=${title} ${message}" \
806 -u "${accountsid}:${accounttoken}" \
807 "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json")
809 if [ "${httpcode}" == "201" ]
811 info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
814 error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
818 [ ${sent} -gt 0 ] && return 0
825 # -----------------------------------------------------------------------------
829 local authtoken="${1}" recipients="${2}" message="${3}" httpcode sent=0 room color sender msg_format notify
831 if [ "${SEND_HIPCHAT}" = "YES" -a ! -z "${authtoken}" -a ! -z "${recipients}" -a ! -z "${message}" ]
834 # A label to be shown in addition to the sender's name
835 # Valid length range: 0 - 64.
838 # Valid values: html, text.
839 # Defaults to 'html'.
842 # Background color for message. Valid values: yellow, green, red, purple, gray, random. Defaults to 'yellow'.
844 WARNING) color="yellow" ;;
845 CRITICAL) color="red" ;;
846 CLEAR) color="green" ;;
850 # Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc).
851 # Each recipient's notification preferences are taken into account.
855 for room in ${recipients}
857 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
858 -H "Content-type: application/json" \
859 -H "Authorization: Bearer ${authtoken}" \
860 -d "{\"color\": \"${color}\", \"from\": \"${netdata}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
861 "https://api.hipchat.com/v2/room/${room}/notification")
863 if [ "${httpcode}" == "200" ]
865 info "sent HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
868 error "failed to send HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}' with HTTP error code ${httpcode}."
872 [ ${sent} -gt 0 ] && return 0
879 # -----------------------------------------------------------------------------
883 local accesskey="${1}" messagebirdnumber="${2}" recipients="${3}" title="${4}" message="${5}" httpcode sent=0 user
884 if [ "${SEND_MESSAGEBIRD}" = "YES" -a ! -z "${accesskey}" -a ! -z "${messagebirdnumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
886 #https://developers.messagebird.com/docs/messaging
887 for user in ${recipients}
889 httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
890 --data-urlencode "originator=${messagebirdnumber}" \
891 --data-urlencode "recipients=${user}" \
892 --data-urlencode "body=${title} ${message}" \
893 --data-urlencode "datacoding=auto" \
894 -H "Authorization: AccessKey ${accesskey}" \
895 "https://rest.messagebird.com/messages")
897 if [ "${httpcode}" == "201" ]
899 info "sent Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
902 error "failed to send Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
906 [ ${sent} -gt 0 ] && return 0
912 # -----------------------------------------------------------------------------
916 local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
918 if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
920 if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
922 for chatid in ${chatids}
924 # https://core.telegram.org/bots/api#sendmessage
925 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
926 --data-urlencode "parse_mode=HTML" \
927 --data-urlencode "disable_web_page_preview=true" \
928 --data-urlencode "text=${message}" \
929 "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
931 if [ "${httpcode}" == "200" ]
933 info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
935 elif [ "${httpcode}" == "401" ]
937 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
939 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
943 [ ${sent} -gt 0 ] && return 0
949 # -----------------------------------------------------------------------------
953 local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
955 [ "${SEND_SLACK}" != "YES" ] && return 1
958 WARNING) color="warning" ;;
959 CRITICAL) color="danger" ;;
960 CLEAR) color="good" ;;
961 *) color="#777777" ;;
964 for channel in ${channels}
968 "channel": "#${channel}",
969 "username": "netdata on ${host}",
970 "icon_url": "${images_base_url}/images/seo-performance-128.png",
971 "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
974 "fallback": "${alarm} - ${chart} (${family}) - ${info}",
977 "title_link": "${goto_url}",
985 "title": "${family}",
989 "thumb_url": "${image}",
990 "footer": "<${goto_url}|${host}>",
998 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
999 if [ "${httpcode}" == "200" ]
1001 info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1004 error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1008 [ ${sent} -gt 0 ] && return 0
1014 # -----------------------------------------------------------------------------
1015 # prepare the content of the notification
1017 # the url to send the user on click
1018 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
1019 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
1020 urlencode "${family}" >/dev/null; url_family="${REPLY}"
1021 urlencode "${name}" >/dev/null; url_name="${REPLY}"
1022 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}"
1024 # the severity of the alarm
1025 severity="${status}"
1027 # the time the alarm was raised
1028 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
1029 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
1030 raised_for="(was ${old_status,,} for ${duration_txt})"
1032 # the key status message
1033 status_message="status unknown"
1035 # the color of the alarm
1039 alarm="${name//_/ } = ${value_string}"
1041 # the image of the alarm
1042 image="${images_base_url}/images/seo-performance-128.png"
1044 # prepare the title based on status
1047 image="${images_base_url}/images/alert-128-red.png"
1048 status_message="is critical"
1053 image="${images_base_url}/images/alert-128-orange.png"
1054 status_message="needs attention"
1059 image="${images_base_url}/images/check-mark-2-128-green.png"
1060 status_message="recovered"
1065 if [ "${status}" = "CLEAR" ]
1067 severity="Recovered from ${old_status}"
1068 if [ ${non_clear_duration} -gt ${duration} ]
1070 raised_for="(alarm was raised for ${non_clear_duration_txt})"
1073 # don't show the value when the status is CLEAR
1074 # for certain alarms, this value might not have any meaning
1075 alarm="${name//_/ } ${raised_for}"
1077 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
1079 severity="Escalated to ${status}"
1080 if [ ${non_clear_duration} -gt ${duration} ]
1082 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1085 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
1087 severity="Demoted to ${status}"
1088 if [ ${non_clear_duration} -gt ${duration} ]
1090 raised_for="(alarm is raised for ${non_clear_duration_txt})"
1097 # prepare HTML versions of elements
1099 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
1102 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
1104 # -----------------------------------------------------------------------------
1105 # send the slack notification
1107 # slack aggregates posts from the same username
1108 # so we use "${host} ${status}" as the bot username, to make them diff
1110 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1113 # -----------------------------------------------------------------------------
1114 # send the pushover notification
1116 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
1117 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>
1118 <small><b>${chart}</b><br/>Chart<br/> </small>
1119 <small><b>${family}</b><br/>Family<br/> </small>
1120 <small><b>${severity}</b><br/>Severity<br/> </small>
1121 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
1122 <a href=\"${goto_url}\">View Netdata</a><br/>
1123 <small><small>The source of this alarm is line ${src}</small></small>
1128 # -----------------------------------------------------------------------------
1129 # send the pushbullet notification
1131 send_pushbullet "${PUSHBULLET_ACCESS_TOKEN}" "${to_pushbullet}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}\n
1132 Severity: ${severity}\n
1135 To View Netdata go to: ${goto_url}\n
1136 The source of this alarm is line ${src}"
1140 # -----------------------------------------------------------------------------
1141 # send the twilio SMS
1143 send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1144 Severity: ${severity}
1151 # -----------------------------------------------------------------------------
1152 # send the messagebird SMS
1154 send_messagebird "${MESSAGEBIRD_ACCESS_KEY}" "${MESSAGEBIRD_NUMBER}" "${to_messagebird}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}
1155 Severity: ${severity}
1163 # -----------------------------------------------------------------------------
1164 # send the telegram.org message
1166 # https://core.telegram.org/bots/api#formatting-options
1167 send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${host} ${status_message} - <b>${name//_/ }</b>
1168 ${chart} (${family})
1169 <a href=\"${goto_url}\">${alarm}</a>
1175 # -----------------------------------------------------------------------------
1176 # send the kafka message
1182 # -----------------------------------------------------------------------------
1183 # send the pagerduty.com message
1189 # -----------------------------------------------------------------------------
1190 # send hipchat message
1192 send_hipchat "${HIPCHAT_AUTH_TOKEN}" "${to_hipchat}" "
1193 <b>${alarm}</b> ${info_html}<br/>
1194 <small><b>${chart}</b><br/>Chart<br/> </small>
1195 <small><b>${family}</b><br/>Family<br/> </small>
1196 <small><b>${severity}</b><br/>Severity<br/> </small>
1197 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
1198 <a href=\"${goto_url}\">View Netdata</a><br/>
1199 <small><small>The source of this alarm is line ${src}</small></small>
1204 # -----------------------------------------------------------------------------
1209 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
1210 Content-Type: text/html
1212 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1213 <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;">
1214 <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;">
1218 <td style="vertical-align: top;" valign="top"></td>
1219 <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
1220 <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
1221 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
1224 <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
1225 <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
1229 <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">
1230 <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
1234 <td style="vertical-align: top;" valign="top">
1235 <div style="margin: 0; padding: 20px; max-width: 700px;">
1236 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
1239 <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">
1240 <span>${chart}</span>
1241 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
1244 <tr style="margin: 0; padding: 0;">
1245 <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">
1246 <span><b>${alarm}</b>${info_html}</span>
1247 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
1251 <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">
1252 <span>${family}</span>
1253 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
1256 <tr style="margin: 0; padding: 0;">
1257 <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">
1258 <span>${severity}</span>
1259 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
1262 <tr style="margin: 0; padding: 0;">
1263 <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>
1264 <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>
1267 <tr style="margin: 0; padding: 0;">
1268 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
1269 <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>
1272 <tr style="text-align: center; margin: 0; padding: 0;">
1273 <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>
1276 <tr style="text-align: center; margin: 0; padding: 0;">
1277 <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
1278 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
1299 # -----------------------------------------------------------------------------
1302 if [ ${SENT_EMAIL} -eq 0 \
1303 -o ${SENT_PUSHOVER} -eq 0 \
1304 -o ${SENT_TELEGRAM} -eq 0 \
1305 -o ${SENT_SLACK} -eq 0 \
1306 -o ${SENT_TWILIO} -eq 0 \
1307 -o ${SENT_HIPCHAT} -eq 0 \
1308 -o ${SENT_MESSAGEBIRD} -eq 0 \
1309 -o ${SENT_PUSHBULLET} -eq 0 \
1310 -o ${SENT_KAFKA} -eq 0 \
1311 -o ${SENT_PD} -eq 0 \
1314 # we did send something
1318 # we did not send anything