From 1d1e7a486ca1ef888ded89a6f6cef21c623f5607 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Thu, 13 Jun 2024 19:10:06 -0600 Subject: [PATCH] check_curl: add expected status code option --- check_curl.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/check_curl.sh b/check_curl.sh index 93d33e5..d753d33 100755 --- a/check_curl.sh +++ b/check_curl.sh @@ -15,6 +15,7 @@ usage() { -M Set the HTTP mode (default: GET) -t Timeout in seconds -s Ignore the response status code. + -e Specify the exact response code that should be returned. Cannot be used with -s option. -e Expected status code. -S Sanitize HTML when printing to console. Good for Icinga2 Web. -n Don't return any perfdata if the status code was not 200." @@ -67,6 +68,10 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do RESOLVE="--resolve $OPTARG" ;; s) + if [ -n "$EXPECTED_STATUS_CODE" ]; then + echo "Cannot use -s option with -e option" + exit 3 + fi IGNORE_STATUS="yes" ;; t) @@ -87,6 +92,13 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do n) DISABLE_PERFDATA=true ;; + e) + if [ "$IGNORE_STATUS" = "yes" ]; then + echo "Cannot use -e option with -s option" + exit 3 + fi + EXPECTED_STATUS_CODE="$OPTARG" + ;; *) echo "Invalid option: -$OPTARG" >&2 usage @@ -399,11 +411,11 @@ perfdata="response_time=${RESPONSE_TIME_MS}ms;${WARN_TIME};${CRIT_TIME};0;" # Check HTTP code if [ -n "$EXPECTED_STATUS_CODE" ]; then - if [ "$HTTP_CODE" -ne "$EXPECTED_STATUS_CODE" ]; then + if [ "$HTTP_CODE" != "$EXPECTED_STATUS_CODE" ]; then echo "CRITICAL - server returned HTTP code $HTTP_CODE, expected $EXPECTED_STATUS_CODE" exit 2 fi -elif [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" -ne 200 ]; then +elif [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" != 200 ]; then echo "CRITICAL - server returned HTTP code $HTTP_CODE" exit 2 fi