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