-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-html.sh
executable file
·137 lines (125 loc) · 4.21 KB
/
generate-html.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
#!/usr/bin/env sh
# Configuration variables
TITLE="DeFiChain Status"
HEADER="DeFiChain Status"
CHECKS_FILE="${1:-checks.csv}"
INCIDENTS_FILE="${2:-incidents.txt}"
OUTAGE_RC=false
TIMEOUT=10
USER_AGENT="User-Agent: Mozilla/5.0 (X11; Linux x86_64; Debian) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
TMP_DIR="$(mktemp -d)"
command_exists(){
if ! command -v "${1}" >/dev/null 2>&1; then
echo >&2 "Error: ${1} missing. Please install it"
exit 1
fi
}
get_element(){
echo "${2}" | awk -v col="${1}" -F',' '{gsub(/^[ \t]+|[ \t]+$/, "", $col); print $col}'
}
check(){
check="${1}"
host="${2}"
name="${3}"
expected_rc="${4}"
id="${5}"
ipversion="$(echo "${check}" | grep -o '[46]$')"
case "${check}" in
http*)
rc="$(curl -${ipversion}sSkLo /dev/null -H "${USER_AGENT}" -m "${TIMEOUT}" -w "%{http_code}" "${host}" 2> "${TMP_DIR}/${id}.ko.info")"
if [ -s "${TMP_DIR}/${id}.ko.info" ]; then
sed -e 's,curl: ([0-9]*) ,,' -i "${TMP_DIR}/${id}.ko.info"
else
echo "Status code: ${rc}, expected: ${expected_rc}" > "${TMP_DIR}/${id}.ko.info"
fi;;
ping*)
ping -${ipversion}W "${TIMEOUT}" -c 1 "${host}" >/dev/null 2>&1
rc=$?
[ "${rc}" -ne "${expected_rc}" ] && echo 'Host unreachable' > "${TMP_DIR}/${id}.ko.info";;
port*)
error="$(nc -${ipversion}w "${TIMEOUT}" -zv ${host} 2>&1)"
rc=$?
[ "${rc}" -ne "${expected_rc}" ] && echo "${error}" | sed -e 's,nc: ,,' > "${TMP_DIR}/${id}.ko.info";;
esac
# Verify status and write files
if [ "${rc}" -eq "${expected_rc}" ]; then
echo "${name}" > "${TMP_DIR}/${id}.ok"
else
echo "${name}" > "${TMP_DIR}/${id}.ko"
fi
}
# Verify requirements
command_exists 'curl'
command_exists 'nc'
command_exists 'ping'
mkdir -p "${TMP_DIR}" || exit 1
# Execute checks
id=0
while IFS="$(printf '\n')" read -r line; do
check="$(get_element 1 "${line}")"
code="$(get_element 2 "${line}")"
name="$(get_element 3 "${line}")"
host="$(get_element 4 "${line}")"
check "${check}" "${host}" "${name}" "${code}" "${id}" &
id=$((id + 1)) # Correct increment
done < "${CHECKS_FILE}"
wait
OUTAGES_COUNT="$(ls "${TMP_DIR}/"*.ko | wc -l)"
# Generate HTML
cat << EOF
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><title>${TITLE}</title><style>
body { font-family: segoe ui,Roboto,Oxygen-Sans,Ubuntu,Cantarell,helvetica neue,Verdana,sans-serif; }
h1 { margin-top: 30px; }
ul { padding: 0px; }
li { list-style: none; margin-bottom: 2px; padding: 5px; border-bottom: 1px solid #ddd; }
.container { max-width: 600px; width: 100%; margin: 15px auto; }
.panel { text-align: center; padding: 10px; border: 0px; border-radius: 5px; }
.failed-bg { color: white; background-color: #E25D6A; }
.success-bg { color: white; background-color: #52B86A; }
.failed { color: #E25D6A; }
.success { color: #52B86A; }
.small { font-size: 80%; }
.status { float: right; }
</style></head>
<body>
<div class='container'>
<h1>${HEADER}</h1>
EOF
if [ "${OUTAGES_COUNT}" -ne 0 ]; then
echo "<ul><li class='panel failed-bg'>${OUTAGES_COUNT} Outage(s)</li></ul>"
else
echo "<ul><li class='panel success-bg'>All Systems Operational</li></ul>"
fi
cat << EOF
<h1>Services</h1>
<ul>
EOF
for file in "${TMP_DIR}/"*.ko; do
[ -e "${file}" ] || continue
echo "<li>$(cat "${file}") <span class='small failed'>($(cat "${file}.info"))</span><span class='status failed'>Disrupted</span></li>"
done
for file in "${TMP_DIR}/"*.ok; do
[ -e "${file}" ] || continue
echo "<li>$(cat "${file}") <span class='status success'>Operational</span></li>"
done
cat << EOF
</ul>
<p class=small> Last check: $(date +%FT%T%z)</p>
EOF
if [ -f "${INCIDENTS_FILE}" ]; then
echo '<h1>Incidents</h1>'
if [ -s "${INCIDENTS_FILE}" ]; then
sed 's|^\(.*\)$|<p>\1</p>|' "${INCIDENTS_FILE}"
else
echo '<p>No incident reported yet ;)</p>'
fi
fi
cat <<EOF
</div>
</body></html>
EOF
# Cleanup and exit
rm -r "${TMP_DIR}" 2>/dev/null
if [ "${OUTAGE_RC}" = true ]; then
exit "${OUTAGES_COUNT}"
fi