From 69ee03fc712240d7f2d54bd99f7bbce5d35f5378 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 6 Sep 2023 10:37:27 +0200
Subject: [PATCH] add check check_http

---
 check_http | 116 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 87 insertions(+), 29 deletions(-)

diff --git a/check_http b/check_http
index 2b45835..ba2ce27 100755
--- a/check_http
+++ b/check_http
@@ -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
 }
@@ -70,17 +97,18 @@ fi
 
 ph.require "curl"
 
-sUrl=$(     ph.getValueWithParam ''    u "$@")
-sMethod=$(  ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:])
+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 "$@")
+iStatus=$(       ph.getValueWithParam ''    s "$@")
+sHeader=$(       ph.getValueWithParam ''    r "$@")
+sBody=$(         ph.getValueWithParam ''    b "$@")
+sNotInBody=$(    ph.getValueWithParam ''    n "$@")
+sJq=$(           ph.getValueWithParam ''    j "$@")
+sLabel=$(        ph.getValueWithParam ""    l "$@")
 
 
-curlParams="-si"
+curlParams="-si -X $sMethod"
 sProblems=
 sOK=
 
@@ -89,14 +117,12 @@ if [ -z "$sUrl" ]; then
     ph.status "Wrong parameters - no url was given."
     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
@@ -126,8 +165,16 @@ if [ -n "$sHeader" ]; then
         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
 
 # --- 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"
 
-ph.status "$sLabel"
+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
 
-- 
GitLab