Skip to content

Instantly share code, notes, and snippets.

@ryran
Created December 2, 2019 18:46
Show Gist options
  • Save ryran/37d31e056fb0eca660f819f7706c7c41 to your computer and use it in GitHub Desktop.
Save ryran/37d31e056fb0eca660f819f7706c7c41 to your computer and use it in GitHub Desktop.
GitLab localhost health-checker for cron
#!/bin/bash
# Created 2019 by Ryan Sawhill Aroha <rsaw@redhat.com>
# Ref: https://docs.gitlab.com/ee/user/admin_area/monitoring/health_check.html
rc=0
url="https://localhost/-"
endpoints="readiness liveness"
file1=$(mktemp) file2=$(mktemp)
trap "rm -rf $file1 $file2" EXIT INT
print_tsv() {
cat >$file2
if jq -e .error $file2 &>/dev/null; then
jq -r 'to_entries[] | [.key, .value] | @tsv' $file2
else
jq -r '.status=[{"status":.status}] | to_entries[] | [.key, .value[].status] | @tsv' $file2 | column -t
fi
}
check_endpoint() {
local e=$1
if ! curl -sSk $url/$e >$file1; then
echo Hit error accessing GitLab $e endpoint
return 1
elif ! jq -e . $file1 >/dev/null; then
echo GitLab $e probe produced invalid json output
return 1
elif jq -e .error $file1 &>/dev/null; then
echo GitLab $e probe shows errors
print_tsv <$file1
return 1
elif ! jq -e '.status=[{"status":.status}]' $file1 >$file2; then
echo Error munging GitLab $e probe output to rearrange global status
return 1
elif [[ $(jq -r .[][].status $file2 | sort -u) != ok ]]; then
echo GitLab $e probe reports problems
print_tsv <$file1
return 1
fi
}
# Verbose mode just prints everything and exits
if [[ $1 == -v ]]; then
printf "Health: "
curl -sSk $url/health
echo -e "\n"
for e in $endpoints; do
echo GitLab $e probe:
curl -sSk $url/$e | print_tsv
echo
done
exit 0
fi
# Otherwise conditionally print on failure
if [[ $(curl -skL $url/health) != "GitLab OK" ]]; then
echo GitLab/health reports application server not running
curl -sSik $url/health
exit 1
fi
for e in $endpoints; do
check_endpoint $e || rc=1
done
exit $rc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment