Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thierrymarianne/67364e1157d56a820aa9f03957b12b59 to your computer and use it in GitHub Desktop.
Save thierrymarianne/67364e1157d56a820aa9f03957b12b59 to your computer and use it in GitHub Desktop.
Assert some monitored domains are available given a page title to be checked
#!/usr/bin/env bash
set -Eeuo pipefail
source "${HOME}/.secrets.sh"
function convert_timestamp_to_date_in_selected_timezone() {
local timestamp
timestamp="${1}"
if [ -z "${timestamp}" ]; then
printf 'A %s is expected as %s (%s).%s' 'non-empty string' 'first argument' '(timestamp)' $'\n' 1>&2
return 1
fi
date --date @${timestamp}
}
alias timestamp-to-date='convert_timestamp_to_date_in_selected_timezone'
function now() {
date -Iminutes |
sed -E 's/T/_/g' |
sed -E 's/:/H/g' |
sed -E 's/(\+.+)//g'
}
alias now='now'
function warn_about() {
local message
message="${1}"
curl -q --silent --json '{"user": "'$SMS_SERVICE_PROVIDER_USER_ID'", "pass": "'$SMS_SERVICE_PROVIDER_PASSWORD'", "msg": "'${message}'"}' "${SMS_SERVICE_PROVIDER_ENDPOINT}"
}
alias warn-about='warn_about'
function warn_about_unavailable_service() {
local message
message="$(echo "${1} is down")"
warn_about "${message}"
}
alias warn-about-unavailable-service='warn_about_unavailable_service'
function assert_title_at_domain_equals() {
(
export PATH="/opt/homebrew/bin:${PATH}"
local domain="$1"
local expected_title="$2"
if [ -z "${expected_title}" ]; then
printf 'A %s is expected as %s (%s).%s' 'non-empty string' 'second argument' 'title' $'\n'
return 1
fi
local actual_title
actual_title="$(curl -sL "${domain}" | htmlq title --text)"
if [ -n "${DEBUG}" ]; then
echo "Requested '${domain}'" 1>&2
echo "Actual title is '${actual_title}'" 1>&2
fi
test "${actual_title}" = "${expected_title}"
exit_code=$?
if [ ${exit_code} -gt 0 ]; then
return ${exit_code}
fi
)
}
alias assert-title-at-domain-equals='assert_title_at_domain_equals'
function assert_monitored_domain_is_available() {
local exit_code
local domain
domain="${1}"
local expected_title
expected_title="${2}"
assert_title_at_domain_equals "${domain}" "${expected_title}"
exit_code=$?
if [ $exit_code -gt 0 ]; then
echo "${domain} is down 🙈."
warn_about_unavailable_service "${domain}"
return ${exit_code}
else
echo "${domain} is up and kicking 💁."
fi
}
alias assert-monitored-domain-is-available='assert_monitored_domain_is_available'
function assert_monitored_domains_are_available() {
printf 'Monitoring starts at %s.%s' "$(now)" $'\n'
(assert_monitored_domain_is_available 'https://example.org' 'Example Domain')
printf 'Monitoring ends at %s.%s' "$(now)" $'\n'
}
alias assert-monitored-domains-are-available='assert_monitored_domains_are_available'
set +Eeuo pipefail
# Copyright (C) 2024 Thierry Marianne <github.6whk7@filtrable.simplelogin.com> -- assert_monitored_domains_are_available.sh
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment