-
Notifications
You must be signed in to change notification settings - Fork 0
/
port-alerter.sh
executable file
·86 lines (78 loc) · 2.53 KB
/
port-alerter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env bash
source "./config.sh"
source "./logger.sh"
source "./checks.sh"
declare -A REPORTED_SOCKS
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}")
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}"
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")
fi
WAIT="tcp://$network_alias:$real_port"
WHERE="via mesh"
fi
if [[ $check_type == "sock" ]]; then
IFS=":" read 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!"
REPORTED_SOCKS[$sock_file]=1
fi
continue
fi
WAIT="$check_value"
WHERE="at $HOSTNAME"
fi
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 [[ -f $pending_file ]]; then
log_warn "Pending alert: $message"
else
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
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)" \
'{
"action": $action,
"unique_id": $unique_id,
"message": $message,
"summary": $summary
}' | /bin/bash -c "$ALERT_SCRIPT"
fi
rm -f $log_file
done < <(./services.sh)
}
log_info "Entering loop with ${LOOP_SLEEP} sleep on entry ..."
while true; do
sleep $LOOP_SLEEP
check_services
done