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