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