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