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."
11 sendmail_from_pipe() {
16 echo >&2 "${me}: Sent notification email for ${status} on '${chart}.${name}'"
19 echo >&2 "${me}: FAILED to send notification email for ${status} on '${chart}.${name}'"
24 name="${1}" # the name of the alarm, as given in netdata health.d entries
25 chart="${2}" # the name of the chart (type.id)
26 status="${3}" # the current status : UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
27 old_status="${4}" # the previous status: UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
28 value="${5}" # the current value
29 old_value="${6}" # the previous value
30 src="${7}" # the line number and file the alarm has been configured
31 duration="${8}" # the duration in seconds the previous state took
32 non_clear_duration="${9}" # the total duration in seconds this is non-clear
34 # get the system hostname
35 hostname="$(hostname)"
37 # get the current date
41 local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
43 s=$(( s - (d * 86400) ))
45 s=$(( s - (h * 3600) ))
51 [ ${m} -ge 30 ] && h=$(( h + 1 ))
52 [ ${d} -gt 1 ] && ds="days"
53 [ ${h} -gt 1 ] && hs="hours"
56 echo "${d} ${ds} and ${h} ${hs}"
62 [ ${s} -ge 30 ] && m=$(( m + 1 ))
63 [ ${h} -gt 1 ] && hs="hours"
64 [ ${m} -gt 1 ] && ms="minutes"
67 echo "${h} ${hs} and ${m} ${ms}"
73 [ ${m} -gt 1 ] && ms="minutes"
74 [ ${s} -gt 1 ] && ss="seconds"
77 echo "${m} ${ms} and ${s} ${ss}"
82 [ ${s} -gt 1 ] && ss="seconds"
88 raised_for="<br/>(was ${old_status,,} for $(duration4human ${duration}))"
89 status_message="status unknown"
91 alarm="${name} = ${value}"
93 # prepare the title based on status
96 status_message="is critical"
101 status_message="needs attention"
106 status_message="recovered"
109 # don't show the value when the status is CLEAR
110 # for certain alarms, this value might not have any meaning
115 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
117 # don't do anything if this is not WARNING, CRITICAL or CLEAR
118 echo >&2 "${me}: not sending notification email for ${status} on '${chart}.${name}'"
120 elif [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
122 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
123 echo >&2 "${me}: not sending notification email for ${status} on '${chart}.${name}' (last status was ${old_status})"
125 elif [ "${status}" = "CLEAR" ]
127 severity="Recovered from ${old_status}"
128 if [ $non_clear_duration -gt $duration ]
130 raised_for="<br/>(had issues for $(duration4human ${non_clear_duration}))"
133 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
135 severity="Escalated to ${status}"
136 if [ $non_clear_duration -gt $duration ]
138 raised_for="<br/>(has issues for $(duration4human ${non_clear_duration}))"
141 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
143 severity="Demoted to ${status}"
144 if [ $non_clear_duration -gt $duration ]
146 raised_for="<br/>(has issues for $(duration4human ${non_clear_duration}))"
154 cat <<EOF | sendmail_from_pipe
156 Subject: ${hostname} ${status_message} - ${chart}.${name}
157 Content-Type: text/html
159 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
160 <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;">
161 <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">
165 <td style="vertical-align:top;" valign="top"></td>
166 <td width="700" style="vertical-align:top;display:block!important;max-width:700px!important;clear:both!important;margin:0 auto;padding:0" valign="top">
167 <div style="max-width:700px;display:block;margin:0 auto;padding:20px">
168 <table width="100%" cellpadding="0" cellspacing="0"
169 style="background:#fff;border:1px solid #e9e9e9">
173 style="padding: 5px 20px 5px 20px;background-color:#eee;">
174 <div style="font-size:20px;color:#777;font-weight: bold;">netdata notification</div>
178 <td bgcolor="${color}"
179 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">
180 <h1 style="font-weight:400;margin:0">${hostname} ${status_message}</h1>
184 <td style="vertical-align:top" valign="top">
185 <div style="margin:0;padding:20px;max-width:700px">
186 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
189 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
190 align="left" valign="top">
191 <span>${chart}</span>
192 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Chart</span>
195 <tr style="margin:0;padding:0">
196 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
197 align="left" valign="top">
198 <span>${alarm}</span>
199 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Alarm</span>
202 <tr style="margin:0;padding:0">
203 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
204 align="left" valign="top">
205 <span>${severity}</span>
206 <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Severity</span>
209 <tr style="margin:0;padding:0">
210 <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
211 align="left" valign="top"><span>${date}</span>
212 <span>${raised_for}</span> <span
213 style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Time</span>
216 <tr style="text-align:center;margin:0;padding:0">
217 <td style="font-size:11px;vertical-align:top;margin:0;padding:10px 0 0 0;color:#666666"
218 align="center" valign="bottom">The source of this alarm is line <code>${src}</code>
221 <tr style="text-align:center;margin:0;padding:0">
222 <td style="font-size:12px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
223 align="center" valign="bottom">Sent by
224 <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.