Skip to content

Commit 0d169b4

Browse files
committed
Fix shellcheck findings
1 parent ccf9782 commit 0d169b4

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

disk-alerter.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ source "./config.sh"
44
source "./logger.sh"
55
source "./checks.sh"
66

7+
DATA_PREFIX="$DATA_DIR/disk-alerter"
8+
79
function check_disks() {
810
local swarm_name=$SWARM_NAME
911
while read -r mount usage; do
1012
unique_name="${swarm_name} ${mount}"
1113
unique_code=$(echo "${unique_name,,}" | sed -e 's/ /_/g' -e 's/[^a-zA-Z0-9_-]/_/g')
1214
random_str=$(tr -dc 'a-zA-Z0-9' </dev/urandom | head -c 10)
1315
read -r unique_id _ < <(echo -n "$unique_name $random_str" | md5sum)
14-
prefix="$DATA_DIR/${unique_code}"
16+
prefix="${DATA_PREFIX}-${unique_code}"
1517
pending_file="${prefix}.pending"
1618
log_file="${prefix}.log"
1719

nodes.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
# shellcheck disable=SC2002
4+
35
source "./config.sh"
46
source "./logger.sh"
57
source "./checks.sh"

port-alerter.sh

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,32 @@ source "./checks.sh"
66

77
declare -A REPORTED_SOCKS
88

9+
DATA_PREFIX="$DATA_DIR/port-alerter"
10+
911
function 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

8182
log_info "Port alerter is entering loop with ${LOOP_SLEEP} sleep on entry ..."
8283

8384
while true; do
84-
sleep $LOOP_SLEEP
85+
sleep "$LOOP_SLEEP"
8586
check_services
8687
done

swarm-alerter.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ source "./config.sh"
44
source "./logger.sh"
55
source "./checks.sh"
66

7+
DATA_PREFIX="$DATA_DIR/swarm-alerter"
8+
79
function check_nodes() {
810
local active_node_count
9-
active_node_count=$(./nodes.sh | wc -l)
1011
local swarm_name=$SWARM_NAME
11-
local prefix="$DATA_DIR/swarm_failure"
12+
local prefix="${DATA_PREFIX}"
1213
local pending_file="${prefix}.pending"
1314
local log_file="${prefix}.log"
1415
local where="at $HOSTNAME"
1516

16-
./nodes.sh --verbose >"$log_file"
17+
active_node_count=$(./nodes.sh | wc -l)
1718

1819
action=""
1920
appendix=""
2021
message="${swarm_name} swarm active managers count $active_node_count"
2122
if ((SWARM_MANAGER_MIN > active_node_count)); then
23+
./nodes.sh --verbose &>"$log_file"
24+
2225
if [[ -f $pending_file ]]; then
2326
log_warn "Pending alert: $message"
2427
else
@@ -48,7 +51,6 @@ function check_nodes() {
4851
"summary": $summary
4952
}' | /bin/bash -c "$ALERT_SCRIPT"
5053
fi
51-
rm -f "$log_file"
5254
}
5355

5456
log_info "Swarm alerter is entering loop with ${LOOP_SLEEP} sleep on entry ..."

0 commit comments

Comments
 (0)