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

add params -m, -l, -j

parent c2a55b20
No related branches found
No related tags found
1 merge request!162check_http first lines
...@@ -22,7 +22,10 @@ function showHelp(){ ...@@ -22,7 +22,10 @@ function showHelp(){
cat <<EOF cat <<EOF
$( ph.showImlHelpHeader ) $( ph.showImlHelpHeader )
TODO Makes an http request to chewck the response by
- http status code
- content in http response header
- content in http response body
SYNTAX: SYNTAX:
$_self [-h] ... $_self [-h] ...
...@@ -31,32 +34,29 @@ OPTIONS: ...@@ -31,32 +34,29 @@ OPTIONS:
-h this help -h this help
PARAMETERS:
Define request:
-u URL Set url to fetch -u URL Set url to fetch
-m METHOD Set a method, eg. HEAD; default: GET
What to check:
-s STATUSCODE Statuscode to ceck -s STATUSCODE Statuscode to ceck
-r REGEX String to check in http response header -r REGEX String to check 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 String to check in response body
TODO Output:
-m METHOD -l LABEL set a custom label; default: METHOD + URL eg.
-j JQ-FILTER "GET https://example.com/status"
EXAMPLES: EXAMPLES:
$_self -u $_self ...
EOF EOF
} }
function getHeader(){
set -vx
echo "$1" | sed -n "1,${2}p"
}
function getHeader(){
set -vx
echo "$1" | sed -n "1,${2}p"
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# MAIN # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -71,9 +71,14 @@ fi ...@@ -71,9 +71,14 @@ fi
ph.require "curl" ph.require "curl"
sUrl=$( ph.getValueWithParam '' u "$@") sUrl=$( ph.getValueWithParam '' u "$@")
sMethod=$( ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:])
iStatus=$( ph.getValueWithParam '200' s "$@") iStatus=$( ph.getValueWithParam '200' s "$@")
sHeader=$( ph.getValueWithParam '' r "$@") sHeader=$( ph.getValueWithParam '' r "$@")
sBody=$( ph.getValueWithParam '' b "$@") sBody=$( ph.getValueWithParam '' b "$@")
sJq=$( ph.getValueWithParam '' j "$@")
sLabel=$( ph.getValueWithParam "$sMethod $sUrl" l "$@")
curlParams="-si" curlParams="-si"
sProblems= sProblems=
...@@ -85,8 +90,7 @@ if [ -z "$sUrl" ]; then ...@@ -85,8 +90,7 @@ if [ -z "$sUrl" ]; then
ph.exit ph.exit
fi fi
test -z "$sBody" && curlParams+=" -I" # test -z "$sBody" && curlParams+=" -I"
out=$( curl $curlParams "$sUrl" ) out=$( curl $curlParams "$sUrl" )
...@@ -97,6 +101,10 @@ iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':') ...@@ -97,6 +101,10 @@ iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':')
_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")
if [ -n "$sJq" ]; then
_body=$( jq "$sJq" <<< "$_body" 2>/dev/null )
fi
# echo "HEADER"; echo "$_header" # echo "HEADER"; echo "$_header"
# echo "BODY"; echo "$_body" # echo "BODY"; echo "$_body"
...@@ -105,15 +113,15 @@ _body=$( echo "$out" | sed -n "${iHeaderEnd},\$p") ...@@ -105,15 +113,15 @@ _body=$( echo "$out" | sed -n "${iHeaderEnd},\$p")
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
ph.setStatus critical ph.setStatus critical
sProblems+="- Statuscode is not [${iStatus}];\n" sProblems+="- Http status is not [${iStatus}];\n"
else else
sOK+="- Statuscode is [${iStatus}];\n" sOK+="- Http status is [${iStatus}];\n"
fi fi
fi fi
# --- search in http response header
if [ -n "$sHeader" ]; then if [ -n "$sHeader" ]; then
if ! grep -i "$sHeader" <<< "${_header}" >/dev/null; then if ! grep -iE "$sHeader" <<< "${_header}" >/dev/null; then
ph.setStatus critical ph.setStatus critical
sProblems+="- Header does not contain [${sHeader}];\n" sProblems+="- Header does not contain [${sHeader}];\n"
else else
...@@ -122,8 +130,9 @@ if [ -n "$sHeader" ]; then ...@@ -122,8 +130,9 @@ if [ -n "$sHeader" ]; then
fi fi
# --- search in http response body
if [ -n "$sBody" ]; then if [ -n "$sBody" ]; then
if ! grep -i "$sBody" <<< "${_body}" >/dev/null; then if ! grep -iE "$sBody" <<< "${_body}" >/dev/null; then
ph.setStatus critical ph.setStatus critical
sProblems+="- Body does not contain [${sBody}];\n" sProblems+="- Body does not contain [${sBody}];\n"
else else
...@@ -132,16 +141,20 @@ if [ -n "$sBody" ]; then ...@@ -132,16 +141,20 @@ if [ -n "$sBody" ]; then
fi fi
# --- 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"
ph.status "Url ${sUrl}" ph.status "$sLabel"
if [ "$sLabel" != "$sMethod $sUrl" ]; then
echo "$sMethod $sUrl"
fi
echo echo
echo -e "${sProblems}${sOK}" echo -e "${sProblems}${sOK}"
echo
# echo "HEADER:"; echo "$_header" echo "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