From 30b2f8b3e35e8a4e8af10c091dd96936f29baf63 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Thu, 23 Nov 2023 21:41:05 -0700 Subject: [PATCH] update check_curl --- check_curl.sh | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/check_curl.sh b/check_curl.sh index ed34fdb..c0c7137 100755 --- a/check_curl.sh +++ b/check_curl.sh @@ -14,7 +14,8 @@ usage() { -R Set curl --resolve option. -M Set the HTTP mode (default: GET) -t Timeout in seconds - -s Ignore the response status code." + -s Ignore the response status code. + -e Expected status code." exit 3 } @@ -29,9 +30,10 @@ RESOLVE="" IGNORE_STATUS="" TIMEOUT="30" HTTP_METHOD="-X GET" +EXPECTED_STATUS_CODE="" # Parse arguments -while getopts "u:w:c:C:LH:M:IpR:st:h" opt; do +while getopts "u:w:c:C:LH:M:IpR:st:e:h" opt; do case $opt in u) URL="$OPTARG" @@ -69,6 +71,9 @@ while getopts "u:w:c:C:LH:M:IpR:st:h" opt; do M) HTTP_METHOD="-X $OPTARG" ;; + e) + EXPECTED_STATUS_CODE="$OPTARG" + ;; h) usage ;; @@ -379,16 +384,20 @@ if [ $CURL_EXIT -ne 0 ]; then exit 2 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;" + # Check HTTP code -if [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" -ne 200 ]; then +if [ -n "$EXPECTED_STATUS_CODE" ]; then + if [ "$HTTP_CODE" -ne "$EXPECTED_STATUS_CODE" ]; then + echo "CRITICAL - server returned HTTP code $HTTP_CODE, expected $EXPECTED_STATUS_CODE -> $URL" + exit 2 + fi +elif [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" -ne 200 ]; then echo "CRITICAL - server returned HTTP code $HTTP_CODE -> $URL" exit 2 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;" - # Check response time if [ $(echo "$RESPONSE_TIME_MS > $CRIT_TIME" | bc) -eq 1 ]; then echo "CRITICAL - Response time $RESPONSE_TIME seconds -> $URL | $perfdata" @@ -402,7 +411,7 @@ fi BODY_CONTAINS=" " if [ -n "$CRIT_STRING" ]; then BODY=$(curl -s $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $URL) - if ! [[ $BODY =~ "$CRIT_STRING" ]]; then + if ! [[ $BODY =~ $CRIT_STRING ]]; then # if ! echo "$BODY" | grep -q "$CRIT_STRING"; then echo "CRITICAL - response body does not contain '$CRIT_STRING': $BODY" exit 2