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