fix check_curl

This commit is contained in:
Cyberes 2023-04-21 23:54:19 -06:00
parent 2a288b99e3
commit 53359eea2b
1 changed files with 23 additions and 7 deletions

30
check_curl Normal file → Executable file
View File

@ -2,7 +2,7 @@
# Define the usage message # Define the usage message
usage() { usage() {
echo "Usage: $0 -u <url> [-w <warning>] [-c <critical>]" echo "Usage: $0 -u <url> [-w <warning>] [-c <critical>] [-C <contains>] [-L] [-I] [-H <headers>] [-p] [-R]"
exit 3 exit 3
} }
@ -48,6 +48,7 @@ done
WARNING_LEVEL=${WARNING_LEVEL:-1} WARNING_LEVEL=${WARNING_LEVEL:-1}
CRITICAL_LEVEL=${CRITICAL_LEVEL:-2} CRITICAL_LEVEL=${CRITICAL_LEVEL:-2}
#FOLLOW_REDIRECTS=${FOLLOW_REDIRECTS:-""} #FOLLOW_REDIRECTS=${FOLLOW_REDIRECTS:-""}
PRINT_ONLY=${PRINT_ONLY:-false}
if [ -z "$URL" ]; then if [ -z "$URL" ]; then
usage usage
@ -56,10 +57,10 @@ fi
HEADER_ARGS="" HEADER_ARGS=""
IFS=',' read -ra values <<<"$HEADERS" IFS=',' read -ra values <<<"$HEADERS"
for value in "${values[@]}"; do for value in "${values[@]}"; do
HEADER_ARGS+="-H '$value'" HEADER_ARGS+=' -H "'$value'" '
done done
CURL_CMD="-s -w '%{http_code}\n%{time_total}' $HEADER_ARGS $FOLLOW_REDIRECTS $INSECURE $RESOLVE $URL" CURL_CMD=""
if $PRINT_ONLY; then if $PRINT_ONLY; then
echo "curl $CURL_CMD" echo "curl $CURL_CMD"
@ -68,7 +69,7 @@ fi
TMP_ERROR_LOG=$(mktemp) TMP_ERROR_LOG=$(mktemp)
TMP_RESPONSE=$(mktemp) TMP_RESPONSE=$(mktemp)
RESPONSE=$(curl -v -o "$TMP_RESPONSE" "$CURL_CMD" 2>"$TMP_ERROR_LOG") RESPONSE=$(curl --output "$TMP_RESPONSE" -s -w "%{http_code}\n%{time_total}" $(echo "${HEADER_ARGS[@]}" | tr -s ' ') $FOLLOW_REDIRECTS $INSECURE $RESOLVE $URL 2>"$TMP_ERROR_LOG")
# shellcheck disable=SC2181 # shellcheck disable=SC2181
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -80,7 +81,7 @@ fi
rm -rf "$TMP_ERROR_LOG" rm -rf "$TMP_ERROR_LOG"
RESPONSE_CODE=$(echo "$RESPONSE" | head -n 1) RESPONSE_CODE=$(echo "$RESPONSE" | head -n 1)
RESPONSE_TIME=$(printf "%.2f" "$(echo "$RESPONSE" | tail -n 1)") RESPONSE_TIME=$(printf "%.2f" $(echo "$RESPONSE" | tail -n 1))
OUTPUT_MSG="" OUTPUT_MSG=""
OUTPUT_CODE=0 OUTPUT_CODE=0
@ -94,11 +95,11 @@ elif [ $RESPONSE_CODE -eq 200 ]; then
OUTPUT_MSG"CRITICAL: response time is very slow ($RESPONSE_TIME seconds)." OUTPUT_MSG"CRITICAL: response time is very slow ($RESPONSE_TIME seconds)."
OUTPUT_CODE=2 OUTPUT_CODE=2
else else
OUTPUT_MSG="CRITICAL: website is not responding, returned $RESPONSE_CODE code." OUTPUT_MSG="CRITICAL: website did not return 200, response was $RESPONSE_CODE code."
OUTPUT_CODE=2 OUTPUT_CODE=2
fi fi
if [ ! -z ${CONTAINS+x} ]; then if [[ -n ${CONTAINS+x} ]]; then
if ! grep -q "$CONTAINS" "$TMP_RESPONSE"; then if ! grep -q "$CONTAINS" "$TMP_RESPONSE"; then
OUTPUT_MSG+="\nCRITICAL: response did not contain required string!\nFound: $(cat "$TMP_RESPONSE")" OUTPUT_MSG+="\nCRITICAL: response did not contain required string!\nFound: $(cat "$TMP_RESPONSE")"
OUTPUT_CODE=2 OUTPUT_CODE=2
@ -110,5 +111,20 @@ rm -rf "$TMP_RESPONSE"
OUTPUT_MSG+=" | response_time=${RESPONSE_TIME}s;$WARNING_LEVEL;$CRITICAL_LEVEL;0" OUTPUT_MSG+=" | response_time=${RESPONSE_TIME}s;$WARNING_LEVEL;$CRITICAL_LEVEL;0"
case $OUTPUT_CODE in
0)
echo "OK: $URL"
;;
1)
echo "WARNING: $URL"
;;
2)
echo "CRITICAL: $URL"
;;
*)
echo "CRITICAL: $URL"
;;
esac
echo -e "$OUTPUT_MSG" echo -e "$OUTPUT_MSG"
exit $OUTPUT_CODE exit $OUTPUT_CODE