Skip to content
Snippets Groups Projects
Commit 69ee03fc authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

add check check_http

parent af1c1137
No related branches found
No related tags found
1 merge request!162check_http first lines
......@@ -3,9 +3,11 @@
#
# CHECK HTTP
#
# Check http request to an url with given method.
# The response header and
#
# -------------------------------------------------------------------------------
# 2023-09-05 v0.1 <axel.hahn@unibe.ch>
# 2023-09-06 v0.1 <axel.hahn@unibe.ch>
# ================================================================================
. $( dirname $0 )/inc_pluginfunctions
......@@ -22,13 +24,20 @@ function showHelp(){
cat <<EOF
$( ph.showImlHelpHeader )
Makes an http request to chewck the response by
Makes an http request with a given method.
Additionally you can verify the response by
- http status code
- content in http response header
- content in http response body
- content that is NOT in http response body
SYNTAX:
$_self [-h] ...
$_self [-h]
$_self [-m METHOD] -u URL \\
[-b REGEX] [-j FILTER] [-n REGEX] \\
[-r REGEX] \\
[-s STATUSCODE] \\
[-l LABEL]
OPTIONS:
......@@ -37,14 +46,16 @@ OPTIONS:
PARAMETERS:
Define request:
-u URL Set url to fetch
-u URL Set url to fetch; eg. https://www.example.com/
-m METHOD Set a method, eg. HEAD; default: GET
What to check:
-s STATUSCODE Statuscode to ceck
-r REGEX String to check in http response header
-s STATUSCODE exact Statuscode to check; 3 digits; by default critical
is a statuscode greater equal 400
-r REGEX Regex must match in http response header
-j JQ-FILTER for JSON Response: filter data by a jq
-b REGEX String to check in response body
-b REGEX Regex must match in response body
-n REGEX Regex must NOT match in response body
Output:
-l LABEL set a custom label; default: METHOD + URL eg.
......@@ -52,7 +63,23 @@ PARAMETERS:
EXAMPLES:
$_self ...
$_self -u https://www.example.com/
Check if GET request to url responds with 200..3xx status.
$_self -m HEAD -u https://www.example.com/
Check if HEAD request to url responds with 200..3xx status
$_self -u [URL] -s 403
Check if the GET request to url has a wanted status code.
You can verify if a protected url is not accessible.
$_self -u [URL] -b "contact"
Check if the GET request to url responds with 200..3xx
status and the response body contains "contact".
$_self -u [URL] -n "error occured"
Check if the GET request to url responds with 200..3xx
status and the response body NOT contains "error occured".
EOF
}
......@@ -73,14 +100,15 @@ ph.require "curl"
sUrl=$( ph.getValueWithParam '' u "$@")
sMethod=$( ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:])
iStatus=$( ph.getValueWithParam '200' s "$@")
iStatus=$( ph.getValueWithParam '' s "$@")
sHeader=$( ph.getValueWithParam '' r "$@")
sBody=$( ph.getValueWithParam '' b "$@")
sNotInBody=$( ph.getValueWithParam '' n "$@")
sJq=$( ph.getValueWithParam '' j "$@")
sLabel=$( ph.getValueWithParam "$sMethod $sUrl" l "$@")
sLabel=$( ph.getValueWithParam "" l "$@")
curlParams="-si"
curlParams="-si -X $sMethod"
sProblems=
sOK=
......@@ -90,13 +118,11 @@ if [ -z "$sUrl" ]; then
ph.exit
fi
# test -z "$sBody" && curlParams+=" -I"
out=$( curl $curlParams "$sUrl" )
iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':' | head -1 )
iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':')
# echo "$out" | grep -n ^$'\r'; echo "cut header and body on line $iHeaderEnd"
_header=$(echo "$out" | sed -n "1,${iHeaderEnd}p")
_body=$( echo "$out" | sed -n "${iHeaderEnd},\$p")
......@@ -110,13 +136,26 @@ fi
# --- test status
typeset -i iHttpStatus
iHttpStatus=$( grep -i "^HTTP/[0-9\.]* " <<< "${_header}" | awk '{ print $2 }')
if [ -n "$iStatus" ]; then
if ! grep -i "^HTTP/[0-9\.]* ${iStatus}" <<< "${_header}" >/dev/null; then
# if ! grep -i "^HTTP/[0-9\.]* ${iStatus}" <<< "${_header}" >/dev/null; then
if [ "$iHttpStatus" != "$iStatus" ]; then
ph.setStatus critical
sProblems+="- Http status is not [${iStatus}];\n"
sProblems+="- Http status is not [${iStatus}] but [${iHttpStatus}];\n"
else
sOK+="- Http status is [${iStatus}];\n"
fi
else
if [ $iHttpStatus -ge 400 ]; then
ph.setStatus critical
sProblems+="- Http status is an http error [${iHttpStatus}];\n"
elif [ $iHttpStatus -ge 300 ]; then
sOK+="- Http status is a 3xx redirect [${iHttpStatus}];\n"
else
sOK+="- Http status is a 2xx OK [${iHttpStatus}];\n"
fi
fi
# --- search in http response header
......@@ -127,7 +166,15 @@ if [ -n "$sHeader" ]; then
else
sOK+="- [${sHeader}] was found in header;\n"
fi
fi
# --- search in http response header
if [ -n "$sNotInHeader" ]; then
if grep -iE "$sNotInHeader" <<< "${_header}" >/dev/null; then
ph.setStatus critical
sProblems+="- Header does contain unwanted [${sNotInHeader}];\n"
else
sOK+="- [${sNotInHeader}] was not found in header;\n"
fi
fi
# --- search in http response body
......@@ -139,22 +186,33 @@ if [ -n "$sBody" ]; then
sOK+="- [${sBody}] was found in body;\n"
fi
fi
if [ -n "$sNotInBody" ]; then
if grep -iE "$sNotInBody" <<< "${_body}" >/dev/null; then
ph.setStatus critical
sProblems+="- Body contains unwanted [${sNotInBody}];\n"
else
sOK+="- [${sNotInBody}] was not found in body;\n"
fi
fi
# --- output
test -n "$sProblems" && sProblems="Problems:\n$sProblems\n"
test -n "$sOK" && sOK="Found:\n$sOK"
test -n "$sLabel" && (
ph.status "$sLabel"
echo "$sMethod $sUrl ($iHttpStatus)"
)
test -n "$sLabel" || ph.status "$sMethod $sUrl ($iHttpStatus)"
if [ "$sLabel" != "$sMethod $sUrl" ]; then
echo "$sMethod $sUrl"
fi
echo
echo Command: curl $curlParams "$sUrl"
echo
echo -e "${sProblems}${sOK}"
echo "HEADER:"; echo; echo "$_header"
echo -n "Eapsed time: "; ph.showtimer
test -n "${sProblems}" && (echo "RESPONSE HEADER:"; echo; echo "$_header")
ph.exit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment