-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-server-liveness-checker.sh
executable file
·144 lines (114 loc) · 4.76 KB
/
web-server-liveness-checker.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
# debugging switches
# set -o errexit # abort on nonzero exitstatus; same as set -e
# set -o nounset # abort on unbound variable; same as set -u
# set -o pipefail # don't hide errors within pipes
# set -o xtrace # show commands being executed; same as set -x
# set -o verbose # verbose mode; same as set -v
source /srv/timbos-hn-reader/functions.sh
source /srv/timbos-hn-reader/thnr-common-functions.sh
# for write-log-message
project_base_dir="/srv/timbos-hn-reader/"
all_logs_dir="${project_base_dir}logs/"
combined_log_identifier="combined"
server_name=thnr
TZ=UTC
utility_account_username=tim
log_prefix_local="web-server-liveness-checker.sh: "
get-http-status-code-for-url() {
local target_url="${1-}"
[[ -z "${target_url}" ]] && die "get-http-status-code-for-url() requires a URL to be specified."
http_status_code_curl=""
if program-available curl; then
http_status_code_curl=$(
curl \
--max-redirs 0 \
--max-time 10 \
--output /dev/null \
--silent \
--write-out "%{http_code}\n" \
"${target_url}"
)
fi
http_status_code_wget=""
if program-available wget; then
http_status_code_wget=$(
wget \
--max-redirect=0 \
--server-response \
--spider \
"${target_url}" 2>&1 |
grep "HTTP/" |
awk '{print $2}'
)
fi
# both 200: very happy path
if [[ "${http_status_code_curl}" == "${http_status_code_wget}" && "${http_status_code_curl}" == "200" ]]; then
echo "200"
# both same: still pretty happy
elif [[ "${http_status_code_curl}" == "${http_status_code_wget}" && "${http_status_code_curl}" != "" ]]; then
echo "${http_status_code_curl}"
# both blank
elif [[ "${http_status_code_curl}" == "${http_status_code_wget}" && "${http_status_code_curl}" == "" ]]; then
echo "failed to get http status code for ${target_url}"
# wget worked, curl didn't
elif [[ "${http_status_code_curl}" == "" ]]; then
echo "${http_status_code_wget}"
# curl worked, wget didn't
elif [[ "${http_status_code_wget}" == "" ]]; then
echo "${http_status_code_curl}"
# invariant now: both curl and wget returned http status codes, but they're not the same code
# one of them is 200
elif [[ "${http_status_code_curl}" == "200" || "${http_status_code_wget}" == "200" ]]; then
echo "200"
# one starts with a "2" and the other starts with a "5"
elif [[ "${http_status_code_curl:0:1}" == "2" && "${http_status_code_wget:0:1}" == "5" ]]; then
echo "${http_status_code_curl}"
elif [[ "${http_status_code_curl:0:1}" == "5" && "${http_status_code_wget:0:1}" == "2" ]]; then
echo "${http_status_code_wget}"
# both start with a "4"
elif [[ "${http_status_code_curl:0:1}" == "4" && "${http_status_code_wget:0:1}" == "4" ]]; then
echo "4xx"
# both start with a "5"
elif [[ "${http_status_code_curl:0:1}" == "5" && "${http_status_code_wget:0:1}" == "5" ]]; then
echo "5xx"
# indeterminate case
else
echo "indeterminate response from ${target_url}"
fi
}
urls_file="/srv/timbos-hn-reader/urls-for-liveness-checks.txt"
die-if-file-not-present "${urls_file}"
while IFS= read -r url; do
[[ "${url}" =~ ^# ]] && continue
http_status_code=$(get-http-status-code-for-url "${url}")
# slugify the url
url_slug=$(echo "${url}" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
"${project_base_dir}send-dashboard-event-to-kafka2.sh" \
"timestamps" "$(get-time-in-unix-seconds)" "" \
"operation" "update-text-content" \
"elementId" "${url_slug}-http-status-code" \
"value" "${http_status_code}"
# "${project_base_dir}send-dashboard-event-to-kafka2.sh" \
# "operation" "update-text-content" \
# "elementId" "${url_slug}-http-status-code-last-updated-iso8601" \
# "value" "$(get-iso8601-date)"
if [[ "${http_status_code}" == "200" ]]; then
# green for good
"${project_base_dir}send-dashboard-event-to-kafka2.sh" \
"operation" "update-color" \
"elementId" "${url_slug}-http-status-code" \
"value" "green"
else
# red for bad
"${project_base_dir}send-dashboard-event-to-kafka2.sh" \
"operation" "update-color" \
"elementId" "${url_slug}-http-status-code" \
"value" "red"
# send alert email
send-alert-email \
"Web server problem: http status code ${http_status_code} for ${url}" \
"Web server problem: http status code ${http_status_code} for ${url}"
fi
done <"${urls_file}"
exit 0