4 # pushover support by Jan Arnold
8 if [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ]
12 echo >&2 "BASH version 4 or later is required."
13 echo >&2 "You are running version: ${BASH_VERSION}"
14 echo >&2 "Please upgrade."
19 # -----------------------------------------------------------------------------
20 # parse command line parameters
22 recipient="${1}" # the recepient of the email
23 host="${2}" # the host this event refers to
24 unique_id="${3}" # the unique id of this event
25 alarm_id="${4}" # the unique id of the alarm that generated this event
26 event_id="${5}" # the incremental id of the event, for this alarm
27 when="${6}" # the timestamp this event occured
28 name="${7}" # the name of the alarm, as given in netdata health.d entries
29 chart="${8}" # the name of the chart (type.id)
30 family="${9}" # the family of the chart
31 status="${10}" # the current status : REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
32 old_status="${11}" # the previous status: REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
33 value="${12}" # the current value
34 old_value="${13}" # the previous value
35 src="${14}" # the line number and file the alarm has been configured
36 duration="${15}" # the duration in seconds the previous state took
37 non_clear_duration="${16}" # the total duration in seconds this is non-clear
38 units="${17}" # the units of the value
39 info="${18}" # a short description of the alarm
41 # -----------------------------------------------------------------------------
42 # screen statuses we don't need to send a notification
44 # don't do anything if this is not WARNING, CRITICAL or CLEAR
45 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
47 echo >&2 "${me}: not sending notification for ${status} on '${chart}.${name}'"
51 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
52 if [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
54 echo >&2 "${me}: not sending notification for ${status} on '${chart}.${name}' (last status was ${old_status})"
58 # -----------------------------------------------------------------------------
61 # this is defined here so that private registries
63 # images_base_url="${NETDATA_REGISTRY_URL}"
64 images_base_url="https://registry.my-netdata.io"
67 # if empty they will be searched in the system path
71 # enable / disable features
78 DEFAULT_RECIPIENT_SLACK=
79 declare -A role_recipients_slack=()
83 DEFAULT_RECIPIENT_PUSHOVER=
84 declare -A role_recipients_pushover=()
87 DEFAULT_RECIPIENT_EMAIL="root"
88 declare -A role_recipients_email=()
90 # load the user configuration
91 # this will overwrite the variables above
92 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
94 source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
97 # -----------------------------------------------------------------------------
98 # filter recipients based on the criticality of each
100 filter_recipient_by_criticality() {
101 local method="${1}" x="${2}" r s
107 # no severity filtering for this person
108 [ "${r}" = "${s}" ] && return 0
110 # the severity is invalid
112 [ "${s}" != "CRITICAL" ] && return 0
114 # the new or the old status matches the severity
115 if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
117 [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
118 mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
120 # we need to keep track of the notifications we sent
121 # so that the same user will receive the recovery
122 # even if old_status does not match the required severity
123 touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
127 # it is a cleared alarm we have sent notification for
128 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
130 rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
137 # -----------------------------------------------------------------------------
138 # find the recipient's addresses per method
140 declare -A arr_slack=()
141 declare -A arr_pushover=()
142 declare -A arr_email=()
144 # netdata may call us with multiple recipients
145 # so, here we find the unique ones
146 for x in ${recipient//,/ }
149 a="${role_recipients_email[${recipient}]}"
150 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
153 filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
157 a="${role_recipients_pushover[${recipient}]}"
158 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
161 filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
165 a="${role_recipients_slack[${recipient}]}"
166 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
169 filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
173 # build the list of slack recipients (channels)
174 to_slack="${!arr_slack[*]}"
175 [ -z "${to_slack}" ] && SEND_SLACK="NO"
177 # build the list of pushover recipients (user tokens)
178 to_pushover="${!arr_pushover[*]}"
179 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
181 # build the list of email recipients (email addresses)
183 for x in "${!arr_email[@]}"
185 [ ! -z "${to_email}" ] && to_email="${to_email}, "
186 to_email="${to_email}${x}"
188 [ -z "${to_email}" ] && SEND_EMAIL="NO"
191 # -----------------------------------------------------------------------------
192 # verify the delivery methods supported
195 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
198 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
200 if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" \) -a -z "${curl}" ]
202 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
210 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
212 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
213 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
216 # check that we have at least a method enabled
217 if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_SLACK}" != "YES" ]
219 echo >&2 "I don't have a means to send a notification. Sorry!"
223 # -----------------------------------------------------------------------------
224 # get the system hostname
226 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
227 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
228 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
230 # -----------------------------------------------------------------------------
231 # get the date the alarm happened
233 date="$(date --date=@${when} 2>/dev/null)"
234 [ -z "${date}" ] && date="$(date 2>/dev/null)"
236 # -----------------------------------------------------------------------------
237 # URL encode a string
240 local string="${1}" strlen encoded pos c o
243 for (( pos=0 ; pos<strlen ; pos++ ))
252 printf -v o '%%%02x' "'$c"
262 # -----------------------------------------------------------------------------
263 # convert a duration in seconds, to a human readable duration
264 # using DAYS, MINUTES, SECONDS
267 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
269 s=$(( s - (d * 86400) ))
271 s=$(( s - (h * 3600) ))
273 s=$(( s - (m * 60) ))
277 [ ${m} -ge 30 ] && h=$(( h + 1 ))
278 [ ${d} -gt 1 ] && ds="days"
279 [ ${h} -gt 1 ] && hs="hours"
282 ret="${d} ${ds} and ${h} ${hs}"
288 [ ${s} -ge 30 ] && m=$(( m + 1 ))
289 [ ${h} -gt 1 ] && hs="hours"
290 [ ${m} -gt 1 ] && ms="minutes"
293 ret="${h} ${hs} and ${m} ${ms}"
299 [ ${m} -gt 1 ] && ms="minutes"
300 [ ${s} -gt 1 ] && ss="seconds"
303 ret="${m} ${ms} and ${s} ${ss}"
308 [ ${s} -gt 1 ] && ss="seconds"
316 # -----------------------------------------------------------------------------
321 if [ "${SEND_EMAIL}" = "YES" ]
329 echo >&2 "${me}: Sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
332 echo >&2 "${me}: Failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
340 # -----------------------------------------------------------------------------
344 local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
346 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
349 # https://pushover.net/api
352 CLEAR) priority=-1;; # low priority: no sound or vibration
353 WARNING) priotity=0;; # normal priority: respect quiet hours
354 CRITICAL) priority=1;; # high priority: bypass quiet hours
355 *) priority=-2;; # lowest priority: no notification at all
358 for user in ${usertokens}
360 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
361 --form-string "token=${apptoken}" \
362 --form-string "user=${user}" \
363 --form-string "html=1" \
364 --form-string "title=${title}" \
365 --form-string "message=${message}" \
366 --form-string "timestamp=${when}" \
367 --form-string "url=${url}" \
368 --form-string "url_title=Open netdata dashboard to view the alarm" \
369 --form-string "priority=${priority}" \
370 https://api.pushover.net/1/messages.json)
372 if [ "${httpcode}" == "200" ]
374 echo >&2 "${me}: Sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
377 echo >&2 "${me}: Failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
381 [ ${sent} -gt 0 ] && return 0
387 # -----------------------------------------------------------------------------
391 local webhook="${1}" channels="${2}" when="${3}" username="${4}" image="${5}" httpcode sent=0 channel color
393 if [ "${SEND_SLACK}" = "YES" -a ! -z "${webhook}" -a ! -z "${channels}" -a ! -z "${username}" -a ! -z "${image}" ]
397 WARNING) color="warning" ;;
398 CRITICAL) color="danger" ;;
399 CLEAR) color="good" ;;
400 *) color="#777777" ;;
403 for channel in ${channels}
405 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode \
406 "payload={\"channel\": \"#${channel}\", \"username\": \"${username}\", \"text\": \"${host} ${status_message} - ${chart} (${family}) ${alarm} - click <${goto_url}|here> to view the netdata dashboard.\", \"icon_url\": \"${image}\", \"attachments\": [{\"fallback\": \"${alarm} - ${info}\", \"color\": \"${color}\", \"title\": \"${alarm}\", \"title_link\": \"${goto_url}\", \"text\": \"${chart} (${family}): ${info}\", \"footer\": \"netdata\", \"footer_icon\": \"${images_base_url}/images/seo-performance-128.png\", \"ts\": ${when}}]}" \
409 if [ "${httpcode}" == "200" ]
411 echo >&2 "${me}: Sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
414 echo >&2 "${me}: Failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
418 [ ${sent} -gt 0 ] && return 0
425 # -----------------------------------------------------------------------------
426 # prepare the content of the notification
428 # the url to send the user on click
429 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
430 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
431 urlencode "${family}" >/dev/null; url_family="${REPLY}"
432 urlencode "${name}" >/dev/null; url_name="${REPLY}"
433 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}"
435 # the severity of the alarm
438 # the time the alarm was raised
439 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
440 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
441 raised_for="(was ${old_status,,} for ${duration_txt}"
443 # the key status message
444 status_message="status unknown"
446 # the color of the alarm
450 alarm="${name//_/ } = ${value} ${units}"
452 # the image of the alarm
453 image="${images_base_url}/images/seo-performance-128.png"
455 # prepare the title based on status
458 image="${images_base_url}/images/alert-128-red.png"
459 status_message="is critical"
464 image="${images_base_url}/images/alert-128-orange.png"
465 status_message="needs attention"
470 image="${images_base_url}/images/check-mark-2-128-green.png"
471 status_message="recovered"
474 # don't show the value when the status is CLEAR
475 # for certain alarms, this value might not have any meaning
476 alarm="${name//_/ } ${raised_for}"
480 if [ "${status}" = "CLEAR" ]
482 severity="Recovered from ${old_status}"
483 if [ $non_clear_duration -gt $duration ]
485 raised_for="(alarm was raised for ${non_clear_duration_txt})"
488 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
490 severity="Escalated to ${status}"
491 if [ $non_clear_duration -gt $duration ]
493 raised_for="(alarm is raised for ${non_clear_duration_txt})"
496 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
498 severity="Demoted to ${status}"
499 if [ $non_clear_duration -gt $duration ]
501 raised_for="(alarm is raised for ${non_clear_duration_txt})"
508 # prepare HTML versions of elements
510 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
513 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
515 # -----------------------------------------------------------------------------
516 # send the slack notification
518 # slack aggregates posts from the same username
519 # so we use "${host} ${status}" as the bot username, to make them diff
521 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}" "${when}" "${host} ${status}" "${image}"
524 # -----------------------------------------------------------------------------
525 # send the pushover notification
527 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
528 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>
529 <small><b>${chart}</b><br/>Chart<br/> </small>
530 <small><b>${family}</b><br/>Family<br/> </small>
531 <small><b>${severity}</b><br/>Severity<br/> </small>
532 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
533 <a href=\"${goto_url}\">View Netdata</a><br/>
534 <small><small>The source of this alarm is line ${src}</small></small>
539 # -----------------------------------------------------------------------------
544 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
545 Content-Type: text/html
547 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
548 <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;">
549 <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;">
553 <td style="vertical-align: top;" valign="top"></td>
554 <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
555 <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
556 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
559 <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
560 <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
564 <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">
565 <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
569 <td style="vertical-align: top;" valign="top">
570 <div style="margin: 0; padding: 20px; max-width: 700px;">
571 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
574 <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">
575 <span>${chart}</span>
576 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
579 <tr style="margin: 0; padding: 0;">
580 <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">
581 <span><b>${alarm}</b>${info_html}</span>
582 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
586 <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">
587 <span>${family}</span>
588 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
591 <tr style="margin: 0; padding: 0;">
592 <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">
593 <span>${severity}</span>
594 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
597 <tr style="margin: 0; padding: 0;">
598 <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>
599 <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>
602 <tr style="margin: 0; padding: 0;">
603 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
604 <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>
607 <tr style="text-align: center; margin: 0; padding: 0;">
608 <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>
611 <tr style="text-align: center; margin: 0; padding: 0;">
612 <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
613 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
634 # -----------------------------------------------------------------------------
637 # we did send somehting
638 [ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0
640 # we did not send anything