Commit 5dbc74af authored by R. van Elst's avatar R. van Elst
Browse files

Shellcheck fixes

parent 3374cdd8
screenshots/fail.png

63.1 KB | W: | H:

screenshots/fail.png

67.9 KB | W: | H:

screenshots/fail.png
screenshots/fail.png
screenshots/fail.png
screenshots/fail.png
  • 2-up
  • Swipe
  • Onion skin
...@@ -28,33 +28,46 @@ statuscode[gist.github.com]=302 ...@@ -28,33 +28,46 @@ statuscode[gist.github.com]=302
# How many curl checks to run at the same time # How many curl checks to run at the same time
maxConcurrentCurls=12 maxConcurrentCurls=12
# Start of script. Do not edit below # Max timeout of a check in seconds
defaultTimeOut=10
# We're writing the status code to a file, reading that later on. Why? # Start of script. Do not edit below
# Because an array cannot be filled via a subprocess (curl ... &). # Exit immediately if a command exits with a non-zero status.
set -e set -e
# patterns which match no files expand to a null string, otherwise later on we'd have *.status as a file...
shopt -s nullglob shopt -s nullglob
# start of the function definitions
# This function allows the script to execute all the curl calls in paralell. # This function allows the script to execute all the curl calls in paralell.
# Otherwise, if one would timeout or take long, the rest after that would be # Otherwise, if one would timeout or take long, the rest after that would be
# slower. # slower. We're writing the status code to a file, reading that later on. Why?
# Because an array cannot be filled via a subprocess (curl ... &)
doRequest() { doRequest() {
name="${1}" name="${1}"
url="${2}" url="${2}"
checkStartTimeMs=$(date +%s%3N) # epoch in microseconds, but stripped so it's milliseconds checkStartTimeMs="$(date +%s%3N)" # epoch in microseconds, but last chars stripped so it's milliseconds
checkStatusCode=$(curl --max-time 10 --silent --show-error --insecure --output /dev/null --write-out "%{http_code}" "$url" 2>${tempfolder}/FAIL/${name}.error) set +e # curl errors don't count for early exit, turn off e
checkEndTimeMs=$(date +%s%3N) checkStatusCode="$(curl --max-time "${defaultTimeOut}" --silent --show-error --insecure --output /dev/null --write-out "%{http_code}" "$url" --stderr "${tempfolder}/FAIL/${name}.error")"
timeCheckTook=$((checkEndTimeMs-checkStartTimeMs)) set -e # turn e back on
checkEndTimeMs="$(date +%s%3N)"
if [[ ! -z ${statuscode[${name}]} ]]; then timeCheckTook="$((checkEndTimeMs-checkStartTimeMs))"
defaultExpectedStatusCode=${statuscode[${name}]}
expectedStatusCode=${defaultExpectedStatusCode}
# check if there is a specific status code for this domain
if [[ -n "${statuscode[${name}]}" ]]; then
expectedStatusCode="${statuscode[${name}]}"
fi fi
if [[ ${defaultExpectedStatusCode} -eq ${checkStatusCode} ]]; then if [[ ${expectedStatusCode} -eq ${checkStatusCode} ]]; then
echo ${timeCheckTook} > ${tempfolder}/OK/${name}.duration echo "${timeCheckTook}" > "${tempfolder}/OK/${name}.duration"
else else
echo ${checkStatusCode} > ${tempfolder}/FAIL/${name}.status echo "${checkStatusCode}" > "${tempfolder}/FAIL/${name}.status"
fi
# if the error file is empty, remove it.
if [[ ! -s "${tempfolder}/FAIL/${name}.error" ]]; then
rm "${tempfolder}/FAIL/${name}.error"
fi fi
} }
...@@ -62,7 +75,7 @@ writeOkayChecks() { ...@@ -62,7 +75,7 @@ writeOkayChecks() {
echo "</div></div>" echo "</div></div>"
echo "<div class=row><div class=col>" echo "<div class=row><div class=col>"
echo "<h2>Checks</h2>" echo "<h2>Checks</h2>"
pushd "${tempfolder}/OK" 2>&1 >/dev/null pushd "${tempfolder}/OK" >/dev/null 2>&1
okFiles=(*.duration) okFiles=(*.duration)
okCount=${#okFiles[@]} okCount=${#okFiles[@]}
if [[ okCount -gt 0 ]]; then if [[ okCount -gt 0 ]]; then
...@@ -70,20 +83,20 @@ writeOkayChecks() { ...@@ -70,20 +83,20 @@ writeOkayChecks() {
if [[ -r $filename ]]; then if [[ -r $filename ]]; then
value="$(cat $filename)" value="$(cat $filename)"
filenameWithoutExt=${filename%.*} filenameWithoutExt=${filename%.*}
echo -n '<a href="#" class="btn btn-success disabled" tabindex="-1" role="button" aria-disabled="true" style="margin-top: 10px; padding: 10px;">' echo '<a href="#" class="btn btn-success disabled" tabindex="-1" role="button" aria-disabled="true" style="margin-top: 10px; padding: 10px;">'
echo -n "${filenameWithoutExt}" echo "${filenameWithoutExt}"
echo -n "<font color=LightGray> (" echo "<font color=LightGray> ("
echo -n ${value} echo ${value}
echo -n " ms)</font>" echo " ms)</font>"
echo -n "</a> &nbsp;" echo "</a> &nbsp;"
fi fi
done done
fi fi
popd 2>&1 >/dev/null popd >/dev/null 2>&1
} }
writeFailedChecks() { writeFailedChecks() {
pushd "${tempfolder}/FAIL" 2>&1 >/dev/null pushd "${tempfolder}/FAIL" >/dev/null 2>&1
failFiles=(*.status) failFiles=(*.status)
failCount=${#failFiles[@]} failCount=${#failFiles[@]}
if [[ failCount -gt 0 ]]; then if [[ failCount -gt 0 ]]; then
...@@ -99,19 +112,23 @@ writeFailedChecks() { ...@@ -99,19 +112,23 @@ writeFailedChecks() {
for filename in "${failFiles[@]}"; do for filename in "${failFiles[@]}"; do
if [[ -r $filename ]]; then if [[ -r $filename ]]; then
filenameWithoutExt=${filename%.*} filenameWithoutExt=${filename%.*}
error="$(cat ${filenameWithoutExt}.error)" if [[ -r "${filenameWithoutExt}.error" ]]; then
curlError="$(cat "${filenameWithoutExt}.error" 2>/dev/null)"
else
curlError="Status code does not match expected code"
fi
status="$(cat ${filename})" status="$(cat ${filename})"
echo -n "<tr class='table-danger'><td>" echo "<tr class='table-danger'><td>"
echo -n "${filenameWithoutExt}" echo "${filenameWithoutExt}"
echo -n "</td><td>" echo "</td><td>"
echo -n "${status} / " echo "${status} / "
if [[ -z ${statuscode[${filenameWithoutExt}]} ]]; then if [[ -z "${statuscode[${filenameWithoutExt}]}" ]]; then
echo -n "200" echo "${defaultExpectedStatusCode}"
else else
echo -n "${statuscode[${filenameWithoutExt}]}" echo "${statuscode[${filenameWithoutExt}]}"
fi fi
echo -n "</td><td>" echo "</td><td>"
echo -n "${error}" echo "${curlError}"
echo "</td></tr>" echo "</td></tr>"
fi fi
done done
...@@ -121,11 +138,11 @@ writeFailedChecks() { ...@@ -121,11 +138,11 @@ writeFailedChecks() {
echo "All is well, all services are up." echo "All is well, all services are up."
echo "</div>" echo "</div>"
fi fi
popd 2>&1 >/dev/null popd >/dev/null 2>&1
} }
cleanupFailedCheckFiles() { cleanupFailedCheckFiles() {
pushd "${tempfolder}/FAIL" 2>&1 >/dev/null pushd "${tempfolder}/FAIL" >/dev/null 2>&1
errorFiles=(*.error) errorFiles=(*.error)
errorCount=${#errorFiles[@]} errorCount=${#errorFiles[@]}
for filename in "${errorFiles[@]}"; do for filename in "${errorFiles[@]}"; do
...@@ -141,12 +158,12 @@ cleanupFailedCheckFiles() { ...@@ -141,12 +158,12 @@ cleanupFailedCheckFiles() {
rm "${filename}" rm "${filename}"
fi fi
done done
popd 2>&1 >/dev/null popd >/dev/null 2>&1
rmdir "${tempfolder}/FAIL" rmdir "${tempfolder}/FAIL"
} }
cleanupOKCheckFiles() { cleanupOKCheckFiles() {
pushd "${tempfolder}/OK" 2>&1 >/dev/null pushd "${tempfolder}/OK" >/dev/null 2>&1
okFiles=(*.duration) okFiles=(*.duration)
okCount=${#okFiles[@]} okCount=${#okFiles[@]}
for filename in "${okFiles[@]}"; do for filename in "${okFiles[@]}"; do
...@@ -154,10 +171,40 @@ cleanupOKCheckFiles() { ...@@ -154,10 +171,40 @@ cleanupOKCheckFiles() {
rm "${filename}" rm "${filename}"
fi fi
done done
popd 2>&1 >/dev/null popd >/dev/null 2>&1
rmdir "${tempfolder}/OK" rmdir "${tempfolder}/OK"
} }
writeHeader() {
echo '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">'
echo '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">'
echo "<title>Monitoring</title>"
echo "</head><body>"
echo "<div class=container><div class=row><div class=col>"
echo "<h1>Monitoring</h1>"
echo "</div></div>"
echo "<div class=row><div class=col>"
}
writeFooter() {
echo "</div></div>"
echo "<div class=row><div class=col>"
echo "<h2>Info</h2>"
echo "<p>Last check: "
date
echo "<br>Total duration: ${runtime} ms"
echo "<br>Monitoring script by <a href='https://raymii.org/'>Remy van Elst</a>. License: GNU AGPLv3. "
echo "<a href='https://github.com/raymiiorg/bash-http-monitoring'>Source code</a>"
echo "</p>"
echo "</div></div></div>"
echo "</body></html>"
}
# script start
# Total script duration timer # Total script duration timer
start=$(date +%s%3N) start=$(date +%s%3N)
...@@ -168,7 +215,7 @@ tempfolder=${tmpdir:-/tmp/statusmon/} ...@@ -168,7 +215,7 @@ tempfolder=${tmpdir:-/tmp/statusmon/}
mkdir -p "${tempfolder}/OK" || exit 1 mkdir -p "${tempfolder}/OK" || exit 1
mkdir -p "${tempfolder}/FAIL" || exit 1 mkdir -p "${tempfolder}/FAIL" || exit 1
pushd "${tempfolder}" 2>&1 >/dev/null pushd "${tempfolder}" >/dev/null 2>&1
# Do the checks parallel # Do the checks parallel
for key in "${!urls[@]}" for key in "${!urls[@]}"
...@@ -181,20 +228,10 @@ do ...@@ -181,20 +228,10 @@ do
done done
wait wait
# Header writeHeader
echo '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">'
echo '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">'
echo "<title>Monitoring</title>"
echo "</head><body>"
echo "<div class=container><div class=row><div class=col>"
echo "<h1>Monitoring</h1>"
echo "</div></div>"
echo "<div class=row><div class=col>"
# Failed checks, if any, go on top # Failed checks, if any, go on top
writeFailedChecks; writeFailedChecks
# Okay checks go below the failed checks # Okay checks go below the failed checks
writeOkayChecks writeOkayChecks
...@@ -203,25 +240,12 @@ writeOkayChecks ...@@ -203,25 +240,12 @@ writeOkayChecks
cleanupFailedCheckFiles cleanupFailedCheckFiles
cleanupOKCheckFiles cleanupOKCheckFiles
# Footer
echo "</div></div>"
echo "<div class=row><div class=col>"
echo "<h2>Info</h2>"
echo "<p>Last check: "
date
# stop the total timer # stop the total timer
end=$(date +%s%3N) end=$(date +%s%3N)
runtime=$((end-start)) runtime=$((end-start))
echo "<br>Total duration: ${runtime} ms"
echo "<br>Monitoring script by <a href='https://raymii.org/'>Remy van Elst</a>. License: GNU AGPLv3. " writeFooter
echo "<a href='https://github.com/raymiiorg/bash-http-monitoring'>Source code</a>"
echo "</p>"
echo "</div></div></div>"
echo "</body></html>"
popd 2>&1 >/dev/null popd >/dev/null 2>&1
rmdir "${tempfolder}" rmdir "${tempfolder}"
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment