@@ -6,30 +6,32 @@ source "./checks.sh"
66
77declare -A REPORTED_SOCKS
88
9+ DATA_PREFIX=" $DATA_DIR /port-alerter"
10+
911function check_services() {
1012 local swarm_name=$SWARM_NAME
11- while read service_name network_alias check_type check_value; do
12- unique_name=$( echo " ${swarm_name} ${service_name} ${network_alias} ${check_type} ${check_value} " )
13+ while read -r service_name network_alias check_type check_value; do
14+ unique_name=" ${swarm_name} ${service_name} ${network_alias} ${check_type} ${check_value} "
1315 unique_code=$( echo " ${unique_name,,} " | sed -e ' s/ /_/g' -e ' s/[^a-zA-Z0-9_-]/_/g' )
1416 random_str=$( tr -dc ' a-zA-Z0-9' < /dev/urandom | head -c 10)
15- read unique_id _ < <( echo -n " $unique_name $random_str " | md5sum)
16- prefix=" $DATA_DIR / ${unique_code} "
17+ read -r unique_id _ < <( echo -n " $unique_name $random_str " | md5sum)
18+ prefix=" ${DATA_PREFIX} - ${unique_code} "
1719 pending_file=" ${prefix} .pending"
1820 log_file=" ${prefix} .log"
1921
2022 if [[ $check_type == " port" ]]; then
2123 port=$check_value
2224 real_port=" $port "
2325 # used for testing
24- if [[ -f " $DATA_DIR / test-change-port-$port " ]]; then
25- real_port=$( < " $DATA_DIR / test-change-port-$port " )
26+ if [[ -f " ${DATA_PREFIX} - test-change-port-$port " ]]; then
27+ real_port=$( < " ${DATA_PREFIX} - test-change-port-$port " )
2628 fi
2729 WAIT=" tcp://$network_alias :$real_port "
2830 WHERE=" via mesh"
2931 fi
3032
3133 if [[ $check_type == " sock" ]]; then
32- IFS=" :" read sock_type sock_file <<< " $check_value"
34+ IFS=" :" read -r _sock_type sock_file <<< " $check_value"
3335 if [[ ! -S $sock_file ]]; then
3436 if [[ ! -v REPORTED_SOCKS[$sock_file ] ]]; then
3537 log_warn " Sock file $sock_file does not exist locally!"
@@ -44,43 +46,42 @@ function check_services() {
4446 action=" "
4547 appendix=" "
4648 message=" ${swarm_name} service ${service_name} (${network_alias} :${check_value} )"
47- /usr/local/bin/dockerize -timeout 5s -wait " $WAIT " true 2> $log_file
48- if [ $? -ne 0 ]; then
49+ if ! /usr/local/bin/dockerize -timeout 5s -wait " $WAIT " true 2> " $log_file " ; then
4950 if [[ -f $pending_file ]]; then
5051 log_warn " Pending alert: $message "
5152 else
52- echo " $unique_id " > $pending_file
53+ echo " $unique_id " > " $pending_file "
5354 action=" create"
5455 appendix=" not available $WHERE "
5556 fi
5657 else
5758 if [[ -f $pending_file ]]; then
5859 action=" resolve"
5960 appendix=" is available $WHERE "
60- unique_id=$( cat $pending_file )
61- rm -f $pending_file
61+ unique_id=$( cat " $pending_file " )
62+ rm -f " $pending_file "
6263 fi
6364 fi
6465 if [[ -n $action ]]; then
6566 jq -n \
6667 --arg action " $action " \
6768 --arg unique_id " $unique_id " \
6869 --arg message " $message $appendix " \
69- --arg summary " $( cat $log_file ) " \
70+ --arg summary " $( cat " $log_file " ) " \
7071 ' {
71- "action": $action,
72- "unique_id": $unique_id,
73- "message": $message,
74- "summary": $summary
75- }' | /bin/bash -c " $ALERT_SCRIPT "
72+ "action": $action,
73+ "unique_id": $unique_id,
74+ "message": $message,
75+ "summary": $summary
76+ }' | /bin/bash -c " $ALERT_SCRIPT "
7677 fi
77- rm -f $log_file
78+ rm -f " $log_file "
7879 done < <( ./services.sh)
7980}
8081
8182log_info " Port alerter is entering loop with ${LOOP_SLEEP} sleep on entry ..."
8283
8384while true ; do
84- sleep $LOOP_SLEEP
85+ sleep " $LOOP_SLEEP "
8586 check_services
8687done
0 commit comments