4 # pushover support by Jan Arnold
8 # -----------------------------------------------------------------------------
9 # parse command line parameters
11 recipient="${1}" # the recepient of the email
12 hostname="${2}" # the hostname this event refers to
13 unique_id="${3}" # the unique id of this event
14 alarm_id="${4}" # the unique id of the alarm that generated this event
15 event_id="${5}" # the incremental id of the event, for this alarm
16 when="${6}" # the timestamp this event occured
17 name="${7}" # the name of the alarm, as given in netdata health.d entries
18 chart="${8}" # the name of the chart (type.id)
19 family="${9}" # the family of the chart
20 status="${10}" # the current status : UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
21 old_status="${11}" # the previous status: UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
22 value="${12}" # the current value
23 old_value="${13}" # the previous value
24 src="${14}" # the line number and file the alarm has been configured
25 duration="${15}" # the duration in seconds the previous state took
26 non_clear_duration="${16}" # the total duration in seconds this is non-clear
27 units="${17}" # the units of the value
28 info="${18}" # a short description of the alarm
30 # -----------------------------------------------------------------------------
31 # screen statuses we don't need to send a notification
33 # don't do anything if this is not WARNING, CRITICAL or CLEAR
34 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
36 echo >&2 "${me}: not sending notification for ${status} on '${chart}.${name}'"
40 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
41 if [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
43 echo >&2 "${me}: not sending notification for ${status} on '${chart}.${name}' (last status was ${old_status})"
47 # -----------------------------------------------------------------------------
51 # if empty they will be searched in the system path
55 # enable / disable features
62 DEFAULT_RECIPIENT_SLACK=
63 declare -A role_recipients_slack=()
67 DEFAULT_RECIPIENT_PUSHOVER=
68 declare -A role_recipients_pushover=()
71 DEFAULT_RECIPIENT_EMAIL="root"
72 declare -A role_recipients_email=()
74 # load the user configuration
75 # this will overwrite the variables above
76 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
78 source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
81 # -----------------------------------------------------------------------------
82 # find the exact recipient per method
84 declare -A arr_slack=()
85 declare -A arr_pushover=()
86 declare -A arr_email=()
88 # netdata may call us with multiple recipients
89 # so, here we find the unique ones
93 a="${role_recipients_email[${recipient}]}"
94 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
95 for r in ${a//,/ }; do arr_email[${r}]="1"; done
98 a="${role_recipients_pushover[${recipient}]}"
99 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
100 for r in ${a//,/ }; do arr_pushover[${r}]="1"; done
103 a="${role_recipients_slack[${recipient}]}"
104 [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
105 for r in ${a//,/ }; do arr_slack[${r}]="1"; done
108 # build the list of slack recipients (channels)
109 to_slack="${!arr_slack[*]}"
110 [ -z "${to_slack}" ] && SEND_SLACK="NO"
112 # build the list of pushover recipients (user tokens)
113 to_pushover="${!arr_pushover[*]}"
114 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
116 # build the list of email recipients (email addresses)
118 for x in "${!arr_email[*]}"
120 [ ! -z "${to_email}" ] && to_email="${to_email}, "
121 to_email="${to_email}${x}"
123 [ -z "${to_email}" ] && to_email="root"
126 # -----------------------------------------------------------------------------
127 # verify the delivery methods supported
130 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
133 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
135 if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" \) -a -z "${curl}" ]
137 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
145 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
147 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
148 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
151 # check that we have at least a method enabled
152 if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_SLACK}" != "YES" ]
154 echo >&2 "I don't have a means to send a notification. Sorry!"
158 # -----------------------------------------------------------------------------
159 # get the system hostname
161 [ -z "${hostname}" ] && hostname="${NETDATA_HOSTNAME}"
162 [ -z "${hostname}" ] && hostname="${NETDATA_REGISTRY_HOSTNAME}"
163 [ -z "${hostname}" ] && hostname="$(hostname 2>/dev/null)"
165 # -----------------------------------------------------------------------------
166 # get the date the alarm happened
168 date="$(date --date=@${when} 2>/dev/null)"
169 [ -z "${date}" ] && date="$(date 2>/dev/null)"
171 # -----------------------------------------------------------------------------
172 # URL encode a string
175 local string="${1}" strlen encoded pos c o
178 for (( pos=0 ; pos<strlen ; pos++ ))
187 printf -v o '%%%02x' "'$c"
195 # -----------------------------------------------------------------------------
196 # convert a duration in seconds, to a human readable duration
197 # using DAYS, MINUTES, SECONDS
200 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
202 s=$(( s - (d * 86400) ))
204 s=$(( s - (h * 3600) ))
206 s=$(( s - (m * 60) ))
210 [ ${m} -ge 30 ] && h=$(( h + 1 ))
211 [ ${d} -gt 1 ] && ds="days"
212 [ ${h} -gt 1 ] && hs="hours"
215 echo "${d} ${ds} and ${h} ${hs}"
221 [ ${s} -ge 30 ] && m=$(( m + 1 ))
222 [ ${h} -gt 1 ] && hs="hours"
223 [ ${m} -gt 1 ] && ms="minutes"
226 echo "${h} ${hs} and ${m} ${ms}"
232 [ ${m} -gt 1 ] && ms="minutes"
233 [ ${s} -gt 1 ] && ss="seconds"
236 echo "${m} ${ms} and ${s} ${ss}"
241 [ ${s} -gt 1 ] && ss="seconds"
246 # -----------------------------------------------------------------------------
250 if [ "${SEND_EMAIL}" = "YES" ]
257 echo >&2 "${me}: Sent notification email for ${status} on '${chart}.${name}' to '${to_email}'"
260 echo >&2 "${me}: FAILED to send notification email for ${status} on '${chart}.${name}' to '${to_email}'"
268 # -----------------------------------------------------------------------------
272 local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
274 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
278 [ "${status}" = "CRITICAL" ] && priority=1
280 for user in ${usertokens}
282 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
283 --form-string "token=${apptoken}" \
284 --form-string "user=${user}" \
285 --form-string "html=1" \
286 --form-string "title=${title}" \
287 --form-string "message=${message}" \
288 --form-string "timestamp=${when}" \
289 --form-string "url=${url}" \
290 --form-string "url_title=Open netdata dashboard to view the alarm" \
291 --form-string "priority=${priority}" \
292 https://api.pushover.net/1/messages.json)
294 if [ "${httpcode}" == "200" ]
296 echo >&2 "${me}: Sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
299 echo >&2 "${me}: Failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
303 [ ${sent} -gt 0 ] && return 0
309 # -----------------------------------------------------------------------------
313 local webhook="${1}" channels="${2}" when="${3}" username="${4}" image="${5}" author="${6}" httpcode sent=0 channel color
315 if [ "${SEND_SLACK}" = "YES" -a ! -z "${webhook}" -a ! -z "${channels}" -a ! -z "${username}" -a ! -z "${image}" -a ! -z "${author}" ]
319 WARNING) color="warning" ;;
320 CRITICAL) color="danger" ;;
321 CLEAR) color="good" ;;
322 *) color="#777777" ;;
325 for channel in ${channels}
327 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode \
328 "payload={\"channel\": \"#${channel}\", \"username\": \"${username}\", \"text\": \"${hostname} ${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\": \"${NETDATA_REGISTRY_URL}/images/seo-performance-128.png\", \"ts\": ${when}}]}" \
331 if [ "${httpcode}" == "200" ]
333 echo >&2 "${me}: Sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
336 echo >&2 "${me}: Failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
340 [ ${sent} -gt 0 ] && return 0
347 # -----------------------------------------------------------------------------
348 # prepare the content of the notification
350 # the url to send the user on click
351 goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?host=$(urlencode "${NETDATA_REGISTRY_HOSTNAME}")&chart=${chart}&family=${family}"
353 # the severity of the alarm
356 # the time the alarm was raised
357 raised_for="(was ${old_status,,} for $(duration4human ${duration}))"
359 # the key status message
360 status_message="status unknown"
362 # the color of the alarm
366 alarm="${name//_/ } = ${value} ${units}"
368 # the image of the alarm
369 image="${NETDATA_REGISTRY_URL}/images/seo-performance-128.png"
371 # prepare the title based on status
374 image="${NETDATA_REGISTRY_URL}/images/alert-128-red.png"
375 status_message="is critical"
380 image="${NETDATA_REGISTRY_URL}/images/alert-128-orange.png"
381 status_message="needs attention"
386 image="${NETDATA_REGISTRY_URL}/images/check-mark-2-128-green.png"
387 status_message="recovered"
390 # don't show the value when the status is CLEAR
391 # for certain alarms, this value might not have any meaning
396 if [ "${status}" = "CLEAR" ]
398 severity="Recovered from ${old_status}"
399 if [ $non_clear_duration -gt $duration ]
401 raised_for="(had issues for $(duration4human ${non_clear_duration}))"
404 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
406 severity="Escalated to ${status}"
407 if [ $non_clear_duration -gt $duration ]
409 raised_for="(has issues for $(duration4human ${non_clear_duration}))"
412 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
414 severity="Demoted to ${status}"
415 if [ $non_clear_duration -gt $duration ]
417 raised_for="(has issues for $(duration4human ${non_clear_duration}))"
424 # prepare HTML versions of elements
426 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
429 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
431 # -----------------------------------------------------------------------------
432 # send the slack notification
434 # slack aggregates posts from the same username
435 # so we use "${hostname} ${status}" as the bot username, to make them diff
437 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}" "${when}" "${hostname} ${status}" "${image}" "${chart} (${family})"
440 # -----------------------------------------------------------------------------
441 # send the pushover notification
443 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${hostname} ${status_message} - ${name//_/ } - ${chart}" "
444 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>
445 <small><b>${chart}</b><br/>Chart<br/> </small>
446 <small><b>${family}</b><br/>Family<br/> </small>
447 <small><b>${severity}</b><br/>Severity<br/> </small>
448 <small><b>${date}${raised_for_html}</b><br/>Time<br/> </small>
449 <a href=\"${goto_url}\">View Netdata</a><br/>
450 <small><small>The source of this alarm is line ${src}</small></small>
455 # -----------------------------------------------------------------------------
460 Subject: ${hostname} ${status_message} - ${name//_/ } - ${chart}
461 Content-Type: text/html
463 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
464 <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;">
465 <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;">
469 <td style="vertical-align: top;" valign="top"></td>
470 <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
471 <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
472 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
475 <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
476 <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
480 <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">
481 <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${hostname} ${status_message}</h1>
485 <td style="vertical-align: top;" valign="top">
486 <div style="margin: 0; padding: 20px; max-width: 700px;">
487 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
490 <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">
491 <span>${chart}</span>
492 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
495 <tr style="margin: 0; padding: 0;">
496 <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">
497 <span><b>${alarm}</b>${info_html}</span>
498 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
502 <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">
503 <span>${family}</span>
504 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
507 <tr style="margin: 0; padding: 0;">
508 <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">
509 <span>${severity}</span>
510 <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
513 <tr style="margin: 0; padding: 0;">
514 <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>
515 <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>
518 <tr style="margin: 0; padding: 0;">
519 <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
520 <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>
523 <tr style="text-align: center; margin: 0; padding: 0;">
524 <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>
527 <tr style="text-align: center; margin: 0; padding: 0;">
528 <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
529 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
550 # -----------------------------------------------------------------------------
553 # we did send somehting
554 [ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0
556 # we did not send anything