#!/bin/bash # author: Remy van Elst - https://raymii.org # license: gnu gpl v3 # Start of configuration. NAME="" #array names must not contain spaces, only a-ZA-Z. declare -A urls # do not remove this line urls[webbed_sight]="https://vulpineawoo.github.io" urls[atheme_services]="http://irc.wppnx.pii.at" urls[irc.wppnx.pii.at]="http://irc.wppnx.pii.at" urls[foxes.are.allowed.org]="https://foxes.are.allowed.org" # The default status code. Can be overriden per URL lower in the script. defaultExpectedStatusCode=200 # Expected code for url, key must match urls[]. Only for URL's you consider UP, but for example require authentication declare -A statuscode # do not remove this line statuscode[gist.github.com]=302 # How many curl checks to run at the same time maxConcurrentCurls=12 # Max timeout of a check in seconds defaultTimeOut=3 # Start of script. Do not edit below # Exit immediately if a command exits with a non-zero status. 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 # start of the function definitions # 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 # 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() { name="${1}" url="${2}" checkStartTimeMs="$(date +%s%3N)" # epoch in microseconds, but last chars stripped so it's milliseconds set +e # curl errors don't count for early exit, turn off e checkStatusCode="$(curl --max-time "${defaultTimeOut}" --silent --show-error --insecure --output /dev/null --write-out "%{http_code}" "$url" --stderr "${tempfolder}/FAIL/${name}.error")" set -e # turn e back on checkEndTimeMs="$(date +%s%3N)" timeCheckTook="$((checkEndTimeMs-checkStartTimeMs))" expectedStatusCode=${defaultExpectedStatusCode} # check if there is a specific status code for this domain if [[ -n "${statuscode[${name}]}" ]]; then expectedStatusCode="${statuscode[${name}]}" fi if [[ ${expectedStatusCode} -eq ${checkStatusCode} ]]; then echo "${timeCheckTook}" > "${tempfolder}/OK/${name}.duration" else 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 } writeOkayChecks() { echo "" echo "
" echo "Status: ${status}" echo "${curlError}" echo "
Last check: "
date
echo "
Total duration: ${runtime} ms"
echo "
Monitoring script by Remy van Elst. License: GNU AGPLv3. "
echo "Source code"
echo "