fix check_curl

This commit is contained in:
Cyberes 2023-06-15 13:32:23 -06:00
parent 792838d50d
commit 2c2a105480
Signed by: cyberes
GPG Key ID: 6B4A33836A9500FE
1 changed files with 32 additions and 11 deletions

View File

@ -1,8 +1,24 @@
#!/bin/bash #!/bin/bash
usage() {
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).
-c Set the critical level for response time (default: 2 seconds).
-C If the body does not contain this string, return CRITICAL.
-L Follow redirects.
-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.
-s Ignore the response status code."
exit 3
}
# Default values # Default values
WARN_TIME=1 WARN_TIME=700
CRIT_TIME=2 CRIT_TIME=1000
FOLLOW_REDIRECTS="" FOLLOW_REDIRECTS=""
INSECURE="" INSECURE=""
HEADERS="" HEADERS=""
@ -12,7 +28,7 @@ IGNORE_STATUS=""
TIMEOUT="" TIMEOUT=""
# Parse arguments # Parse arguments
while getopts "u:w:c:C:LH:IpR:st:" opt; do while getopts "u:w:c:C:LH:IpR:st:h" opt; do
case $opt in case $opt in
u) u)
URL="$OPTARG" URL="$OPTARG"
@ -47,8 +63,12 @@ while getopts "u:w:c:C:LH:IpR:st:" opt; do
t) t)
TIMEOUT="--max-time $OPTARG" TIMEOUT="--max-time $OPTARG"
;; ;;
h)
usage
;;
*) *)
echo "Invalid option: -$OPTARG" >&2 echo "Invalid option: -$OPTARG" >&2
usage
exit 3 exit 3
;; ;;
esac esac
@ -355,28 +375,29 @@ fi
# Check HTTP code # Check HTTP code
if [ "$IGNORE_STATUS" != "yes" ] && [ "$HTTP_CODE" -ne 200 ]; then if [ "$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)
# Check response time # Check response time
if [ $(echo "$RESPONSE_TIME > $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 | response_time=${RESPONSE_TIME}s" echo "CRITICAL - Response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME_MS}ms"
exit 2 exit 2
elif [ $(echo "$RESPONSE_TIME > $WARN_TIME" | bc) -eq 1 ]; then elif [ $(echo "$RESPONSE_TIME_MS > $WARN_TIME" | bc) -eq 1 ]; then
echo "WARNING - Response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME}s" echo "WARNING - response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME}s"
exit 1 exit 1
fi fi
# Check critical string # Check critical string
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 ! echo "$BODY" | grep -q "$CRIT_STRING"; then
echo "CRITICAL - Response body does not contain '$CRIT_STRING'" echo "CRITICAL - response body does not contain '$CRIT_STRING': $BODY"
exit 2 exit 2
fi fi
fi fi
# All checks passed # All checks passed
echo "OK - Response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME}s" echo "OK - response time was $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME_MS}ms;$WARN_TIME;$CRIT_TIME;0;;"
exit 0 exit 0