From 0f047a898d003b9a5b1f2a5137a934154cb8f44c Mon Sep 17 00:00:00 2001 From: Cyberes Date: Mon, 18 Nov 2024 19:07:28 -0700 Subject: [PATCH] check_curl: handle both -C and -e when used together --- check_curl.sh | 57 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/check_curl.sh b/check_curl.sh index 7cc40ac..de12195 100755 --- a/check_curl.sh +++ b/check_curl.sh @@ -16,7 +16,6 @@ usage() { -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. -a Specify the username and password for authentication in the format username:password" @@ -411,49 +410,69 @@ fi RESPONSE_TIME_MS=$(echo "$RESPONSE_TIME * 1000" | bc | xargs printf "%0.0f\n") perfdata="response_time=${RESPONSE_TIME_MS}ms;${WARN_TIME};${CRIT_TIME};0;" +EXIT_CODE=0 +ERROR_MSG="" + # Check HTTP code if [ -n "$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 + STATUS_CODE_MSG="server returned HTTP code $HTTP_CODE, expected $EXPECTED_STATUS_CODE" + ERROR_MSG="${ERROR_MSG}${STATUS_CODE_MSG}; " + EXIT_CODE=2 fi elif [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" != 200 ]; then - echo "CRITICAL - server returned HTTP code $HTTP_CODE" - exit 2 + STATUS_CODE_MSG="server returned HTTP code $HTTP_CODE" + ERROR_MSG="${ERROR_MSG}${STATUS_CODE_MSG}; " + EXIT_CODE=2 fi # Check response time -if [ $(echo "$RESPONSE_TIME_MS > $CRIT_TIME" | bc) -eq 1 ]; then - echo "CRITICAL - Response time $RESPONSE_TIME seconds | $perfdata" - exit 2 -elif [ $(echo "$RESPONSE_TIME_MS > $WARN_TIME" | bc) -eq 1 ]; then - echo "WARNING - response time $RESPONSE_TIME seconds | $perfdata" - exit 1 +if [ "$(echo "$RESPONSE_TIME_MS > $CRIT_TIME" | bc)" -eq 1 ]; then + RESPONSE_TIME_MSG="Response time $RESPONSE_TIME seconds exceeded critical threshold" + ERROR_MSG="${ERROR_MSG}${RESPONSE_TIME_MSG}; " + EXIT_CODE=2 +elif [ "$(echo "$RESPONSE_TIME_MS > $WARN_TIME" | bc)" -eq 1 ]; then + RESPONSE_TIME_MSG="Response time $RESPONSE_TIME seconds exceeded warning threshold" + ERROR_MSG="${ERROR_MSG}${RESPONSE_TIME_MSG}; " + if [ $EXIT_CODE -lt 1 ]; then + EXIT_CODE=1 + fi fi # Check critical string BODY_CONTAINS="" if [ -n "$CRIT_STRING" ]; then BODY=$(curl -s $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $AUTHENTICATION "$URL") - # shellcheck disable=SC2076 - if ! [[ $BODY =~ "$CRIT_STRING" ]]; then + if ! [[ $BODY =~ $CRIT_STRING ]]; then if $SANITIZE_HTML; then BODY=$(echo "$BODY" | sed 's//\>/g') fi - echo "CRITICAL - response body does not contain the required string: $BODY" - exit 2 + CRIT_STRING_MSG="response body does not contain the required string" + ERROR_MSG="${ERROR_MSG}${CRIT_STRING_MSG}; " + EXIT_CODE=2 else BODY_CONTAINS=", and contained substring" fi fi -# All checks passed - +# Prepare perfdata_str if $DISABLE_PERFDATA && [ "$HTTP_CODE" != 200 ]; then perfdata_str="" else perfdata_str="| $perfdata" fi -echo "OK - response time was $RESPONSE_TIME seconds, code $HTTP_CODE${BODY_CONTAINS}. $perfdata_str" -exit 0 +# Remove trailing spaces and the `;` char from the message. +ERROR_MSG=$(echo "$ERROR_MSG" | xargs | sed 's/;*$//g') + +# Output result +if [ $EXIT_CODE -eq 0 ]; then + echo "OK - response time was $RESPONSE_TIME seconds, code $HTTP_CODE${BODY_CONTAINS} $perfdata_str" + exit 0 +elif [ $EXIT_CODE -eq 1 ]; then + echo "WARNING - $ERROR_MSG $perfdata_str" + exit 1 +else + echo "CRITICAL - $ERROR_MSG $perfdata_str" + exit 2 +fi \ No newline at end of file