From 11fed2215ce590a1d0c493ae1155edec846ce534 Mon Sep 17 00:00:00 2001 From: Chl Date: Wed, 24 Jan 2024 01:36:04 +0100 Subject: [PATCH] Adding check_gitlab_readiness.sh --- nagios/check_gitlab_readiness.sh | 203 +++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100755 nagios/check_gitlab_readiness.sh diff --git a/nagios/check_gitlab_readiness.sh b/nagios/check_gitlab_readiness.sh new file mode 100755 index 0000000..9b10246 --- /dev/null +++ b/nagios/check_gitlab_readiness.sh @@ -0,0 +1,203 @@ +#!/bin/bash + +# Little monitoring script to check the readiness of a Gitlab instance. +# +# Remember that you have to whitelist the IP of your monitoring system : +# gitlab.rb: +# gitlab_rails['monitoring_whitelist'] = ['127.0.0.0/8', '::1/128', ...] +# See https://docs.gitlab.com/ee/administration/monitoring/health_check.html +# +# Licence: WTFPL +# Copyright: chl-dev@bugness.org 2024 + +PROGPATH=$( echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,' ) +REVISION="0.1" + +# Default values +GITLAB_HOSTNAME="localhost" +GITLAB_URL="" +GITLAB_URL_PROTOCOL="https" +NB_CHECKS_RANGE="15:20" + +# Include check_range() and STATE_OK, STATE_WARNING, ... variables +. $PROGPATH/utils.sh + +# Output +OUTPUT_CPT_CHECKS=0 +OUTPUT_EXIT_STATUS=0 +OUTPUT_DETAIL_OK="" +OUTPUT_DETAIL_WARNING="" +OUTPUT_DETAIL_CRITICAL="" +OUTPUT_DETAIL_UNKNOWN="" +OUTPUT_PERFDATA="" + + +# Stop at first uncaught error +set -eu + +# Wrapper to use whatever is available to make HTTP queries +fetch_with_curl_wget_or_whatever () { + if which "wget" >/dev/null 2>&1; then + wget -q -O - "$1" + elif which "curl" >/dev/null 2>&1 ; then + curl -s "$1" + else + echo "UNKNOWN: no wget/curl/whatever available to make HTTP queries." + exit $STATE_UNKNOWN + fi +} + +check_http_status_200() { + if which "wget" >/dev/null 2>&1; then + wget -q --spider "$1" + elif which "curl" >/dev/null 2>&1 ; then + test "$( curl -s -o /dev/null -I -w "%{http_code}" "$1" )" == "200" + else + echo "UNKNOWN: no wget/curl/whatever available to make HTTP queries." + exit $STATE_UNKNOWN + fi +} + +print_full_url() { + if [ -n "$GITLAB_URL" ]; then + echo "$GITLAB_URL" + else + echo "$GITLAB_URL_PROTOCOL://$GITLAB_HOSTNAME/-/readiness?all=1" + fi +} + +# Help function +usage() { + cat </dev/null; then + echo "UNKNOWN command 'jq' not available." + exit $STATE_UNKNOWN +fi + +# First, let's check that the URL responds with 200/OK +#if ! check_http_status_200 "$( print_full_url )"; then +# echo TODO later. Since it needs 2 queries, it could lead to inconsistencies if we get a 200 now and an error later. +# Would the best way be to set another probe ? +# check_https!-H $HOSTNAME$ -f warning -u /-/readiness?all=1 +# check_https!-H $HOSTNAME$ -f warning -u /-/health -s "GitLab OK" +#fi + +if ! JSONDATA="$( fetch_with_curl_wget_or_whatever "$( print_full_url )" )"; then + echo "UNKNOWN error fetching the URL '$( print_full_url )'" + exit $STATE_UNKNOWN +fi + +while read KEY; do + OUTPUT_CPT_CHECKS=$(( $OUTPUT_CPT_CHECKS + 1 )) + if [ "$KEY" == "status" ]; then + STATUS="$( printf "%s\n" "$JSONDATA" | jq ".$KEY" )" + else + STATUS="$( printf "%s\n" "$JSONDATA" | jq ".$KEY[0].status" )" + fi + if [ "$STATUS" != '"ok"' ]; then + OUTPUT_DETAIL_CRITICAL="$( printf "%s Status for key '$KEY': %s" "$OUTPUT_DETAIL_CRITICAL" "$STATUS" )" + OUTPUT_EXIT_STATUS=$STATE_CRITICAL + fi +done <