]> arthur.barton.de Git - netdata.git/blob - plugins.d/alarm-email.sh
alarm-email.sh should exit if there is no way to send an email
[netdata.git] / plugins.d / alarm-email.sh
1 #!/usr/bin/env bash
2
3 me="${0}"
4
5 sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
6 if [ -z "${sendmail}" ]
7 then
8     echo >&2 "I cannot send emails - there is no sendmail command available."
9     exit 1
10 fi
11
12 default_recipient_for_all_roles="root"
13 declare -A recipients=()
14 if [ -f "${NETDATA_CONFIG_DIR}/health_email_recipients.conf" ]
15     then
16     source "${NETDATA_CONFIG_DIR}/health_email_recipients.conf"
17 fi
18
19 sendmail_from_pipe() {
20     "${sendmail}" -t
21
22     if [ $? -eq 0 ]
23     then
24         echo >&2 "${me}: Sent notification email for ${status} on '${chart}.${name}'"
25         return 0
26     else
27         echo >&2 "${me}: FAILED to send notification email for ${status} on '${chart}.${name}'"
28         return 1
29     fi
30 }
31
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
50
51 to="${recipients[${recipient}]}"
52 [ -z "${to}" ] && to="${default_recipient_for_all_roles}"
53 [ -z "${to}" ] && to="root"
54
55 [ ! -z "${info}" ] && info=" <small><br/>${info}</small>"
56
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)"
61
62 goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?machine_guid=${NETDATA_REGISTRY_UNIQUE_ID}&chart=${chart}&family=${family}"
63
64 date="$(date --date=@${when} 2>/dev/null)"
65 [ -z "${date}" ] && date="$(date 2>/dev/null)"
66
67 # convert a duration in seconds, to a human readable duration
68 # using DAYS, MINUTES, SECONDS
69 duration4human() {
70     local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
71     d=$(( s / 86400 ))
72     s=$(( s - (d * 86400) ))
73     h=$(( s / 3600 ))
74     s=$(( s - (h * 3600) ))
75     m=$(( s / 60 ))
76     s=$(( s - (m * 60) ))
77
78     if [ ${d} -gt 0 ]
79     then
80         [ ${m} -ge 30 ] && h=$(( h + 1 ))
81         [ ${d} -gt 1 ] && ds="days"
82         [ ${h} -gt 1 ] && hs="hours"
83         if [ ${h} -gt 0 ]
84         then
85             echo "${d} ${ds} and ${h} ${hs}"
86         else
87             echo "${d} ${ds}"
88         fi
89     elif [ ${h} -gt 0 ]
90     then
91         [ ${s} -ge 30 ] && m=$(( m + 1 ))
92         [ ${h} -gt 1 ] && hs="hours"
93         [ ${m} -gt 1 ] && ms="minutes"
94         if [ ${m} -gt 0 ]
95         then
96             echo "${h} ${hs} and ${m} ${ms}"
97         else
98             echo "${h} ${hs}"
99         fi
100     elif [ ${m} -gt 0 ]
101     then
102         [ ${m} -gt 1 ] && ms="minutes"
103         [ ${s} -gt 1 ] && ss="seconds"
104         if [ ${s} -gt 0 ]
105         then
106             echo "${m} ${ms} and ${s} ${ss}"
107         else
108             echo "${m} ${ms}"
109         fi
110     else
111         [ ${s} -gt 1 ] && ss="seconds"
112         echo "${s} ${ss}"
113     fi
114 }
115
116 severity="${status}"
117 raised_for="<br/><small>(was ${old_status,,} for $(duration4human ${duration}))</small>"
118 status_message="status unknown"
119 color="grey"
120 alarm="${name} = ${value} ${units}"
121
122 # prepare the title based on status
123 case "${status}" in
124         CRITICAL)
125         status_message="is critical"
126         color="#ca414b"
127         ;;
128
129     WARNING)
130         status_message="needs attention"
131         color="#caca4b"
132                 ;;
133
134         CLEAR)
135         status_message="recovered"
136                 color="#77ca6d"
137
138                 # don't show the value when the status is CLEAR
139                 # for certain alarms, this value might not have any meaning
140                 alarm="${name}"
141                 ;;
142 esac
143
144 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
145 then
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}'"
148     exit 0
149 elif [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
150 then
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})"
153     exit 0
154 elif [ "${status}" = "CLEAR" ]
155 then
156     severity="Recovered from ${old_status}"
157     if [ $non_clear_duration -gt $duration ]
158     then
159         raised_for="<br/><small>(had issues for $(duration4human ${non_clear_duration}))</small>"
160     fi
161
162 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
163 then
164     severity="Escalated to ${status}"
165     if [ $non_clear_duration -gt $duration ]
166     then
167         raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
168     fi
169
170 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
171 then
172     severity="Demoted to ${status}"
173     if [ $non_clear_duration -gt $duration ]
174     then
175         raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
176     fi
177
178 else
179     raised_for=
180 fi
181
182 # send the email
183 cat <<EOF | sendmail_from_pipe
184 To: ${to}
185 Subject: ${hostname} ${status_message} - ${chart}.${name}
186 Content-Type: text/html
187
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">
191 <table>
192     <tbody>
193     <tr>
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">
199                     <tbody>
200                     <tr>
201                         <td bgcolor="#eee"
202                             style="padding: 5px 20px 5px 20px;background-color:#eee;">
203                             <div style="font-size:20px;color:#777;font-weight: bold;">netdata notification</div>
204                         </td>
205                     </tr>
206                     <tr>
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>
210                         </td>
211                     </tr>
212                     <tr>
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">
216                                     <tbody>
217                                     <tr>
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>
222                                         </td>
223                                     </tr>
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>
229                                         </td>
230                                     </tr>
231                                     <tr>
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>
236                                         </td>
237                                     </tr>
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>
243                                         </td>
244                                     </tr>
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>
250                                         </td>
251                                     </tr>
252                                     <!--
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>
256                                         </td>
257                                     </tr>
258                                     -->
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>
262                                         </td>
263                                     </tr>
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.
268                                         </td>
269                                     </tr>
270                                     </tbody>
271                                 </table>
272                             </div>
273                         </td>
274                     </tr>
275                     </tbody>
276                 </table>
277             </div>
278         </td>
279     </tr>
280     </tbody>
281 </table>
282 </body>
283 </html>
284 EOF