]> arthur.barton.de Git - netdata.git/blob - plugins.d/alarm-email.sh
3f1176b156f5a2475044f198f05c022f50ee920a
[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 fi
10
11 default_recipient_for_all_roles="root"
12 declare -A recipients=()
13 if [ -f "${NETDATA_CONFIG_DIR}/health_email_recipients.conf" ]
14     then
15     source "${NETDATA_CONFIG_DIR}/health_email_recipients.conf"
16 fi
17
18 sendmail_from_pipe() {
19     "${sendmail}" -t
20
21     if [ $? -eq 0 ]
22     then
23         echo >&2 "${me}: Sent notification email for ${status} on '${chart}.${name}'"
24         return 0
25     else
26         echo >&2 "${me}: FAILED to send notification email for ${status} on '${chart}.${name}'"
27         return 1
28     fi
29 }
30
31 recipient="${1}"   # the recepient of the email
32 hostname="${2}"    # the hostname this event refers to
33 unique_id="${3}"   # the unique id of this event
34 alarm_id="${4}"    # the unique id of the alarm that generated this event
35 event_id="${5}"    # the incremental id of the event, for this alarm
36 when="${6}"        # the timestamp this event occured
37 name="${7}"        # the name of the alarm, as given in netdata health.d entries
38 chart="${8}"       # the name of the chart (type.id)
39 family="${9}"      # the family of the chart
40 status="${10}"     # the current status : UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
41 old_status="${11}" # the previous status: UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
42 value="${12}"      # the current value
43 old_value="${13}"  # the previous value
44 src="${14}"        # the line number and file the alarm has been configured
45 duration="${15}"   # the duration in seconds the previous state took
46 non_clear_duration="${16}" # the total duration in seconds this is non-clear
47 units="${17}"      # the units of the value
48 info="${18}"       # a short description of the alarm
49
50 to="${recipients[${recipient}]}"
51 [ -z "${to}" ] && to="${default_recipient_for_all_roles}"
52 [ -z "${to}" ] && to="root"
53
54 [ ! -z "${info}" ] && info=" <small><br/>${info}</small>"
55
56 # get the system hostname
57 [ -z "${hostname}" ] && hostname="${NETDATA_HOSTNAME}"
58 [ -z "${hostname}" ] && hostname="${NETDATA_REGISTRY_HOSTNAME}"
59 [ -z "${hostname}" ] && hostname="$(hostname 2>/dev/null)"
60
61 goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?machine_guid=${NETDATA_REGISTRY_UNIQUE_ID}&chart=${chart}&family=${family}"
62
63 date="$(date --date=@${when} 2>/dev/null)"
64 [ -z "${date}" ] && date="$(date 2>/dev/null)"
65
66 # convert a duration in seconds, to a human readable duration
67 # using DAYS, MINUTES, SECONDS
68 duration4human() {
69     local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second"
70     d=$(( s / 86400 ))
71     s=$(( s - (d * 86400) ))
72     h=$(( s / 3600 ))
73     s=$(( s - (h * 3600) ))
74     m=$(( s / 60 ))
75     s=$(( s - (m * 60) ))
76
77     if [ ${d} -gt 0 ]
78     then
79         [ ${m} -ge 30 ] && h=$(( h + 1 ))
80         [ ${d} -gt 1 ] && ds="days"
81         [ ${h} -gt 1 ] && hs="hours"
82         if [ ${h} -gt 0 ]
83         then
84             echo "${d} ${ds} and ${h} ${hs}"
85         else
86             echo "${d} ${ds}"
87         fi
88     elif [ ${h} -gt 0 ]
89     then
90         [ ${s} -ge 30 ] && m=$(( m + 1 ))
91         [ ${h} -gt 1 ] && hs="hours"
92         [ ${m} -gt 1 ] && ms="minutes"
93         if [ ${m} -gt 0 ]
94         then
95             echo "${h} ${hs} and ${m} ${ms}"
96         else
97             echo "${h} ${hs}"
98         fi
99     elif [ ${m} -gt 0 ]
100     then
101         [ ${m} -gt 1 ] && ms="minutes"
102         [ ${s} -gt 1 ] && ss="seconds"
103         if [ ${s} -gt 0 ]
104         then
105             echo "${m} ${ms} and ${s} ${ss}"
106         else
107             echo "${m} ${ms}"
108         fi
109     else
110         [ ${s} -gt 1 ] && ss="seconds"
111         echo "${s} ${ss}"
112     fi
113 }
114
115 severity="${status}"
116 raised_for="<br/><small>(was ${old_status,,} for $(duration4human ${duration}))</small>"
117 status_message="status unknown"
118 color="grey"
119 alarm="${name} = ${value} ${units}"
120
121 # prepare the title based on status
122 case "${status}" in
123         CRITICAL)
124         status_message="is critical"
125         color="#ca414b"
126         ;;
127
128     WARNING)
129         status_message="needs attention"
130         color="#caca4b"
131                 ;;
132
133         CLEAR)
134         status_message="recovered"
135                 color="#77ca6d"
136
137                 # don't show the value when the status is CLEAR
138                 # for certain alarms, this value might not have any meaning
139                 alarm="${name}"
140                 ;;
141 esac
142
143 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
144 then
145     # don't do anything if this is not WARNING, CRITICAL or CLEAR
146     echo >&2 "${me}: not sending notification email for ${status} on '${chart}.${name}'"
147     exit 0
148 elif [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
149 then
150     # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
151     echo >&2 "${me}: not sending notification email for ${status} on '${chart}.${name}' (last status was ${old_status})"
152     exit 0
153 elif [ "${status}" = "CLEAR" ]
154 then
155     severity="Recovered from ${old_status}"
156     if [ $non_clear_duration -gt $duration ]
157     then
158         raised_for="<br/><small>(had issues for $(duration4human ${non_clear_duration}))</small>"
159     fi
160
161 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
162 then
163     severity="Escalated to ${status}"
164     if [ $non_clear_duration -gt $duration ]
165     then
166         raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
167     fi
168
169 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
170 then
171     severity="Demoted to ${status}"
172     if [ $non_clear_duration -gt $duration ]
173     then
174         raised_for="<br/><small>(has issues for $(duration4human ${non_clear_duration}))</small>"
175     fi
176
177 else
178     raised_for=
179 fi
180
181 # send the email
182 cat <<EOF | sendmail_from_pipe
183 To: ${to}
184 Subject: ${hostname} ${status_message} - ${chart}.${name}
185 Content-Type: text/html
186
187 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
188 <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;">
189 <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">
190 <table>
191     <tbody>
192     <tr>
193         <td style="vertical-align:top;" valign="top"></td>
194         <td width="700" style="vertical-align:top;display:block!important;max-width:700px!important;clear:both!important;margin:0 auto;padding:0" valign="top">
195             <div style="max-width:700px;display:block;margin:0 auto;padding:20px">
196                 <table width="100%" cellpadding="0" cellspacing="0"
197                        style="background:#fff;border:1px solid #e9e9e9">
198                     <tbody>
199                     <tr>
200                         <td bgcolor="#eee"
201                             style="padding: 5px 20px 5px 20px;background-color:#eee;">
202                             <div style="font-size:20px;color:#777;font-weight: bold;">netdata notification</div>
203                         </td>
204                     </tr>
205                     <tr>
206                         <td bgcolor="${color}"
207                             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">
208                             <h1 style="font-weight:400;margin:0">${hostname} ${status_message}</h1>
209                         </td>
210                     </tr>
211                     <tr>
212                         <td style="vertical-align:top" valign="top">
213                             <div style="margin:0;padding:20px;max-width:700px">
214                                 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
215                                     <tbody>
216                                     <tr>
217                                         <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
218                                             align="left" valign="top">
219                                             <span>${chart}</span>
220                                             <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Chart</span>
221                                         </td>
222                                     </tr>
223                                     <tr style="margin:0;padding:0">
224                                         <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
225                                             align="left" valign="top">
226                                             <span><b>${alarm}</b>${info}</span>
227                                             <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Alarm</span>
228                                         </td>
229                                     </tr>
230                                     <tr>
231                                         <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
232                                             align="left" valign="top">
233                                             <span>${family}</span>
234                                             <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Family</span>
235                                         </td>
236                                     </tr>
237                                     <tr style="margin:0;padding:0">
238                                         <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
239                                             align="left" valign="top">
240                                             <span>${severity}</span>
241                                             <span style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Severity</span>
242                                         </td>
243                                     </tr>
244                                     <tr style="margin:0;padding:0">
245                                         <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px"
246                                             align="left" valign="top"><span>${date}</span>
247                                             <span>${raised_for}</span> <span
248                                                     style="display:block;color:#666666;font-size:12px;font-weight:300;line-height:1;text-transform:uppercase">Time</span>
249                                         </td>
250                                     </tr>
251                                     <!--
252                                     <tr style="margin:0;padding:0">
253                                         <td style="font-size:18px;vertical-align:top;margin:0;padding:0 0 20px">
254                                             <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>
255                                         </td>
256                                     </tr>
257                                     -->
258                                     <tr style="text-align:center;margin:0;padding:0">
259                                         <td style="font-size:11px;vertical-align:top;margin:0;padding:10px 0 0 0;color:#666666"
260                                             align="center" valign="bottom">The source of this alarm is line <code>${src}</code>
261                                         </td>
262                                     </tr>
263                                     <tr style="text-align:center;margin:0;padding:0">
264                                         <td style="font-size:12px;vertical-align:top;margin:0;padding:20px 0 0 0;color:#666666;border-top:1px solid #f0f0f0"
265                                             align="center" valign="bottom">Sent by
266                                             <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
267                                         </td>
268                                     </tr>
269                                     </tbody>
270                                 </table>
271                             </div>
272                         </td>
273                     </tr>
274                     </tbody>
275                 </table>
276             </div>
277         </td>
278     </tr>
279     </tbody>
280 </table>
281 </body>
282 </html>
283 EOF