Skip to content

Commit

Permalink
Fix shellcheck findings
Browse files Browse the repository at this point in the history
  • Loading branch information
brablc committed Jan 22, 2025
1 parent ccf9782 commit 0d169b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
4 changes: 3 additions & 1 deletion disk-alerter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ source "./config.sh"
source "./logger.sh"
source "./checks.sh"

DATA_PREFIX="$DATA_DIR/disk-alerter"

function check_disks() {
local swarm_name=$SWARM_NAME
while read -r mount usage; do
unique_name="${swarm_name} ${mount}"
unique_code=$(echo "${unique_name,,}" | sed -e 's/ /_/g' -e 's/[^a-zA-Z0-9_-]/_/g')
random_str=$(tr -dc 'a-zA-Z0-9' </dev/urandom | head -c 10)
read -r unique_id _ < <(echo -n "$unique_name $random_str" | md5sum)
prefix="$DATA_DIR/${unique_code}"
prefix="${DATA_PREFIX}-${unique_code}"
pending_file="${prefix}.pending"
log_file="${prefix}.log"

Expand Down
2 changes: 2 additions & 0 deletions nodes.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

# shellcheck disable=SC2002

source "./config.sh"
source "./logger.sh"
source "./checks.sh"
Expand Down
41 changes: 21 additions & 20 deletions port-alerter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@ source "./checks.sh"

declare -A REPORTED_SOCKS

DATA_PREFIX="$DATA_DIR/port-alerter"

function check_services() {
local swarm_name=$SWARM_NAME
while read service_name network_alias check_type check_value; do
unique_name=$(echo "${swarm_name} ${service_name} ${network_alias} ${check_type} ${check_value}")
while read -r service_name network_alias check_type check_value; do
unique_name="${swarm_name} ${service_name} ${network_alias} ${check_type} ${check_value}"
unique_code=$(echo "${unique_name,,}" | sed -e 's/ /_/g' -e 's/[^a-zA-Z0-9_-]/_/g')
random_str=$(tr -dc 'a-zA-Z0-9' </dev/urandom | head -c 10)
read unique_id _ < <(echo -n "$unique_name $random_str" | md5sum)
prefix="$DATA_DIR/${unique_code}"
read -r unique_id _ < <(echo -n "$unique_name $random_str" | md5sum)
prefix="${DATA_PREFIX}-${unique_code}"
pending_file="${prefix}.pending"
log_file="${prefix}.log"

if [[ $check_type == "port" ]]; then
port=$check_value
real_port="$port"
# used for testing
if [[ -f "$DATA_DIR/test-change-port-$port" ]]; then
real_port=$(<"$DATA_DIR/test-change-port-$port")
if [[ -f "${DATA_PREFIX}-test-change-port-$port" ]]; then
real_port=$(<"${DATA_PREFIX}-test-change-port-$port")
fi
WAIT="tcp://$network_alias:$real_port"
WHERE="via mesh"
fi

if [[ $check_type == "sock" ]]; then
IFS=":" read sock_type sock_file <<<"$check_value"
IFS=":" read -r _sock_type sock_file <<<"$check_value"
if [[ ! -S $sock_file ]]; then
if [[ ! -v REPORTED_SOCKS[$sock_file] ]]; then
log_warn "Sock file $sock_file does not exist locally!"
Expand All @@ -44,43 +46,42 @@ function check_services() {
action=""
appendix=""
message="${swarm_name} service ${service_name} (${network_alias}:${check_value})"
/usr/local/bin/dockerize -timeout 5s -wait "$WAIT" true 2>$log_file
if [ $? -ne 0 ]; then
if ! /usr/local/bin/dockerize -timeout 5s -wait "$WAIT" true 2>"$log_file"; then
if [[ -f $pending_file ]]; then
log_warn "Pending alert: $message"
else
echo "$unique_id" >$pending_file
echo "$unique_id" >"$pending_file"
action="create"
appendix="not available $WHERE"
fi
else
if [[ -f $pending_file ]]; then
action="resolve"
appendix="is available $WHERE"
unique_id=$(cat $pending_file)
rm -f $pending_file
unique_id=$(cat "$pending_file")
rm -f "$pending_file"
fi
fi
if [[ -n $action ]]; then
jq -n \
--arg action "$action" \
--arg unique_id "$unique_id" \
--arg message "$message $appendix" \
--arg summary "$(cat $log_file)" \
--arg summary "$(cat "$log_file")" \
'{
"action": $action,
"unique_id": $unique_id,
"message": $message,
"summary": $summary
}' | /bin/bash -c "$ALERT_SCRIPT"
"action": $action,
"unique_id": $unique_id,
"message": $message,
"summary": $summary
}' | /bin/bash -c "$ALERT_SCRIPT"
fi
rm -f $log_file
rm -f "$log_file"
done < <(./services.sh)
}

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

while true; do
sleep $LOOP_SLEEP
sleep "$LOOP_SLEEP"
check_services
done
10 changes: 6 additions & 4 deletions swarm-alerter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ source "./config.sh"
source "./logger.sh"
source "./checks.sh"

DATA_PREFIX="$DATA_DIR/swarm-alerter"

function check_nodes() {
local active_node_count
active_node_count=$(./nodes.sh | wc -l)
local swarm_name=$SWARM_NAME
local prefix="$DATA_DIR/swarm_failure"
local prefix="${DATA_PREFIX}"
local pending_file="${prefix}.pending"
local log_file="${prefix}.log"
local where="at $HOSTNAME"

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

action=""
appendix=""
message="${swarm_name} swarm active managers count $active_node_count"
if ((SWARM_MANAGER_MIN > active_node_count)); then
./nodes.sh --verbose &>"$log_file"

if [[ -f $pending_file ]]; then
log_warn "Pending alert: $message"
else
Expand Down Expand Up @@ -48,7 +51,6 @@ function check_nodes() {
"summary": $summary
}' | /bin/bash -c "$ALERT_SCRIPT"
fi
rm -f "$log_file"
}

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

0 comments on commit 0d169b4

Please sign in to comment.