check_curl: add expected status code option

This commit is contained in:
Cyberes 2024-06-13 19:10:06 -06:00
parent 50e052f380
commit 1d1e7a486c
1 changed files with 14 additions and 2 deletions

View File

@ -15,6 +15,7 @@ usage() {
-M Set the HTTP mode (default: GET) -M Set the HTTP mode (default: GET)
-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 Expected status code. -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."
@ -67,6 +68,10 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do
RESOLVE="--resolve $OPTARG" RESOLVE="--resolve $OPTARG"
;; ;;
s) s)
if [ -n "$EXPECTED_STATUS_CODE" ]; then
echo "Cannot use -s option with -e option"
exit 3
fi
IGNORE_STATUS="yes" IGNORE_STATUS="yes"
;; ;;
t) t)
@ -87,6 +92,13 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do
n) n)
DISABLE_PERFDATA=true 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 echo "Invalid option: -$OPTARG" >&2
usage usage
@ -399,11 +411,11 @@ perfdata="response_time=${RESPONSE_TIME_MS}ms;${WARN_TIME};${CRIT_TIME};0;"
# Check HTTP code # Check HTTP code
if [ -n "$EXPECTED_STATUS_CODE" ]; then 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" echo "CRITICAL - server returned HTTP code $HTTP_CODE, expected $EXPECTED_STATUS_CODE"
exit 2 exit 2
fi 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" echo "CRITICAL - server returned HTTP code $HTTP_CODE"
exit 2 exit 2
fi fi