check_curl: handle both -C and -e when used together

This commit is contained in:
Cyberes 2024-11-18 19:07:28 -07:00
parent 6dbeb6024e
commit 0f047a898d
1 changed files with 38 additions and 19 deletions

View File

@ -16,7 +16,6 @@ usage() {
-t Timeout in seconds -t Timeout in seconds
-s Ignore the response status code. -s Ignore the response status code.
-e Specify the exact response code that should be returned. Cannot be used with -s option. -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. -S Sanitize HTML when printing to console. Good for Icinga2 Web.
-n Don't return any perfdata if the status code was not 200. -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" -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") 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;" perfdata="response_time=${RESPONSE_TIME_MS}ms;${WARN_TIME};${CRIT_TIME};0;"
EXIT_CODE=0
ERROR_MSG=""
# Check HTTP code # Check HTTP code
if [ -n "$EXPECTED_STATUS_CODE" ]; then if [ -n "$EXPECTED_STATUS_CODE" ]; then
if [ "$HTTP_CODE" != "$EXPECTED_STATUS_CODE" ]; then if [ "$HTTP_CODE" != "$EXPECTED_STATUS_CODE" ]; then
echo "CRITICAL - server returned HTTP code $HTTP_CODE, expected $EXPECTED_STATUS_CODE" STATUS_CODE_MSG="server returned HTTP code $HTTP_CODE, expected $EXPECTED_STATUS_CODE"
exit 2 ERROR_MSG="${ERROR_MSG}${STATUS_CODE_MSG}; "
EXIT_CODE=2
fi fi
elif [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" != 200 ]; then elif [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" != 200 ]; then
echo "CRITICAL - server returned HTTP code $HTTP_CODE" STATUS_CODE_MSG="server returned HTTP code $HTTP_CODE"
exit 2 ERROR_MSG="${ERROR_MSG}${STATUS_CODE_MSG}; "
EXIT_CODE=2
fi fi
# Check response time # Check response time
if [ $(echo "$RESPONSE_TIME_MS > $CRIT_TIME" | bc) -eq 1 ]; then if [ "$(echo "$RESPONSE_TIME_MS > $CRIT_TIME" | bc)" -eq 1 ]; then
echo "CRITICAL - Response time $RESPONSE_TIME seconds | $perfdata" RESPONSE_TIME_MSG="Response time $RESPONSE_TIME seconds exceeded critical threshold"
exit 2 ERROR_MSG="${ERROR_MSG}${RESPONSE_TIME_MSG}; "
elif [ $(echo "$RESPONSE_TIME_MS > $WARN_TIME" | bc) -eq 1 ]; then EXIT_CODE=2
echo "WARNING - response time $RESPONSE_TIME seconds | $perfdata" elif [ "$(echo "$RESPONSE_TIME_MS > $WARN_TIME" | bc)" -eq 1 ]; then
exit 1 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 fi
# Check critical string # Check critical string
BODY_CONTAINS="" BODY_CONTAINS=""
if [ -n "$CRIT_STRING" ]; then if [ -n "$CRIT_STRING" ]; then
BODY=$(curl -s $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $AUTHENTICATION "$URL") 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 if $SANITIZE_HTML; then
BODY=$(echo "$BODY" | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g') BODY=$(echo "$BODY" | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g')
fi fi
echo "CRITICAL - response body does not contain the required string: $BODY" CRIT_STRING_MSG="response body does not contain the required string"
exit 2 ERROR_MSG="${ERROR_MSG}${CRIT_STRING_MSG}; "
EXIT_CODE=2
else else
BODY_CONTAINS=", and contained substring" BODY_CONTAINS=", and contained substring"
fi fi
fi fi
# All checks passed # Prepare perfdata_str
if $DISABLE_PERFDATA && [ "$HTTP_CODE" != 200 ]; then if $DISABLE_PERFDATA && [ "$HTTP_CODE" != 200 ]; then
perfdata_str="" perfdata_str=""
else else
perfdata_str="| $perfdata" perfdata_str="| $perfdata"
fi fi
echo "OK - response time was $RESPONSE_TIME seconds, code $HTTP_CODE${BODY_CONTAINS}. $perfdata_str" # Remove trailing spaces and the `;` char from the message.
exit 0 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