5 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
6 if [ -z "${sendmail}" ]
8 echo >&2 "I cannot send emails - there is no sendmail command available."
12 default_recipient_for_all_roles="root"
13 declare -A recipients=()
14 if [ -f "${NETDATA_CONFIG_DIR}/health_email_recipients.conf" ]
16 source "${NETDATA_CONFIG_DIR}/health_email_recipients.conf"
19 sendmail_from_pipe() {
24 echo >&2 "${me}: Sent notification email for ${status} on '${chart}.${name}'"
27 echo >&2 "${me}: FAILED to send notification email for ${status} on '${chart}.${name}'"
32 recipient="${1}" # the recepient of the email
33 hostname="${2}" # the hostname this event refers to
34 unique_id="${3}" # the unique id of this event
35 alarm_id="${4}" # the unique id of the alarm that generated this event
36 event_id="${5}" # the incremental id of the event, for this alarm
37 when="${6}" # the timestamp this event occured
38 name="${7}" # the name of the alarm, as given in netdata health.d entries
39 chart="${8}" # the name of the chart (type.id)
40 family="${9}" # the family of the chart
41 status="${10}" # the current status : UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
42 old_status="${11}" # the previous status: UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
43 value="${12}" # the current value
44 old_value="${13}" # the previous value
45 src="${14}" # the line number and file the alarm has been configured
46 duration="${15}" # the duration in seconds the previous state took
47 non_clear_duration="${16}" # the total duration in seconds this is non-clear
48 units="${17}" # the units of the value
49 info="${18}" # a short description of the alarm
51 to="${recipients[${recipient}]}"
52 [ -z "${to}" ] && to="${default_recipient_for_all_roles}"
53 [ -z "${to}" ] && to="root"
55 [ ! -z "${info}" ] && info=" <small><br/>${info}</small>"
57 # get the system hostname
58 [ -z "${hostname}" ] && hostname="${NETDATA_HOSTNAME}"
59 [ -z "${hostname}" ] && hostname="${NETDATA_REGISTRY_HOSTNAME}"
60 [ -z "${hostname}" ] && hostname="$(hostname 2>/dev/null)"
62 goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?machine_guid=${NETDATA_REGISTRY_UNIQUE_ID}&chart=${chart}&family=${family}"
64 date="$(date --date=@${when} 2>/dev/null)"
65 [ -z "${date}" ] && date="$(date 2>/dev/null)"
67 # convert a duration in seconds, to a human readable duration
68 # using DAYS, MINUTES, SECONDS
70 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
72 s=$(( s - (d * 86400) ))
74 s=$(( s - (h * 3600) ))
80 [ ${m} -ge 30 ] && h=$(( h + 1 ))
81 [ ${d} -gt 1 ] && ds="days"
82 [ ${h} -gt 1 ] && hs="hours"
85 echo "${d} ${ds} and ${h} ${hs}"
91 [ ${s} -ge 30 ] && m=$(( m + 1 ))
92 [ ${h} -gt 1 ] && hs="hours"
93 [ ${m} -gt 1 ] && ms="minutes"
96 echo "${h} ${hs} and ${m} ${ms}"
102 [ ${m} -gt 1 ] && ms="minutes"
103 [ ${s} -gt 1 ] && ss="seconds"
106 echo "${m} ${ms} and ${s} ${ss}"
111 [ ${s} -gt 1 ] && ss="seconds"
117 raised_for="<br/><small>(was ${old_status,,} for $(duration4human ${duration}))</small>"
118 status_message="status unknown"
120 alarm="${name} = ${value} ${units}"
122 # prepare the title based on status
125 status_message="is critical"
130 status_message="needs attention"
135 status_message="recovered"
138 # don't show the value when the status is CLEAR
139 # for certain alarms, this value might not have any meaning
144 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
146 # don't do anything if this is not WARNING, CRITICAL or CLEAR
147 echo >&2 "${me}: not sending notification email for ${status} on '${chart}.${name}'"
149 elif [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
151 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
152 echo >&2 "${me}: not sending notification email for ${status} on '${chart}.${name}' (last status was ${old_status})"
154 elif [ "${status}" = "CLEAR" ]
156 severity="Recovered from ${old_status}"
157 if [ $non_clear_duration -gt $duration ]
159 raised_for="<br/><small>(had issues for $(duration4human ${non_clear_duration}))</small>"
162 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
164 severity="Escalated to ${status}"
165 if [ $non_clear_duration -gt $duration ]
167 raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
170 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
172 severity="Demoted to ${status}"
173 if [ $non_clear_duration -gt $duration ]
175 raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
183 cat <<EOF | sendmail_from_pipe
185 Subject: ${hostname} ${status_message} - ${chart}.${name}
186 Content-Type: text/html
188 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
189 <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;">
190 <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">
194 <td style="vertical-align:top;" valign="top"></td>
195 <td width="700" style="vertical-align:top;display:block!important;max-width:700px!important;clear:both!important;margin:0 auto;padding:0" valign="top">
196 <div style="max-width:700px;display:block;margin:0 auto;padding:20px">
197 <table width="100%" cellpadding="0" cellspacing="0"
198 style="background:#fff;border:1px solid #e9e9e9">
202 style="padding: 5px 20px 5px 20px;background-color:#eee;">
203 <div style="font-size:20px;color:#777;font-weight: bold;">netdata notification</div>
207 <td bgcolor="${color}"
208 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">
209 <h1 style="font-weight:400;margin:0">${hostname} ${status_message}</h1>
213 <td style="vertical-align:top" valign="top">
214 <div style="margin:0;padding:20px;max-width:700px">
215 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
218 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
219 align="left" valign="top">
220 <span>${chart}</span>
221 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Chart</span>
224 <tr style="margin:0;padding:0">
225 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
226 align="left" valign="top">
227 <span><b>${alarm}</b>${info}</span>
228 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Alarm</span>
232 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
233 align="left" valign="top">
234 <span>${family}</span>
235 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Family</span>
238 <tr style="margin:0;padding:0">
239 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
240 align="left" valign="top">
241 <span>${severity}</span>
242 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Severity</span>
245 <tr style="margin:0;padding:0">
246 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
247 align="left" valign="top"><span>${date}</span>
248 <span>${raised_for}</span> <span
249 style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Time</span>
253 <tr style="margin:0;padding:0">
254 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px">
255 <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>
259 <tr style="text-align:center;margin:0;padding:0">
260 <td style="font-size:11px;vertical-align:top;margin:0;padding:10px 0 0 0;color:#666666"
261 align="center" valign="bottom">The source of this alarm is line <code>${src}</code>
264 <tr style="text-align:center;margin:0;padding:0">
265 <td style="font-size:12px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
266 align="center" valign="bottom">Sent by
267 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.