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 # find the recipient's addresses per method
100 declare -A arr_slack=()
101 declare -A arr_pushover=()
102 declare -A arr_email=()
104 # netdata may call us with multiple recipients
105 # so, here we find the unique ones
106 for x in ${recipient//,/ }
109 a="${role_recipients_email[${recipient}]}"
110 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
111 for r in ${a//,/ }; do arr_email[${r}]="1"; done
114 a="${role_recipients_pushover[${recipient}]}"
115 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
116 for r in ${a//,/ }; do arr_pushover[${r}]="1"; done
119 a="${role_recipients_slack[${recipient}]}"
120 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
121 for r in ${a//,/ }; do arr_slack[${r}]="1"; done
124 # build the list of slack recipients (channels)
125 to_slack="${!arr_slack[*]}"
126 [ -z "${to_slack}" ] && SEND_SLACK="NO"
128 # build the list of pushover recipients (user tokens)
129 to_pushover="${!arr_pushover[*]}"
130 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
132 # build the list of email recipients (email addresses)
134 for x in "${!arr_email[@]}"
136 [ ! -z "${to_email}" ] && to_email="${to_email}, "
137 to_email="${to_email}${x}"
139 [ -z "${to_email}" ] && to_email="root"
142 # -----------------------------------------------------------------------------
143 # verify the delivery methods supported
146 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
149 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
151 if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" \) -a -z "${curl}" ]
153 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
161 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
163 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
164 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
167 # check that we have at least a method enabled
168 if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_SLACK}" != "YES" ]
170 echo >&2 "I don't have a means to send a notification. Sorry!"
174 # -----------------------------------------------------------------------------
175 # get the system hostname
177 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
178 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
179 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
181 # -----------------------------------------------------------------------------
182 # get the date the alarm happened
184 date="$(date --date=@${when} 2>/dev/null)"
185 [ -z "${date}" ] && date="$(date 2>/dev/null)"
187 # -----------------------------------------------------------------------------
188 # URL encode a string
191 local string="${1}" strlen encoded pos c o
194 for (( pos=0 ; pos<strlen ; pos++ ))
203 printf -v o '%%%02x' "'$c"
213 # -----------------------------------------------------------------------------
214 # convert a duration in seconds, to a human readable duration
215 # using DAYS, MINUTES, SECONDS
218 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
220 s=$(( s - (d * 86400) ))
222 s=$(( s - (h * 3600) ))
224 s=$(( s - (m * 60) ))
228 [ ${m} -ge 30 ] && h=$(( h + 1 ))
229 [ ${d} -gt 1 ] && ds="days"
230 [ ${h} -gt 1 ] && hs="hours"
233 ret="${d} ${ds} and ${h} ${hs}"
239 [ ${s} -ge 30 ] && m=$(( m + 1 ))
240 [ ${h} -gt 1 ] && hs="hours"
241 [ ${m} -gt 1 ] && ms="minutes"
244 ret="${h} ${hs} and ${m} ${ms}"
250 [ ${m} -gt 1 ] && ms="minutes"
251 [ ${s} -gt 1 ] && ss="seconds"
254 ret="${m} ${ms} and ${s} ${ss}"
259 [ ${s} -gt 1 ] && ss="seconds"
267 # -----------------------------------------------------------------------------
272 if [ "${SEND_EMAIL}" = "YES" ]
280 echo >&2 "${me}: Sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
283 echo >&2 "${me}: Failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
291 # -----------------------------------------------------------------------------
295 local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
297 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
300 # https://pushover.net/api
303 CLEAR) priority=-1;; # low priority: no sound or vibration
304 WARNING) priotity=0;; # normal priority: respect quiet hours
305 CRITICAL) priority=1;; # high priority: bypass quiet hours
306 *) priority=-2;; # lowest priority: no notification at all
309 for user in ${usertokens}
311 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
312 --form-string "token=${apptoken}" \
313 --form-string "user=${user}" \
314 --form-string "html=1" \
315 --form-string "title=${title}" \
316 --form-string "message=${message}" \
317 --form-string "timestamp=${when}" \
318 --form-string "url=${url}" \
319 --form-string "url_title=Open netdata dashboard to view the alarm" \
320 --form-string "priority=${priority}" \
321 https://api.pushover.net/1/messages.json)
323 if [ "${httpcode}" == "200" ]
325 echo >&2 "${me}: Sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
328 echo >&2 "${me}: Failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
332 [ ${sent} -gt 0 ] && return 0
338 # -----------------------------------------------------------------------------
342 local webhook="${1}" channels="${2}" when="${3}" username="${4}" image="${5}" author="${6}" httpcode sent=0 channel color
344 if [ "${SEND_SLACK}" = "YES" -a ! -z "${webhook}" -a ! -z "${channels}" -a ! -z "${username}" -a ! -z "${image}" -a ! -z "${author}" ]
348 WARNING) color="warning" ;;
349 CRITICAL) color="danger" ;;
350 CLEAR) color="good" ;;
351 *) color="#777777" ;;
354 for channel in ${channels}
356 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode \
357 "payload={\"channel\": \"#${channel}\", \"username\": \"${username}\", \"text\": \"${host} ${status_message} - ${author} ${raised_for} - 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\": \"${info}\", \"footer\": \"netdata\", \"footer_icon\": \"${images_base_url}/images/seo-performance-128.png\", \"ts\": ${when}}]}" \
360 if [ "${httpcode}" == "200" ]
362 echo >&2 "${me}: Sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
365 echo >&2 "${me}: Failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
369 [ ${sent} -gt 0 ] && return 0
376 # -----------------------------------------------------------------------------
377 # prepare the content of the notification
379 # the url to send the user on click
380 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
381 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
382 urlencode "${family}" >/dev/null; url_family="${REPLY}"
383 urlencode "${name}" >/dev/null; url_name="${REPLY}"
384 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}"
386 # the severity of the alarm
389 # the time the alarm was raised
390 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
391 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
392 raised_for="(was ${old_status,,} for ${duration_txt}"
394 # the key status message
395 status_message="status unknown"
397 # the color of the alarm
401 alarm="${name//_/ } = ${value} ${units}"
403 # the image of the alarm
404 image="${images_base_url}/images/seo-performance-128.png"
406 # prepare the title based on status
409 image="${images_base_url}/images/alert-128-red.png"
410 status_message="is critical"
415 image="${images_base_url}/images/alert-128-orange.png"
416 status_message="needs attention"
421 image="${images_base_url}/images/check-mark-2-128-green.png"
422 status_message="recovered"
425 # don't show the value when the status is CLEAR
426 # for certain alarms, this value might not have any meaning
431 if [ "${status}" = "CLEAR" ]
433 severity="Recovered from ${old_status}"
434 if [ $non_clear_duration -gt $duration ]
436 raised_for="(alarm was raised for ${non_clear_duration_txt})"
439 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
441 severity="Escalated to ${status}"
442 if [ $non_clear_duration -gt $duration ]
444 raised_for="(alarm is raised for ${non_clear_duration_txt})"
447 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
449 severity="Demoted to ${status}"
450 if [ $non_clear_duration -gt $duration ]
452 raised_for="(alarm is raised for ${non_clear_duration_txt})"
459 # prepare HTML versions of elements
461 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
464 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
466 # -----------------------------------------------------------------------------
467 # send the slack notification
469 # slack aggregates posts from the same username
470 # so we use "${host} ${status}" as the bot username, to make them diff
472 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}" "${when}" "${host} ${status}" "${image}" "${chart} (${family})"
475 # -----------------------------------------------------------------------------
476 # send the pushover notification
478 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
479 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>
480 <small><b>${chart}</b><br/>Chart<br/> </small>
481 <small><b>${family}</b><br/>Family<br/> </small>
482 <small><b>${severity}</b><br/>Severity<br/> </small>
483 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
484 <a href=\"${goto_url}\">View Netdata</a><br/>
485 <small><small>The source of this alarm is line ${src}</small></small>
490 # -----------------------------------------------------------------------------
495 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
496 Content-Type: text/html
498 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
499 <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;">
500 <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;">
504 <td style="vertical-align: top;" valign="top"></td>
505 <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
506 <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
507 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
510 <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
511 <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
515 <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">
516 <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
520 <td style="vertical-align: top;" valign="top">
521 <div style="margin: 0; padding: 20px; max-width: 700px;">
522 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
525 <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">
526 <span>${chart}</span>
527 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
530 <tr style="margin: 0; padding: 0;">
531 <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">
532 <span><b>${alarm}</b>${info_html}</span>
533 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
537 <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">
538 <span>${family}</span>
539 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
542 <tr style="margin: 0; padding: 0;">
543 <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">
544 <span>${severity}</span>
545 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
548 <tr style="margin: 0; padding: 0;">
549 <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>
550 <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>
553 <tr style="margin: 0; padding: 0;">
554 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
555 <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>
558 <tr style="text-align: center; margin: 0; padding: 0;">
559 <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>
562 <tr style="text-align: center; margin: 0; padding: 0;">
563 <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
564 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
585 # -----------------------------------------------------------------------------
588 # we did send somehting
589 [ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0
591 # we did not send anything