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
61 DEFAULT_RECIPIENT_PUSHOVER=
62 declare -A role_recipients_pushover=()
65 DEFAULT_RECIPIENT_EMAIL="root"
66 declare -A role_recipients_email=()
68 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
70 source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
73 # -----------------------------------------------------------------------------
74 # find the exact recipient per method
76 to_email="${role_recipients_email[${recipient}]}"
77 [ -z "${to_email}" ] && to_email="${DEFAULT_RECIPIENT_EMAIL}"
78 [ -z "${to_email}" ] && to_email="root"
80 to_pushover="${role_recipients_pushover[${recipient}]}"
81 [ -z "${to_pushover}" ] && to_pushover="${DEFAULT_RECIPIENT_EMAIL}"
82 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
84 # -----------------------------------------------------------------------------
85 # verify the delivery methods supported
87 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
89 if [ "${SEND_PUSHOVER}" = "YES" -a -z "${curl}" ]
91 curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
92 [ -z "${curl}" ] && SEND_PUSHOVER="NO"
95 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
97 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
98 [ -z "${sendmail}" ] && SEND_EMAIL="NO"
101 # check that we have at least a method enabled
102 if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" ]
104 echo >&2 "I don't have a means to send a notification. Sorry!"
108 # -----------------------------------------------------------------------------
109 # get the system hostname
111 [ -z "${hostname}" ] && hostname="${NETDATA_HOSTNAME}"
112 [ -z "${hostname}" ] && hostname="${NETDATA_REGISTRY_HOSTNAME}"
113 [ -z "${hostname}" ] && hostname="$(hostname 2>/dev/null)"
115 # -----------------------------------------------------------------------------
116 # get the date the alarm happened
118 date="$(date --date=@${when} 2>/dev/null)"
119 [ -z "${date}" ] && date="$(date 2>/dev/null)"
121 # -----------------------------------------------------------------------------
122 # convert a duration in seconds, to a human readable duration
123 # using DAYS, MINUTES, SECONDS
126 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
128 s=$(( s - (d * 86400) ))
130 s=$(( s - (h * 3600) ))
132 s=$(( s - (m * 60) ))
136 [ ${m} -ge 30 ] && h=$(( h + 1 ))
137 [ ${d} -gt 1 ] && ds="days"
138 [ ${h} -gt 1 ] && hs="hours"
141 echo "${d} ${ds} and ${h} ${hs}"
147 [ ${s} -ge 30 ] && m=$(( m + 1 ))
148 [ ${h} -gt 1 ] && hs="hours"
149 [ ${m} -gt 1 ] && ms="minutes"
152 echo "${h} ${hs} and ${m} ${ms}"
158 [ ${m} -gt 1 ] && ms="minutes"
159 [ ${s} -gt 1 ] && ss="seconds"
162 echo "${m} ${ms} and ${s} ${ss}"
167 [ ${s} -gt 1 ] && ss="seconds"
172 # -----------------------------------------------------------------------------
176 if [ "${SEND_EMAIL}" = "YES" ]
183 echo >&2 "${me}: Sent notification email for ${status} on '${chart}.${name}'"
186 echo >&2 "${me}: FAILED to send notification email for ${status} on '${chart}.${name}'"
194 # -----------------------------------------------------------------------------
198 local apptoken="${1}" usertoken="${2}" title="${3}" message="${4}" httpcode sent=0 user
200 if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertoken}" -a ! -z "${title}" -a ! -z "${message}" ]
203 for user in ${usertoken//,/ }
205 httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
206 --form-string "token=${apptoken}" \
207 --form-string "user=${user}" \
208 --form-string "html=1" \
209 --form-string "title=${title}" \
210 --form-string "message=${message}" \
211 https://api.pushover.net/1/messages.json)
213 if [ "${httpcode}" == "200" ]
215 echo >&2 "${me}: Sent notification push for ${status} on '${chart}.${name}' to '${user}'"
218 echo >&2 "${me}: FAILED to send notification push for ${status} on '${chart}.${name}' to '${user}' with HTTP error code ${httpcode}."
222 [ ${sent} -gt 0 ] && return 0
229 # -----------------------------------------------------------------------------
230 # prepare the content of the notification
232 # description of the alarm
233 [ ! -z "${info}" ] && info=" <small><br/>${info}</small>"
235 # the url to send the user on click
236 goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?machine_guid=${NETDATA_REGISTRY_UNIQUE_ID}&chart=${chart}&family=${family}"
238 # the severity of the alarm
241 # the time the alarm was raised
242 raised_for="<br/><small>(was ${old_status,,} for $(duration4human ${duration}))</small>"
244 # the key status message
245 status_message="status unknown"
247 # the color of the alarm
251 alarm="${name//_/ } = ${value} ${units}"
253 # prepare the title based on status
256 status_message="is critical"
261 status_message="needs attention"
266 status_message="recovered"
269 # don't show the value when the status is CLEAR
270 # for certain alarms, this value might not have any meaning
275 if [ "${status}" = "CLEAR" ]
277 severity="Recovered from ${old_status}"
278 if [ $non_clear_duration -gt $duration ]
280 raised_for="<br/><small>(had issues for $(duration4human ${non_clear_duration}))</small>"
283 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
285 severity="Escalated to ${status}"
286 if [ $non_clear_duration -gt $duration ]
288 raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
291 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
293 severity="Demoted to ${status}"
294 if [ $non_clear_duration -gt $duration ]
296 raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
304 # -----------------------------------------------------------------------------
307 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${hostname} ${status_message} - ${chart}.${name}" "<font size="5" color=\"${color}\"><b>${hostname} ${status_message}</b></font>
309 <b>${alarm}</b>${info}
313 Severity: ${severity}
316 <a href=\"${goto_url}\">View Netdata</a>
318 <small>The source of this alarm is line ${src}</small>"
322 # -----------------------------------------------------------------------------
325 cat <<EOF | send_email
327 Subject: ${hostname} ${status_message} - ${chart}.${name}
328 Content-Type: text/html
330 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
331 <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;">
332 <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">
336 <td style="vertical-align:top;" valign="top"></td>
337 <td width="700" style="vertical-align:top;display:block!important;max-width:700px!important;clear:both!important;margin:0 auto;padding:0" valign="top">
338 <div style="max-width:700px;display:block;margin:0 auto;padding:20px">
339 <table width="100%" cellpadding="0" cellspacing="0"
340 style="background:#fff;border:1px solid #e9e9e9">
344 style="padding: 5px 20px 5px 20px;background-color:#eee;">
345 <div style="font-size:20px;color:#777;font-weight: bold;">netdata notification</div>
349 <td bgcolor="${color}"
350 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">
351 <h1 style="font-weight:400;margin:0">${hostname} ${status_message}</h1>
355 <td style="vertical-align:top" valign="top">
356 <div style="margin:0;padding:20px;max-width:700px">
357 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
360 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
361 align="left" valign="top">
362 <span>${chart}</span>
363 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Chart</span>
366 <tr style="margin:0;padding:0">
367 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
368 align="left" valign="top">
369 <span><b>${alarm}</b>${info}</span>
370 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Alarm</span>
374 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
375 align="left" valign="top">
376 <span>${family}</span>
377 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Family</span>
380 <tr style="margin:0;padding:0">
381 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
382 align="left" valign="top">
383 <span>${severity}</span>
384 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Severity</span>
387 <tr style="margin:0;padding:0">
388 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
389 align="left" valign="top"><span>${date}</span>
390 <span>${raised_for}</span> <span
391 style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Time</span>
395 <tr style="margin:0;padding:0">
396 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px">
397 <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>
401 <tr style="text-align:center;margin:0;padding:0">
402 <td style="font-size:11px;vertical-align:top;margin:0;padding:10px 0 0 0;color:#666666"
403 align="center" valign="bottom">The source of this alarm is line <code>${src}</code>
406 <tr style="text-align:center;margin:0;padding:0">
407 <td style="font-size:12px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
408 align="center" valign="bottom">Sent by
409 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
430 # -----------------------------------------------------------------------------
433 # we did send somehting
434 [ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 ] && exit 0
436 # we did not send anything