]> arthur.barton.de Git - netdata.git/blob - plugins.d/alarm-notify.sh
Initial implementation of Discord notifications
[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 by @ktsaou
18 #  - slack.com notifications by @ktsaou
19 #  - discordapp.com notifications by @lowfive
20 #  - pushover.net notifications by @ktsaou
21 #  - pushbullet.com push notifications by Tiago Peralta @tperalta82 PR #1070
22 #  - telegram.org notifications by @hashworks PR #1002
23 #  - twilio.com notifications by Levi Blaney @shadycuz PR #1211
24 #  - kafka notifications by @ktsaou #1342
25 #  - pagerduty.com notifications by Jim Cooley @jimcooley PR #1373
26 #  - messagebird.com notifications by @tech_no_logical #1453
27 #  - hipchart notifications by @ktsaou #1561
28
29 # -----------------------------------------------------------------------------
30 # testing notifications
31
32 if [ \( "${1}" = "test" -o "${2}" = "test" \) -a "${#}" -le 2 ]
33 then
34     if [ "${2}" = "test" ]
35     then
36         recipient="${1}"
37     else
38         recipient="${2}"
39     fi
40
41     [ -z "${recipient}" ] && recipient="sysadmin"
42
43     id=1
44     last="CLEAR"
45     for x in "CRITICAL" "WARNING" "CLEAR"
46     do
47         echo >&2
48         echo >&2 "# SENDING TEST ${x} ALARM TO ROLE: ${recipient}"
49
50         "${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"
51         if [ $? -ne 0 ]
52         then
53             echo >&2 "# FAILED"
54         else
55             echo >&2 "# OK"
56         fi
57
58         last="${x}"
59         id=$((id + 1))
60     done
61
62     exit 1
63 fi
64
65 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
66 export LC_ALL=C
67
68 # -----------------------------------------------------------------------------
69
70 PROGRAM_NAME="$(basename "${0}")"
71
72 logdate() {
73     date "+%Y-%m-%d %H:%M:%S"
74 }
75
76 log() {
77     local status="${1}"
78     shift
79
80     echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
81
82 }
83
84 warning() {
85     log WARNING "${@}"
86 }
87
88 error() {
89     log ERROR "${@}"
90 }
91
92 info() {
93     log INFO "${@}"
94 }
95
96 fatal() {
97     log FATAL "${@}"
98     exit 1
99 }
100
101 debug=0
102 debug() {
103     [ ${debug} -eq 1 ] && log DEBUG "${@}"
104 }
105
106 # -----------------------------------------------------------------------------
107
108 # check for BASH v4+ (required for associative arrays)
109 [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ] && \
110     fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
111
112 # -----------------------------------------------------------------------------
113 # defaults to allow running this script by hand
114
115 NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
116 NETDATA_CACHE_DIR="${NETDATA_CACHE_DIR-/var/cache/netdata}"
117 [ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
118 [ -z "${NETDATA_HOSTNAME}" ] && NETDATA_HOSTNAME="$(hostname)"
119 [ -z "${NETDATA_REGISTRY_HOSTNAME}" ] && NETDATA_REGISTRY_HOSTNAME="${NETDATA_HOSTNAME}"
120
121 # -----------------------------------------------------------------------------
122 # parse command line parameters
123
124 roles="${1}"       # the roles that should be notified for this event
125 host="${2}"        # the host generated this event
126 unique_id="${3}"   # the unique id of this event
127 alarm_id="${4}"    # the unique id of the alarm that generated this event
128 event_id="${5}"    # the incremental id of the event, for this alarm id
129 when="${6}"        # the timestamp this event occurred
130 name="${7}"        # the name of the alarm, as given in netdata health.d entries
131 chart="${8}"       # the name of the chart (type.id)
132 family="${9}"      # the family of the chart
133 status="${10}"     # the current status : REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
134 old_status="${11}" # the previous status: REMOVED, UNITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
135 value="${12}"      # the current value of the alarm
136 old_value="${13}"  # the previous value of the alarm
137 src="${14}"        # the line number and file the alarm has been configured
138 duration="${15}"   # the duration in seconds of the previous alarm state
139 non_clear_duration="${16}" # the total duration in seconds this is/was non-clear
140 units="${17}"      # the units of the value
141 info="${18}"       # a short description of the alarm
142
143 # -----------------------------------------------------------------------------
144 # screen statuses we don't need to send a notification
145
146 # don't do anything if this is not WARNING, CRITICAL or CLEAR
147 if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a "${status}" != "CLEAR" ]
148 then
149     info "not sending notification for ${status} on '${chart}.${name}'"
150     exit 1
151 fi
152
153 # don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
154 if [ "${old_status}" != "WARNING" -a "${old_status}" != "CRITICAL" -a "${status}" = "CLEAR" ]
155 then
156     info "not sending notification for ${status} on '${chart}.${name}' (last status was ${old_status})"
157     exit 1
158 fi
159
160 # -----------------------------------------------------------------------------
161 # load configuration
162
163 # By default fetch images from the global public registry.
164 # This is required by default, since all notification methods need to download
165 # images via the Internet, and private registries might not be reachable.
166 # This can be overwritten at the configuration file.
167 images_base_url="https://registry.my-netdata.io"
168
169 # needed commands
170 # if empty they will be searched in the system path
171 curl=
172 sendmail=
173
174 # enable / disable features
175 SEND_SLACK="YES"
176 SEND_DISCORD="YES"
177 SEND_PUSHOVER="YES"
178 SEND_TWILIO="YES"
179 SEND_HIPCHAT="YES"
180 SEND_MESSAGEBIRD="YES"
181 SEND_TELEGRAM="YES"
182 SEND_EMAIL="YES"
183 SEND_PUSHBULLET="YES"
184 SEND_KAFKA="YES"
185 SEND_PD="YES"
186
187 # slack configs
188 SLACK_WEBHOOK_URL=
189 DEFAULT_RECIPIENT_SLACK=
190 declare -A role_recipients_slack=()
191
192 # discord configs
193 DISCORD_WEBHOOK_URL=
194 DEFAULT_RECIPIENT_DISCORD=
195 declare -A role_recipients_discord=()
196
197 # pushover configs
198 PUSHOVER_APP_TOKEN=
199 DEFAULT_RECIPIENT_PUSHOVER=
200 declare -A role_recipients_pushover=()
201
202 # pushbullet configs
203 PUSHBULLET_ACCESS_TOKEN=
204 DEFAULT_RECIPIENT_PUSHBULLET=
205 declare -A role_recipients_pushbullet=()
206
207 # twilio configs
208 TWILIO_ACCOUNT_SID=
209 TWILIO_ACCOUNT_TOKEN=
210 TWILIO_NUMBER=
211 DEFAULT_RECIPIENT_TWILIO=
212 declare -A role_recipients_twilio=()
213
214 # hipchat configs
215 HIPCHAT_AUTH_TOKEN=
216 DEFAULT_RECIPIENT_HIPCHAT=
217 declare -A role_recipients_hipchat=()
218
219 # messagebird configs
220 MESSAGEBIRD_ACCESS_KEY=
221 MESSAGEBIRD_NUMBER=
222 DEFAULT_RECIPIENT_MESSAGEBIRD=
223 declare -A role_recipients_messagebird=()
224
225 # telegram configs
226 TELEGRAM_BOT_TOKEN=
227 DEFAULT_RECIPIENT_TELEGRAM=
228 declare -A role_recipients_telegram=()
229
230 # kafka configs
231 KAFKA_URL=
232 KAFKA_SENDER_IP=
233
234 # pagerduty.com configs
235 PD_SERVICE_KEY=
236 declare -A role_recipients_pd=()
237
238 # email configs
239 DEFAULT_RECIPIENT_EMAIL="root"
240 declare -A role_recipients_email=()
241
242 # load the user configuration
243 # this will overwrite the variables above
244 if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
245     then
246     source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
247 fi
248
249 # -----------------------------------------------------------------------------
250 # filter a recipient based on alarm event severity
251
252 filter_recipient_by_criticality() {
253     local method="${1}" x="${2}" r s
254     shift
255
256     r="${x/|*/}" # the recipient
257     s="${x/*|/}" # the severity required for notifying this recipient
258
259     # no severity filtering for this person
260     [ "${r}" = "${s}" ] && return 0
261
262     # the severity is invalid
263     s="${s^^}"
264     [ "${s}" != "CRITICAL" ] && return 0
265
266     # the new or the old status matches the severity
267     if [ "${s}" = "${status}" -o "${s}" = "${old_status}" ]
268         then
269         [ ! -d "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}" ] && \
270             mkdir -p "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}"
271
272         # we need to keep track of the notifications we sent
273         # so that the same user will receive the recovery
274         # even if old_status does not match the required severity
275         touch "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
276         return 0
277     fi
278
279     # it is a cleared alarm we have sent notification for
280     if [ "${status}" != "WARNING" -a "${status}" != "CRITICAL" -a -f "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}" ]
281         then
282         rm "${NETDATA_CACHE_DIR}/alarm-notify/${method}/${r}/${alarm_id}"
283         return 0
284     fi
285
286     return 1
287 }
288
289 # -----------------------------------------------------------------------------
290 # find the recipients' addresses per method
291
292 declare -A arr_slack=()
293 declare -A arr_discord=()
294 declare -A arr_pushover=()
295 declare -A arr_pushbullet=()
296 declare -A arr_twilio=()
297 declare -A arr_hipchat=()
298 declare -A arr_telegram=()
299 declare -A arr_pd=()
300 declare -A arr_email=()
301
302 # netdata may call us with multiple roles, and roles may have multiple but
303 # overlapping recipients - so, here we find the unique recipients.
304 for x in ${roles//,/ }
305 do
306     # the roles 'silent' and 'disabled' mean:
307     # don't send a notification for this role
308     [ "${x}" = "silent" -o "${x}" = "disabled" ] && continue
309
310     # email
311     a="${role_recipients_email[${x}]}"
312     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_EMAIL}"
313     for r in ${a//,/ }
314     do
315         [ "${r}" != "disabled" ] && filter_recipient_by_criticality email "${r}" && arr_email[${r/|*/}]="1"
316     done
317
318     # pushover
319     a="${role_recipients_pushover[${x}]}"
320     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHOVER}"
321     for r in ${a//,/ }
322     do
323         [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushover "${r}" && arr_pushover[${r/|*/}]="1"
324     done
325
326     # pushbullet
327     a="${role_recipients_pushbullet[${x}]}"
328     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PUSHBULLET}"
329     for r in ${a//,/ }
330     do
331         [ "${r}" != "disabled" ] && filter_recipient_by_criticality pushbullet "${r}" && arr_pushbullet[${r/|*/}]="1"
332     done
333
334     # twilio
335     a="${role_recipients_twilio[${x}]}"
336     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TWILIO}"
337     for r in ${a//,/ }
338     do
339         [ "${r}" != "disabled" ] && filter_recipient_by_criticality twilio "${r}" && arr_twilio[${r/|*/}]="1"
340     done
341
342     # hipchat
343     a="${role_recipients_hipchat[${x}]}"
344     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_HIPCHAT}"
345     for r in ${a//,/ }
346     do
347         [ "${r}" != "disabled" ] && filter_recipient_by_criticality hipchat "${r}" && arr_hipchat[${r/|*/}]="1"
348     done
349
350     # messagebird
351     a="${role_recipients_messagebird[${x}]}"
352     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_MESSAGEBIRD}"
353     for r in ${a//,/ }
354     do
355         [ "${r}" != "disabled" ] && filter_recipient_by_criticality messagebird "${r}" && arr_messagebird[${r/|*/}]="1"
356     done
357
358     # telegram
359     a="${role_recipients_telegram[${x}]}"
360     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_TELEGRAM}"
361     for r in ${a//,/ }
362     do
363         [ "${r}" != "disabled" ] && filter_recipient_by_criticality telegram "${r}" && arr_telegram[${r/|*/}]="1"
364     done
365
366     # slack
367     a="${role_recipients_slack[${x}]}"
368     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_SLACK}"
369     for r in ${a//,/ }
370     do
371         [ "${r}" != "disabled" ] && filter_recipient_by_criticality slack "${r}" && arr_slack[${r/|*/}]="1"
372     done
373
374     # discord
375     a="${role_recipients_discord[${x}]}"
376     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_DISCORD}"
377     for r in ${a//,/ }
378     do
379         [ "${r}" != "disabled" ] && filter_recipient_by_criticality discord "${r}" && arr_discord[${r/|*/}]="1"
380     done
381
382     # pagerduty.com
383     a="${role_recipients_pd[${x}]}"
384     [ -z "${a}" ] && a="${DEFAULT_RECIPIENT_PD}"
385     for r in ${a//,/ }
386     do
387         [ "${r}" != "disabled" ] && filter_recipient_by_criticality pd "${r}" && arr_pd[${r/|*/}]="1"
388     done
389 done
390
391 # build the list of slack recipients (channels)
392 to_slack="${!arr_slack[*]}"
393 [ -z "${to_slack}" ] && SEND_SLACK="NO"
394
395 # build the list of discord recipients (channels)
396 to_discord="${!arr_discord[*]}"
397 [ -z "${to_discord}" ] && SEND_DISCORD="NO"
398
399 # build the list of pushover recipients (user tokens)
400 to_pushover="${!arr_pushover[*]}"
401 [ -z "${to_pushover}" ] && SEND_PUSHOVER="NO"
402
403 # build the list of pushbulet recipients (user tokens)
404 to_pushbullet="${!arr_pushbullet[*]}"
405 [ -z "${to_pushbullet}" ] && SEND_PUSHBULLET="NO"
406
407 # build the list of twilio recipients (phone numbers)
408 to_twilio="${!arr_twilio[*]}"
409 [ -z "${to_twilio}" ] && SEND_TWILIO="NO"
410
411 # build the list of hipchat recipients (rooms)
412 to_hipchat="${!arr_hipchat[*]}"
413 [ -z "${to_hipchat}" ] && SEND_HIPCHAT="NO"
414
415 # build the list of messagebird recipients (phone numbers)
416 to_messagebird="${!arr_messagebird[*]}"
417 [ -z "${to_messagebird}" ] && SEND_MESSAGEBIRD="NO"
418
419 # check array of telegram recipients (chat ids)
420 to_telegram="${!arr_telegram[*]}"
421 [ -z "${to_telegram}" ] && SEND_TELEGRAM="NO"
422
423 # build the list of pagerduty recipients (service keys)
424 to_pd="${!arr_pd[*]}"
425 [ -z "${to_pd}" ] && SEND_PD="NO"
426
427 # build the list of email recipients (email addresses)
428 to_email=
429 for x in "${!arr_email[@]}"
430 do
431     [ ! -z "${to_email}" ] && to_email="${to_email}, "
432     to_email="${to_email}${x}"
433 done
434 [ -z "${to_email}" ] && SEND_EMAIL="NO"
435
436
437 # -----------------------------------------------------------------------------
438 # verify the delivery methods supported
439
440 # check slack
441 [ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
442
443 # check discord
444 [ -z "${DISCORD_WEBHOOK_URL}" ] && SEND_DISCORD="NO"
445
446 # check pushover
447 [ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
448
449 # check pushbullet
450 [ -z "${PUSHBULLET_ACCESS_TOKEN}" ] && SEND_PUSHBULLET="NO"
451
452 # check twilio
453 [ -z "${TWILIO_ACCOUNT_TOKEN}" -o -z "${TWILIO_ACCOUNT_SID}" -o -z "${TWILIO_NUMBER}" ] && SEND_TWILIO="NO"
454
455 # check hipchat
456 [ -z "${HIPCHAT_AUTH_TOKEN}" ] && SEND_HIPCHAT="NO"
457
458 # check messagebird
459 [ -z "${MESSAGEBIRD_ACCESS_KEY}" -o -z "${MESSAGEBIRD_NUMBER}" ] && SEND_MESSAGEBIRD="NO"
460
461 # check telegram
462 [ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
463
464 # check kafka
465 [ -z "${KAFKA_URL}" -o -z "${KAFKA_SENDER_IP}" ] && SEND_KAFKA="NO"
466
467 # check pagerduty.com
468 # if we need pd-send, check for the pd-send command
469 # https://www.pagerduty.com/docs/guides/agent-install-guide/
470 if [ "${SEND_PD}" = "YES" ]
471     then
472     pd_send="$(which pd-send 2>/dev/null || command -v pd-send 2>/dev/null)"
473     if [ -z "${pd_send}" ]
474         then
475         # no pd-send available
476         # disable pagerduty.com
477         SEND_PD="NO"
478     fi
479 fi
480
481 # if we need curl, check for the curl command
482 if [ \( \
483            "${SEND_PUSHOVER}"    = "YES" \
484         -o "${SEND_SLACK}"       = "YES" \
485         -o "${SEND_DISCORD}"       = "YES" \
486         -o "${SEND_HIPCHAT}"     = "YES" \
487         -o "${SEND_TWILIO}"      = "YES" \
488         -o "${SEND_MESSAGEBIRD}" = "YES" \
489         -o "${SEND_TELEGRAM}"    = "YES" \
490         -o "${SEND_PUSHBULLET}"  = "YES" \
491         -o "${SEND_KAFKA}"       = "YES" \
492     \) -a -z "${curl}" ]
493     then
494     curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
495     if [ -z "${curl}" ]
496         then
497         # no curl available
498         # disable all curl based methods
499         SEND_PUSHOVER="NO"
500         SEND_PUSHBULLET="NO"
501         SEND_TELEGRAM="NO"
502         SEND_SLACK="NO"
503         SEND_DISCORD="NO"
504         SEND_TWILIO="NO"
505         SEND_HIPCHAT="NO"
506         SEND_MESSAGEBIRD="NO"
507         SEND_KAFKA="NO"
508     fi
509 fi
510
511 # if we need sendmail, check for the sendmail command
512 if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
513     then
514     sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
515     [ -z "${sendmail}" ] && SEND_EMAIL="NO"
516 fi
517
518 # check that we have at least a method enabled
519 if [   "${SEND_EMAIL}"          != "YES" \
520     -a "${SEND_PUSHOVER}"       != "YES" \
521     -a "${SEND_TELEGRAM}"       != "YES" \
522     -a "${SEND_SLACK}"          != "YES" \
523     -a "${SEND_DISCORD}"          != "YES" \
524     -a "${SEND_TWILIO}"         != "YES" \
525     -a "${SEND_HIPCHAT}"        != "YES" \
526     -a "${SEND_MESSAGEBIRD}"    != "YES" \
527     -a "${SEND_PUSHBULLET}"     != "YES" \
528     -a "${SEND_KAFKA}"          != "YES" \
529     -a "${SEND_PD}"             != "YES" \
530     ]
531     then
532     fatal "All notification methods are disabled. Not sending notification to '${roles}' for '${name}' = '${value}' of chart '${chart}' for status '${status}'."
533 fi
534
535 # -----------------------------------------------------------------------------
536 # find a suitable hostname to use, if netdata did not supply a hostname
537
538 [ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
539 [ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
540 [ -z "${host}" ] && host="$(hostname 2>/dev/null)"
541
542 # -----------------------------------------------------------------------------
543 # get the date the alarm happened
544
545 date="$(date --date=@${when} 2>/dev/null)"
546 [ -z "${date}" ] && date="$(date 2>/dev/null)"
547
548 # -----------------------------------------------------------------------------
549 # function to URL encode a string
550
551 urlencode() {
552     local string="${1}" strlen encoded pos c o
553
554     strlen=${#string}
555     for (( pos=0 ; pos<strlen ; pos++ ))
556     do
557         c=${string:${pos}:1}
558         case "${c}" in
559             [-_.~a-zA-Z0-9])
560                 o="${c}"
561                 ;;
562
563             *)
564                 printf -v o '%%%02x' "'${c}"
565                 ;;
566         esac
567         encoded+="${o}"
568     done
569
570     REPLY="${encoded}"
571     echo "${REPLY}"
572 }
573
574 # -----------------------------------------------------------------------------
575 # function to convert a duration in seconds, to a human readable duration
576 # using DAYS, MINUTES, SECONDS
577
578 duration4human() {
579     local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
580     d=$(( s / 86400 ))
581     s=$(( s - (d * 86400) ))
582     h=$(( s / 3600 ))
583     s=$(( s - (h * 3600) ))
584     m=$(( s / 60 ))
585     s=$(( s - (m * 60) ))
586
587     if [ ${d} -gt 0 ]
588     then
589         [ ${m} -ge 30 ] && h=$(( h + 1 ))
590         [ ${d} -gt 1 ] && ds="days"
591         [ ${h} -gt 1 ] && hs="hours"
592         if [ ${h} -gt 0 ]
593         then
594             ret="${d} ${ds} and ${h} ${hs}"
595         else
596             ret="${d} ${ds}"
597         fi
598     elif [ ${h} -gt 0 ]
599     then
600         [ ${s} -ge 30 ] && m=$(( m + 1 ))
601         [ ${h} -gt 1 ] && hs="hours"
602         [ ${m} -gt 1 ] && ms="minutes"
603         if [ ${m} -gt 0 ]
604         then
605             ret="${h} ${hs} and ${m} ${ms}"
606         else
607             ret="${h} ${hs}"
608         fi
609     elif [ ${m} -gt 0 ]
610     then
611         [ ${m} -gt 1 ] && ms="minutes"
612         [ ${s} -gt 1 ] && ss="seconds"
613         if [ ${s} -gt 0 ]
614         then
615             ret="${m} ${ms} and ${s} ${ss}"
616         else
617             ret="${m} ${ms}"
618         fi
619     else
620         [ ${s} -gt 1 ] && ss="seconds"
621         ret="${s} ${ss}"
622     fi
623
624     REPLY="${ret}"
625     echo "${REPLY}"
626 }
627
628 # -----------------------------------------------------------------------------
629 # email sender
630
631 send_email() {
632     local ret=
633     if [ "${SEND_EMAIL}" = "YES" ]
634         then
635
636         "${sendmail}" -t
637         ret=$?
638
639         if [ ${ret} -eq 0 ]
640         then
641             info "sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
642             return 0
643         else
644             error "failed to send email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}' with error code ${ret}."
645             return 1
646         fi
647     fi
648
649     return 1
650 }
651
652 # -----------------------------------------------------------------------------
653 # pushover sender
654
655 send_pushover() {
656     local apptoken="${1}" usertokens="${2}" when="${3}" url="${4}" status="${5}" title="${6}" message="${7}" httpcode sent=0 user priority
657
658     if [ "${SEND_PUSHOVER}" = "YES" -a ! -z "${apptoken}" -a ! -z "${usertokens}" -a ! -z "${title}" -a ! -z "${message}" ]
659         then
660
661         # https://pushover.net/api
662         priority=-2
663         case "${status}" in
664             CLEAR) priority=-1;;   # low priority: no sound or vibration
665             WARNING) priotity=0;;  # normal priority: respect quiet hours
666             CRITICAL) priority=1;; # high priority: bypass quiet hours
667             *) priority=-2;;       # lowest priority: no notification at all
668         esac
669
670         for user in ${usertokens}
671         do
672             httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
673                 --form-string "token=${apptoken}" \
674                 --form-string "user=${user}" \
675                 --form-string "html=1" \
676                 --form-string "title=${title}" \
677                 --form-string "message=${message}" \
678                 --form-string "timestamp=${when}" \
679                 --form-string "url=${url}" \
680                 --form-string "url_title=Open netdata dashboard to view the alarm" \
681                 --form-string "priority=${priority}" \
682                 https://api.pushover.net/1/messages.json)
683
684             if [ "${httpcode}" == "200" ]
685             then
686                 info "sent pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
687                 sent=$((sent + 1))
688             else
689                 error "failed to send pushover notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
690             fi
691         done
692
693         [ ${sent} -gt 0 ] && return 0
694     fi
695
696     return 1
697 }
698
699 # -----------------------------------------------------------------------------
700 # pushbullet sender
701
702 send_pushbullet() {
703     local userapikey="${1}" recipients="${2}"  title="${3}" message="${4}" httpcode sent=0 user
704     if [ "${SEND_PUSHBULLET}" = "YES" -a ! -z "${userapikey}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
705         then
706         #https://docs.pushbullet.com/#create-push
707         for user in ${recipients}
708         do
709             httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
710               --header 'Access-Token: '${userapikey}'' \
711               --header 'Content-Type: application/json' \
712               --data-binary  @<(cat <<EOF
713                               {"title": "${title}",
714                               "type": "note",
715                               "email": "${user}",
716                               "body": "$( echo -n ${message})"}
717 EOF
718                ) "https://api.pushbullet.com/v2/pushes" -X POST)
719
720             if [ "${httpcode}" == "200" ]
721             then
722                 info "sent pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}'"
723                 sent=$((sent + 1))
724             else
725                 error "failed to send pushbullet notification for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
726             fi
727         done
728
729         [ ${sent} -gt 0 ] && return 0
730     fi
731
732     return 1
733 }
734
735 # -----------------------------------------------------------------------------
736 # kafka sender
737
738 send_kafka() {
739     local httpcode sent=0 
740     if [ "${SEND_KAFKA}" = "YES" ]
741         then
742             httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
743                 --data "{host_ip:\"${KAFKA_SENDER_IP}\",when:${when},name:\"${name}\",chart:\"${chart}\",family:\"${family}\",status:\"${status}\",old_status:\"${old_status}\",value:${value},old_value:${old_value},duration:${duration},non_clear_duration:${non_clear_duration},units:\"${units}\",info:\"${info}\"}" \
744                 "${KAFKA_URL}")
745
746             if [ "${httpcode}" == "204" ]
747             then
748                 info "sent kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}'"
749                 sent=$((sent + 1))
750             else
751                 error "failed to send kafka data for: ${host} ${chart}.${name} is ${status} and ip '${KAFKA_SENDER_IP}' with HTTP error code ${httpcode}."
752             fi
753
754         [ ${sent} -gt 0 ] && return 0
755     fi
756
757     return 1
758 }
759
760 # -----------------------------------------------------------------------------
761 # pagerduty.com sender
762
763 send_pd() {
764     local recipients="${1}" sent=0
765     unset t
766     case ${status} in
767         CLEAR)    t='resolve';;
768         WARNING)  t='trigger';;
769         CRITICAL) t='trigger';;
770     esac
771
772     if [ ${SEND_PD} = "YES" -a ! -z "${t}" ]
773         then
774         for PD_SERVICE_KEY in ${recipients}
775         do
776             d="${status} ${name}=${value} ${units} - ${host}, ${family}"
777             ${pd_send} -k ${PD_SERVICE_KEY} \
778                        -t ${t} \
779                        -d "${d}" \
780                        -i ${alarm_id} \
781                        -f 'info'="${info}" \
782                        -f 'value_w_units'="${value} ${units}" \
783                        -f 'when'="${when}" \
784                        -f 'duration'="${duration}" \
785                        -f 'roles'="${roles}" \
786                        -f 'host'="${host}" \
787                        -f 'unique_id'="${unique_id}" \
788                        -f 'alarm_id'="${alarm_id}" \
789                        -f 'event_id'="${event_id}" \
790                        -f 'name'="${name}" \
791                        -f 'chart'="${chart}" \
792                        -f 'family'="${family}" \
793                        -f 'status'="${status}" \
794                        -f 'old_status'="${old_status}" \
795                        -f 'value'="${value}" \
796                        -f 'old_value'="${old_value}" \
797                        -f 'src'="${src}" \
798                        -f 'non_clear_duration'="${non_clear_duration}" \
799                        -f 'units'="${units}"
800             retval=$?
801             if [ ${retval} -eq 0 ]
802                 then
803                     info "sent pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}....: ${d}"
804                     sent=$((sent + 1))
805                 else
806                     error "failed to send pagerduty.com notification using service key ${PD_SERVICE_KEY::-26}.... (error code ${retval}): ${d}"
807             fi
808         done
809
810         [ ${sent} -gt 0 ] && return 0
811     fi
812
813     return 1
814 }
815
816 # -----------------------------------------------------------------------------
817 # twilio sender
818
819 send_twilio() {
820     local accountsid="${1}" accounttoken="${2}" twilionumber="${3}" recipients="${4}"  title="${5}" message="${6}" httpcode sent=0 user
821     if [ "${SEND_TWILIO}" = "YES" -a ! -z "${accountsid}" -a ! -z "${accounttoken}" -a ! -z "${twilionumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
822         then
823         #https://www.twilio.com/packages/labs/code/bash/twilio-sms
824         for user in ${recipients}
825         do
826             httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
827                 --data-urlencode "From=${twilionumber}" \
828                 --data-urlencode "To=${user}" \
829                 --data-urlencode "Body=${title} ${message}" \
830                 -u "${accountsid}:${accounttoken}" \
831                 "https://api.twilio.com/2010-04-01/Accounts/${accountsid}/Messages.json")
832
833             if [ "${httpcode}" == "201" ]
834             then
835                 info "sent Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
836                 sent=$((sent + 1))
837             else
838                 error "failed to send Twilio SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
839             fi
840         done
841
842         [ ${sent} -gt 0 ] && return 0
843     fi
844
845     return 1
846 }
847
848
849 # -----------------------------------------------------------------------------
850 # hipchat sender
851
852 send_hipchat() {
853     local authtoken="${1}" recipients="${2}" message="${3}" httpcode sent=0 room color sender msg_format notify
854
855     if [ "${SEND_HIPCHAT}" = "YES" -a ! -z "${authtoken}" -a ! -z "${recipients}" -a ! -z "${message}" ]
856         then
857
858         # A label to be shown in addition to the sender's name
859         # Valid length range: 0 - 64. 
860         sender="netdata"
861
862         # Valid values: html, text.
863         # Defaults to 'html'.
864         msg_format="text"
865
866         # Background color for message. Valid values: yellow, green, red, purple, gray, random. Defaults to 'yellow'.
867         case "${status}" in
868             WARNING)  color="yellow" ;;
869             CRITICAL) color="red" ;;
870             CLEAR)    color="green" ;;
871             *)        color="gray" ;;
872         esac
873
874         # Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc).
875         # Each recipient's notification preferences are taken into account.
876         # Defaults to false.
877         notify="true"
878
879         for room in ${recipients}
880         do
881             httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
882                     -H "Content-type: application/json" \
883                     -H "Authorization: Bearer ${authtoken}" \
884                     -d "{\"color\": \"${color}\", \"from\": \"${netdata}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
885                     "https://api.hipchat.com/v2/room/${room}/notification")
886
887             if [ "${httpcode}" == "200" ]
888             then
889                 info "sent HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
890                 sent=$((sent + 1))
891             else
892                 error "failed to send HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}' with HTTP error code ${httpcode}."
893             fi
894         done
895
896         [ ${sent} -gt 0 ] && return 0
897     fi
898
899     return 1
900 }
901
902
903 # -----------------------------------------------------------------------------
904 # messagebird sender
905
906 send_messagebird() {
907     local accesskey="${1}" messagebirdnumber="${2}" recipients="${3}"  title="${4}" message="${5}" httpcode sent=0 user
908     if [ "${SEND_MESSAGEBIRD}" = "YES" -a ! -z "${accesskey}" -a ! -z "${messagebirdnumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
909         then
910         #https://developers.messagebird.com/docs/messaging
911         for user in ${recipients}
912         do
913             httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
914                 --data-urlencode "originator=${messagebirdnumber}" \
915                 --data-urlencode "recipients=${user}" \
916                 --data-urlencode "body=${title} ${message}" \
917                         --data-urlencode "datacoding=auto" \
918                 -H "Authorization: AccessKey ${accesskey}" \
919                 "https://rest.messagebird.com/messages")
920
921             if [ "${httpcode}" == "201" ]
922             then
923                 info "sent Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
924                 sent=$((sent + 1))
925             else
926                 error "failed to send Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
927             fi
928         done
929
930         [ ${sent} -gt 0 ] && return 0
931     fi
932
933     return 1
934 }
935
936 # -----------------------------------------------------------------------------
937 # telegram sender
938
939 send_telegram() {
940     local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
941
942     if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
943
944     if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
945     then
946         for chatid in ${chatids}
947         do
948             # https://core.telegram.org/bots/api#sendmessage
949             httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
950                 --data-urlencode "parse_mode=HTML" \
951                 --data-urlencode "disable_web_page_preview=true" \
952                 --data-urlencode "text=${message}" \
953                 "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
954
955             if [ "${httpcode}" == "200" ]
956             then
957                 info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
958                 sent=$((sent + 1))
959             elif [ "${httpcode}" == "401" ]
960             then
961                 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
962             else
963                 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
964             fi
965         done
966
967         [ ${sent} -gt 0 ] && return 0
968     fi
969
970     return 1
971 }
972
973 # -----------------------------------------------------------------------------
974 # slack sender
975
976 send_slack() {
977     local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
978
979     [ "${SEND_SLACK}" != "YES" ] && return 1
980
981     case "${status}" in
982         WARNING)  color="warning" ;;
983         CRITICAL) color="danger" ;;
984         CLEAR)    color="good" ;;
985         *)        color="#777777" ;;
986     esac
987
988     for channel in ${channels}
989     do
990         payload="$(cat <<EOF
991         {
992             "channel": "#${channel}",
993             "username": "netdata on ${host}",
994             "icon_url": "${images_base_url}/images/seo-performance-128.png",
995             "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
996             "attachments": [
997                 {
998                     "fallback": "${alarm} - ${chart} (${family}) - ${info}",
999                     "color": "${color}",
1000                     "title": "${alarm}",
1001                     "title_link": "${goto_url}",
1002                     "text": "${info}",
1003                     "fields": [
1004                         {
1005                             "title": "${chart}",
1006                             "short": true
1007                         },
1008                         {
1009                             "title": "${family}",
1010                             "short": true
1011                         }
1012                     ],
1013                     "thumb_url": "${image}",
1014                     "footer": "<${goto_url}|${host}>",
1015                     "ts": ${when}
1016                 }
1017             ]
1018         }
1019 EOF
1020         )"
1021
1022         httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1023         if [ "${httpcode}" == "200" ]
1024         then
1025             info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1026             sent=$((sent + 1))
1027         else
1028             error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1029         fi
1030     done
1031
1032     [ ${sent} -gt 0 ] && return 0
1033
1034     return 1
1035 }
1036
1037 # -----------------------------------------------------------------------------
1038 # discord sender
1039
1040 send_discord() {
1041     local webhook="${1}/slack" channels="${2}" httpcode sent=0 channel color payload
1042
1043     [ "${SEND_DISCORD}" != "YES" ] && return 1
1044
1045     case "${status}" in
1046         WARNING)  color="warning" ;;
1047         CRITICAL) color="danger" ;;
1048         CLEAR)    color="good" ;;
1049         *)        color="#777777" ;;
1050     esac
1051
1052     for channel in ${channels}
1053     do
1054         payload="$(cat <<EOF
1055         {
1056             "channel": "#${channel}",
1057             "username": "netdata on ${host}",
1058             "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1059             "icon_url": "https://discordapp.com/assets/f78426a064bc9dd24847519259bc42af.png",
1060             "attachments": [
1061                 {
1062                     "color": "${color}",
1063                     "title": "${alarm}",
1064                     "title_link": "${goto_url}",
1065                     "text": "${info}",
1066                     "fields": [
1067                         {
1068                             "title": "${chart}",
1069                             "value": "${family}"
1070                         }
1071                     ],
1072                     "thumb_url": "${image}",
1073                     "footer_icon": "${images_base_url}/images/seo-performance-128.png",
1074                     "footer": "${host}",
1075                     "ts": ${when}
1076                 }
1077             ]
1078         }
1079 EOF
1080         )"
1081
1082         httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1083         if [ "${httpcode}" == "200" ]
1084         then
1085             info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1086             sent=$((sent + 1))
1087         else
1088             error "failed to send discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1089         fi
1090     done
1091
1092     [ ${sent} -gt 0 ] && return 0
1093
1094     return 1
1095 }
1096
1097
1098 # -----------------------------------------------------------------------------
1099 # prepare the content of the notification
1100
1101 # the url to send the user on click
1102 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
1103 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
1104 urlencode "${family}" >/dev/null; url_family="${REPLY}"
1105 urlencode "${name}" >/dev/null; url_name="${REPLY}"
1106 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}"
1107
1108 # the severity of the alarm
1109 severity="${status}"
1110
1111 # the time the alarm was raised
1112 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
1113 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
1114 raised_for="(was ${old_status,,} for ${duration_txt})"
1115
1116 # the key status message
1117 status_message="status unknown"
1118
1119 # the color of the alarm
1120 color="grey"
1121
1122 # the alarm value
1123 alarm="${name//_/ } = ${value} ${units}"
1124
1125 # the image of the alarm
1126 image="${images_base_url}/images/seo-performance-128.png"
1127
1128 # prepare the title based on status
1129 case "${status}" in
1130         CRITICAL)
1131         image="${images_base_url}/images/alert-128-red.png"
1132         status_message="is critical"
1133         color="#ca414b"
1134         ;;
1135
1136     WARNING)
1137         image="${images_base_url}/images/alert-128-orange.png"
1138         status_message="needs attention"
1139         color="#caca4b"
1140                 ;;
1141
1142         CLEAR)
1143         image="${images_base_url}/images/check-mark-2-128-green.png"
1144         status_message="recovered"
1145                 color="#77ca6d"
1146                 ;;
1147 esac
1148
1149 if [ "${status}" = "CLEAR" ]
1150 then
1151     severity="Recovered from ${old_status}"
1152     if [ ${non_clear_duration} -gt ${duration} ]
1153     then
1154         raised_for="(alarm was raised for ${non_clear_duration_txt})"
1155     fi
1156
1157     # don't show the value when the status is CLEAR
1158     # for certain alarms, this value might not have any meaning
1159     alarm="${name//_/ } ${raised_for}"
1160
1161 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
1162 then
1163     severity="Escalated to ${status}"
1164     if [ ${non_clear_duration} -gt ${duration} ]
1165     then
1166         raised_for="(alarm is raised for ${non_clear_duration_txt})"
1167     fi
1168
1169 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
1170 then
1171     severity="Demoted to ${status}"
1172     if [ ${non_clear_duration} -gt ${duration} ]
1173     then
1174         raised_for="(alarm is raised for ${non_clear_duration_txt})"
1175     fi
1176
1177 else
1178     raised_for=
1179 fi
1180
1181 # prepare HTML versions of elements
1182 info_html=
1183 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
1184
1185 raised_for_html=
1186 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
1187
1188 # -----------------------------------------------------------------------------
1189 # send the slack notification
1190
1191 # slack aggregates posts from the same username
1192 # so we use "${host} ${status}" as the bot username, to make them diff
1193
1194 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1195 SENT_SLACK=$?
1196
1197 # -----------------------------------------------------------------------------
1198 # send the discord notification
1199
1200 # discord aggregates posts from the same username
1201 # so we use "${host} ${status}" as the bot username, to make them diff
1202
1203 send_discord "${DISCORD_WEBHOOK_URL}" "${to_discord}"
1204 SENT_DISCORD=$?
1205
1206 # -----------------------------------------------------------------------------
1207 # send the pushover notification
1208
1209 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
1210 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>&nbsp;
1211 <small><b>${chart}</b><br/>Chart<br/>&nbsp;</small>
1212 <small><b>${family}</b><br/>Family<br/>&nbsp;</small>
1213 <small><b>${severity}</b><br/>Severity<br/>&nbsp;</small>
1214 <small><b>${date}${raised_for_html}</b><br/>Time<br/>&nbsp;</small>
1215 <a href=\"${goto_url}\">View Netdata</a><br/>&nbsp;
1216 <small><small>The source of this alarm is line ${src}</small></small>
1217 "
1218
1219 SENT_PUSHOVER=$?
1220
1221 # -----------------------------------------------------------------------------
1222 # send the pushbullet notification
1223
1224 send_pushbullet "${PUSHBULLET_ACCESS_TOKEN}" "${to_pushbullet}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}\n
1225 Severity: ${severity}\n
1226 Chart: ${chart}\n
1227 Family: ${family}\n
1228 To View Netdata go to: ${goto_url}\n
1229 The source of this alarm is line ${src}"
1230
1231 SENT_PUSHBULLET=$?
1232
1233 # -----------------------------------------------------------------------------
1234 # send the twilio SMS
1235
1236 send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm} 
1237 Severity: ${severity}
1238 Chart: ${chart}
1239 Family: ${family}
1240 ${info}"
1241
1242 SENT_TWILIO=$?
1243
1244 # -----------------------------------------------------------------------------
1245 # send the messagebird SMS
1246
1247 send_messagebird "${MESSAGEBIRD_ACCESS_KEY}" "${MESSAGEBIRD_NUMBER}" "${to_messagebird}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm} 
1248 Severity: ${severity}
1249 Chart: ${chart}
1250 Family: ${family}
1251 ${info}"
1252
1253 SENT_MESSAGEBIRD=$?
1254
1255
1256 # -----------------------------------------------------------------------------
1257 # send the telegram.org message
1258
1259 # https://core.telegram.org/bots/api#formatting-options
1260 send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${host} ${status_message} - <b>${name//_/ }</b>
1261 ${chart} (${family})
1262 <a href=\"${goto_url}\">${alarm}</a>
1263 <i>${info}</i>"
1264
1265 SENT_TELEGRAM=$?
1266
1267
1268 # -----------------------------------------------------------------------------
1269 # send the kafka message
1270
1271 send_kafka
1272 SENT_KAFKA=$?
1273
1274
1275 # -----------------------------------------------------------------------------
1276 # send the pagerduty.com message
1277
1278 send_pd "${to_pd}"
1279 SENT_PD=$?
1280
1281
1282 # -----------------------------------------------------------------------------
1283 # send hipchat message
1284
1285 send_hipchat "${HIPCHAT_AUTH_TOKEN}" "${to_hipchat}" "
1286 <b>${alarm}</b> ${info_html}<br/>&nbsp;
1287 <small><b>${chart}</b><br/>Chart<br/>&nbsp;</small>
1288 <small><b>${family}</b><br/>Family<br/>&nbsp;</small>
1289 <small><b>${severity}</b><br/>Severity<br/>&nbsp;</small>
1290 <small><b>${date}${raised_for_html}</b><br/>Time<br/>&nbsp;</small>
1291 <a href=\"${goto_url}\">View Netdata</a><br/>&nbsp;
1292 <small><small>The source of this alarm is line ${src}</small></small>
1293 "
1294
1295 SENT_HIPCHAT=$?
1296
1297 # -----------------------------------------------------------------------------
1298 # send the email
1299
1300 send_email <<EOF
1301 To: ${to_email}
1302 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
1303 Content-Type: text/html
1304
1305 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1306 <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;">
1307 <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;">
1308 <table>
1309     <tbody>
1310     <tr>
1311         <td style="vertical-align: top;" valign="top"></td>
1312         <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
1313             <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
1314                 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
1315                     <tbody>
1316                     <tr>
1317                         <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
1318                             <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
1319                         </td>
1320                     </tr>
1321                     <tr>
1322                         <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">
1323                             <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
1324                         </td>
1325                     </tr>
1326                     <tr>
1327                         <td style="vertical-align: top;" valign="top">
1328                             <div style="margin: 0; padding: 20px; max-width: 700px;">
1329                                 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
1330                                     <tbody>
1331                                     <tr>
1332                                         <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">
1333                                             <span>${chart}</span>
1334                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
1335                                         </td>
1336                                     </tr>
1337                                     <tr style="margin: 0; padding: 0;">
1338                                         <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">
1339                                             <span><b>${alarm}</b>${info_html}</span>
1340                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
1341                                         </td>
1342                                     </tr>
1343                                     <tr>
1344                                         <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">
1345                                             <span>${family}</span>
1346                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</span>
1347                                         </td>
1348                                     </tr>
1349                                     <tr style="margin: 0; padding: 0;">
1350                                         <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">
1351                                             <span>${severity}</span>
1352                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
1353                                         </td>
1354                                     </tr>
1355                                     <tr style="margin: 0; padding: 0;">
1356                                         <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>
1357                                             <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>
1358                                         </td>
1359                                     </tr>
1360                                     <tr style="margin: 0; padding: 0;">
1361                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
1362                                             <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>
1363                                         </td>
1364                                     </tr>
1365                                     <tr style="text-align: center; margin: 0; padding: 0;">
1366                                         <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>
1367                                         </td>
1368                                     </tr>
1369                                     <tr style="text-align: center; margin: 0; padding: 0;">
1370                                         <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
1371                                             <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
1372                                         </td>
1373                                     </tr>
1374                                     </tbody>
1375                                 </table>
1376                             </div>
1377                         </td>
1378                     </tr>
1379                     </tbody>
1380                 </table>
1381             </div>
1382         </td>
1383     </tr>
1384     </tbody>
1385 </table>
1386 </body>
1387 </html>
1388 EOF
1389
1390 SENT_EMAIL=$?
1391
1392 # -----------------------------------------------------------------------------
1393 # let netdata know
1394
1395 if [   ${SENT_EMAIL}        -eq 0 \
1396     -o ${SENT_PUSHOVER}     -eq 0 \
1397     -o ${SENT_TELEGRAM}     -eq 0 \
1398     -o ${SENT_SLACK}        -eq 0 \
1399     -o ${SENT_DISCORD}      -eq 0 \
1400     -o ${SENT_TWILIO}       -eq 0 \
1401     -o ${SENT_HIPCHAT}      -eq 0 \
1402     -o ${SENT_MESSAGEBIRD}  -eq 0 \
1403     -o ${SENT_PUSHBULLET}   -eq 0 \
1404     -o ${SENT_KAFKA}        -eq 0 \
1405     -o ${SENT_PD}           -eq 0 \
1406     ]
1407     then
1408     # we did send something
1409     exit 0
1410 fi
1411
1412 # we did not send anything
1413 exit 1
1414