From da8c752b6144cef0d649b5c1c9c9469ff32352e3 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Mon, 18 Nov 2024 18:54:27 -0700 Subject: [PATCH] check_curl: add authentication --- check_curl.sh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/check_curl.sh b/check_curl.sh index eab02ed..f631001 100755 --- a/check_curl.sh +++ b/check_curl.sh @@ -18,7 +18,8 @@ usage() { -e Specify the exact response code that should be returned. Cannot be used with -s option. -e Expected status code. -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 } @@ -31,14 +32,15 @@ HEADERS="" PRINT_CURL="" RESOLVE="" IGNORE_STATUS="" -TIMEOUT="30" +TIMEOUT="--max-time 30" HTTP_METHOD="-X GET" EXPECTED_STATUS_CODE="" SANITIZE_HTML=false DISABLE_PERFDATA=false +AUTHENTICATION="" # 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 u) URL="$OPTARG" @@ -80,9 +82,6 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do M) HTTP_METHOD="-X $OPTARG" ;; - e) - EXPECTED_STATUS_CODE="$OPTARG" - ;; h) usage ;; @@ -99,6 +98,9 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:hSn" opt; do fi EXPECTED_STATUS_CODE="$OPTARG" ;; + a) + AUTHENTICATION="--user $OPTARG" + ;; *) echo "Invalid option: -$OPTARG" >&2 usage @@ -114,7 +116,8 @@ if [ -z "$URL" ]; then fi # 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 if [ "$PRINT_CURL" = "yes" ]; then @@ -432,7 +435,7 @@ fi # Check critical string BODY_CONTAINS="" 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 if ! [[ $BODY =~ "$CRIT_STRING" ]]; then if $SANITIZE_HTML; then