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 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