-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
alarm-notify.sh.in
executable file
·3521 lines (3014 loc) · 133 KB
/
alarm-notify.sh.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#shellcheck source=/dev/null disable=SC2086,SC2154
# netdata
# real-time performance and health monitoring, done right!
# (C) 2023 Netdata Inc.
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Script to send alarm notifications for netdata
#
# Features:
# - multiple notification methods
# - multiple roles per alarm
# - multiple recipients per role
# - severity filtering per recipient
#
# Supported notification methods:
# - emails by @ktsaou
# - slack.com notifications by @ktsaou
# - alerta.io notifications by @kattunga
# - discord.com notifications by @lowfive
# - pushover.net notifications by @ktsaou
# - pushbullet.com push notifications by Tiago Peralta @tperalta82 #1070
# - telegram.org notifications by @hashworks #1002
# - twilio.com notifications by Levi Blaney @shadycuz #1211
# - kafka notifications by @ktsaou #1342
# - pagerduty.com notifications by Jim Cooley @jimcooley #1373
# - messagebird.com notifications by @tech_no_logical #1453
# - hipchat notifications by @ktsaou #1561
# - fleep notifications by @Ferroin
# - prowlapp.com notifications by @Ferroin
# - irc notifications by @manosf
# - custom notifications by @ktsaou
# - syslog messages by @Ferroin
# - Microsoft Team notification by @tioumen
# - RocketChat notifications by @Hermsi1337 #3777
# - Dynatrace Event by @illumine
# - Opsgenie by @thiaoftsm #9858
# - Gotify by @coffeegrind123
# - ntfy.sh by @Dim-P
# -----------------------------------------------------------------------------
# testing notifications
if { [ "${1}" = "test" ] || [ "${2}" = "test" ]; } && [ "${#}" -le 2 ]; then
if [ "${2}" = "test" ]; then
recipient="${1}"
else
recipient="${2}"
fi
[ -z "${recipient}" ] && recipient="sysadmin"
id=1
last="CLEAR"
test_res=0
for x in "WARNING" "CRITICAL" "CLEAR"; do
echo >&2
echo >&2 "# SENDING TEST ${x} ALARM TO ROLE: ${recipient}"
"${0}" "${recipient}" "$(hostname)" 1 1 "${id}" "$(date +%s)" "test_alarm" "test.chart" "${x}" "${last}" 100 90 "${0}" 1 $((0 + id)) "units" "this is a test alarm to verify notifications work" "new value" "old value" "evaluated expression" "expression variable values" 0 0 "" "" "Test" "command to edit the alarm=0=$(hostname)" "" "" "a test alarm"
#shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo >&2 "# FAILED"
test_res=1
else
echo >&2 "# OK"
fi
last="${x}"
id=$((id + 1))
done
exit $test_res
fi
export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
export LC_ALL=C
# -----------------------------------------------------------------------------
PROGRAM_NAME="$(basename "${0}")"
LOG_LEVEL_ERR=1
LOG_LEVEL_WARN=2
LOG_LEVEL_INFO=3
LOG_LEVEL="$LOG_LEVEL_INFO"
set_log_severity_level() {
case ${NETDATA_LOG_SEVERITY_LEVEL,,} in
"info") LOG_LEVEL="$LOG_LEVEL_INFO";;
"warn" | "warning") LOG_LEVEL="$LOG_LEVEL_WARN";;
"err" | "error") LOG_LEVEL="$LOG_LEVEL_ERR";;
esac
}
set_log_severity_level
logdate() {
date "+%Y-%m-%d %H:%M:%S"
}
log() {
local status="${1}"
shift
echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
}
info() {
[[ -n "$LOG_LEVEL" && "$LOG_LEVEL_INFO" -gt "$LOG_LEVEL" ]] && return
log INFO "${@}"
}
warning() {
[[ -n "$LOG_LEVEL" && "$LOG_LEVEL_WARN" -gt "$LOG_LEVEL" ]] && return
log WARNING "${@}"
}
error() {
[[ -n "$LOG_LEVEL" && "$LOG_LEVEL_ERR" -gt "$LOG_LEVEL" ]] && return
log ERROR "${@}"
}
fatal() {
log FATAL "${@}"
exit 1
}
debug=${NETDATA_ALARM_NOTIFY_DEBUG-0}
debug() {
[ "${debug}" = "1" ] && log DEBUG "${@}"
}
docurl() {
if [ -z "${curl}" ]; then
error "${curl} is unset."
return 1
fi
if [ "${debug}" = "1" ]; then
echo >&2 "--- BEGIN curl command ---"
printf >&2 "%q " ${curl} "${@}"
echo >&2
echo >&2 "--- END curl command ---"
local out code ret
out=$(mktemp /tmp/netdata-health-alarm-notify-XXXXXXXX)
code=$(${curl} ${curl_options} --write-out "%{http_code}" --output "${out}" --silent --show-error "${@}")
ret=$?
echo >&2 "--- BEGIN received response ---"
cat >&2 "${out}"
echo >&2
echo >&2 "--- END received response ---"
echo >&2 "RECEIVED HTTP RESPONSE CODE: ${code}"
rm "${out}"
echo "${code}"
return ${ret}
fi
${curl} ${curl_options} --write-out "%{http_code}" --output /dev/null --silent --show-error "${@}"
return $?
}
# -----------------------------------------------------------------------------
# List of all the notification mechanisms we support.
# Used in a couple of places to write more compact code.
method_names="
email
pushover
pushbullet
telegram
slack
alerta
flock
discord
hipchat
twilio
messagebird
pd
fleep
syslog
custom
msteams
kavenegar
prowl
irc
awssns
rocketchat
sms
dynatrace
matrix
ntfy
"
# -----------------------------------------------------------------------------
# this is to be overwritten by the config file
custom_sender() {
info "not sending custom notification for ${status} of '${host}.${chart}.${name}'"
}
# -----------------------------------------------------------------------------
# check for BASH v4+ (required for associative arrays)
if [ ${BASH_VERSINFO[0]} -lt 4 ]; then
fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
fi
# -----------------------------------------------------------------------------
# defaults to allow running this script by hand
[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
[ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
[ -z "${NETDATA_CACHE_DIR}" ] && NETDATA_CACHE_DIR="@cachedir_POST@"
[ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
[ -z "${NETDATA_REGISTRY_CLOUD_BASE_URL}" ] && NETDATA_REGISTRY_CLOUD_BASE_URL="https://app.netdata.cloud"
# -----------------------------------------------------------------------------
# parse command line parameters
if [[ ${1} = "unittest" ]]; then
unittest=1 # enable unit testing mode
roles="${2}" # the role that should be used for unit testing
cfgfile="${3}" # the location of the config file to use for unit testing
status="${4}" # the current status : REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
old_status="${5}" # the previous status: REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
elif [[ ${1} = "dump_methods" ]]; then
dump_methods=1
status="WARNING"
else
roles="${1}" # the roles that should be notified for this event
args_host="${2}" # the host generated this event
unique_id="${3}" # the unique id of this event
alarm_id="${4}" # the unique id of the alarm that generated this event
event_id="${5}" # the incremental id of the event, for this alarm id
when="${6}" # the timestamp this event occurred
name="${7}" # the name of the alarm, as given in netdata health.d entries
chart="${8}" # the name of the chart (type.id)
status="${9}" # the current status : REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
old_status="${10}" # the previous status: REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL
value="${11}" # the current value of the alarm
old_value="${12}" # the previous value of the alarm
src="${13}" # the line number and file the alarm has been configured
duration="${14}" # the duration in seconds of the previous alarm state
non_clear_duration="${15}" # the total duration in seconds this is/was non-clear
units="${16}" # the units of the value
info="${17}" # a short description of the alarm
value_string="${18}" # friendly value (with units)
# shellcheck disable=SC2034
# variable is unused, but https://github.com/netdata/netdata/pull/5164#discussion_r255572947
old_value_string="${19}" # friendly old value (with units), previously named "old_value_string"
calc_expression="${20}" # contains the expression that was evaluated to trigger the alarm
calc_param_values="${21}" # the values of the parameters in the expression, at the time of the evaluation
total_warnings="${22}" # Total number of alarms in WARNING state
total_critical="${23}" # Total number of alarms in CRITICAL state
total_warn_alarms="${24}" # List of alarms in warning state
total_crit_alarms="${25}" # List of alarms in critical state
classification="${26}" # The class field from .conf files
edit_command_line="${27}" # The command to edit the alarm, with the line number
child_machine_guid="${28}" # the machine_guid of the child
transition_id="${29}" # the transition_id of the alert
summary="${30}" # the summary text field of the alert
fi
# -----------------------------------------------------------------------------
# find a suitable hostname to use, if netdata did not supply a hostname
if [ -z "${args_host}" ]; then
this_host=$(hostname -s 2>/dev/null)
host="${this_host}"
args_host="${this_host}"
else
host="${args_host}"
fi
# -----------------------------------------------------------------------------
# screen statuses we don't need to send a notification
# don't do anything if this is not WARNING, CRITICAL or CLEAR
if [ "${status}" != "WARNING" ] && [ "${status}" != "CRITICAL" ] && [ "${status}" != "CLEAR" ]; then
info "not sending notification for ${status} of '${host}.${chart}.${name}'"
exit 1
fi
# don't do anything if this is CLEAR, but it was not WARNING or CRITICAL
if [ "${clear_alarm_always}" != "YES" ] && [ "${old_status}" != "WARNING" ] && [ "${old_status}" != "CRITICAL" ] && [ "${status}" = "CLEAR" ]; then
info "not sending notification for ${status} of '${host}.${chart}.${name}' (last status was ${old_status})"
exit 1
fi
# -----------------------------------------------------------------------------
# load configuration
# By default fetch images from the global public registry.
# This is required by default, since all notification methods need to download
# images via the Internet, and private registries might not be reachable.
# This can be overwritten at the configuration file.
images_base_url="https://registry.my-netdata.io"
# curl options to use
curl_options=""
# hostname handling
use_fqdn="NO"
# needed commands
# if empty they will be searched in the system path
curl=
sendmail=
# enable / disable features
for method_name in ${method_names^^}; do
declare SEND_${method_name}="YES"
declare DEFAULT_RECIPIENT_${method_name}
done
for method_name in ${method_names}; do
declare -A role_recipients_${method_name}
done
# slack configs
SLACK_WEBHOOK_URL=
# Microsoft Teams configs
MSTEAMS_WEBHOOK_URL=
# Legacy Microsoft Teams configs for backwards compatibility:
declare -A role_recipients_msteam
# rocketchat configs
ROCKETCHAT_WEBHOOK_URL=
# alerta configs
ALERTA_WEBHOOK_URL=
ALERTA_API_KEY=
# flock configs
FLOCK_WEBHOOK_URL=
# discord configs
DISCORD_WEBHOOK_URL=
# pushover configs
PUSHOVER_APP_TOKEN=
# pushbullet configs
PUSHBULLET_ACCESS_TOKEN=
PUSHBULLET_SOURCE_DEVICE=
# twilio configs
TWILIO_ACCOUNT_SID=
TWILIO_ACCOUNT_TOKEN=
TWILIO_NUMBER=
# hipchat configs
HIPCHAT_SERVER=
HIPCHAT_AUTH_TOKEN=
# messagebird configs
MESSAGEBIRD_ACCESS_KEY=
MESSAGEBIRD_NUMBER=
# kavenegar configs
KAVENEGAR_API_KEY=
KAVENEGAR_SENDER=
# telegram configs
TELEGRAM_BOT_TOKEN=
# kafka configs
SEND_KAFKA="YES"
KAFKA_URL=
KAFKA_SENDER_IP=
# pagerduty.com configs
PD_SERVICE_KEY=
USE_PD_VERSION=
# fleep.io configs
FLEEP_SENDER="${host}"
# Amazon SNS configs
AWSSNS_MESSAGE_FORMAT=
# Matrix configs
MATRIX_HOMESERVER=
MATRIX_ACCESSTOKEN=
# syslog configs
SYSLOG_FACILITY=
# email configs
EMAIL_SENDER=
EMAIL_CHARSET=$(locale charmap 2>/dev/null)
EMAIL_THREADING=
EMAIL_PLAINTEXT_ONLY=
# irc configs
IRC_NICKNAME=
IRC_REALNAME=
IRC_NETWORK=
IRC_PORT=6667
# dynatrace configs
DYNATRACE_SPACE=
DYNATRACE_SERVER=
DYNATRACE_TOKEN=
DYNATRACE_TAG_VALUE=
DYNATRACE_ANNOTATION_TYPE=
DYNATRACE_EVENT=
SEND_DYNATRACE=
# gotify configs
GOTIFY_APP_URL=
GOTIFY_APP_TOKEN=
# opsgenie configs
OPSGENIE_API_KEY=
# load the stock and user configuration files
# these will overwrite the variables above
if [ ${unittest} ]; then
if source "${cfgfile}"; then
error "Failed to load requested config file."
exit 1
fi
else
for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/health_alarm_notify.conf" "${NETDATA_USER_CONFIG_DIR}/health_alarm_notify.conf"; do
if [ -f "${CONFIG}" ]; then
debug "Loading config file '${CONFIG}'..."
source "${CONFIG}" || error "Failed to load config file '${CONFIG}'."
else
warning "Cannot find file '${CONFIG}'."
fi
done
fi
if [[ ! $curl_options =~ .*\--connect-timeout ]]; then
curl_options+=" --connect-timeout 5"
fi
OPSGENIE_API_URL=${OPSGENIE_API_URL:-"https://api.opsgenie.com"}
# If we didn't autodetect the character set for e-mail and it wasn't
# set by the user, we need to set it to a reasonable default. UTF-8
# should be correct for almost all modern UNIX systems.
if [ -z ${EMAIL_CHARSET} ]; then
EMAIL_CHARSET="UTF-8"
fi
# If we've been asked to use FQDN's for the URL's in the alarm, do so,
# unless we're sending an alarm for a child system which we can't get the
# FQDN of easily.
if [ "${use_fqdn}" = "YES" ] && [ "${host}" = "$(hostname -s 2>/dev/null)" ]; then
host="$(hostname -f 2>/dev/null)"
fi
# -----------------------------------------------------------------------------
# migrate old Microsoft Teams configuration keys after loading configuration
msteams_migration() {
SEND_MSTEAMS=${SEND_MSTEAM:-$SEND_MSTEAMS}
unset -v SEND_MSTEAM
DEFAULT_RECIPIENT_MSTEAMS=${DEFAULT_RECIPIENT_MSTEAM:-$DEFAULT_RECIPIENT_MSTEAMS}
MSTEAMS_WEBHOOK_URL=${MSTEAM_WEBHOOK_URL:-$MSTEAMS_WEBHOOK_URL}
MSTEAMS_ICON_DEFAULT=${MSTEAM_ICON_DEFAULT:-$MSTEAMS_ICON_DEFAULT}
MSTEAMS_ICON_CLEAR=${MSTEAM_ICON_CLEAR:-$MSTEAMS_ICON_CLEAR}
MSTEAMS_ICON_WARNING=${MSTEAM_ICON_WARNING:-$MSTEAMS_ICON_WARNING}
MSTEAMS_ICON_CRITICAL=${MSTEAM_ICON_CRITICAL:-$MSTEAMS_ICON_CRITICAL}
MSTEAMS_COLOR_DEFAULT=${MSTEAM_COLOR_DEFAULT:-$MSTEAMS_COLOR_DEFAULT}
MSTEAMS_COLOR_CLEAR=${MSTEAM_COLOR_CLEAR:-$MSTEAMS_COLOR_CLEAR}
MSTEAMS_COLOR_WARNING=${MSTEAM_COLOR_WARNING:-$MSTEAMS_COLOR_WARNING}
MSTEAMS_COLOR_CRITICAL=${MSTEAM_COLOR_CRITICAL:-$MSTEAMS_COLOR_CRITICAL}
# migrate role specific recipients:
for key in "${!role_recipients_msteam[@]}"; do
# Disable check, if role_recipients_msteams is ever used:
# The role_recipients_$method are created and used programmatically
# by iterating over $methods. shellcheck therefore doesn't realize
# that role_recipients_msteams is actually used in the block
# "find the recipients' addresses per method".
# shellcheck disable=SC2034
role_recipients_msteams["$key"]="${role_recipients_msteam["$key"]}"
done
}
msteams_migration
# -----------------------------------------------------------------------------
# filter a recipient based on alarm event severity
filter_recipient_by_criticality() {
local method="${1}" recipient_arg="${2}"
local tracking_dir tracking_file modifier modifiers recipient="${recipient_arg/|*/}"
local mod_critical=0 mod_noclear=0 mod_nowarn=0
# no severity filtering for this person
[ "${recipient}" = "${recipient_arg}" ] && return 0
# find out which modifiers are set
modifiers="${recipient_arg#*|}"
modifiers="${modifiers//|/ }" # replace pipes with spaces
modifiers="${modifiers,,}" # lowercase
for modifier in ${modifiers}; do
case "${modifier}" in
critical) mod_critical=1 ;;
noclear) mod_noclear=1 ;;
nowarn) mod_nowarn=1 ;;
*)
error "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: invalid modifier '${modifier}'."
# invalid modifier, always send notification
return 0
;;
esac
done
# set status tracking directory/file var
tracking_dir="${NETDATA_CACHE_DIR}/alarm-notify/${method}/${recipient}"
tracking_file="${tracking_dir}/${alarm_id}"
# create the status tracking directory for this user if "critical" modifier is set
[ "${mod_critical}" == "1" ] && [ ! -d "${tracking_dir}" ] && mkdir -p "${tracking_dir}"
case "${status}" in
CRITICAL)
# "critical" modifier set, create tracking file for future status changes
if [ "${mod_critical}" == "1" ]; then
touch "${tracking_file}"
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: ALLOW: the alarm is CRITICAL (will now receive next status change)"
return 0
fi
# always send CRITICAL notification
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: ALLOW: the alarm is CRITICAL"
return 0
;;
WARNING)
# "nowarn" modifier set, block notification
if [ "${mod_nowarn}" == "1" ]; then
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: BLOCK: recipient should not receive this notification (nowarn modifier set)"
return 1
fi
# "critical" modifier not set, send notification
if [ "${mod_critical}" == "0" ]; then
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: ALLOW: the alarm is WARNING"
return 0
fi
# "critical" modifier set, send notification if tracking file exists
if [ "${mod_critical}" == "1" ] && [ -f "${tracking_file}" ]; then
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: ALLOW: recipient has been notified for this alarm in the past (will still receive next status change)"
return 0
fi
;;
CLEAR)
# remove tracking file
[ -f "${tracking_file}" ] && rm "${tracking_file}"
# "noclear" modifier set, block notification
if [ "${mod_noclear}" == "1" ]; then
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: BLOCK: recipient should not receive this notification (noclear modifier set)"
return 1
fi
# "critical" modifier not set, send notification
if [ "${mod_critical}" == "0" ]; then
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: ALLOW: the alarm is CLEAR"
return 0
fi
# "critical" modifier set, send notification if tracking file exists
if [ "${mod_critical}" == "1" ] && [ -f "${tracking_file}" ]; then
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: ALLOW: recipient has been notified for this alarm in the past (no status change will be sent from now)"
return 0
fi
;;
*)
# "critical" modifier set, send notification if tracking file exists
if [ "${mod_critical}" == "1" ] && [ -f "${tracking_file}" ]; then
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: ALLOW: recipient has been notified for this alarm in the past (will still receive next status change)"
return 0
fi
;;
esac
debug "SEVERITY FILTERING for ${recipient_arg} VIA ${method}: BLOCK: recipient should not receive this notification"
return 1
}
# -----------------------------------------------------------------------------
# verify the delivery methods supported
# check slack
[ -z "${SLACK_WEBHOOK_URL}" ] && SEND_SLACK="NO"
# check rocketchat
[ -z "${ROCKETCHAT_WEBHOOK_URL}" ] && SEND_ROCKETCHAT="NO"
# check alerta
[ -z "${ALERTA_WEBHOOK_URL}" ] && SEND_ALERTA="NO"
# check flock
[ -z "${FLOCK_WEBHOOK_URL}" ] && SEND_FLOCK="NO"
# check discord
[ -z "${DISCORD_WEBHOOK_URL}" ] && SEND_DISCORD="NO"
# check pushover
[ -z "${PUSHOVER_APP_TOKEN}" ] && SEND_PUSHOVER="NO"
# check pushbullet
[ -z "${PUSHBULLET_ACCESS_TOKEN}" ] && SEND_PUSHBULLET="NO"
# check twilio
{ [ -z "${TWILIO_ACCOUNT_TOKEN}" ] || [ -z "${TWILIO_ACCOUNT_SID}" ] || [ -z "${TWILIO_NUMBER}" ]; } && SEND_TWILIO="NO"
# check hipchat
[ -z "${HIPCHAT_AUTH_TOKEN}" ] && SEND_HIPCHAT="NO"
# check messagebird
{ [ -z "${MESSAGEBIRD_ACCESS_KEY}" ] || [ -z "${MESSAGEBIRD_NUMBER}" ]; } && SEND_MESSAGEBIRD="NO"
# check kavenegar
{ [ -z "${KAVENEGAR_API_KEY}" ] || [ -z "${KAVENEGAR_SENDER}" ]; } && SEND_KAVENEGAR="NO"
# check telegram
[ -z "${TELEGRAM_BOT_TOKEN}" ] && SEND_TELEGRAM="NO"
# check kafka
{ [ -z "${KAFKA_URL}" ] || [ -z "${KAFKA_SENDER_IP}" ]; } && SEND_KAFKA="NO"
# check irc
[ -z "${IRC_NETWORK}" ] && SEND_IRC="NO"
# check fleep
#shellcheck disable=SC2153
{ [ -z "${FLEEP_SERVER}" ] || [ -z "${FLEEP_SENDER}" ]; } && SEND_FLEEP="NO"
# check dynatrace
{ [ -z "${DYNATRACE_SPACE}" ] ||
[ -z "${DYNATRACE_SERVER}" ] ||
[ -z "${DYNATRACE_TOKEN}" ] ||
[ -z "${DYNATRACE_TAG_VALUE}" ] ||
[ -z "${DYNATRACE_EVENT}" ]; } && SEND_DYNATRACE="NO"
# check opsgenie
[ -z "${OPSGENIE_API_KEY}" ] && SEND_OPSGENIE="NO"
# check matrix
{ [ -z "${MATRIX_HOMESERVER}" ] || [ -z "${MATRIX_ACCESSTOKEN}" ]; } && SEND_MATRIX="NO"
# check gotify
{ [ -z "${GOTIFY_APP_TOKEN}" ] || [ -z "${GOTIFY_APP_URL}" ]; } && SEND_GOTIFY="NO"
# check ntfy
[ -z "${DEFAULT_RECIPIENT_NTFY}" ] && SEND_NTFY="NO"
# check msteams
[ -z "${MSTEAMS_WEBHOOK_URL}" ] && SEND_MSTEAMS="NO"
# check pd
[ -z "${DEFAULT_RECIPIENT_PD}" ] && SEND_PD="NO"
# check prowl
[ -z "${DEFAULT_RECIPIENT_PROWL}" ] && SEND_PROWL="NO"
# check custom
[ -z "${DEFAULT_RECIPIENT_CUSTOM}" ] && SEND_CUSTOM="NO"
if [ "${SEND_PUSHOVER}" = "YES" ] ||
[ "${SEND_SLACK}" = "YES" ] ||
[ "${SEND_ROCKETCHAT}" = "YES" ] ||
[ "${SEND_ALERTA}" = "YES" ] ||
[ "${SEND_PD}" = "YES" ] ||
[ "${SEND_FLOCK}" = "YES" ] ||
[ "${SEND_DISCORD}" = "YES" ] ||
[ "${SEND_HIPCHAT}" = "YES" ] ||
[ "${SEND_TWILIO}" = "YES" ] ||
[ "${SEND_MESSAGEBIRD}" = "YES" ] ||
[ "${SEND_KAVENEGAR}" = "YES" ] ||
[ "${SEND_TELEGRAM}" = "YES" ] ||
[ "${SEND_PUSHBULLET}" = "YES" ] ||
[ "${SEND_KAFKA}" = "YES" ] ||
[ "${SEND_FLEEP}" = "YES" ] ||
[ "${SEND_PROWL}" = "YES" ] ||
[ "${SEND_MATRIX}" = "YES" ] ||
[ "${SEND_CUSTOM}" = "YES" ] ||
[ "${SEND_MSTEAMS}" = "YES" ] ||
[ "${SEND_DYNATRACE}" = "YES" ] ||
[ "${SEND_OPSGENIE}" = "YES" ] ||
[ "${SEND_GOTIFY}" = "YES" ] ||
[ "${SEND_NTFY}" = "YES" ]; then
# if we need curl, check for the curl command
if [ -z "${curl}" ]; then
curl="$(command -v curl 2>/dev/null)"
fi
if [ -z "${curl}" ]; then
error "Cannot find curl command in the system path. Disabling all curl based notifications."
SEND_PUSHOVER="NO"
SEND_PUSHBULLET="NO"
SEND_TELEGRAM="NO"
SEND_SLACK="NO"
SEND_MSTEAMS="NO"
SEND_ROCKETCHAT="NO"
SEND_ALERTA="NO"
SEND_PD="NO"
SEND_FLOCK="NO"
SEND_DISCORD="NO"
SEND_TWILIO="NO"
SEND_HIPCHAT="NO"
SEND_MESSAGEBIRD="NO"
SEND_KAVENEGAR="NO"
SEND_KAFKA="NO"
SEND_FLEEP="NO"
SEND_PROWL="NO"
SEND_MATRIX="NO"
SEND_CUSTOM="NO"
SEND_DYNATRACE="NO"
SEND_OPSGENIE="NO"
SEND_GOTIFY="NO"
SEND_NTFY="NO"
fi
fi
if [ "${SEND_SMS}" = "YES" ]; then
if [ -z "${sendsms}" ]; then
sendsms="$(command -v sendsms 2>/dev/null)"
fi
if [ -z "${sendsms}" ]; then
SEND_SMS="NO"
fi
fi
# if we need sendmail, check for the sendmail command
if [ "${SEND_EMAIL}" = "YES" ] && [ -z "${sendmail}" ]; then
sendmail="$(command -v sendmail 2>/dev/null)"
if [ -z "${sendmail}" ]; then
debug "Cannot find sendmail command in the system path. Disabling email notifications."
SEND_EMAIL="NO"
fi
fi
# if we need logger, check for the logger command
if [ "${SEND_SYSLOG}" = "YES" ] && [ -z "${logger}" ]; then
logger="$(command -v logger 2>/dev/null)"
if [ -z "${logger}" ]; then
debug "Cannot find logger command in the system path. Disabling syslog notifications."
SEND_SYSLOG="NO"
fi
fi
# if we need aws, check for the aws command
if [ "${SEND_AWSSNS}" = "YES" ] && [ -z "${aws}" ]; then
aws="$(command -v aws 2>/dev/null)"
if [ -z "${aws}" ]; then
debug "Cannot find aws command in the system path. Disabling Amazon SNS notifications."
SEND_AWSSNS="NO"
fi
fi
# if we need nc, check for the nc command
if [ "${SEND_IRC}" = "YES" ] && [ -z "${nc}" ]; then
nc="$(command -v nc 2>/dev/null)"
if [ -z "${nc}" ]; then
debug "Cannot find nc command in the system path. Disabling IRC notifications."
SEND_IRC="NO"
fi
fi
if [ ${dump_methods} ]; then
for name in "${!SEND_@}"; do
if [ "${!name}" = "YES" ]; then
echo "$name"
fi
done
exit
fi
# -----------------------------------------------------------------------------
# find the recipients' addresses per method
# netdata may call us with multiple roles, and roles may have multiple but
# overlapping recipients - so, here we find the unique recipients.
for method_name in ${method_names}; do
send_var="SEND_${method_name^^}"
if [ "${!send_var}" = "NO" ]; then
continue
fi
declare -A arr_var=()
for x in ${roles//,/ }; do
# the roles 'silent' and 'disabled' mean:
# don't send a notification for this role
if [ "${x}" = "silent" ] || [ "${x}" = "disabled" ]; then
continue
fi
role_recipients="role_recipients_${method_name}[$x]"
default_recipient_var="DEFAULT_RECIPIENT_${method_name^^}"
a="${!role_recipients}"
[ -z "${a}" ] && a="${!default_recipient_var}"
for r in ${a//,/ }; do
[ "${r}" != "disabled" ] && filter_recipient_by_criticality ${method_name} "${r}" && arr_var[${r/|*/}]="1"
done
done
# build the list of recipients
to_var="to_${method_name}"
declare to_${method_name}="${!arr_var[*]}"
[ -z "${!to_var}" ] && declare ${send_var}="NO"
done
# -----------------------------------------------------------------------------
# handle fixup of the email recipient list.
fix_to_email() {
to_email=
while [ -n "${1}" ]; do
[ -n "${to_email}" ] && to_email="${to_email}, "
to_email="${to_email}${1}"
shift 1
done
}
# ${to_email} without quotes here
fix_to_email ${to_email}
# -----------------------------------------------------------------------------
# handle output if we're running in unit test mode
if [ ${unittest} ]; then
for method_name in ${method_names}; do
to_var="to_${method_name}"
echo "results: ${method_name}: ${!to_var}"
done
exit 0
fi
# -----------------------------------------------------------------------------
# check that we have at least a method enabled
proceed=0
for method in "${SEND_EMAIL}" \
"${SEND_PUSHOVER}" \
"${SEND_TELEGRAM}" \
"${SEND_SLACK}" \
"${SEND_ROCKETCHAT}" \
"${SEND_ALERTA}" \
"${SEND_FLOCK}" \
"${SEND_DISCORD}" \
"${SEND_TWILIO}" \
"${SEND_HIPCHAT}" \
"${SEND_MESSAGEBIRD}" \
"${SEND_KAVENEGAR}" \
"${SEND_PUSHBULLET}" \
"${SEND_KAFKA}" \
"${SEND_PD}" \
"${SEND_FLEEP}" \
"${SEND_PROWL}" \
"${SEND_MATRIX}" \
"${SEND_CUSTOM}" \
"${SEND_IRC}" \
"${SEND_AWSSNS}" \
"${SEND_SYSLOG}" \
"${SEND_SMS}" \
"${SEND_MSTEAMS}" \
"${SEND_DYNATRACE}" \
"${SEND_OPSGENIE}" \
"${SEND_GOTIFY}" \
"${SEND_NTFY}" ; do
if [ "${method}" == "YES" ]; then
proceed=1
break
fi
done
if [ "$proceed" -eq 0 ]; then
fatal "All notification methods are disabled. Not sending notification for host '${host}', chart '${chart}' to '${roles}' for '${name}' = '${value}' for status '${status}'."
fi
# -----------------------------------------------------------------------------
# get the date the alarm happened
date=$(date --date=@${when} "${date_format}" 2>/dev/null)
[ -z "${date}" ] && date=$(date "${date_format}" 2>/dev/null)
[ -z "${date}" ] && date=$(date --date=@${when} 2>/dev/null)
[ -z "${date}" ] && date=$(date 2>/dev/null)
# -----------------------------------------------------------------------------
# get the date in utc the alarm happened
date_utc=$(date --date=@${when} "${date_format}" -u 2>/dev/null)
[ -z "${date_utc}" ] && date_utc=$(date -u "${date_format}" 2>/dev/null)
[ -z "${date_utc}" ] && date_utc=$(date -u --date=@${when} 2>/dev/null)
[ -z "${date_utc}" ] && date_utc=$(date -u 2>/dev/null)
# ----------------------------------------------------------------------------
# prepare some extra headers if we've been asked to thread e-mails
if [ "${SEND_EMAIL}" == "YES" ] && [ "${EMAIL_THREADING}" != "NO" ]; then
email_thread_headers="In-Reply-To: <${chart}-${name}@${host}>\\r\\nReferences: <${chart}-${name}@${host}>"
else
email_thread_headers=
fi
# -----------------------------------------------------------------------------
# function to URL encode a string
urlencode() {
local string="${1}" strlen encoded pos c o
strlen=${#string}
for ((pos = 0; pos < strlen; pos++)); do
c=${string:pos:1}
case "${c}" in
[-_.~a-zA-Z0-9])
o="${c}"
;;
*)
printf -v o '%%%02x' "'${c}"
;;
esac
encoded+="${o}"
done
REPLY="${encoded}"
echo "${REPLY}"
}
# -----------------------------------------------------------------------------
# function to convert a duration in seconds, to a human readable duration
# using DAYS, MINUTES, SECONDS
duration4human() {
local s="${1}" d=0 h=0 m=0 ds="day" hs="hour" ms="minute" ss="second" ret
d=$((s / 86400))
s=$((s - (d * 86400)))
h=$((s / 3600))
s=$((s - (h * 3600)))
m=$((s / 60))
s=$((s - (m * 60)))
if [ ${d} -gt 0 ]; then
[ ${m} -ge 30 ] && h=$((h + 1))
[ ${d} -gt 1 ] && ds="days"
[ ${h} -gt 1 ] && hs="hours"
if [ ${h} -gt 0 ]; then
ret="${d} ${ds} and ${h} ${hs}"
else
ret="${d} ${ds}"
fi
elif [ ${h} -gt 0 ]; then
[ ${s} -ge 30 ] && m=$((m + 1))
[ ${h} -gt 1 ] && hs="hours"
[ ${m} -gt 1 ] && ms="minutes"
if [ ${m} -gt 0 ]; then
ret="${h} ${hs} and ${m} ${ms}"
else
ret="${h} ${hs}"
fi
elif [ ${m} -gt 0 ]; then
[ ${m} -gt 1 ] && ms="minutes"
[ ${s} -gt 1 ] && ss="seconds"
if [ ${s} -gt 0 ]; then
ret="${m} ${ms} and ${s} ${ss}"
else
ret="${m} ${ms}"
fi
else
[ ${s} -gt 1 ] && ss="seconds"
ret="${s} ${ss}"
fi
REPLY="${ret}"
echo "${REPLY}"
}
# -----------------------------------------------------------------------------
# email sender
send_email() {
local ret opts=() sender_email="${EMAIL_SENDER}" sender_name=
if [ "${SEND_EMAIL}" = "YES" ]; then
if [ -n "${EMAIL_SENDER}" ]; then
if [[ ${EMAIL_SENDER} =~ ^\".*\"\ \<.*\>$ ]]; then
# the name includes double quotes
sender_email="$(echo "${EMAIL_SENDER}" | cut -d '<' -f 2 | cut -d '>' -f 1)"
sender_name="$(echo "${EMAIL_SENDER}" | cut -d '"' -f 2)"
elif [[ ${EMAIL_SENDER} =~ ^\'.*\'\ \<.*\>$ ]]; then