diff --git a/check_http b/check_http index ea797ea154d00b685976bfea02a93039ff0a03ad..4888bbbd1ef215436cf7031b885594a6db439ae3 100755 --- a/check_http +++ b/check_http @@ -33,7 +33,7 @@ Additionally you can verify the response by SYNTAX: $_self [-h] - $_self [-m METHOD] -u URL \\ + $_self [-m METHOD] -u URL [-c PARAMS]\\ [-b REGEX] [-j FILTER] [-n REGEX] \\ [-r REGEX] \\ [-s STATUSCODE] \\ @@ -48,7 +48,8 @@ PARAMETERS: Define request: -u URL Set url to fetch; eg. https://www.example.com/ -m METHOD Set a method, eg. HEAD; default: GET - -c PARAMS additional curl params + -c PARAMS additional curl params; curl will be executed + with '[PARAMS] -si -X [METHOD] --connect-timeout 10 [URL]' What to check: -s STATUSCODE exact Statuscode to check; 3 digits; by default critical @@ -112,7 +113,7 @@ sNotInBody=$( ph.getValueWithParam '' n "$@") sJq=$( ph.getValueWithParam '' j "$@") sLabel=$( ph.getValueWithParam "" l "$@") -curlParams+=" -si -X $sMethod" +curlParams+=" -si -X $sMethod --connect-timeout 10" sProblems= sOK= @@ -123,82 +124,82 @@ if [ -z "$sUrl" ]; then fi out=$( curl $curlParams "$sUrl" ) +if [ -z "$out" ]; then + ph.setStatus critical + sProblems+="- Url [${sUrl}] not reachable - response is empty;\n" + iHttpStatus='???' +else + iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':' | head -1 ) -iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':' | head -1 ) - -# 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") - -if [ -n "$sJq" ]; then - _body=$( jq "$sJq" <<< "$_body" 2>/dev/null ) -fi - -# echo "HEADER"; echo "$_header" -# echo "BODY"; echo "$_body" - - -# --- test status -typeset -i iHttpStatus -iHttpStatus=$( grep -i "^HTTP/[0-9\.]* " <<< "${_header}" | awk '{ print $2 }') + _header=$(echo "$out" | sed -n "1,${iHeaderEnd}p") + _body=$( echo "$out" | sed -n "${iHeaderEnd},\$p") -if [ -n "$iStatus" ]; 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}] but [${iHttpStatus}];\n" - else - sOK+="- Http status is [${iStatus}];\n" + if [ -n "$sJq" ]; then + _body=$( jq "$sJq" <<< "$_body" 2>/dev/null ) 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" + + # --- 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 [ "$iHttpStatus" != "$iStatus" ]; then + ph.setStatus critical + sProblems+="- Http status is not [${iStatus}] but [${iHttpStatus}];\n" + else + sOK+="- Http status is [${iStatus}];\n" + fi else - sOK+="- Http status is a 2xx OK [${iHttpStatus}];\n" + 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 -if [ -n "$sHeader" ]; then - if ! grep -iE "$sHeader" <<< "${_header}" >/dev/null; then - ph.setStatus critical - sProblems+="- Header does not contain [${sHeader}];\n" - 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" + # --- search in http response header + if [ -n "$sHeader" ]; then + if ! grep -iE "$sHeader" <<< "${_header}" >/dev/null; then + ph.setStatus critical + sProblems+="- Header does not contain [${sHeader}];\n" + 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 -fi -# --- search in http response body -if [ -n "$sBody" ]; then - if ! grep -iE "$sBody" <<< "${_body}" >/dev/null; then - ph.setStatus critical - sProblems+="- Body does not contain [${sBody}];\n" - else - sOK+="- [${sBody}] was found in body;\n" + # --- search in http response body + if [ -n "$sBody" ]; then + if ! grep -iE "$sBody" <<< "${_body}" >/dev/null; then + ph.setStatus critical + sProblems+="- Body does not contain [${sBody}];\n" + else + 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" + 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 @@ -211,8 +212,6 @@ test -n "$sLabel" && ( ) test -n "$sLabel" || ph.status "$sMethod $sUrl ($iHttpStatus)" -echo -echo Command: curl $curlParams "$sUrl" echo echo -e "${sProblems}${sOK}" diff --git a/docs/20_Checks/check_http.md b/docs/20_Checks/check_http.md index d229b217f4d74df4ac2114fd6fdaf9be6f9ebfb3..67da17a9edf155829461f4fa3986373893354cea 100644 --- a/docs/20_Checks/check_http.md +++ b/docs/20_Checks/check_http.md @@ -47,7 +47,8 @@ PARAMETERS: Define request: -u URL Set url to fetch; eg. https://www.example.com/ -m METHOD Set a method, eg. HEAD; default: GET - -c PARAMS additional curl params + -c PARAMS additional curl params; curl will be executed + with '[PARAMS] -si -X [METHOD] --connect-timeout 10 [URL]' What to check: -s STATUSCODE exact Statuscode to check; 3 digits; by default critical