alarm-notify.sh now enables debug mode when NETDATA_ALARM_NOTIFY_DEBUG=1
Costa Tsaousis (ktsaou) committed
Jul 21, 2017 at 00:41 UTC
47c1365347f65c5e300571e3af7bfe3248b75261
1 file changed
+50
-16
plugins.d/alarm-notify.sh
+50
-16
@@ -99,11 +99,41 @@ fatal() {
99
exit 1
100
}
101
102
-debug=0
102
+debug=${NETDATA_ALARM_NOTIFY_DEBUG-0}
103
debug() {
104
- [ ${debug} -eq 1 ] && log DEBUG "${@}"
104
+ [ "${debug}" = "1" ] && log DEBUG "${@}"
105
}
106
107
+docurl() {
108
+ if [ -z "${curl}" ]
109
+ then
110
+ error "\${curl} is unset."
111
+ return 1
112
+ fi
113
+
114
+ if [ "${debug}" = "1" ]
115
+ then
116
+ echo >&2 "--- BEGIN curl command ---"
117
+ printf >&2 "%q " ${curl} "${@}"
118
+ echo >&2
119
+ echo >&2 "--- END curl command ---"
120
+
121
+ local out=$(mktemp /tmp/netdata-health-alarm-notify-XXXXXXXX)
122
+ local code=$(${curl} --write-out %{http_code} --output "${out}" --silent --show-error "${@}")
123
+ local ret=$?
124
+ echo >&2 "--- BEGIN received response ---"
125
+ cat >&2 "${out}"
126
+ echo >&2
127
+ echo >&2 "--- END received response ---"
128
+ echo >&2 "RECEIVED HTTP RESPONSE CODE: ${code}"
129
+ rm "${out}"
130
+ echo "${code}"
131
+ return ${ret}
132
+ fi
133
+
134
+ ${curl} --write-out %{http_code} --output /dev/null --silent --show-error "${@}"
135
+ return $?
136
+}
137
138
# -----------------------------------------------------------------------------
139
# this is to be overwritten by the config file
@@ -266,6 +296,8 @@ declare -A role_recipients_email=()
296
if [ -f "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf" ]
297
then
298
source "${NETDATA_CONFIG_DIR}/health_alarm_notify.conf"
299
+else
300
+ error "Cannot find file ${NETDATA_CONFIG_DIR}/health_alarm_notify.conf. Using internal defaults."
301
fi
302
303
# -----------------------------------------------------------------------------
@@ -508,8 +540,7 @@ if [ "${SEND_PD}" = "YES" ]
540
pd_send="$(which pd-send 2>/dev/null || command -v pd-send 2>/dev/null)"
541
if [ -z "${pd_send}" ]
542
then
511
- # no pd-send available
512
- # disable pagerduty.com
543
+ error "Cannot find pd-send command in the system path. Disabling pagerduty.com notifications."
544
SEND_PD="NO"
545
fi
546
fi
@@ -530,8 +561,7 @@ if [ \( \
561
curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
562
if [ -z "${curl}" ]
563
then
533
- # no curl available
534
- # disable all curl based methods
564
+ error "Cannot find curl command in the system path. Disabling all curl based notifications."
565
SEND_PUSHOVER="NO"
566
SEND_PUSHBULLET="NO"
567
SEND_TELEGRAM="NO"
@@ -548,7 +578,11 @@ fi
578
if [ "${SEND_EMAIL}" = "YES" -a -z "${sendmail}" ]
579
then
580
sendmail="$(which sendmail 2>/dev/null || command -v sendmail 2>/dev/null)"
551
- [ -z "${sendmail}" ] && SEND_EMAIL="NO"
581
+ if [ -z "${sendmail}" ]
582
+ then
583
+ debug "Cannot find sendmail command in the system path. Disabling email notifications."
584
+ SEND_EMAIL="NO"
585
+ fi
586
fi
587
588
# check that we have at least a method enabled
@@ -699,7 +733,7 @@ send_pushover() {
733
734
for user in ${usertokens}
735
do
702
- httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
736
+ httpcode=$(docurl \
737
--form-string "token=${apptoken}" \
738
--form-string "user=${user}" \
739
--form-string "html=1" \
@@ -736,7 +770,7 @@ send_pushbullet() {
770
#https://docs.pushbullet.com/#create-push
771
for user in ${recipients}
772
do
739
- httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
773
+ httpcode=$(docurl \
774
--header 'Access-Token: '${userapikey}'' \
775
--header 'Content-Type: application/json' \
776
--data-binary @<(cat <<EOF
@@ -769,7 +803,7 @@ send_kafka() {
803
local httpcode sent=0
804
if [ "${SEND_KAFKA}" = "YES" ]
805
then
772
- httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
806
+ httpcode=$(docurl -X POST \
807
--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}\"}" \
808
"${KAFKA_URL}")
809
@@ -853,7 +887,7 @@ send_twilio() {
887
#https://www.twilio.com/packages/labs/code/bash/twilio-sms
888
for user in ${recipients}
889
do
856
- httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
890
+ httpcode=$(docurl -X POST \
891
--data-urlencode "From=${twilionumber}" \
892
--data-urlencode "To=${user}" \
893
--data-urlencode "Body=${title} ${message}" \
@@ -907,7 +941,7 @@ send_hipchat() {
941
942
for room in ${recipients}
943
do
910
- httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
944
+ httpcode=$(docurl -X POST \
945
-H "Content-type: application/json" \
946
-H "Authorization: Bearer ${authtoken}" \
947
-d "{\"color\": \"${color}\", \"from\": \"${netdata}\", \"message_format\": \"${msg_format}\", \"message\": \"${message}\", \"notify\": \"${notify}\"}" \
@@ -939,7 +973,7 @@ send_messagebird() {
973
#https://developers.messagebird.com/docs/messaging
974
for user in ${recipients}
975
do
942
- httpcode=$(${curl} -X POST --write-out %{http_code} --silent --output /dev/null \
976
+ httpcode=$(docurl -X POST \
977
--data-urlencode "originator=${messagebirdnumber}" \
978
--data-urlencode "recipients=${user}" \
979
--data-urlencode "body=${title} ${message}" \
@@ -982,7 +1016,7 @@ send_telegram() {
1016
for chatid in ${chatids}
1017
do
1018
# https://core.telegram.org/bots/api#sendmessage
985
- httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
1019
+ httpcode=$(docurl ${disableNotification} \
1020
--data-urlencode "parse_mode=HTML" \
1021
--data-urlencode "disable_web_page_preview=true" \
1022
--data-urlencode "text=${emoji} ${message}" \
@@ -1055,7 +1089,7 @@ send_slack() {
1089
EOF
1090
)"
1091
1058
- httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1092
+ httpcode=$(docurl -X POST --data-urlencode "payload=${payload}" "${webhook}")
1093
if [ "${httpcode}" == "200" ]
1094
then
1095
info "sent slack notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"
@@ -1118,7 +1152,7 @@ send_discord() {
1152
EOF
1153
)"
1154
1121
- httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null -X POST --data-urlencode "payload=${payload}" "${webhook}")
1155
+ httpcode=$(docurl -X POST --data-urlencode "payload=${payload}" "${webhook}")
1156
if [ "${httpcode}" == "200" ]
1157
then
1158
info "sent discord notification for: ${host} ${chart}.${name} is ${status} to '${channel}'"