]> arthur.barton.de Git - netdata.git/blob - plugins.d/alarm-notify.sh
ab-debian 0.20170311.01-0ab1, upstream v1.5.0-573-g0fba967b
[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_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 authtoken="${1}" recipients="${2}" message="${3}" httpcode sent=0 room color sender msg_format notify
857
858     if [ "${SEND_HIPCHAT}" = "YES" -a ! -z "${HIPCHAT_SERVER}" -a ! -z "${authtoken}" -a ! -z "${recipients}" -a ! -z "${message}" ]
859     then
860         # A label to be shown in addition to the sender's name
861         # Valid length range: 0 - 64. 
862         sender="netdata"
863
864         # Valid values: html, text.
865         # Defaults to 'html'.
866         msg_format="html"
867
868         # Background color for message. Valid values: yellow, green, red, purple, gray, random. Defaults to 'yellow'.
869         case "${status}" in
870             WARNING)  color="yellow" ;;
871             CRITICAL) color="red" ;;
872             CLEAR)    color="green" ;;
873             *)        color="gray" ;;
874         esac
875
876         # Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc).
877         # Each recipient's notification preferences are taken into account.
878         # Defaults to false.
879         notify="true"
880
881         for room in ${recipients}
882         do
883             httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
884                     -H "Content-type: application/json" \
885                     -H "Authorization: Bearer ${authtoken}" \
886                     -d "{\"color\": \"${color}\", \"from\": \"${netdata}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
887                     "https://${HIPCHAT_SERVER}/v2/room/${room}/notification")
888  
889             if [ "${httpcode}" == "204" ]
890             then
891                 info "sent HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
892                 sent=$((sent + 1))
893             else
894                 error "failed to send HipChat notification for: ${host} ${chart}.${name} is ${status} to '${room}' with HTTP error code ${httpcode}."
895             fi
896         done
897
898         [ ${sent} -gt 0 ] && return 0
899     fi
900
901     return 1
902 }
903
904
905 # -----------------------------------------------------------------------------
906 # messagebird sender
907
908 send_messagebird() {
909     local accesskey="${1}" messagebirdnumber="${2}" recipients="${3}"  title="${4}" message="${5}" httpcode sent=0 user
910     if [ "${SEND_MESSAGEBIRD}" = "YES" -a ! -z "${accesskey}" -a ! -z "${messagebirdnumber}" -a ! -z "${recipients}" -a ! -z "${message}" -a ! -z "${title}" ]
911         then
912         #https://developers.messagebird.com/docs/messaging
913         for user in ${recipients}
914         do
915             httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
916                 --data-urlencode "originator=${messagebirdnumber}" \
917                 --data-urlencode "recipients=${user}" \
918                 --data-urlencode "body=${title} ${message}" \
919                         --data-urlencode "datacoding=auto" \
920                 -H "Authorization: AccessKey ${accesskey}" \
921                 "https://rest.messagebird.com/messages")
922
923             if [ "${httpcode}" == "201" ]
924             then
925                 info "sent Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}'"
926                 sent=$((sent + 1))
927             else
928                 error "failed to send Messagebird SMS for: ${host} ${chart}.${name} is ${status} to '${user}' with HTTP error code ${httpcode}."
929             fi
930         done
931
932         [ ${sent} -gt 0 ] && return 0
933     fi
934
935     return 1
936 }
937
938 # -----------------------------------------------------------------------------
939 # telegram sender
940
941 send_telegram() {
942     local bottoken="${1}" chatids="${2}" message="${3}" httpcode sent=0 chatid disableNotification=""
943
944     if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi
945
946     if [ "${SEND_TELEGRAM}" = "YES" -a ! -z "${bottoken}" -a ! -z "${chatids}" -a ! -z "${message}" ];
947     then
948         for chatid in ${chatids}
949         do
950             # https://core.telegram.org/bots/api#sendmessage
951             httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
952                 --data-urlencode "parse_mode=HTML" \
953                 --data-urlencode "disable_web_page_preview=true" \
954                 --data-urlencode "text=${message}" \
955                 "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
956
957             if [ "${httpcode}" == "200" ]
958             then
959                 info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'"
960                 sent=$((sent + 1))
961             elif [ "${httpcode}" == "401" ]
962             then
963                 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token."
964             else
965                 error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP error code ${httpcode}."
966             fi
967         done
968
969         [ ${sent} -gt 0 ] && return 0
970     fi
971
972     return 1
973 }
974
975 # -----------------------------------------------------------------------------
976 # slack sender
977
978 send_slack() {
979     local webhook="${1}" channels="${2}" httpcode sent=0 channel color payload
980
981     [ "${SEND_SLACK}" != "YES" ] && return 1
982
983     case "${status}" in
984         WARNING)  color="warning" ;;
985         CRITICAL) color="danger" ;;
986         CLEAR)    color="good" ;;
987         *)        color="#777777" ;;
988     esac
989
990     for channel in ${channels}
991     do
992         payload="$(cat <<EOF
993         {
994             "channel": "#${channel}",
995             "username": "netdata on ${host}",
996             "icon_url": "${images_base_url}/images/seo-performance-128.png",
997             "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
998             "attachments": [
999                 {
1000                     "fallback": "${alarm} - ${chart} (${family}) - ${info}",
1001                     "color": "${color}",
1002                     "title": "${alarm}",
1003                     "title_link": "${goto_url}",
1004                     "text": "${info}",
1005                     "fields": [
1006                         {
1007                             "title": "${chart}",
1008                             "short": true
1009                         },
1010                         {
1011                             "title": "${family}",
1012                             "short": true
1013                         }
1014                     ],
1015                     "thumb_url": "${image}",
1016                     "footer": "<${goto_url}|${host}>",
1017                     "ts": ${when}
1018                 }
1019             ]
1020         }
1021 EOF
1022         )"
1023
1024         httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1025         if [ "${httpcode}" == "200" ]
1026         then
1027             info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1028             sent=$((sent + 1))
1029         else
1030             error "failed to send slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1031         fi
1032     done
1033
1034     [ ${sent} -gt 0 ] && return 0
1035
1036     return 1
1037 }
1038
1039 # -----------------------------------------------------------------------------
1040 # discord sender
1041
1042 send_discord() {
1043     local webhook="${1}/slack" channels="${2}" httpcode sent=0 channel color payload
1044
1045     [ "${SEND_DISCORD}" != "YES" ] && return 1
1046
1047     case "${status}" in
1048         WARNING)  color="warning" ;;
1049         CRITICAL) color="danger" ;;
1050         CLEAR)    color="good" ;;
1051         *)        color="#777777" ;;
1052     esac
1053
1054     for channel in ${channels}
1055     do
1056         payload="$(cat <<EOF
1057         {
1058             "channel": "#${channel}",
1059             "username": "netdata on ${host}",
1060             "text": "${host} ${status_message}, \`${chart}\` (_${family}_), *${alarm}*",
1061             "icon_url": "${images_base_url}/images/seo-performance-128.png",
1062             "attachments": [
1063                 {
1064                     "color": "${color}",
1065                     "title": "${alarm}",
1066                     "title_link": "${goto_url}",
1067                     "text": "${info}",
1068                     "fields": [
1069                         {
1070                             "title": "${chart}",
1071                             "value": "${family}"
1072                         }
1073                     ],
1074                     "thumb_url": "${image}",
1075                     "footer_icon": "${images_base_url}/images/seo-performance-128.png",
1076                     "footer": "${host}",
1077                     "ts": ${when}
1078                 }
1079             ]
1080         }
1081 EOF
1082         )"
1083
1084         httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1085         if [ "${httpcode}" == "200" ]
1086         then
1087             info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
1088             sent=$((sent + 1))
1089         else
1090             error "failed to send discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}', with HTTP error code ${httpcode}."
1091         fi
1092     done
1093
1094     [ ${sent} -gt 0 ] && return 0
1095
1096     return 1
1097 }
1098
1099
1100 # -----------------------------------------------------------------------------
1101 # prepare the content of the notification
1102
1103 # the url to send the user on click
1104 urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
1105 urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
1106 urlencode "${family}" >/dev/null; url_family="${REPLY}"
1107 urlencode "${name}" >/dev/null; url_name="${REPLY}"
1108 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}"
1109
1110 # the severity of the alarm
1111 severity="${status}"
1112
1113 # the time the alarm was raised
1114 duration4human ${duration} >/dev/null; duration_txt="${REPLY}"
1115 duration4human ${non_clear_duration} >/dev/null; non_clear_duration_txt="${REPLY}"
1116 raised_for="(was ${old_status,,} for ${duration_txt})"
1117
1118 # the key status message
1119 status_message="status unknown"
1120
1121 # the color of the alarm
1122 color="grey"
1123
1124 # the alarm value
1125 alarm="${name//_/ } = ${value_string}"
1126
1127 # the image of the alarm
1128 image="${images_base_url}/images/seo-performance-128.png"
1129
1130 # prepare the title based on status
1131 case "${status}" in
1132         CRITICAL)
1133         image="${images_base_url}/images/alert-128-red.png"
1134         status_message="is critical"
1135         color="#ca414b"
1136         ;;
1137
1138     WARNING)
1139         image="${images_base_url}/images/alert-128-orange.png"
1140         status_message="needs attention"
1141         color="#caca4b"
1142                 ;;
1143
1144         CLEAR)
1145         image="${images_base_url}/images/check-mark-2-128-green.png"
1146         status_message="recovered"
1147                 color="#77ca6d"
1148                 ;;
1149 esac
1150
1151 if [ "${status}" = "CLEAR" ]
1152 then
1153     severity="Recovered from ${old_status}"
1154     if [ ${non_clear_duration} -gt ${duration} ]
1155     then
1156         raised_for="(alarm was raised for ${non_clear_duration_txt})"
1157     fi
1158
1159     # don't show the value when the status is CLEAR
1160     # for certain alarms, this value might not have any meaning
1161     alarm="${name//_/ } ${raised_for}"
1162
1163 elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
1164 then
1165     severity="Escalated to ${status}"
1166     if [ ${non_clear_duration} -gt ${duration} ]
1167     then
1168         raised_for="(alarm is raised for ${non_clear_duration_txt})"
1169     fi
1170
1171 elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
1172 then
1173     severity="Demoted to ${status}"
1174     if [ ${non_clear_duration} -gt ${duration} ]
1175     then
1176         raised_for="(alarm is raised for ${non_clear_duration_txt})"
1177     fi
1178
1179 else
1180     raised_for=
1181 fi
1182
1183 # prepare HTML versions of elements
1184 info_html=
1185 [ ! -z "${info}" ] && info_html=" <small><br/>${info}</small>"
1186
1187 raised_for_html=
1188 [ ! -z "${raised_for}" ] && raised_for_html="<br/><small>${raised_for}</small>"
1189
1190 # -----------------------------------------------------------------------------
1191 # send the slack notification
1192
1193 # slack aggregates posts from the same username
1194 # so we use "${host} ${status}" as the bot username, to make them diff
1195
1196 send_slack "${SLACK_WEBHOOK_URL}" "${to_slack}"
1197 SENT_SLACK=$?
1198
1199 # -----------------------------------------------------------------------------
1200 # send the discord notification
1201
1202 # discord aggregates posts from the same username
1203 # so we use "${host} ${status}" as the bot username, to make them diff
1204
1205 send_discord "${DISCORD_WEBHOOK_URL}" "${to_discord}"
1206 SENT_DISCORD=$?
1207
1208 # -----------------------------------------------------------------------------
1209 # send the pushover notification
1210
1211 send_pushover "${PUSHOVER_APP_TOKEN}" "${to_pushover}" "${when}" "${goto_url}" "${status}" "${host} ${status_message} - ${name//_/ } - ${chart}" "
1212 <font color=\"${color}\"><b>${alarm}</b></font>${info_html}<br/>&nbsp;
1213 <small><b>${chart}</b><br/>Chart<br/>&nbsp;</small>
1214 <small><b>${family}</b><br/>Family<br/>&nbsp;</small>
1215 <small><b>${severity}</b><br/>Severity<br/>&nbsp;</small>
1216 <small><b>${date}${raised_for_html}</b><br/>Time<br/>&nbsp;</small>
1217 <a href=\"${goto_url}\">View Netdata</a><br/>&nbsp;
1218 <small><small>The source of this alarm is line ${src}</small></small>
1219 "
1220
1221 SENT_PUSHOVER=$?
1222
1223 # -----------------------------------------------------------------------------
1224 # send the pushbullet notification
1225
1226 send_pushbullet "${PUSHBULLET_ACCESS_TOKEN}" "${to_pushbullet}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm}\n
1227 Severity: ${severity}\n
1228 Chart: ${chart}\n
1229 Family: ${family}\n
1230 To View Netdata go to: ${goto_url}\n
1231 The source of this alarm is line ${src}"
1232
1233 SENT_PUSHBULLET=$?
1234
1235 # -----------------------------------------------------------------------------
1236 # send the twilio SMS
1237
1238 send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm} 
1239 Severity: ${severity}
1240 Chart: ${chart}
1241 Family: ${family}
1242 ${info}"
1243
1244 SENT_TWILIO=$?
1245
1246 # -----------------------------------------------------------------------------
1247 # send the messagebird SMS
1248
1249 send_messagebird "${MESSAGEBIRD_ACCESS_KEY}" "${MESSAGEBIRD_NUMBER}" "${to_messagebird}" "${host} ${status_message} - ${name//_/ } - ${chart}" "${alarm} 
1250 Severity: ${severity}
1251 Chart: ${chart}
1252 Family: ${family}
1253 ${info}"
1254
1255 SENT_MESSAGEBIRD=$?
1256
1257
1258 # -----------------------------------------------------------------------------
1259 # send the telegram.org message
1260
1261 # https://core.telegram.org/bots/api#formatting-options
1262 send_telegram "${TELEGRAM_BOT_TOKEN}" "${to_telegram}" "${host} ${status_message} - <b>${name//_/ }</b>
1263 ${chart} (${family})
1264 <a href=\"${goto_url}\">${alarm}</a>
1265 <i>${info}</i>"
1266
1267 SENT_TELEGRAM=$?
1268
1269
1270 # -----------------------------------------------------------------------------
1271 # send the kafka message
1272
1273 send_kafka
1274 SENT_KAFKA=$?
1275
1276
1277 # -----------------------------------------------------------------------------
1278 # send the pagerduty.com message
1279
1280 send_pd "${to_pd}"
1281 SENT_PD=$?
1282
1283
1284 # -----------------------------------------------------------------------------
1285 # send hipchat message
1286
1287 send_hipchat "${HIPCHAT_AUTH_TOKEN}" "${to_hipchat}" " \
1288 ${host} ${status_message}<br/> \
1289 <b>${alarm}</b> ${info_html}<br/> \
1290 <b>${chart}</b> (family <b>${family}</b>)<br/> \
1291 <b>${date}${raised_for_html}</b><br/> \
1292 <a href=\\\"${goto_url}\\\">View netdata dashboard</a> \
1293 (source of alarm ${src}) \
1294 "
1295
1296 SENT_HIPCHAT=$?
1297
1298 # -----------------------------------------------------------------------------
1299 # send the email
1300
1301 send_email <<EOF
1302 To: ${to_email}
1303 Subject: ${host} ${status_message} - ${name//_/ } - ${chart}
1304 Content-Type: text/html
1305
1306 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1307 <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;">
1308 <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;">
1309 <table>
1310     <tbody>
1311     <tr>
1312         <td style="vertical-align: top;" valign="top"></td>
1313         <td width="700" style="vertical-align: top; display: block !important; max-width: 700px !important; clear: both !important; margin: 0 auto; padding: 0;" valign="top">
1314             <div style="max-width: 700px; display: block; margin: 0 auto; padding: 20px;">
1315                 <table width="100%" cellpadding="0" cellspacing="0" style="background: #fff; border: 1px solid #e9e9e9;">
1316                     <tbody>
1317                     <tr>
1318                         <td bgcolor="#eee" style="padding: 5px 20px 5px 20px; background-color: #eee;">
1319                             <div style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 20px; color: #777; font-weight: bold;">netdata notification</div>
1320                         </td>
1321                     </tr>
1322                     <tr>
1323                         <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">
1324                             <h1 style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: 400; margin: 0;">${host} ${status_message}</h1>
1325                         </td>
1326                     </tr>
1327                     <tr>
1328                         <td style="vertical-align: top;" valign="top">
1329                             <div style="margin: 0; padding: 20px; max-width: 700px;">
1330                                 <table width="100%" cellpadding="0" cellspacing="0" style="max-width:700px">
1331                                     <tbody>
1332                                     <tr>
1333                                         <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">
1334                                             <span>${chart}</span>
1335                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Chart</span>
1336                                         </td>
1337                                     </tr>
1338                                     <tr style="margin: 0; padding: 0;">
1339                                         <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">
1340                                             <span><b>${alarm}</b>${info_html}</span>
1341                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Alarm</span>
1342                                         </td>
1343                                     </tr>
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>${family}</span>
1347                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Family</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>${severity}</span>
1353                                             <span style="display: block; color: #666666; font-size: 12px; font-weight: 300; line-height: 1; text-transform: uppercase;">Severity</span>
1354                                         </td>
1355                                     </tr>
1356                                     <tr style="margin: 0; padding: 0;">
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"><span>${date}</span>
1358                                             <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>
1359                                         </td>
1360                                     </tr>
1361                                     <tr style="margin: 0; padding: 0;">
1362                                         <td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;">
1363                                             <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>
1364                                         </td>
1365                                     </tr>
1366                                     <tr style="text-align: center; margin: 0; padding: 0;">
1367                                         <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>
1368                                         </td>
1369                                     </tr>
1370                                     <tr style="text-align: center; margin: 0; padding: 0;">
1371                                         <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
1372                                             <a href="https://mynetdata.io/" target="_blank">netdata</a>, the real-time performance monitoring.
1373                                         </td>
1374                                     </tr>
1375                                     </tbody>
1376                                 </table>
1377                             </div>
1378                         </td>
1379                     </tr>
1380                     </tbody>
1381                 </table>
1382             </div>
1383         </td>
1384     </tr>
1385     </tbody>
1386 </table>
1387 </body>
1388 </html>
1389 EOF
1390
1391 SENT_EMAIL=$?
1392
1393 # -----------------------------------------------------------------------------
1394 # let netdata know
1395
1396 if [   ${SENT_EMAIL}        -eq 0 \
1397     -o ${SENT_PUSHOVER}     -eq 0 \
1398     -o ${SENT_TELEGRAM}     -eq 0 \
1399     -o ${SENT_SLACK}        -eq 0 \
1400     -o ${SENT_DISCORD}      -eq 0 \
1401     -o ${SENT_TWILIO}       -eq 0 \
1402     -o ${SENT_HIPCHAT}      -eq 0 \
1403     -o ${SENT_MESSAGEBIRD}  -eq 0 \
1404     -o ${SENT_PUSHBULLET}   -eq 0 \
1405     -o ${SENT_KAFKA}        -eq 0 \
1406     -o ${SENT_PD}           -eq 0 \
1407     ]
1408     then
1409     # we did send something
1410     exit 0
1411 fi
1412
1413 # we did not send anything
1414 exit 1
1415