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