From 84b4fb0541b2d96ac20e8935564111d441b36fd3 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Tue, 5 Sep 2023 19:32:21 +0200 Subject: [PATCH] add params -m, -l, -j --- check_http | 65 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/check_http b/check_http index 043924a..2b45835 100755 --- a/check_http +++ b/check_http @@ -22,7 +22,10 @@ function showHelp(){ cat <<EOF $( 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: $_self [-h] ... @@ -31,32 +34,29 @@ OPTIONS: -h this help +PARAMETERS: + + Define request: -u URL Set url to fetch + -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 + -j JQ-FILTER for JSON Response: filter data by a jq -b REGEX String to check in response body - TODO - -m METHOD - -j JQ-FILTER + Output: + -l LABEL set a custom label; default: METHOD + URL eg. + "GET https://example.com/status" EXAMPLES: - $_self -u + $_self ... EOF } -function getHeader(){ - set -vx - echo "$1" | sed -n "1,${2}p" -} - -function getHeader(){ - set -vx - echo "$1" | sed -n "1,${2}p" -} - # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- @@ -71,9 +71,14 @@ fi ph.require "curl" sUrl=$( ph.getValueWithParam '' u "$@") +sMethod=$( ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:]) + iStatus=$( ph.getValueWithParam '200' s "$@") sHeader=$( ph.getValueWithParam '' r "$@") sBody=$( ph.getValueWithParam '' b "$@") +sJq=$( ph.getValueWithParam '' j "$@") +sLabel=$( ph.getValueWithParam "$sMethod $sUrl" l "$@") + curlParams="-si" sProblems= @@ -85,8 +90,7 @@ if [ -z "$sUrl" ]; then ph.exit fi -test -z "$sBody" && curlParams+=" -I" - +# test -z "$sBody" && curlParams+=" -I" out=$( curl $curlParams "$sUrl" ) @@ -97,6 +101,10 @@ iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':') _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" @@ -105,15 +113,15 @@ _body=$( echo "$out" | sed -n "${iHeaderEnd},\$p") if [ -n "$iStatus" ]; then if ! grep -i "^HTTP/[0-9\.]* ${iStatus}" <<< "${_header}" >/dev/null; then ph.setStatus critical - sProblems+="- Statuscode is not [${iStatus}];\n" + sProblems+="- Http status is not [${iStatus}];\n" else - sOK+="- Statuscode is [${iStatus}];\n" + sOK+="- Http status is [${iStatus}];\n" fi fi - +# --- search in http response header if [ -n "$sHeader" ]; then - if ! grep -i "$sHeader" <<< "${_header}" >/dev/null; then + if ! grep -iE "$sHeader" <<< "${_header}" >/dev/null; then ph.setStatus critical sProblems+="- Header does not contain [${sHeader}];\n" else @@ -122,8 +130,9 @@ if [ -n "$sHeader" ]; then fi +# --- search in http response body if [ -n "$sBody" ]; then - if ! grep -i "$sBody" <<< "${_body}" >/dev/null; then + if ! grep -iE "$sBody" <<< "${_body}" >/dev/null; then ph.setStatus critical sProblems+="- Body does not contain [${sBody}];\n" else @@ -132,16 +141,20 @@ if [ -n "$sBody" ]; then fi - +# --- output test -n "$sProblems" && sProblems="Problems:\n$sProblems\n" 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 -e "${sProblems}${sOK}" -echo -# echo "HEADER:"; echo "$_header" +echo "HEADER:"; echo; echo "$_header" +echo -n "Eapsed time: "; ph.showtimer ph.exit -- GitLab