check_curl Ignore the response status code

This commit is contained in:
Cyberes 2023-04-21 23:54:19 -06:00
parent 9fa8c95210
commit e266940304
1 changed files with 15 additions and 6 deletions

View File

@ -2,7 +2,7 @@
# Define the usage message
usage() {
echo "Usage: $0 -u <url> [-w <warning>] [-c <critical>] [-C <contains>] [-L] [-I] [-H <headers>] [-p] [-R]"
echo "Usage: $0 -u <url> [-w <warning>] [-c <critical>] [-C <contains>] [-L] [-I] [-H <headers>] [-p] [-R] [-s]"
echo "[Arguments]:
-u Specify the URL to check (required).
-w Set the warn level for response time (default: 1 second).
@ -12,12 +12,13 @@ usage() {
-I Insecure mode (--insecure).
-H Specify headers. Formatted like \"Header1: value,Header2: value\"
-p Print the curl command and exit
-R Set curl --resolve option."
-R Set curl --resolve option.
-s Ignore the response status code."
exit 3
}
# Parse the command-line arguments
while getopts "u:w:c:C:H:R:LhIp" opt; do
while getopts "u:w:c:C:H:R:LhIps" opt; do
case $opt in
u)
URL=$OPTARG
@ -46,6 +47,9 @@ while getopts "u:w:c:C:H:R:LhIp" opt; do
R)
RESOLVE="--resolve $OPTARG"
;;
s)
IGNORE_STATUS_CODE=true
;;
h)
usage
;;
@ -59,6 +63,7 @@ WARNING_LEVEL=${WARNING_LEVEL:-1}
CRITICAL_LEVEL=${CRITICAL_LEVEL:-2}
#FOLLOW_REDIRECTS=${FOLLOW_REDIRECTS:-""}
PRINT_ONLY=${PRINT_ONLY:-false}
IGNORE_STATUS_CODE=${IGNORE_STATUS_CODE:-false}
if [ -z "$URL" ]; then
usage
@ -94,13 +99,13 @@ RESPONSE_TIME=$(printf "%.2f" $(echo "$RESPONSE" | tail -n 1))
OUTPUT_MSG=""
OUTPUT_CODE=0
if [ $RESPONSE_CODE -eq 200 ] && [ "$(echo "$RESPONSE_TIME < $CRITICAL_LEVEL" | bc -l)" -eq 1 ]; then
if ([ $RESPONSE_CODE -eq 200 ] || $IGNORE_STATUS_CODE) && [ "$(echo "$RESPONSE_TIME < $CRITICAL_LEVEL" | bc -l)" -eq 1 ]; then
OUTPUT_MSG="OK: ${RESPONSE_TIME}s - $URL"
OUTPUT_CODE=0
elif [ $RESPONSE_CODE -eq 200 ] && [ "$(echo "$RESPONSE_TIME < $WARNING_LEVEL" | bc -l)" -eq 1 ]; then
elif ([ $RESPONSE_CODE -eq 200 ] || $IGNORE_STATUS_CODE) && [ "$(echo "$RESPONSE_TIME < $WARNING_LEVEL" | bc -l)" -eq 1 ]; then
OUTPUT_MSG="WARNING: response time is slow ($RESPONSE_TIME seconds)."
OUTPUT_CODE=1
elif [ $RESPONSE_CODE -eq 200 ]; then
elif ([ $RESPONSE_CODE -eq 200 ] || $IGNORE_STATUS_CODE); then
OUTPUT_MSG"CRITICAL: response time is very slow ($RESPONSE_TIME seconds)."
OUTPUT_CODE=2
else
@ -118,6 +123,10 @@ if [[ -n ${CONTAINS+x} ]]; then
fi
rm -rf "$TMP_RESPONSE"
if $IGNORE_STATUS_CODE && [[ $RESPONSE_CODE -ne 200 ]]; then
OUTPUT_MSG+="\nResponse code was $RESPONSE_CODE."
fi
OUTPUT_MSG+=" | response_time=${RESPONSE_TIME}s;$WARNING_LEVEL;$CRITICAL_LEVEL;0"
case $OUTPUT_CODE in