icinga2-checks/check_curl.sh

186 lines
4.2 KiB
Bash
Raw Normal View History

2023-04-21 23:54:17 -06:00
#!/bin/bash
2023-04-21 23:54:18 -06:00
# Define the usage message
2023-04-21 23:54:17 -06:00
usage() {
echo "Usage: $0 -u <url> [-w <warning>] [-c <critical>] [-C <contains>] [-L] [-I] [-H <headers>] [-p] [-R] [-s]"
2023-04-21 23:54:19 -06:00
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."
2023-04-21 23:54:18 -06:00
exit 3
2023-04-21 23:54:17 -06:00
}
2023-04-21 23:54:18 -06:00
# Parse the command-line arguments
while getopts "u:w:c:C:H:R:LhIps" opt; do
2023-04-21 23:54:17 -06:00
case $opt in
2023-04-21 23:54:18 -06:00
u)
URL=$OPTARG
;;
w)
WARNING_LEVEL=$OPTARG
;;
c)
CRITICAL_LEVEL=$OPTARG
;;
2023-04-21 23:54:19 -06:00
L)
2023-04-21 23:54:18 -06:00
FOLLOW_REDIRECTS="-L"
;;
C)
CONTAINS=$OPTARG
;;
I)
INSECURE="--insecure"
;;
H)
HEADERS=$OPTARG
;;
p)
PRINT_ONLY=true
;;
R)
RESOLVE="--resolve $OPTARG"
;;
s)
IGNORE_STATUS_CODE=true
;;
2023-04-21 23:54:18 -06:00
h)
usage
;;
*)
usage
;;
2023-04-21 23:54:17 -06:00
esac
done
2023-04-21 23:54:18 -06:00
WARNING_LEVEL=${WARNING_LEVEL:-1}
CRITICAL_LEVEL=${CRITICAL_LEVEL:-2}
#FOLLOW_REDIRECTS=${FOLLOW_REDIRECTS:-""}
2023-04-21 23:54:19 -06:00
PRINT_ONLY=${PRINT_ONLY:-false}
IGNORE_STATUS_CODE=${IGNORE_STATUS_CODE:-false}
2023-04-21 23:54:18 -06:00
if [ -z "$URL" ]; then
2023-04-21 23:54:17 -06:00
usage
fi
2023-04-21 23:54:18 -06:00
HEADER_ARGS=""
IFS=',' read -ra values <<<"$HEADERS"
for value in "${values[@]}"; do
2023-04-21 23:54:19 -06:00
HEADER_ARGS+=' -H "'$value'" '
2023-04-21 23:54:18 -06:00
done
2023-04-21 23:54:19 -06:00
TMP_ERROR_LOG=$(mktemp)
TMP_RESPONSE=$(mktemp)
2023-04-21 23:54:19 -06:00
if $PRINT_ONLY; then
2023-04-21 23:54:19 -06:00
printf "%s" "curl --output \"$TMP_RESPONSE\" -s -w \"%{http_code}\n%{time_total}\" $(echo "${HEADER_ARGS[@]}" | tr -s ' ') $FOLLOW_REDIRECTS $INSECURE $RESOLVE $URL\n"
2023-04-21 23:54:19 -06:00
exit 3
fi
2023-04-21 23:54:17 -06:00
2023-04-21 23:54:19 -06:00
RESPONSE=$(curl --output "$TMP_RESPONSE" -w "%{http_code}\n%{time_total}" $(echo "${HEADER_ARGS[@]}" | tr -s ' ') $FOLLOW_REDIRECTS $INSECURE $RESOLVE $URL 2>"$TMP_ERROR_LOG")
2023-04-21 23:54:18 -06:00
# shellcheck disable=SC2181
2023-04-21 23:54:19 -06:00
status=$?
if [ $status -ne 0 ]; then
case $status in
1)
msg="CRITICAL: Unsupported protocol"
;;
3)
2023-04-21 23:54:19 -06:00
msg="CRITICAL: Malformed URL $URL"
2023-04-21 23:54:19 -06:00
;;
# 5)
2023-04-21 23:54:19 -06:00
# msg="CRITICAL: Could not resolve proxy $proxy"
2023-04-21 23:54:19 -06:00
# ;;
6)
2023-04-21 23:54:19 -06:00
msg="CRITICAL: Could not resolve host $URL"
2023-04-21 23:54:19 -06:00
;;
# 7)
2023-04-21 23:54:19 -06:00
# msg="CRITICAL: Could not connect to proxy $proxy"
2023-04-21 23:54:19 -06:00
# ;;
22)
msg="CRITICAL: Server returned http code >= 400"
;;
52)
msg="CRITICAL: Server returned empty response (52)"
;;
56)
msg="CRITICAL: Failure recieving network data (56)"
;;
60)
msg="CRITICAL: SSL/TLS connection problem (60)"
;;
*)
echo "UNKNOWN: $status - $URL"
exit 3
;;
esac
echo -e "$msg"
# echo "Error log:"
# cat "$TMP_ERROR_LOG"
2023-04-21 23:54:19 -06:00
rm -rf "$TMP_ERROR_LOG"
exit 2
fi
rm -rf "$TMP_ERROR_LOG"
2023-04-21 23:54:18 -06:00
RESPONSE_CODE=$(echo "$RESPONSE" | head -n 1)
2023-04-21 23:54:19 -06:00
RESPONSE_TIME=$(printf "%.3f" $(echo "$RESPONSE" | tail -n 1))
2023-04-21 23:54:17 -06:00
2023-04-21 23:54:18 -06:00
OUTPUT_MSG=""
OUTPUT_CODE=0
if ([ $RESPONSE_CODE -eq 200 ] || $IGNORE_STATUS_CODE) && [ "$(echo "$RESPONSE_TIME < $CRITICAL_LEVEL" | bc -l)" -eq 1 ]; then
2023-04-21 23:54:19 -06:00
OUTPUT_MSG="OK: ${RESPONSE_TIME}s - $URL"
2023-04-21 23:54:19 -06:00
OUTPUT_CODE=0
elif ([ $RESPONSE_CODE -eq 200 ] || $IGNORE_STATUS_CODE) && [ "$(echo "$RESPONSE_TIME < $WARNING_LEVEL" | bc -l)" -eq 1 ]; then
2023-04-21 23:54:18 -06:00
OUTPUT_MSG="WARNING: response time is slow ($RESPONSE_TIME seconds)."
OUTPUT_CODE=1
elif ([ $RESPONSE_CODE -eq 200 ] || $IGNORE_STATUS_CODE); then
2023-04-21 23:54:18 -06:00
OUTPUT_MSG"CRITICAL: response time is very slow ($RESPONSE_TIME seconds)."
OUTPUT_CODE=2
else
2023-04-21 23:54:19 -06:00
OUTPUT_MSG="CRITICAL: website did not return 200, response was $RESPONSE_CODE code."
2023-04-21 23:54:18 -06:00
OUTPUT_CODE=2
2023-04-21 23:54:17 -06:00
fi
2023-04-21 23:54:19 -06:00
if [[ -n ${CONTAINS+x} ]]; then
2023-04-21 23:54:18 -06:00
if ! grep -q "$CONTAINS" "$TMP_RESPONSE"; then
OUTPUT_MSG+="\nCRITICAL: response did not contain required string!\nFound: $(cat "$TMP_RESPONSE")"
OUTPUT_CODE=2
2023-04-21 23:54:19 -06:00
# else
# OUTPUT_MSG+="\nOK: response contained required string."
2023-04-21 23:54:17 -06:00
fi
fi
2023-04-21 23:54:18 -06:00
rm -rf "$TMP_RESPONSE"
if $IGNORE_STATUS_CODE && [[ $RESPONSE_CODE -ne 200 ]]; then
OUTPUT_MSG+="\nResponse code was $RESPONSE_CODE."
fi
2023-04-21 23:54:18 -06:00
OUTPUT_MSG+=" | response_time=${RESPONSE_TIME}s;$WARNING_LEVEL;$CRITICAL_LEVEL;0"
2023-04-21 23:54:17 -06:00
2023-04-21 23:54:19 -06:00
case $OUTPUT_CODE in
2023-04-21 23:54:19 -06:00
#0)
# echo "OK: $URL"
# ;;
2023-04-21 23:54:19 -06:00
1)
echo "WARNING: $URL"
;;
2)
echo "CRITICAL: $URL"
;;
2023-04-21 23:54:19 -06:00
3)
echo "UNKNOWN: $URL"
2023-04-21 23:54:19 -06:00
;;
esac
2023-04-21 23:54:18 -06:00
echo -e "$OUTPUT_MSG"
exit $OUTPUT_CODE