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 @@ ...@@ -3,9 +3,11 @@
# #
# CHECK HTTP # 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 . $( dirname $0 )/inc_pluginfunctions
...@@ -22,13 +24,20 @@ function showHelp(){ ...@@ -22,13 +24,20 @@ function showHelp(){
cat <<EOF cat <<EOF
$( ph.showImlHelpHeader ) $( 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 - http status code
- content in http response header - content in http response header
- content in http response body - content in http response body
- content that is NOT in http response body
SYNTAX: SYNTAX:
$_self [-h] ... $_self [-h]
$_self [-m METHOD] -u URL \\
[-b REGEX] [-j FILTER] [-n REGEX] \\
[-r REGEX] \\
[-s STATUSCODE] \\
[-l LABEL]
OPTIONS: OPTIONS:
...@@ -37,14 +46,16 @@ OPTIONS: ...@@ -37,14 +46,16 @@ OPTIONS:
PARAMETERS: PARAMETERS:
Define request: 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 -m METHOD Set a method, eg. HEAD; default: GET
What to check: What to check:
-s STATUSCODE Statuscode to ceck -s STATUSCODE exact Statuscode to check; 3 digits; by default critical
-r REGEX String to check in http response header 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 -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: Output:
-l LABEL set a custom label; default: METHOD + URL eg. -l LABEL set a custom label; default: METHOD + URL eg.
...@@ -52,7 +63,23 @@ PARAMETERS: ...@@ -52,7 +63,23 @@ PARAMETERS:
EXAMPLES: 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 EOF
} }
...@@ -73,14 +100,15 @@ ph.require "curl" ...@@ -73,14 +100,15 @@ ph.require "curl"
sUrl=$( ph.getValueWithParam '' u "$@") sUrl=$( ph.getValueWithParam '' u "$@")
sMethod=$( ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:]) sMethod=$( ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:])
iStatus=$( ph.getValueWithParam '200' s "$@") iStatus=$( ph.getValueWithParam '' s "$@")
sHeader=$( ph.getValueWithParam '' r "$@") sHeader=$( ph.getValueWithParam '' r "$@")
sBody=$( ph.getValueWithParam '' b "$@") sBody=$( ph.getValueWithParam '' b "$@")
sNotInBody=$( ph.getValueWithParam '' n "$@")
sJq=$( ph.getValueWithParam '' j "$@") sJq=$( ph.getValueWithParam '' j "$@")
sLabel=$( ph.getValueWithParam "$sMethod $sUrl" l "$@") sLabel=$( ph.getValueWithParam "" l "$@")
curlParams="-si" curlParams="-si -X $sMethod"
sProblems= sProblems=
sOK= sOK=
...@@ -90,13 +118,11 @@ if [ -z "$sUrl" ]; then ...@@ -90,13 +118,11 @@ if [ -z "$sUrl" ]; then
ph.exit ph.exit
fi fi
# test -z "$sBody" && curlParams+=" -I"
out=$( curl $curlParams "$sUrl" ) 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") _header=$(echo "$out" | sed -n "1,${iHeaderEnd}p")
_body=$( echo "$out" | sed -n "${iHeaderEnd},\$p") _body=$( echo "$out" | sed -n "${iHeaderEnd},\$p")
...@@ -110,13 +136,26 @@ fi ...@@ -110,13 +136,26 @@ fi
# --- test status # --- test status
typeset -i iHttpStatus
iHttpStatus=$( grep -i "^HTTP/[0-9\.]* " <<< "${_header}" | awk '{ print $2 }')
if [ -n "$iStatus" ]; then 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 ph.setStatus critical
sProblems+="- Http status is not [${iStatus}];\n" sProblems+="- Http status is not [${iStatus}] but [${iHttpStatus}];\n"
else else
sOK+="- Http status is [${iStatus}];\n" sOK+="- Http status is [${iStatus}];\n"
fi 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 fi
# --- search in http response header # --- search in http response header
...@@ -127,7 +166,15 @@ if [ -n "$sHeader" ]; then ...@@ -127,7 +166,15 @@ if [ -n "$sHeader" ]; then
else else
sOK+="- [${sHeader}] was found in header;\n" sOK+="- [${sHeader}] was found in header;\n"
fi 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 fi
# --- search in http response body # --- search in http response body
...@@ -139,22 +186,33 @@ if [ -n "$sBody" ]; then ...@@ -139,22 +186,33 @@ if [ -n "$sBody" ]; then
sOK+="- [${sBody}] was found in body;\n" sOK+="- [${sBody}] was found in body;\n"
fi 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 fi
# --- output # --- output
test -n "$sProblems" && sProblems="Problems:\n$sProblems\n" test -n "$sProblems" && sProblems="Problems:\n$sProblems\n"
test -n "$sOK" && sOK="Found:\n$sOK" test -n "$sOK" && sOK="Found:\n$sOK"
test -n "$sLabel" && (
ph.status "$sLabel" ph.status "$sLabel"
echo "$sMethod $sUrl ($iHttpStatus)"
)
test -n "$sLabel" || ph.status "$sMethod $sUrl ($iHttpStatus)"
if [ "$sLabel" != "$sMethod $sUrl" ]; then echo
echo "$sMethod $sUrl" echo Command: curl $curlParams "$sUrl"
fi
echo echo
echo -e "${sProblems}${sOK}" echo -e "${sProblems}${sOK}"
echo "HEADER:"; echo; echo "$_header" test -n "${sProblems}" && (echo "RESPONSE HEADER:"; echo; echo "$_header")
echo -n "Eapsed time: "; ph.showtimer
ph.exit ph.exit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment