use bash variables properly
Costa Tsaousis (ktsaou) committed
Oct 23, 2016 at 23:56 UTC
f1ccf3c1d6e8aa0724ba3c8571d9702b608f0be2
1 file changed
+18
-22
plugins.d/alarm-notify.sh
+18
-22
@@ -40,18 +40,14 @@ then
40
for x in "CRITICAL" "WARNING" "CLEAR"
41
do
42
echo >&2
43
- echo >&2 ">> SENDING TEST ${x} ALARM TO ROLE: ${recipient} <<"
43
+ echo >&2 "# SENDING TEST ${x} ALARM TO ROLE: ${recipient}"
44
45
- # printf >&2 "Execute: "
46
- # printf >&2 "%q " "${0}" "${recipient}" "$(hostname)" "1" "1" "${id}" "$(date +%s)" "test_alarm" "test.chart" "test.family" "${x}" "${last}" '100' '90' "${0}" "1" "$((id))" "units" "this is a test alarm to verify notifications work"
47
- # printf >&2 "\n"
48
-
49
- "${0}" "${recipient}" "$(hostname)" "1" "1" "${id}" "$(date +%s)" "test_alarm" "test.chart" "test.family" "${x}" "${last}" '100' '90' "${0}" "$((id))" "1" "units" "this is a test alarm to verify notifications work"
45
+ "${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"
46
if [ $? -ne 0 ]
47
then
52
- echo >&2 ">> FAILED <<"
48
+ echo >&2 "# FAILED"
49
else
54
- echo >&2 ">> OK <<"
50
+ echo >&2 "# OK"
51
fi
52
53
last="${x}"
@@ -99,7 +95,7 @@ fatal() {
95
96
debug=0
97
debug() {
102
- [ $debug -eq 1 ] && log DEBUG "${@}"
98
+ [ ${debug} -eq 1 ] && log DEBUG "${@}"
99
}
100
101
# -----------------------------------------------------------------------------
@@ -393,14 +389,14 @@ urlencode() {
389
strlen=${#string}
390
for (( pos=0 ; pos<strlen ; pos++ ))
391
do
396
- c=${string:$pos:1}
397
- case "$c" in
392
+ c=${string:${pos}:1}
393
+ case "${c}" in
394
[-_.~a-zA-Z0-9])
395
o="${c}"
396
;;
397
398
*)
403
- printf -v o '%%%02x' "'$c"
399
+ printf -v o '%%%02x' "'${c}"
400
;;
401
esac
402
encoded+="${o}"
@@ -475,7 +471,7 @@ send_email() {
471
"${sendmail}" -t
472
ret=$?
473
478
- if [ $ret -eq 0 ]
474
+ if [ ${ret} -eq 0 ]
475
then
476
info "sent email notification for: ${host} ${chart}.${name} is ${status} to '${to_email}'"
477
return 0
@@ -546,7 +542,7 @@ send_pushbullet() {
542
for user in ${recipients}
543
do
544
httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null \
549
- --header 'Access-Token: '$userapikey'' \
545
+ --header 'Access-Token: '${userapikey}'' \
546
--header 'Content-Type: application/json' \
547
--data-binary @<(cat <<EOF
548
{"title": "${title}",
@@ -587,8 +583,8 @@ send_telegram() {
583
httpcode=$(${curl} --write-out %{http_code} --silent --output /dev/null ${disableNotification} \
584
--data-urlencode "parse_mode=HTML" \
585
--data-urlencode "disable_web_page_preview=true" \
590
- --data-urlencode "text=$message" \
591
- "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=$chatid")
586
+ --data-urlencode "text=${message}" \
587
+ "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${chatid}")
588
589
if [ "${httpcode}" == "200" ]
590
then
@@ -617,10 +613,10 @@ send_slack() {
613
[ "${SEND_SLACK}" != "YES" ] && return 1
614
615
case "${status}" in
620
- WARNING) color="warning" ;;
616
+ WARNING) color="warning" ;;
617
CRITICAL) color="danger" ;;
622
- CLEAR) color="good" ;;
623
- *) color="#777777" ;;
618
+ CLEAR) color="good" ;;
619
+ *) color="#777777" ;;
620
esac
621
622
for channel in ${channels}
@@ -731,7 +727,7 @@ esac
727
if [ "${status}" = "CLEAR" ]
728
then
729
severity="Recovered from ${old_status}"
734
- if [ $non_clear_duration -gt $duration ]
730
+ if [ ${non_clear_duration} -gt ${duration} ]
731
then
732
raised_for="(alarm was raised for ${non_clear_duration_txt})"
733
fi
@@ -739,7 +735,7 @@ then
735
elif [ "${old_status}" = "WARNING" -a "${status}" = "CRITICAL" ]
736
then
737
severity="Escalated to ${status}"
742
- if [ $non_clear_duration -gt $duration ]
738
+ if [ ${non_clear_duration} -gt ${duration} ]
739
then
740
raised_for="(alarm is raised for ${non_clear_duration_txt})"
741
fi
@@ -747,7 +743,7 @@ then
743
elif [ "${old_status}" = "CRITICAL" -a "${status}" = "WARNING" ]
744
then
745
severity="Demoted to ${status}"
750
- if [ $non_clear_duration -gt $duration ]
746
+ if [ ${non_clear_duration} -gt ${duration} ]
747
then
748
raised_for="(alarm is raised for ${non_clear_duration_txt})"
749
fi