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