]> arthur.barton.de Git - netdata.git/blob - plugins.d/alarm-notify.sh
Merge pull request #995 from xuefer/sensor-grouping-fix
[netdata.git] / plugins.d / alarm-notify.sh
1 #!/usr/bin/env bash
2
3 # (C) Costa Tsaousis
4 # pushover support by Jan Arnold
5
6 me="${0}"
7
8 if [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ]
9 then
10     echo >&2
11     echo >&2 "$me: ERROR"
12     echo >&2 "BASH version 4 or later is required."
13     echo >&2 "You are running version: ${BASH_VERSION}"
14     echo >&2 "Please upgrade."
15     echo >&2
16     exit 1
17 fi
18
19 # defaults to allow running this script by hand
20 NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
21 NETDATA_CACHE_DIR="${NETDATA_CACHE_DIR-/var/cache/netdata}"
22 [ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
23 [ -z "${NETDATA_HOSTNAME}" ] && NETDATA_HOSTNAME="$(hostname)"
24 [ -z "${NETDATA_REGISTRY_HOSTNAME}" ] && NETDATA_REGISTRY_HOSTNAME="${NETDATA_HOSTNAME}"
25
26 # -----------------------------------------------------------------------------
27 # parse command line parameters
28
29 recipient="${1}"   # the recepient of the email
30 host="${2}"        # the host this event refers to
31 unique_id="${3}"   # the unique id of this event
32 alarm_id="${4}"    # the unique id of the alarm that generated this event
33 event_id="${5}"    # the incremental id of the event, for this alarm
34 when="${6}"        # the timestamp this event occured
35 name="${7}"        # the name of the alarm, as given in netdata health.d entries
36 chart="${8}"       # the name of the chart (type.id)
37 family="${9}"      # the family of the chart
38 status="${10}"     # the current status : REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
39 old_status="${11}" # the previous status: REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
40 value="${12}"      # the current value
41 old_value="${13}"  # the previous value
42 src="${14}"        # the line number and file the alarm has been configured
43 duration="${15}"   # the duration in seconds the previous state took
44 non_clear_duration="${16}" # the total duration in seconds this is non-clear
45 units="${17}"      # the units of the value
46 info="${18}"       # a short description of the alarm
47
48 # -----------------------------------------------------------------------------
49 # screen statuses we don't need to send a notification
50
51 # don't do anything if this is not WARNING, CRITICAL or CLEAR
52 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
53 then
54     echo >&2 "${me}: not sending notification for ${status} on '${chart}.${name}'"
55     exit 1
56 fi
57
58 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
59 if [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
60 then
61     echo >&2 "${me}: not sending notification for ${status} on '${chart}.${name}' (last status was ${old_status})"
62     exit 1
63 fi
64
65 # -----------------------------------------------------------------------------
66 # load configuration
67
68 # this is defined here so that private registries
69 # can setup their own
70 # images_base_url="${NETDATA_REGISTRY_URL}"
71 images_base_url="https://registry.my-netdata.io"
72
73 # needed commands
74 # if empty they will be searched in the system path
75 curl=
76 sendmail=
77
78 # enable / disable features
79 SEND_SLACK="YES"
80 SEND_PUSHOVER="YES"
81 SEND_TELEGRAM="YES"
82 SEND_EMAIL="YES"
83
84 # slack configs
85 SLACK_WEBHOOK_URL=
86 DEFAULT_RECIPIENT_SLACK=
87 declare -A role_recipients_slack=()
88
89 # pushover configs
90 PUSHOVER_APP_TOKEN=
91 DEFAULT_RECIPIENT_PUSHOVER=
92 declare -A role_recipients_pushover=()
93
94 # telegram configs
95 TELEGRAM_BOT_TOKEN=
96 DEFAULT_RECIPIENT_TELEGRAM=
97 declare -A role_recipients_telegram=()
98
99 # email configs
100 DEFAULT_RECIPIENT_EMAIL="root"
101 declare -A role_recipients_email=()
102
103 # load the user configuration
104 # this will overwrite the variables above
105 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
106     then
107     source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
108 fi
109
110 # -----------------------------------------------------------------------------
111 # filter recipients based on the criticality of each
112
113 filter_recipient_by_criticality() {
114     local method="${1}" x="${2}" r s
115     shift
116
117     r="${x/|*/}"
118     s="${x/*|/}"
119
120     # no severity filtering for this person
121     [ "${r}" = "${s}" ] && return 0
122
123     # the severity is invalid
124     s="${s^^}"
125     [ "${s}" != "CRITICAL" ] && return 0
126
127     # the new or the old status matches the severity
128     if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
129         then
130         [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
131             mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
132
133         # we need to keep track of the notifications we sent
134         # so that the same user will receive the recovery
135         # even if old_status does not match the required severity
136         touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
137         return 0
138     fi
139
140     # it is a cleared alarm we have sent notification for
141     if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
142         then
143         rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
144         return 0
145     fi
146
147     return 1
148 }
149
150 # -----------------------------------------------------------------------------
151 # find the recipient's addresses per method
152
153 declare -A arr_slack=()
154 declare -A arr_pushover=()
155 declare -A arr_telegram=()
156 declare -A arr_email=()
157
158 # netdata may call us with multiple recipients
159 # so, here we find the unique ones
160 for x in ${recipient//,/ }
161 do
162     # the recipient 'silent' means, don't send a notification for this event
163     [ "${x}" = "silent" ] && continue
164
165     # email
166     a="${role_recipients_email[${recipient}]}"
167     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
168     for r in ${a//,/ }
169     do
170         [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
171     done
172
173     # pushover
174     a="${role_recipients_pushover[${recipient}]}"
175     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
176     for r in ${a//,/ }
177     do
178         [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
179     done
180
181     # telegram
182     a="${role_recipients_telegram[${recipient}]}"
183     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
184     for r in ${a//,/ }
185     do
186         [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
187     done
188
189     # slack
190     a="${role_recipients_slack[${recipient}]}"
191     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
192     for r in ${a//,/ }
193     do
194         [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
195     done
196 done
197
198 # build the list of slack recipients (channels)
199 to_slack="${!arr_slack[*]}"
200 [ -z "${to_slack}" ] && SEND_SLACK="NO"
201
202 # build the list of pushover recipients (user tokens)
203 to_pushover="${!arr_pushover[*]}"
204 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
205
206 # check array of telegram recipients (chat ids)
207 to_telegram="${!arr_telegram[*]}"
208 [ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
209
210 # build the list of email recipients (email addresses)
211 to_email=
212 for x in "${!arr_email[@]}"
213 do
214     [ ! -z "${to_email}" ] && to_email="${to_email}, "
215     to_email="${to_email}${x}"
216 done
217 [ -z "${to_email}" ] && SEND_EMAIL="NO"
218
219
220 # -----------------------------------------------------------------------------
221 # verify the delivery methods supported
222
223 # check slack
224 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
225
226 # check pushover
227 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
228
229 # check telegram
230 [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
231
232 if [ \( "${SEND_PUSHOVER}" = "YES" -o "${SEND_SLACK}" = "YES" -o "${SEND_TELEGRAM}" = "YES" \) -a -z "${curl}" ]
233     then
234     curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
235     if [ -z "${curl}" ]
236         then
237         SEND_PUSHOVER="NO"
238         SEND_TELEGRAM="NO"
239         SEND_SLACK="NO"
240     fi
241 fi
242
243 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
244     then
245     sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
246     [ -z "${sendmail}" ] && SEND_EMAIL="NO"
247 fi
248
249 # check that we have at least a method enabled
250 if [ "${SEND_EMAIL}" != "YES" -a "${SEND_PUSHOVER}" != "YES" -a "${SEND_TELEGRAM}" != "YES" -a "${SEND_SLACK}" != "YES" ]
251     then
252     echo >&2 "All notification methods are disabled. Not sending a notification."
253     exit 1
254 fi
255
256 # -----------------------------------------------------------------------------
257 # get the system hostname
258
259 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
260 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
261 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
262
263 # -----------------------------------------------------------------------------
264 # get the date the alarm happened
265
266 date="$(date --date=@${when} 2>/dev/null)"
267 [ -z "${date}" ] && date="$(date 2>/dev/null)"
268
269 # -----------------------------------------------------------------------------
270 # URL encode a string
271
272 urlencode() {
273     local string="${1}" strlen encoded pos c o
274
275     strlen=${#string}
276     for (( pos=0 ; pos<strlen ; pos++ ))
277     do
278         c=${string:$pos:1}
279         case "$c" in
280             [-_.~a-zA-Z0-9])
281                 o="${c}"
282                 ;;
283
284             *)
285                 printf -v o '%%%02x' "'$c"
286                 ;;
287         esac
288         encoded+="${o}"
289     done
290
291     REPLY="${encoded}"
292     echo "${REPLY}"
293 }
294
295 # -----------------------------------------------------------------------------
296 # convert a duration in seconds, to a human readable duration
297 # using DAYS, MINUTES, SECONDS
298
299 duration4human() {
300     local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
301     d=$(( s / 86400 ))
302     s=$(( s - (d * 86400) ))
303     h=$(( s / 3600 ))
304     s=$(( s - (h * 3600) ))
305     m=$(( s / 60 ))
306     s=$(( s - (m * 60) ))
307
308     if [ ${d} -gt 0 ]
309     then
310         [ ${m} -ge 30 ] && h=$(( h + 1 ))
311         [ ${d} -gt 1 ] && ds="days"
312         [ ${h} -gt 1 ] && hs="hours"
313         if [ ${h} -gt 0 ]
314         then
315             ret="${d} ${ds} and ${h} ${hs}"
316         else
317             ret="${d} ${ds}"
318         fi
319     elif [ ${h} -gt 0 ]
320     then
321         [ ${s} -ge 30 ] && m=$(( m + 1 ))
322         [ ${h} -gt 1 ] && hs="hours"
323         [ ${m} -gt 1 ] && ms="minutes"
324         if [ ${m} -gt 0 ]
325         then
326             ret="${h} ${hs} and ${m} ${ms}"
327         else
328             ret="${h} ${hs}"
329         fi
330     elif [ ${m} -gt 0 ]
331     then
332         [ ${m} -gt 1 ] && ms="minutes"
333         [ ${s} -gt 1 ] && ss="seconds"
334         if [ ${s} -gt 0 ]
335         then
336             ret="${m} ${ms} and ${s} ${ss}"
337         else
338             ret="${m} ${ms}"
339         fi
340     else
341         [ ${s} -gt 1 ] && ss="seconds"
342         ret="${s} ${ss}"
343     fi
344
345     REPLY="${ret}"
346     echo "${REPLY}"
347 }
348
349 # -----------------------------------------------------------------------------
350 # email sender
351
352 send_email() {
353     local ret=
354     if [ "${SEND_EMAIL}" = "YES" ]
355         then
356
357         "${sendmail}" -t
358         ret=$?
359
360         if [ $ret -eq 0 ]
361         then
362             echo >&2 "${me}: Sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
363             return 0
364         else
365             echo >&2 "${me}: Failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
366             return 1
367         fi
368     fi
369
370     return 1
371 }
372
373 # -----------------------------------------------------------------------------
374 # pushover sender
375
376 send_pushover() {
377     local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
378
379     if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
380         then
381
382         # https://pushover.net/api
383         priority=-2
384         case "${status}" in
385             CLEAR) priority=-1;;   # low priority: no sound or vibration
386             WARNING) priotity=0;;  # normal priority: respect quiet hours
387             CRITICAL) priority=1;; # high priority: bypass quiet hours
388             *) priority=-2;;       # lowest priority: no notification at all
389         esac
390
391         for user in ${usertokens}
392         do
393             httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
394                 --form-string "token=${apptoken}" \
395                 --form-string "user=${user}" \
396                 --form-string "html=1" \
397                 --form-string "title=${title}" \
398                 --form-string "message=${message}" \
399                 --form-string "timestamp=${when}" \
400                 --form-string "url=${url}" \
401                 --form-string "url_title=Open netdata dashboard to view the alarm" \
402                 --form-string "priority=${priority}" \
403                 https://api.pushover.net/1/messages.json)
404
405             if [ "${httpcode}" == "200" ]
406             then
407                 echo >&2 "${me}: Sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
408                 sent=$((sent + 1))
409             else
410                 echo >&2 "${me}: Failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
411             fi
412         done
413
414         [ ${sent} -gt 0 ] && return 0
415     fi
416
417     return 1
418 }
419
420
421 # -----------------------------------------------------------------------------
422 # telegram sender
423
424 send_telegram() {
425     local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
426
427     if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
428
429     if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
430     then
431         for chatid in ${chatids}
432         do
433             # https://core.telegram.org/bots/api#sendmessage
434             httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
435                 --data-urlencode "parse_mode=HTML" \
436                 --data-urlencode "disable_web_page_preview=true" \
437                 --data-urlencode "text=$message" \
438                 "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=$chatid")
439
440             if [ "${httpcode}" == "200" ]
441             then
442                 echo >&2 "${me}: Sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
443                 sent=$((sent + 1))
444             elif [ "${httpcode}" == "401" ]
445             then
446                 echo >&2 "${me}: Failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
447             else
448                 echo >&2 "${me}: Failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
449             fi
450         done
451
452         [ ${sent} -gt 0 ] && return 0
453     fi
454
455     return 1
456 }
457
458 # -----------------------------------------------------------------------------
459 # slack sender
460
461 send_slack() {
462     local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
463
464     [ "${SEND_SLACK}" != "YES" ] && return 1
465
466     case "${status}" in
467         WARNING) color="warning" ;;
468         CRITICAL) color="danger" ;;
469         CLEAR) color="good" ;;
470         *) color="#777777" ;;
471     esac
472
473     for channel in ${channels}
474     do
475         payload="$(cat <<EOF
476         {
477             "channel": "#${channel}",
478             "username": "netdata on ${host}",
479             "icon_url": "${images_base_url}/images/seo-performance-128.png",
480             "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
481             "attachments": [
482                 {
483                     "fallback": "${alarm} - ${chart} (${family}) - ${info}",
484                     "color": "${color}",
485                     "title": "${alarm}",
486                     "title_link": "${goto_url}",
487                     "text": "${info}",
488                     "fields": [
489                         {
490                             "title": "${chart}",
491                             "short": true
492                         },
493                         {
494                             "title": "${family}",
495                             "short": true
496                         }
497                     ],
498                     "thumb_url": "${image}",
499                     "footer": "<${goto_url}|${host}>",
500                     "ts": ${when}
501                 }
502             ]
503         }
504 EOF
505         )"
506
507         httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
508         if [ "${httpcode}" == "200" ]
509         then
510             echo >&2 "${me}: Sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
511             sent=$((sent + 1))
512         else
513             echo >&2 "${me}: Failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
514         fi
515     done
516
517     [ ${sent} -gt 0 ] && return 0
518
519     return 1
520 }
521
522
523 # -----------------------------------------------------------------------------
524 # prepare the content of the notification
525
526 # the url to send the user on click
527 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
528 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
529 urlencode "${family}" >/dev/null; url_family="${REPLY}"
530 urlencode "${name}" >/dev/null; url_name="${REPLY}"
531 goto_url="${NETDATA_REGISTRY_URL}/goto-host-from-alarm.html?host=${url_host}&chart=${url_chart}&family=${url_family}&alarm=${url_name}&alarm_unique_id=${unique_id}&alarm_id=${alarm_id}&alarm_event_id=${event_id}"
532
533 # the severity of the alarm
534 severity="${status}"
535
536 # the time the alarm was raised
537 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
538 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
539 raised_for="(was ${old_status,,} for ${duration_txt})"
540
541 # the key status message
542 status_message="status unknown"
543
544 # the color of the alarm
545 color="grey"
546
547 # the alarm value
548 alarm="${name//_/ } = ${value} ${units}"
549
550 # the image of the alarm
551 image="${images_base_url}/images/seo-performance-128.png"
552
553 # prepare the title based on status
554 case "${status}" in
555         CRITICAL)
556         image="${images_base_url}/images/alert-128-red.png"
557         status_message="is critical"
558         color="#ca414b"
559         ;;
560
561     WARNING)
562         image="${images_base_url}/images/alert-128-orange.png"
563         status_message="needs attention"
564         color="#caca4b"
565                 ;;
566
567         CLEAR)
568         image="${images_base_url}/images/check-mark-2-128-green.png"
569         status_message="recovered"
570                 color="#77ca6d"
571
572                 # don't show the value when the status is CLEAR
573                 # for certain alarms, this value might not have any meaning
574                 alarm="${name//_/ } ${raised_for}"
575                 ;;
576 esac
577
578 if [ "${status}" = "CLEAR" ]
579 then
580     severity="Recovered from ${old_status}"
581     if [ $non_clear_duration -gt $duration ]
582     then
583         raised_for="(alarm was raised for ${non_clear_duration_txt})"
584     fi
585
586 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
587 then
588     severity="Escalated to ${status}"
589     if [ $non_clear_duration -gt $duration ]
590     then
591         raised_for="(alarm is raised for ${non_clear_duration_txt})"
592     fi
593
594 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
595 then
596     severity="Demoted to ${status}"
597     if [ $non_clear_duration -gt $duration ]
598     then
599         raised_for="(alarm is raised for ${non_clear_duration_txt})"
600     fi
601
602 else
603     raised_for=
604 fi
605
606 # prepare HTML versions of elements
607 info_html=
608 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
609
610 raised_for_html=
611 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
612
613 # -----------------------------------------------------------------------------
614 # send the slack notification
615
616 # slack aggregates posts from the same username
617 # so we use "${host} ${status}" as the bot username, to make them diff
618
619 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
620 SENT_SLACK=$?
621
622 # -----------------------------------------------------------------------------
623 # send the pushover notification
624
625 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
626 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>&nbsp;
627 <small><b>${chart}</b><br/>Chart<br/>&nbsp;</small>
628 <small><b>${family}</b><br/>Family<br/>&nbsp;</small>
629 <small><b>${severity}</b><br/>Severity<br/>&nbsp;</small>
630 <small><b>${date}${raised_for_html}</b><br/>Time<br/>&nbsp;</small>
631 <a href=\"${goto_url}\">View Netdata</a><br/>&nbsp;
632 <small><small>The source of this alarm is line ${src}</small></small>
633 "
634
635 SENT_PUSHOVER=$?
636
637 # -----------------------------------------------------------------------------
638 # send the telegram.org message
639
640 # https://core.telegram.org/bots/api#formatting-options
641 telegram_message="<b>${severity}"
642 [ "${status_message}" != "recovered" ] && telegram_message="${telegram_message}, ${status_message}"
643 telegram_message="${telegram_message}
644 ${chart} (${family})</b>
645 <a href=\"${goto_url}\">${alarm}</a>
646 <i>${info}</i>"
647
648 send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${telegram_message}"
649
650 SENT_TELEGRAM=$?
651
652 # -----------------------------------------------------------------------------
653 # send the email
654
655 send_email <<EOF
656 To: ${to_email}
657 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
658 Content-Type: text/html
659
660 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
661 <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;">
662 <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;">
663 <table>
664     <tbody>
665     <tr>
666         <td style="vertical-align: top;" valign="top"></td>
667         <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
668             <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
669                 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
670                     <tbody>
671                     <tr>
672                         <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
673                             <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
674                         </td>
675                     </tr>
676                     <tr>
677                         <td bgcolor="${color}" 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">
678                             <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
679                         </td>
680                     </tr>
681                     <tr>
682                         <td style="vertical-align: top;" valign="top">
683                             <div style="margin: 0; padding: 20px; max-width: 700px;">
684                                 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
685                                     <tbody>
686                                     <tr>
687                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding:0 0 20px;" align="left" valign="top">
688                                             <span>${chart}</span>
689                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
690                                         </td>
691                                     </tr>
692                                     <tr style="margin: 0; padding: 0;">
693                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
694                                             <span><b>${alarm}</b>${info_html}</span>
695                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
696                                         </td>
697                                     </tr>
698                                     <tr>
699                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
700                                             <span>${family}</span>
701                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
702                                         </td>
703                                     </tr>
704                                     <tr style="margin: 0; padding: 0;">
705                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top">
706                                             <span>${severity}</span>
707                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
708                                         </td>
709                                     </tr>
710                                     <tr style="margin: 0; padding: 0;">
711                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" align="left" valign="top"><span>${date}</span>
712                                             <span>${raised_for_html}</span> <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Time</span>
713                                         </td>
714                                     </tr>
715                                     <tr style="margin: 0; padding: 0;">
716                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
717                                             <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>
718                                         </td>
719                                     </tr>
720                                     <tr style="text-align: center; margin: 0; padding: 0;">
721                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 11px; vertical-align: top; margin: 0; padding: 10px 0 0 0; color: #666666;" align="center" valign="bottom">The source of this alarm is line <code>${src}</code>
722                                         </td>
723                                     </tr>
724                                     <tr style="text-align: center; margin: 0; padding: 0;">
725                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 12px; vertical-align: top; margin:0; padding: 20px 0 0 0; color: #666666; border-top: 1px solid #f0f0f0;" align="center" valign="bottom">Sent by
726                                             <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
727                                         </td>
728                                     </tr>
729                                     </tbody>
730                                 </table>
731                             </div>
732                         </td>
733                     </tr>
734                     </tbody>
735                 </table>
736             </div>
737         </td>
738     </tr>
739     </tbody>
740 </table>
741 </body>
742 </html>
743 EOF
744
745 SENT_EMAIL=$?
746
747 # -----------------------------------------------------------------------------
748 # let netdata know
749
750 # we did send somehting
751 [ ${SENT_EMAIL} -eq 0 -o ${SENT_PUSHOVER} -eq 0 -o ${SENT_TELEGRAM} -eq 0 -o ${SENT_SLACK} -eq 0 ] && exit 0
752
753 # we did not send anything
754 exit 1