diff --git a/check_curl.sh b/check_curl.sh index b0cdf69..dd7fe70 100755 --- a/check_curl.sh +++ b/check_curl.sh @@ -1,8 +1,24 @@ #!/bin/bash +usage() { + echo "Usage: $0 -u [-w ] [-c ] [-C ] [-L] [-I] [-H ] [-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 -WARN_TIME=1 -CRIT_TIME=2 +WARN_TIME=700 +CRIT_TIME=1000 FOLLOW_REDIRECTS="" INSECURE="" HEADERS="" @@ -12,7 +28,7 @@ IGNORE_STATUS="" TIMEOUT="" # 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 u) URL="$OPTARG" @@ -47,8 +63,12 @@ while getopts "u:w:c:C:LH:IpR:st:" opt; do t) TIMEOUT="--max-time $OPTARG" ;; + h) + usage + ;; *) echo "Invalid option: -$OPTARG" >&2 + usage exit 3 ;; esac @@ -355,28 +375,29 @@ fi # Check HTTP code 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 fi +RESPONSE_TIME_MS=$(echo "$RESPONSE_TIME * 1000" | bc) # Check response time -if [ $(echo "$RESPONSE_TIME > $CRIT_TIME" | bc) -eq 1 ]; then - echo "CRITICAL - Response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME}s" +if [ $(echo "$RESPONSE_TIME_MS > $CRIT_TIME" | bc) -eq 1 ]; then + echo "CRITICAL - Response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME_MS}ms" exit 2 -elif [ $(echo "$RESPONSE_TIME > $WARN_TIME" | bc) -eq 1 ]; then - echo "WARNING - Response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME}s" +elif [ $(echo "$RESPONSE_TIME_MS > $WARN_TIME" | bc) -eq 1 ]; then + echo "WARNING - response time $RESPONSE_TIME seconds -> $URL | response_time=${RESPONSE_TIME}s" exit 1 fi # Check critical string if [ -n "$CRIT_STRING" ]; then BODY=$(curl -s $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $URL) - if [[ ! $BODY =~ $CRIT_STRING ]]; then - echo "CRITICAL - Response body does not contain '$CRIT_STRING'" + if ! echo "$BODY" | grep -q "$CRIT_STRING"; then + echo "CRITICAL - response body does not contain '$CRIT_STRING': $BODY" exit 2 fi fi # 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