update check_curl

This commit is contained in:
Cyberes 2023-11-23 21:41:05 -07:00
parent 38cc049a04
commit 30b2f8b3e3
1 changed files with 17 additions and 8 deletions

View File

@ -14,7 +14,8 @@ usage() {
-R Set curl --resolve option. -R Set curl --resolve option.
-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 Expected status code."
exit 3 exit 3
} }
@ -29,9 +30,10 @@ RESOLVE=""
IGNORE_STATUS="" IGNORE_STATUS=""
TIMEOUT="30" TIMEOUT="30"
HTTP_METHOD="-X GET" HTTP_METHOD="-X GET"
EXPECTED_STATUS_CODE=""
# Parse arguments # 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 case $opt in
u) u)
URL="$OPTARG" URL="$OPTARG"
@ -69,6 +71,9 @@ while getopts "u:w:c:C:LH:M:IpR:st:h" opt; do
M) M)
HTTP_METHOD="-X $OPTARG" HTTP_METHOD="-X $OPTARG"
;; ;;
e)
EXPECTED_STATUS_CODE="$OPTARG"
;;
h) h)
usage usage
;; ;;
@ -379,16 +384,20 @@ if [ $CURL_EXIT -ne 0 ]; then
exit 2 exit 2
fi 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 # 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" echo "CRITICAL - server returned HTTP code $HTTP_CODE -> $URL"
exit 2 exit 2
fi 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 # 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 -> $URL | $perfdata" echo "CRITICAL - Response time $RESPONSE_TIME seconds -> $URL | $perfdata"
@ -402,7 +411,7 @@ fi
BODY_CONTAINS=" " BODY_CONTAINS=" "
if [ -n "$CRIT_STRING" ]; then if [ -n "$CRIT_STRING" ]; then
BODY=$(curl -s $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $URL) 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 # if ! echo "$BODY" | grep -q "$CRIT_STRING"; then
echo "CRITICAL - response body does not contain '$CRIT_STRING': $BODY" echo "CRITICAL - response body does not contain '$CRIT_STRING': $BODY"
exit 2 exit 2