check_curl: add authentication

This commit is contained in:
Cyberes 2024-11-18 18:54:27 -07:00
parent 8ea97d8227
commit da8c752b61
1 changed files with 11 additions and 8 deletions

View File

@ -18,7 +18,8 @@ usage() {
-e Specify the exact response code that should be returned. Cannot be used with -s option. -e Specify the exact response code that should be returned. Cannot be used with -s option.
-e Expected status code. -e Expected status code.
-S Sanitize HTML when printing to console. Good for Icinga2 Web. -S Sanitize HTML when printing to console. Good for Icinga2 Web.
-n Don't return any perfdata if the status code was not 200." -n Don't return any perfdata if the status code was not 200.
-a Specify the username and password for authentication in the format username:password"
exit 3 exit 3
} }
@ -31,14 +32,15 @@ HEADERS=""
PRINT_CURL="" PRINT_CURL=""
RESOLVE="" RESOLVE=""
IGNORE_STATUS="" IGNORE_STATUS=""
TIMEOUT="30" TIMEOUT="--max-time 30"
HTTP_METHOD="-X GET" HTTP_METHOD="-X GET"
EXPECTED_STATUS_CODE="" EXPECTED_STATUS_CODE=""
SANITIZE_HTML=false SANITIZE_HTML=false
DISABLE_PERFDATA=false DISABLE_PERFDATA=false
AUTHENTICATION=""
# Parse arguments # Parse arguments
while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do while getopts "u:w:c:C:LH:M:IpR:st:e:hSna:" opt; do
case $opt in case $opt in
u) u)
URL="$OPTARG" URL="$OPTARG"
@ -80,9 +82,6 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do
M) M)
HTTP_METHOD="-X $OPTARG" HTTP_METHOD="-X $OPTARG"
;; ;;
e)
EXPECTED_STATUS_CODE="$OPTARG"
;;
h) h)
usage usage
;; ;;
@ -99,6 +98,9 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do
fi fi
EXPECTED_STATUS_CODE="$OPTARG" EXPECTED_STATUS_CODE="$OPTARG"
;; ;;
a)
AUTHENTICATION="--user $OPTARG"
;;
*) *)
echo "Invalid option: -$OPTARG" >&2 echo "Invalid option: -$OPTARG" >&2
usage usage
@ -114,7 +116,8 @@ if [ -z "$URL" ]; then
fi fi
# Prepare curl command # Prepare curl command
CURL_CMD="curl -w %{http_code};%{time_total} -o /dev/null -s $HTTP_METHOD $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $URL" CURL_CMD="curl -w %{http_code};%{time_total} -o /dev/null -s $HTTP_METHOD $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $AUTHENTICATION $URL"
echo $CURL_CMD
# Print curl command if requested # Print curl command if requested
if [ "$PRINT_CURL" = "yes" ]; then if [ "$PRINT_CURL" = "yes" ]; then
@ -432,7 +435,7 @@ fi
# Check critical string # Check critical string
BODY_CONTAINS="" BODY_CONTAINS=""
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 $AUTHENTICATION "$URL")
# shellcheck disable=SC2076 # shellcheck disable=SC2076
if ! [[ $BODY =~ "$CRIT_STRING" ]]; then if ! [[ $BODY =~ "$CRIT_STRING" ]]; then
if $SANITIZE_HTML; then if $SANITIZE_HTML; then